Problem including dispatcher servlet with spring security


First of all here’s my way to integrate the dispatcher servlet:

1. Create a controller class to handle the RCP requests:


public class GWTController extends RemoteServiceServlet implements Controller, ServletContextAware {

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = -6044751640456464234L;
    // Instance fields
    /** The remote service. */
    private RemoteService remoteService;

    /** The remote service class. */
    private Class remoteServiceClass;

    /** The servlet context. */
    private transient ServletContext servletContext;

    // Public methods
    /**
     * Call GWT's RemoteService doPost() method and return null.
     *
     * @param request
     *            The current HTTP request
     * @param response
     *            The current HTTP response
     * @return A ModelAndView to render, or null if handled directly
     * @throws Exception
     *             In case of errors
     */
    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        doPost(request, response);
        return null; // response handled by GWT RPC over XmlHttpRequest
    }

    /**
     * Process the RPC request encoded into the payload string and return a string that encodes either the method return
     * or an exception thrown by it.
     *
     * @param payload
     *            The RPC payload
     * @return the string
     * @throws SerializationException
     *             the serialization exception
     */
    @Override
    public String processCall(String payload) throws SerializationException {
        try {
            if (remoteService instanceof DependencyInjectionRemoteServiceServlet) {
                try {
                    DependencyInjectionRemoteServiceServlet d = (DependencyInjectionRemoteServiceServlet) remoteService;
                    d.setServletContext(getServletContext());
                    d.init();
                } catch (ServletException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            RPCRequest rpcRequest = RPC.decodeRequest(payload, this.remoteServiceClass);

            // delegate work to the spring injected service
            return RPC.invokeAndEncodeResponse(this.remoteService, rpcRequest.getMethod(), rpcRequest.getParameters());
        } catch (IncompatibleRemoteServiceException e) {
            return RPC.encodeResponseForFailure(null, e);
        }
    }

    /**
     * Setter for Spring injection of the GWT RemoteService object.
     *
     * @param remoteService
     *            the new remote service
     */
    public void setRemoteService(RemoteService remoteService) {
        this.remoteService = remoteService;
        this.remoteServiceClass = this.remoteService.getClass();
    }

    @Override
    public ServletContext getServletContext() {
        return servletContext;
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }
}

This class includes the remoteServlet as property which is injected by Springs property injection.
Property injection is used because there are much services available which are handles by the
controller class and the used service has to be injected.

In this class I set the ServletContext and call the init method of DependencyInjectionRemoteServiceServlet.
I do this to handle the problems I described above.

2. Change the web.xml to use the dispatcher servlet instead of the documentServlet:

		dispatcher
		org.springframework.web.servlet.DispatcherServlet

contextConfigLocation
classpath:spring/dispatcher-servlet.xml

		1

		dispatcher
		/gwtspring/documentService

3. This is the dispatcher-servlet.xml file:


	

				/gwtspring/documentService=documentGWTService

4. Include a message property in the ServiceSecurityException and create a custumFildSerializer for the
ServiceSecurityException to handle the serialisation of the message.

ServiceSecurityException.java:

@SuppressWarnings("serial")
public class ServiceSecurityException extends Exception {

    private String message;

    /**
     * @return the message
     */
    @Override
    public String getMessage() {
        return message;
    }

    /**
     * @param message
     *            the message to set
     */
    public void setMessage(String message) {
        this.message = message;
    }

}

ServiceSecurityException_CustomFieldSerializer.java:

public final class ServiceSecurityException_CustomFieldSerializer {

    /**
     * Instantiates a new object name dto_ custom field serializer.
     */
    private ServiceSecurityException_CustomFieldSerializer() {

    }

    /**
     * Initiate.
     *
     * @param reader
     *            the reader
     * @return the object name dto
     * @throws SerializationException
     *             the serialization exception
     */
    public static ServiceSecurityException initiate(SerializationStreamReader reader) throws SerializationException {

        return new ServiceSecurityException();

    }

    /**
     * Serialize.
     *
     * @param writer
     *            the writer
     * @param instance
     *            the instance
     * @throws SerializationException
     *             the serialization exception
     */
    public static void serialize(SerializationStreamWriter writer, ServiceSecurityException instance)
            throws SerializationException {
        if (instance == null) {
            throw new NullPointerException("ServiceSecurityException object is null");
        } else {
            writer.writeString(instance.getMessage());
        }
    }

    /**
     * Deserialize.
     *
     * @param reader
     *            the reader
     * @param instance
     *            the instance
     * @throws SerializationException
     *             the serialization exception
     */
    public static void deserialize(SerializationStreamReader reader, ServiceSecurityException instance)
            throws SerializationException {
        instance.setMessage(reader.readString());
    }
}

,

  1. No comments yet.
(will not be published)
*