use of org.apache.sling.api.SlingHttpServletRequest in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class CoreFormHandlingServletTest method testDoPost.
@Test
public void testDoPost() throws Exception {
SlingHttpServletRequest request = new MockSlingHttpServletRequest(context.resourceResolver());
SlingHttpServletResponse response = new MockSlingHttpServletResponse();
servlet.doPost(request, response);
verify(formsHandlingServletHelper).doPost(request, response);
}
use of org.apache.sling.api.SlingHttpServletRequest in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class CoreFormHandlingServletTest method testDoFilter.
@Test
public void testDoFilter() throws Exception {
SlingHttpServletRequest request = new MockSlingHttpServletRequest(context.resourceResolver());
SlingHttpServletResponse response = new MockSlingHttpServletResponse();
FilterChain filterChain = mock(FilterChain.class);
servlet.doFilter(request, response, filterChain);
verify(formsHandlingServletHelper).handleFilter(request, response, filterChain, EXTENSION, SELECTOR);
}
use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class RequestData method initResource.
public Resource initResource(ResourceResolver resourceResolver) {
// keep the resource resolver for request processing
this.resourceResolver = resourceResolver;
// resolve the resource
requestProgressTracker.startTimer("ResourceResolution");
final SlingHttpServletRequest request = getSlingRequest();
StringBuffer requestURL = servletRequest.getRequestURL();
String path = request.getPathInfo();
if (requestURL.indexOf(";") > -1 && !path.contains(";")) {
final String decodedURL;
try {
decodedURL = URLDecoder.decode(requestURL.toString(), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new AssertionError("UTF-8 encoding is not supported");
}
path = path.concat(decodedURL.substring(decodedURL.indexOf(';')));
}
Resource resource = resourceResolver.resolve(request, path);
if (request.getAttribute(REQUEST_RESOURCE_PATH_ATTR) == null) {
request.setAttribute(REQUEST_RESOURCE_PATH_ATTR, resource.getPath());
}
requestProgressTracker.logTimer("ResourceResolution", "URI={0} resolves to Resource={1}", getServletRequest().getRequestURI(), resource);
return resource;
}
use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class SlingRequestProcessorImpl method doProcessRequest.
/**
* This method is directly called by the Sling main servlet.
*/
public void doProcessRequest(final HttpServletRequest servletRequest, final HttpServletResponse servletResponse, final ResourceResolver resourceResolver) throws IOException {
// setting the Sling request and response
final RequestData requestData = new RequestData(this, servletRequest, servletResponse);
final SlingHttpServletRequest request = requestData.getSlingRequest();
final SlingHttpServletResponse response = requestData.getSlingResponse();
// record the request for the web console display
RequestHistoryConsolePlugin.recordRequest(request);
try {
final ServletResolver sr = this.servletResolver;
// check that we have all required services
if (resourceResolver == null) {
throw new UnavailableException("ResourceResolver");
} else if (sr == null) {
throw new UnavailableException("ServletResolver");
}
// initialize the request data - resolve resource and servlet
Resource resource = requestData.initResource(resourceResolver);
requestData.initServlet(resource, sr);
FilterHandle[] filters = filterManager.getFilters(FilterChainType.REQUEST);
if (filters != null) {
FilterChain processor = new RequestSlingFilterChain(this, filters);
request.getRequestProgressTracker().log("Applying " + FilterChainType.REQUEST + "filters");
processor.doFilter(request, response);
} else {
// no filters, directly call resource level filters and servlet
processComponent(request, response, FilterChainType.COMPONENT);
}
} catch (final SlingHttpServletResponseImpl.WriterAlreadyClosedException wace) {
log.error("Writer has already been closed.", wace);
} catch (ResourceNotFoundException rnfe) {
// send this exception as a 404 status
log.info("service: Resource {} not found", rnfe.getResource());
handleError(HttpServletResponse.SC_NOT_FOUND, rnfe.getMessage(), request, response);
} catch (final SlingException se) {
// we assume, that this is the name of the causing servlet
if (requestData.getActiveServletName() != null) {
request.setAttribute(ERROR_SERVLET_NAME, requestData.getActiveServletName());
}
// send this exception as is (albeit unwrapping and wrapped
// exception.
Throwable t = se;
while (t instanceof SlingException && t.getCause() != null) {
t = t.getCause();
}
log.error("service: Uncaught SlingException", t);
handleError(t, request, response);
} catch (AccessControlException ace) {
// SLING-319 if anything goes wrong, send 403/FORBIDDEN
log.info("service: Authenticated user {} does not have enough rights to executed requested action", request.getRemoteUser());
handleError(HttpServletResponse.SC_FORBIDDEN, null, request, response);
} catch (UnavailableException ue) {
// exception is thrown before the SlingHttpServletRequest/Response
// is properly set up due to missing dependencies. In this case
// we must not use the Sling error handling infrastructure but
// just return a 503 status response handled by the servlet
// container environment
final int status = HttpServletResponse.SC_SERVICE_UNAVAILABLE;
final String errorMessage = ue.getMessage() + " service missing, cannot service requests";
log.error("{} , sending status {}", errorMessage, status);
servletResponse.sendError(status, errorMessage);
} catch (IOException ioe) {
// forward IOException up the call chain to properly handle it
throw ioe;
} catch (Throwable t) {
// we assume, that this is the name of the causing servlet
if (requestData.getActiveServletName() != null) {
request.setAttribute(ERROR_SERVLET_NAME, requestData.getActiveServletName());
}
log.error("service: Uncaught Throwable", t);
handleError(t, request, response);
} finally {
if (mbean != null) {
mbean.addRequestData(requestData);
}
}
}
use of org.apache.sling.api.SlingHttpServletRequest in project sling by apache.
the class RequestProgressTrackerLogFilter method doFilter.
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
chain.doFilter(request, response);
if (request instanceof SlingHttpServletRequest) {
final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
final RequestProgressTracker rpt = slingRequest.getRequestProgressTracker();
rpt.done();
if (log.isDebugEnabled() && allowDuration(rpt) && allowExtension(extractExtension(slingRequest))) {
if (configuration.compactLogFormat()) {
logCompactFormat(rpt);
} else {
logDefaultFormat(rpt);
}
}
}
}
Aggregations