DefaultServlet.java
6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* 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;
}
}