Java tutorial
/** * * Copyright 2013 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package com.pagecrumb.proxy; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.pagecrumb.proxy.util.filter.GenericResponseWrapper; /** * * Filter to identify whether the response needs to be "transformed" * i.e., whether to parse and modify URLs from the HTML that is * returned * * @author Kerby Martino * */ public class ProxyFilter implements Filter { // TODO: must pass the requested domain URL and pass // down to the re-write parser protected final Log log = LogFactory.getLog(getClass()); private FilterConfig config = null; @Override public void destroy() { this.config = null; } /** * TODO Add hander for null baseURL * @param ServletRequest servletrequest - request from client * @param ervletResponse servletresponse - response from server */ @Override public void doFilter(ServletRequest servletrequest, final ServletResponse servletresponse, FilterChain chain) throws IOException, ServletException { log.info("Before invoking chain"); try { String baseURL = ((HttpServletRequest) servletrequest).getQueryString(); if (baseURL != null) { log.info(this.getClass().toString() + " " + "Requested URL: " + baseURL); // TODO Must not pass request to .css or .ico etc. to the GenericResponseWrapper // Must use regex here, every thing that ends with ".*" must not be passed except //if (baseURL.matches(".*?\\.css.*")) { // GenericResponseWrapper responseWrapper // = new GenericResponseWrapper((HttpServletResponse) servletresponse, baseURL, "css"); // chain.doFilter(servletrequest, responseWrapper); //} if (baseURL.matches(".*?\\.png.*") || baseURL.matches(".*?\\.ico.*") || baseURL.matches(".*?\\.gif.*") || baseURL.matches(".*?\\.jpeg.*") || baseURL.matches(".*?\\.jpg.*") || baseURL.matches(".*?\\.js.*")) { // Do not process Javascript for now // Pass the wrapper on to the next filter or servlet log.info("Bypassing Parser - Just do Filter"); chain.doFilter(servletrequest, servletresponse); } else { String gwtModuleBase = (String) servletrequest.getAttribute("X-GWT-Module-Base"); log.info("Module-Base: " + gwtModuleBase); GenericResponseWrapper responseWrapper = new GenericResponseWrapper( (HttpServletResponse) servletresponse, baseURL); chain.doFilter(servletrequest, responseWrapper); log.info("Content type was " + responseWrapper.getContentType()); } } else { PrintWriter pw = servletresponse.getWriter(); pw.println("<html><body><p>Oops, query URL is missing.</p></body></html>"); } } catch (ServletException e) { log.error("Caught Servlet Exception"); Throwable rootCause = e.getRootCause(); log.error("Root cause is " + rootCause.toString()); if (rootCause instanceof RuntimeException) { // This is true for any FacesException. log.error("Rethrowing exception as RuntimeException" + rootCause.toString()); throw (RuntimeException) rootCause; // Throw wrapped RuntimeException instead of ServletException. } else { throw e; } } log.info("After invoking chain"); } @Override public void init(FilterConfig filterConfig) throws ServletException { this.config = filterConfig; log.info("Logger initiated in " + log.getClass().getName()); } }