Java Code Examples for org.apache.sling.api.SlingHttpServletRequest#getMethod()
The following examples show how to use
org.apache.sling.api.SlingHttpServletRequest#getMethod() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LinkRewriterFilter.java From publick-sling-blog with Apache License 2.0 | 5 votes |
/** * Filter all GET requests for HTML pages. * * Wrap the request and convert the response to a String where we can pass * it to the Link Rewriter service. * * @param request The Sling HTTP Servlet Request. * @param response The Sling HTTP Servlet Response. * @param chain The Filter Chain to continue processin the response. */ @Override public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { // Since this is a Sling Filter, the request and response objects are guaranteed // to be of types SlingHttpServletRequest and SlingHttpServletResponse. final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest)request; final String path = slingRequest.getPathInfo().toLowerCase(); final String host = slingRequest.getServerName(); final String method = slingRequest.getMethod(); response.setCharacterEncoding(CharEncoding.UTF_8); if (linkRewriter != null && "GET".equals(method) && path.endsWith(".html")) { PrintWriter out = response.getWriter(); CharResponseWrapper responseWrapper = new CharResponseWrapper((HttpServletResponse)response); try { chain.doFilter(request, responseWrapper); } catch (Exception e) { LOGGER.error("Could not continue chain", e); chain.doFilter(request, response); } String servletResponse = new String(responseWrapper.toString()); out.write(linkRewriter.rewriteAllLinks(servletResponse, host)); } else { chain.doFilter(request, response); } }
Example 2
Source File: SystemFilter.java From publick-sling-blog with Apache License 2.0 | 5 votes |
/** * Prevent unauthorables from accessing Sling's user admin. * * @param request The Sling HTTP Servlet Request. * @param response The Sling HTTP Servlet Response. * @param chain The Filter Chain to continue processin the response. */ @Override public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { // Since this is a Sling Filter, the request and response objects are guaranteed // to be of types SlingHttpServletRequest and SlingHttpServletResponse. final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest)request; final SlingHttpServletResponse slingResponse = (SlingHttpServletResponse)response; final String path = slingRequest.getPathInfo().toLowerCase(); final String method = slingRequest.getMethod(); response.setCharacterEncoding(CharEncoding.UTF_8); if ("POST".equals(method) && path.startsWith("/system")) { if (userService != null) { final boolean allow = userService.isAuthorable(slingRequest.getResourceResolver().adaptTo(Session.class)); if (allow) { chain.doFilter(request, response); } else { slingResponse.sendError(SlingHttpServletResponse.SC_FORBIDDEN); } } else { slingResponse.sendError(SlingHttpServletResponse.SC_FORBIDDEN); } } else { chain.doFilter(request, response); } }
Example 3
Source File: PreviewFilter.java From publick-sling-blog with Apache License 2.0 | 4 votes |
/** * Handle blog posts that are not published. If the user is authenticated, * display a preview bar. If the user is anonymous, seve a 404. * * @param request The Sling HTTP Servlet Request. * @param response The Sling HTTP Servlet Response. * @param chain The Filter Chain to continue processin the response. */ @Override public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { // Since this is a Sling Filter, the request and response objects are guaranteed // to be of types SlingHttpServletRequest and SlingHttpServletResponse. final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest)request; final SlingHttpServletResponse slingResponse = (SlingHttpServletResponse)response; final Resource resource = slingRequest.getResource(); final String method = slingRequest.getMethod(); final String resourceType = resource.getResourceType(); response.setCharacterEncoding(CharEncoding.UTF_8); if ("GET".equals(method) && PublickConstants.PAGE_TYPE_BLOG.equals(resourceType)) { if (!resource.getValueMap().get("visible", false)) { final boolean authorable = userService.isAuthorable(slingRequest.getResourceResolver().adaptTo(Session.class)); /* If user is logged in and page isn't published, inject a preview bar. */ if (authorable) { PrintWriter out = response.getWriter(); CharResponseWrapper responseWrapper = new CharResponseWrapper((HttpServletResponse)response); try { chain.doFilter(request, responseWrapper); } catch (Exception e) { LOGGER.error("Could not continue chain", e); chain.doFilter(request, response); } final String servletResponse = new String(responseWrapper.toString()); final String previewHeader = getPreviewHeader(slingRequest, PREVIEW_HEADER_PATH); /* Insert component before body close tag. Append to end if body close tag doesn't exist. */ if (servletResponse != null && servletResponse.contains(INSERTION_TAG)) { String[] html = servletResponse.split(INSERTION_TAG); out.write(html[0] + INSERTION_TAG + previewHeader + html[1]); } else { out.write(servletResponse + previewHeader); } } else { /* If user is not logged in and page isn't published, forward to 404. */ slingResponse.sendError(SlingHttpServletResponse.SC_NOT_FOUND); } } else { chain.doFilter(request, slingResponse); } } else { chain.doFilter(request, slingResponse); } }