DefaultServlet.java 6.38 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.servlet.ServletException
 *  javax.servlet.http.HttpServletResponse
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceNotFoundException
 *  org.apache.sling.api.servlets.SlingAllMethodsServlet
 *  org.apache.sling.servlets.post.PostResponse
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.rest.impl.servlet;

import com.adobe.granite.rest.impl.servlet.ApiResourceUnwrapperRequestWrapper;
import com.adobe.granite.rest.impl.servlet.CopyRequest;
import com.adobe.granite.rest.impl.servlet.DeleteRequest;
import com.adobe.granite.rest.impl.servlet.MoveRequest;
import com.adobe.granite.rest.impl.servlet.PostRequest;
import com.adobe.granite.rest.impl.servlet.PutRequest;
import com.adobe.granite.rest.impl.servlet.RequestHandler;
import com.adobe.granite.rest.impl.servlet.SirenResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceNotFoundException;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.servlets.post.PostResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service
@Properties(value={@Property(name="sling.servlet.methods", value={"COPY", "DELETE", "MOVE", "POST", "PUT"}), @Property(name="sling.servlet.resourceTypes", value={"granite/rest/core/resource"}), @Property(name="sling.servlet.prefix", intValue={0})})
public final class DefaultServlet
extends SlingAllMethodsServlet {
    private Logger logger;

    public DefaultServlet() {
        this.logger = LoggerFactory.getLogger(this.getClass());
    }

    protected void handleRequest(SlingHttpServletRequest request, SlingHttpServletResponse response, RequestHandler requestHandler) throws IOException {
        SirenResponse apiResponse = new SirenResponse();
        apiResponse.setReferer(request.getHeader("referer"));
        try {
            requestHandler.handle((SlingHttpServletRequest)new ApiResourceUnwrapperRequestWrapper(request), (PostResponse)apiResponse);
        }
        catch (ResourceNotFoundException rnfe) {
            apiResponse.setStatus(404, rnfe.getMessage());
        }
        catch (Exception e) {
            this.logger.debug("Exception while handling request " + request.getResource().getPath() + " with " + requestHandler.getClass().getName(), (Throwable)e);
            apiResponse.setError((Throwable)e);
        }
        apiResponse.send((HttpServletResponse)response, true);
    }

    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        this.logger.debug("Processing POST request {}", (Object)request);
        this.handleRequest(request, response, new PostRequest());
    }

    protected void doPatch(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        this.handleMethodNotImplemented(request, response);
    }

    protected void doPut(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        this.logger.debug("Processing PUT request {}", (Object)request);
        this.handleRequest(request, response, new PutRequest());
    }

    protected void doDelete(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        this.logger.debug("Processing DELETE request {}", (Object)request);
        this.handleRequest(request, response, new DeleteRequest());
    }

    protected void doCopy(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        this.logger.debug("Processing COPY request {}", (Object)request);
        this.handleRequest(request, response, new CopyRequest());
    }

    protected void doMove(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        this.logger.debug("Processing MOVE request {}", (Object)request);
        this.handleRequest(request, response, new MoveRequest());
    }

    protected StringBuffer getAllowedRequestMethods(Map<String, Method> declaredMethods) {
        StringBuffer allowBuf = super.getAllowedRequestMethods(declaredMethods);
        String className = SlingAllMethodsServlet.class.getName();
        if (this.isMethodValid(declaredMethods.get("doCopy"), className)) {
            allowBuf.append(", ").append("COPY");
        } else if (this.isMethodValid(declaredMethods.get("doMove"), className)) {
            allowBuf.append(", ").append("MOVE");
        } else if (this.isMethodValid(declaredMethods.get("doPatch"), className)) {
            allowBuf.append(", ").append("PATCH");
        }
        return allowBuf;
    }

    protected boolean mayService(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        if (super.mayService(request, response)) {
            return true;
        }
        boolean methodKnown = true;
        String method = request.getMethod();
        if ("POST".equals(method)) {
            this.doPost(request, response);
        } else if ("PUT".equals(method)) {
            this.doPut(request, response);
        } else if ("DELETE".equals(method)) {
            this.doDelete(request, response);
        } else if ("COPY".equals(method)) {
            this.doCopy(request, response);
        } else if ("MOVE".equals(method)) {
            this.doMove(request, response);
        } else if ("PATCH".equals(method)) {
            this.doPatch(request, response);
        } else {
            methodKnown = false;
        }
        return methodKnown;
    }
}