/*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.apache.sling.api.servlets;
 
 import java.util.Map;
 
 
Helper base class for read-only Servlets used in Sling. This base class is actually just a better implementation of the Servlet API HttpServlet class which accounts for extensibility. So extensions of this class have great control over what methods to overwrite.

If any of the default HTTP methods is to be implemented just overwrite the respective doXXX method. If additional methods should be supported implement appropriate doXXX methods and overwrite the mayService(org.apache.sling.api.SlingHttpServletRequest,org.apache.sling.api.SlingHttpServletResponse) method to dispatch to the doXXX methods as appropriate and overwrite the getAllowedRequestMethods(java.util.Map) to add the new method names.

Please note, that this base class is intended for applications where data is only read. As such, this servlet by itself does not support the POST, PUT and DELETE methods. Extensions of this class should either overwrite any of the doXXX methods of this class or add support for other read-only methods only. Applications wishing to support data modification should rather use or extend the SlingAllMethodsServlet which also contains support for the POST, PUT and DELETE methods. This latter class should also be overwritten to add support for HTTP methods modifying data.

Implementors note: The methods in this class are all declared to throw the exceptions according to the intentions of the Servlet API rather than throwing their Sling RuntimeException counter parts. This is done to easy the integration with traditional servlets.

 
 public class SlingSafeMethodsServlet extends GenericServlet {

    
Handles the HEAD method.

This base implementation just calls the doGet(org.apache.sling.api.SlingHttpServletRequest,org.apache.sling.api.SlingHttpServletResponse) method dropping the output. Implementations of this class may overwrite this method if they have a more performing implementation. Otherwise, they may just keep this base implementation.

Parameters:
request The HTTP request
response The HTTP response which only gets the headers set
Throws:
javax.servlet.ServletException Forwarded from the doGet(org.apache.sling.api.SlingHttpServletRequest,org.apache.sling.api.SlingHttpServletResponse) method called by this implementation.
java.io.IOException Forwarded from the doGet(org.apache.sling.api.SlingHttpServletRequest,org.apache.sling.api.SlingHttpServletResponse) method called by this implementation.
 
     protected void doHead(SlingHttpServletRequest request,
             SlingHttpServletResponse responsethrows ServletException,
             IOException {
 
         // the null-output wrapper
         NoBodyResponse wrappedResponse = new NoBodyResponse(response);
 
         // do a normal get request, dropping the output
         doGet(requestwrappedResponse);
 
        // ensure the content length is set as gathered by the null-output
        wrappedResponse.setContentLength();
    }

    
Called by the mayService(org.apache.sling.api.SlingHttpServletRequest,org.apache.sling.api.SlingHttpServletResponse) method to handle an HTTP GET request.

This default implementation reports back to the client that the method is not supported.

Implementations of this class should overwrite this method with their implementation for the HTTP GET method support.

Parameters:
request The HTTP request
response The HTTP response
Throws:
javax.servlet.ServletException Not thrown by this implementation.
java.io.IOException If the error status cannot be reported back to the client.
    protected void doGet(SlingHttpServletRequest request,
            SlingHttpServletResponse responsethrows ServletException,
            IOException {
        handleMethodNotImplemented(requestresponse);
    }

    
Handles the OPTIONS method by setting the HTTP Allow header on the response depending on the methods declared in this class.

Extensions of this class should generally not overwrite this method but rather the getAllowedRequestMethods(java.util.Map) method. This method gathers all declared public and protected methods for the concrete class (upto but not including this class) and calls the getAllowedRequestMethods(java.util.Map) method with the methods gathered. The returned value is then used as the value of the Allow header set.

Parameters:
request The HTTP request object. Not used.
response The HTTP response object on which the header is set.
Throws:
javax.servlet.ServletException Not thrown by this implementation.
java.io.IOException Not thrown by this implementation.
    protected void doOptions(SlingHttpServletRequest request,
            SlingHttpServletResponse responsethrows ServletException,
            IOException {
        Map<StringMethodmethods = getAllDeclaredMethods(getClass());
        StringBuffer allowBuf = getAllowedRequestMethods(methods);
        response.setHeader("Allow"allowBuf.toString());
    }

    
Handles the TRACE method by just returning the list of all header values in the response body.

Extensions of this class do not generally need to overwrite this method as it contains all there is to be done to the TRACE method.

Parameters:
request The HTTP request whose headers are returned.
response The HTTP response into which the request headers are written.
Throws:
javax.servlet.ServletException Not thrown by this implementation.
java.io.IOException May be thrown if there is an problem sending back the request headers in the response stream.
    protected void doTrace(SlingHttpServletRequest request,
            SlingHttpServletResponse responsethrows ServletException,
            IOException {
        String CRLF = "\r\n";
        StringBuffer responseString = new StringBuffer();
        responseString.append("TRACE ").append(request.getRequestURI());
        responseString.append(' ').append(request.getProtocol());
        Enumeration<?> reqHeaderEnum = request.getHeaderNames();
        while (reqHeaderEnum.hasMoreElements()) {
            String headerName = (String) reqHeaderEnum.nextElement();
            Enumeration<?> reqHeaderValEnum = request.getHeaders(headerName);
            while (reqHeaderValEnum.hasMoreElements()) {
                responseString.append(CRLF);
                responseString.append(headerName).append(": ");
                responseString.append(reqHeaderValEnum.nextElement());
            }
        }
        responseString.append(CRLF);
        String charset = "UTF-8";
        byte[] rawResponse = responseString.toString().getBytes(charset);
        int responseLength = rawResponse.length;
        response.setContentType("message/http");
        response.setCharacterEncoding(charset);
        response.setContentLength(responseLength);
        ServletOutputStream out = response.getOutputStream();
        out.write(rawResponse);
    }

    
Called by the service(org.apache.sling.api.SlingHttpServletRequest,org.apache.sling.api.SlingHttpServletResponse) method to handle a request for an HTTP method, which is not known and handled by this class or its extension.

This default implementation reports back to the client that the method is not supported.

This method should be overwritten with great care. It is better to overwrite the mayService(org.apache.sling.api.SlingHttpServletRequest,org.apache.sling.api.SlingHttpServletResponse) method and add support for any extension HTTP methods through an additional doXXX method.

Parameters:
request The HTTP request
response The HTTP response
Throws:
javax.servlet.ServletException Not thrown by this implementation.
java.io.IOException If the error status cannot be reported back to the client.
    protected void doGeneric(SlingHttpServletRequest request,
            SlingHttpServletResponse responsethrows ServletException,
            IOException {
        handleMethodNotImplemented(requestresponse);
    }

    
Tries to handle the request by calling a Java method implemented for the respective HTTP request method.

This base class implentation dispatches the HEAD, GET, OPTIONS and TRACE to the respective doXXX methods and returns true if any of these methods is requested. Otherwise false is just returned.

Implementations of this class may overwrite this method but should first call this base implementation and in case false is returned add handling for any other method and of course return whether the requested method was known or not.

Parameters:
request The HTTP request
response The HTTP response
Returns:
true if the requested method (request.getMethod()) is known. Otherwise false is returned.
Throws:
javax.servlet.ServletException Forwarded from any of the dispatched methods
java.io.IOException Forwarded from any of the dispatched methods
    protected boolean mayService(SlingHttpServletRequest request,
            SlingHttpServletResponse responsethrows ServletException,
            IOException {
        // assume the method is known for now
        boolean methodKnown = true;
        String method = request.getMethod();
        if (HttpConstants.METHOD_HEAD.equals(method)) {
            doHead(requestresponse);
        } else if (HttpConstants.METHOD_GET.equals(method)) {
            doGet(requestresponse);
        } else if (HttpConstants.METHOD_OPTIONS.equals(method)) {
            doOptions(requestresponse);
        } else if (HttpConstants.METHOD_TRACE.equals(method)) {
            doTrace(requestresponse);
        } else {
            // actually we do not know the method
            methodKnown = false;
        }
        // return whether we actually knew the request method or not
        return methodKnown;
    }

    
Helper method which causes an appropriate HTTP response to be sent for an unhandled HTTP request method. In case of HTTP/1.1 a 405 status code (Method Not Allowed) is returned, otherwise a 400 status (Bad Request) is returned.

Parameters:
request The HTTP request from which the method and protocol values are extracted to build the appropriate message.
response The HTTP response to which the error status is sent.
Throws:
java.io.IOException Thrown if the status cannot be sent to the client.
    protected void handleMethodNotImplemented(SlingHttpServletRequest request,
            SlingHttpServletResponse responsethrows IOException {
        String protocol = request.getProtocol();
        String msg = "Method " + request.getMethod() + " not supported";
        if (protocol.endsWith("1.1")) {
            // for HTTP/1.1 use 405 Method Not Allowed
            response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWEDmsg);
        } else {
            // otherwise use 400 Bad Request
            response.sendError(HttpServletResponse.SC_BAD_REQUESTmsg);
        }
    }

    
Called by the service(javax.servlet.ServletRequest,javax.servlet.ServletResponse) method to handle the HTTP request. This implementation calls the mayService(org.apache.sling.api.SlingHttpServletRequest,org.apache.sling.api.SlingHttpServletResponse) method and depedending on its return value call the doGeneric(org.apache.sling.api.SlingHttpServletRequest,org.apache.sling.api.SlingHttpServletResponse) method. If the mayService(org.apache.sling.api.SlingHttpServletRequest,org.apache.sling.api.SlingHttpServletResponse) method can handle the request, the doGeneric(org.apache.sling.api.SlingHttpServletRequest,org.apache.sling.api.SlingHttpServletResponse) method is not called otherwise it is called.

Implementations of this class should not generally overwrite this method. Rather the mayService(org.apache.sling.api.SlingHttpServletRequest,org.apache.sling.api.SlingHttpServletResponse) method should be overwritten to add support for more HTTP methods.

    protected void service(SlingHttpServletRequest request,
            SlingHttpServletResponse responsethrows ServletException,
            IOException {
        // first try to handle the request by the known methods
        boolean methodKnown = mayService(requestresponse);
        // otherwise try to handle it through generic means
        if (!methodKnown) {
            doGeneric(requestresponse);
        }
    }

    
Forwards the request to the service(org.apache.sling.api.SlingHttpServletRequest,org.apache.sling.api.SlingHttpServletResponse) method if the request is a HTTP request.

Implementations of this class will not generally overwrite this method.

    public void service(ServletRequest reqServletResponse res)
            throws ServletExceptionIOException {
        if ((req instanceof SlingHttpServletRequest)
            && (res instanceof SlingHttpServletResponse)) {
            service((SlingHttpServletRequest) req,
                (SlingHttpServletResponse) res);
        } else {
            throw new ServletException("Not a Sling HTTP request/response");
        }
    }

    
Returns the simple class name of this servlet class. Extensions of this class may overwrite to return more specific information.
    public String getServletInfo() {
        return getClass().getSimpleName();
    }

    
Helper method called by doOptions(org.apache.sling.api.SlingHttpServletRequest,org.apache.sling.api.SlingHttpServletResponse) to calculate the value of the Allow header sent as the response to the HTTP OPTIONS request.

This base class implementation checks whether any doXXX methods exist for GET and HEAD and returns the list of methods supported found. The list returned always includes the HTTP OPTIONS and TRACE methods.

Implementations of this class may overwrite this method check for more methods supported by the extension (generally the same list as used in the mayService(org.apache.sling.api.SlingHttpServletRequest,org.apache.sling.api.SlingHttpServletResponse) method). This base class implementation should always be called to make sure the default HTTP methods are included in the list.

Parameters:
declaredMethods The public and protected methods declared in the extension of this class.
Returns:
A StringBuffer containing the list of HTTP methods supported.
            Map<StringMethoddeclaredMethods) {
        StringBuffer allowBuf = new StringBuffer();
        // OPTIONS and TRACE are always supported by this servlet
        allowBuf.append(HttpConstants.METHOD_OPTIONS);
        allowBuf.append(", ").append(HttpConstants.METHOD_TRACE);
        // add more method names depending on the methods found
        if (declaredMethods.containsKey("doHead")
            && !declaredMethods.containsKey("doGet")) {
            allowBuf.append(", ").append(HttpConstants.METHOD_HEAD);
        } else if (declaredMethods.containsKey("doGet")) {
            allowBuf.append(", ").append(HttpConstants.METHOD_GET);
            allowBuf.append(", ").append(HttpConstants.METHOD_HEAD);
        }
        return allowBuf;
    }

    
Returns a map of methods declared by the class indexed by method name. This method is called by the doOptions(org.apache.sling.api.SlingHttpServletRequest,org.apache.sling.api.SlingHttpServletResponse) method to find the methods to be checked by the getAllowedRequestMethods(java.util.Map) method. Note, that only extension classes of this class are considered to be sure to not account for the default implementations of the doXXX methods in this class.

Parameters:
c The Class to get the declared methods from
Returns:
The Map of methods considered for support checking.
    private Map<StringMethodgetAllDeclaredMethods(Class<?> c) {
        // stop (and do not include) the AbstractSlingServletClass
        if (c == null
            || c.getName().equals(SlingSafeMethodsServlet.class.getName())) {
            return new HashMap<StringMethod>();
        }
        // get the declared methods from the base class
        Map<StringMethodmethodSet = getAllDeclaredMethods(c.getSuperclass());
        // add declared methods of c (maybe overwrite base class methods)
        Method[] declaredMethods = c.getDeclaredMethods();
        for (Method method : declaredMethods) {
            // only consider public and protected methods
            if (Modifier.isProtected(method.getModifiers())
                || Modifier.isPublic(method.getModifiers())) {
                methodSet.put(method.getName(), method);
            }
        }
        return methodSet;
    }

    
A response that includes no body, for use in (dumb) "HEAD" support. This just swallows that body, counting the bytes in order to set the content length appropriately.
    private class NoBodyResponse extends SlingHttpServletResponseWrapper {

        
The byte sink and counter
        private NoBodyOutputStream noBody;

        
Optional writer around the byte sink
        private PrintWriter writer;

        
Whether the request processor set the content length itself or not.
        private boolean didSetContentLength;
        NoBodyResponse(SlingHttpServletResponse wrappedResponse) {
            super(wrappedResponse);
            noBody = new NoBodyOutputStream();
        }

        
Called at the end of request processing to ensure the content length is set. If the processor already set the length, this method does not do anything. Otherwise the number of bytes written through the null-output is set on the response.
        void setContentLength() {
            if (!didSetContentLength) {
                setContentLength(noBody.getContentLength());
            }
        }

        
Overwrite this to prevent setting the content length at the end of the request through setContentLength()
        public void setContentLength(int len) {
            super.setContentLength(len);
            didSetContentLength = true;
        }

        
Just return the null output stream and don't check whether a writer has already been acquired.
        public ServletOutputStream getOutputStream() {
            return noBody;
        }

        
Just return the writer to the null output stream and don't check whether an output stram has already been acquired.
        public PrintWriter getWriter() throws UnsupportedEncodingException {
            if (writer == null) {
                OutputStreamWriter w;
                w = new OutputStreamWriter(noBodygetCharacterEncoding());
                writer = new PrintWriter(w);
            }
            return writer;
        }
    }

    
Simple ServletOutputStream which just does not write but counts the bytes written through it. This class is used by the NoBodyResponse.
    private class NoBodyOutputStream extends ServletOutputStream {
        private int contentLength = 0;

        

Returns:
the number of bytes "written" through this stream
        int getContentLength() {
            return contentLength;
        }
        public void write(int b) {
            contentLength++;
        }
        public void write(byte buf[], int offsetint len) {
            if (len >= 0) {
                contentLength += len;
            } else {
                throw new IndexOutOfBoundsException();
            }
        }
    }
New to GrepCode? Check out our FAQ X