Case 2. Use Jersey to develop sub-resources based on existing resources of iPortal |
If you want to develop a new REST API under the existing resources of iPortal, for example: add a "My WMS Service" resource under the "My Service" resource, you can use the back-end resource extension method based on Jersey provided by iPortal.The sample code used in this case is located at %SuperMap iPortal_HOME%\samples\code\CustomPortal\Custom_Portal.
1. Before developing, please build the iPortal customized development environment.
2. Please use Jersey to develop your custom resources, and add the annotation @ExtendPortalResource in the entry class. The example code of custom resource implementation class is located at src> com.supermap.iportal.web.custom.rest.resource.impl, the code is as follows :
package com.supermap.iportal.web.custom.rest.resource.impl;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.WebApplicationException;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ThreadContext;
import org.springframework.beans.factory.annotation.Autowired;
import com.supermap.iportal.web.commontypes.ServiceInfo;
import com.supermap.iportal.web.commontypes.ServiceSearchParameter;
import com.supermap.iportal.web.commontypes.SourceType;
import com.supermap.iportal.web.components.ServiceInfoComponent;
import com.supermap.iportal.web.rest.extention.ExtendPortalResource;
import com.supermap.services.components.commontypes.Page;
/**
* User's extend resources
*
* @author Administrator
*
*/
@ExtendPortalResource
@Path("/")
public class CustomServiceResources {
@Autowired
ServiceInfoComponent serviceInfoComponent;//Inject component layer
@GET//Specify the request method as GET
@Path("/mycontent/my-wms-services")//Specify the resource path
public Page<ServiceInfo> getMyWmsServices() {
Page<ServiceInfo> page = null;
ServiceSearchParameter searchParameter = new ServiceSearchParameter();
searchParameter.userNames = new String[] { getCurrentUserName() };
searchParameter.types = new SourceType[] { SourceType.WMS };
page = serviceInfoComponent.getServiceInfosByUser(searchParameter);
return (page != null) ? page : new Page<ServiceInfo>();
}
protected String getCurrentUserName() {
String userName = null;
Subject subject = ThreadContext.getSubject();
if (subject != null && subject.getPrincipal() != null) {
userName = subject.getPrincipal().toString();
} else {
throw new WebApplicationException(401);
}
return userName;
}
}
In this example, a new sub-resource my-wms-services is added to the mycontent resource, which returns a list of all WMS service resources whose author is the currently logged-in user. The response body of the resource will be automatically converted to the corresponding type according to the different request bodies (json, rjson, xml).
3. Add the extendPortalResources.xml file under the WebContent\WEB-INF\config folder as the Spring configuration file of the new resource. The configuration file corresponding to the above example is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"
default-lazy-init="true">
<bean class="com.supermap.iportal.web.custom.rest.resource.impl.CustomServiceResources" />
</beans>
The code in bold is the resource class loaded by the configuration.
4. Visit localhost:8091/iportal/web/mycontent/my-wms-services.json to return all WMS-type services in my service.