package com.day.cq.filter; import org.osgi.service.component.ComponentContext; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Dictionary; /** * The <code>NoExtensionRedirectFilter</code> is a request filter, which hooks * itself into the handling of requests without extension and sends an external * redirect the user to the same URL with ".html" appended * * @scr.component label="Day - No Extension Redirect Filter" description="Filter that handles requests which are missing the extension" immediate="true" * @scr.property name="filter.scope" value="request" private="true" * @scr.service */ public class NoExtensionRedirectFilter implements Filter { /** * @scr.property label="Filter Enabled" description="Setting this property enables this filter." type="Boolean" */ private static final String NOEXTENSIONREDIRECTFILTER_ISENABLED = "noextensionredirectfilter.isenabled"; private Boolean noExtensionRedirectFilterIsEnabled = false; /** * A component's implementation class may optionally implement an activate method. * If a component implements this method, this method will be called when a component configuration is activated to * provide the component instance's Component Context object. * * @param context Component Context object is used by a component instance to interact with its execution * context including locating services by reference name. * @see {@link org.osgi.service.component.ComponentContext} */ protected void activate(ComponentContext context) { setup(context); } /** * This method sets up and initializes the service. * * @param context The {@link org.osgi.service.component.ComponentContext} provided by the ContentItemServiceImpl#activate()} * method. */ private void setup(ComponentContext context) { /** * Extract the configuration properties from the component context as provided * by the felix framework. */ Dictionary properties = context.getProperties(); noExtensionRedirectFilterIsEnabled = (Boolean) properties.get(NOEXTENSIONREDIRECTFILTER_ISENABLED); if (noExtensionRedirectFilterIsEnabled == null) noExtensionRedirectFilterIsEnabled = false; } public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { try { // get HTTP requests HttpServletRequest httpRequest = (HttpServletRequest) servletRequest; HttpServletResponse httpResponse = (HttpServletResponse) servletResponse; // get URI via HTTP request cast String uri = httpRequest.getRequestURI(); // only filter if enabled and request not for some special URIs if (noExtensionRedirectFilterIsEnabled && !"/".equals(uri) && !"/cf".equals(uri) && uri.indexOf("/bin") != 0) { // check if request ends with slash -> redirect /xy/ to /xy.html if (uri.endsWith("/")) { httpResponse.sendRedirect(uri.substring(0, uri.length() - 1) + ".html"); } else { // get last part of the URI int behindlastSlashPosition = uri.lastIndexOf("/") + 1; String lastUriPart = uri.substring(behindlastSlashPosition); // check if request is missing extension -> redirect /xy to /xy.html if (lastUriPart.indexOf(".") == -1) { httpResponse.sendRedirect(uri + ".html"); } // normal URI -> continue regular filtering chain else { filterChain.doFilter(servletRequest, servletResponse); } } } // this filter did not apply -> continue regular filtering chain else { filterChain.doFilter(servletRequest, servletResponse); } } // exception -> continue regular filtering chain catch (Exception e) { filterChain.doFilter(servletRequest, servletResponse); } } /** * INIT * Does nothing. This filter has not init requirement. */ public void init(FilterConfig filterConfig) throws ServletException { } /** * DESTROY * Does nothing. This filter has not destroyal requirement. */ public void destroy() { } } |