Extending an HTTP Request Handler

Feedback


The REST Service Publishing Mechanism section gives an introduction to the REST service publishing mechanism. We can know that when the HTTP request arrives at the REST application object, the process of handling the request is managed by the HTTP handler. SuperMap iServer provides the default HTTP request handler HTTP implementation (DefaultMethodHandler) to manage this process.

SuperMap iServer also provides the extension mechanism for the HTTP request handler, which gives users access to control the HTTP request handling process. The two steps for extending the HTTP request handler is as follows:

  1. Implementing an HTTP Request Handler Class

  2. Configuring the HTTP Request Handler

1. Implementing an HTTP Request Handler Class

The REST implementation framework of SuperMap iServer provides the abstract class com.supermap.services.rest.AbstractMethodHandler, from which all HTTP request handlers. DefaultMethodHandler is the default HTTP request handler provided by SuperMap iServer .

When extending the HTTP request handler, users need to inherit from the AbstractMethodHandler abstract class and implement the abstract methods in the class, or users can inherit from DefaultMethodHandler and overwrite the methods in it. Below is a list of some important methods involved.

Method Description
handleGet(ResourceBase, Request, Response) Handles the GET request using the target resource.
handleHead(ResourceBase, Request, Response) Handles the HEAD request using the target resource.
handlePost(ResourceBase, Request, Response) Handles the POST request using the target resource.
handlePut(ResourceBase, Request, Response) Handles the PUT request using the target resource.
handleDelete(ResourceBase, Request, Response) Handles the DELETE request using the target resource.
handleOptions(ResourceBase, Request, Response) Handles the OPTIONS request using the target resource.

For illustration, here we implement a simple HTTP request handler--MyMethodHandler, with the handleGET method implemented. The logic below is followed:

Do not read the expected representation format included in the HTTP request, and return the XML format representation for all GET requests.

The implementation of MyMethodHandler, which inherits from the AbstractMethodHandler abstract class, is as follows:

Sample code:

package com.supermap.sample.extendREST;

import org.restlet.Request;

import org.restlet.Response;

import org.restlet.data.MediaType;

import org.restlet.data.Status;

import org.restlet.representation.Representation;

import com.supermap.services.rest.AbstractMethodHandler;

import com.supermap.services.rest.encoders.Encoder;

import com.supermap.services.rest.encoders.XMLEncoder;

import com.supermap.services.rest.resources.ResourceBase;

public class MyMethodHandler extends AbstractMethodHandler {

        @Override

        public void handleGet(ResourceBase targetResource, Request request, Response response) {

                // TODO Auto-generated method stub

                Encoder encoder = null;

                if (targetResource.isResourceExist()) {

                    //Mandatorily use the XML encoder

                    encoder = new XMLEncoder();

                Object content = targetResource.getResourceContent();

                if (content != null) {

                    // Return the XML format representation regardless of the request

                    Representation entity = encoder.toRepresentation(MediaType.TEXT_XML, content);

                    response.setEntity(entity);

                    response.setStatus(Status.SUCCESS_OK);

                } else {

                    // The resource is not represented.

                    response.setStatus(Status.SUCCESS_NO_CONTENT);

                }

                } else {

                    // Handle the resource not exist exception

                        response.setStatus(Status.CLIENT_ERROR_NOT_FOUND);

                }

        }

        @Override

        public void handleHead(ResourceBase arg0, Request arg1, Response arg2) {

                // TODO Auto-generated method stub

        }

        @Override

        public void handleOptions(ResourceBase arg0, Request arg1, Response arg2) {

                // TODO Auto-generated method stub

        }

        @Override

        public void handlePost(ResourceBase arg0, Request arg1, Response arg2) {

                // TODO Auto-generated method stub

        }

        @Override

        public void handlePut(ResourceBase arg0, Request arg1, Response arg2) {

                // TODO Auto-generated method stub

        }

        @Override

        public void handleDelete(ResourceBase arg0, Request arg1, Response arg2) {

        }

}

Up till now, an HTTP request handler class is finisehd. You can find the sample code for this example in %SuperMap iServer_HOME%/samples/code/ExtendREST.

Afer compiling the MyMethodHandler class, we need to package it into a Jar package and put the Jar package in %SuperMap iServer_HOME%/webapps/iserver/WEB-INF/lib. In this example, the class is packaged into extendREST.jar.

2. Configuring the HTTP Request Handler

The process for configuring a custom HTTP request handler to the REST service is similar to Configuring the Encoder. First, register a Bean component, and then configure for a single resource or all resources.

Please note that the node configured in iserver-rest-resources.xml  turns to <extensionHttpActionHandlerBeanName/>. And when configuring <util:map id="restConfig"/> in iserver-rest-appContext.xml, the key value of <entry/> is <entry/> of defaultHttpActionHandler.

After registering MyMethodHandler as a Bean component named MyHTTPHandler, configure MyHTTPHandler for the maps resource, as shown below. The configuration information for the maps resource is in mappingResources.xml in %SuperMap iServer_HOME%/lib/iserver-all-{version}.jar/config/rest.

<resource>

        <configID>maps</configID>

        <urlTemplate>/maps</urlTemplate>

        <resourceType>CatalogList</resourceType>

        <implementClass>com.supermap.services.rest.resources.impl.MapsResource</implementClass>

        <extensionEncoderBeanNames></extensionEncoderBeanNames>

        <extensionDecoderBeanNames></extensionDecoderBeanNames>

        <extensionHttpActionHandlerBeanName>MyHTTPHandler</extensionHttpActionHandlerBeanName>

</resource>

 

Up till now, the extension of an HTTP request handler is finished. Note that a resource can have multiple encoders and decoders, but only one HTTP request handler.

Restart the service, and access the URI below:

http://localhost:8090/iserver/services/components-rest/rest/maps.json

You will find the response is in XML format, but not the expected json format. That is because the default HTTP request handler has been replaced with the custom MyHTTPHandler, which mandatorily generates the XML format representation when handling the GET request.

In practical situations, you can extend the HTTP request handler to change the HTTP request handling process, add additional business logic, etc.