001/* 002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 */ 020 021package org.nuxeo.ecm.platform.ui.web.rest; 022 023import java.io.IOException; 024 025import javax.servlet.Filter; 026import javax.servlet.FilterChain; 027import javax.servlet.FilterConfig; 028import javax.servlet.RequestDispatcher; 029import javax.servlet.ServletContext; 030import javax.servlet.ServletException; 031import javax.servlet.ServletRequest; 032import javax.servlet.ServletResponse; 033import javax.servlet.http.HttpServletRequest; 034import javax.servlet.http.HttpServletResponse; 035 036import org.apache.commons.logging.Log; 037import org.apache.commons.logging.LogFactory; 038import org.nuxeo.ecm.core.io.download.DownloadHelper; 039import org.nuxeo.ecm.platform.ui.web.rest.api.URLPolicyService; 040import org.nuxeo.ecm.platform.url.api.DocumentView; 041import org.nuxeo.runtime.api.Framework; 042 043/** 044 * Filter used to decode URLs and wrap requests to enable encoding. This filter is useful because Nuxeo support 045 * pluggable URL patterns 046 * 047 * @author tiry 048 */ 049public class FancyURLFilter implements Filter { 050 051 private static final Log log = LogFactory.getLog(FancyURLFilter.class); 052 053 protected URLPolicyService urlService; 054 055 protected ServletContext servletContext; 056 057 @Override 058 public void init(FilterConfig conf) throws ServletException { 059 log.debug("Nuxeo5 URLFilter started"); 060 servletContext = conf.getServletContext(); 061 } 062 063 protected URLPolicyService getUrlService() { 064 if (urlService == null) { 065 urlService = Framework.getService(URLPolicyService.class); 066 } 067 return urlService; 068 } 069 070 @Override 071 public void destroy() { 072 urlService = null; 073 } 074 075 @Override 076 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, 077 ServletException { 078 HttpServletRequest httpRequest = (HttpServletRequest) request; 079 HttpServletResponse httpResponse = (HttpServletResponse) response; 080 try { 081 getUrlService(); 082 // initialize its view id manager if necessary 083 urlService.initViewIdManager(servletContext, httpRequest, httpResponse); 084 085 // check if this is an URL that needs to be parsed 086 if (urlService.isCandidateForDecoding(httpRequest)) { 087 DocumentView docView = urlService.getDocumentViewFromRequest(httpRequest); 088 // if parse succeeded => process 089 if (docView != null) { 090 // put it in request 091 urlService.setDocumentViewInRequest(httpRequest, docView); 092 093 // get the view id for navigation from the stored outcome 094 String jsfOutcome = docView.getViewId(); 095 096 // get target page according to navigation rules 097 String target = urlService.getViewIdFromOutcome(jsfOutcome, httpRequest); 098 099 // dispatch 100 RequestDispatcher dispatcher; 101 if (target != null) { 102 dispatcher = httpRequest.getRequestDispatcher(target); 103 } else { 104 // Use a dummy dispatcher if the target is not needed. 105 // This comes handy for instance for nxfile url 106 dispatcher = httpRequest.getRequestDispatcher("/malformed_url_error_page.faces"); 107 } 108 // set force encoding in case forward triggers a 109 // redirect (when a seam page is processed for instance). 110 request.setAttribute(URLPolicyService.FORCE_URL_ENCODING_REQUEST_KEY, Boolean.TRUE); 111 // forward request to the target viewId 112 dispatcher.forward(new FancyURLRequestWrapper(httpRequest, docView), 113 wrapResponse(httpRequest, httpResponse)); 114 return; 115 } 116 } 117 118 // do not filter if it's candidate for encoding so soon : document 119 // view has not been set in the request yet => always wrap 120 chain.doFilter(request, wrapResponse(httpRequest, httpResponse)); 121 122 } catch (IOException e) { 123 String url = httpRequest.getRequestURL().toString(); 124 if (DownloadHelper.isClientAbortError(e)) { 125 DownloadHelper.logClientAbort(e); 126 if (log.isDebugEnabled()) { 127 log.debug(String.format("Client disconnected from URL %s : %s", url, e.getMessage())); 128 } 129 } else { 130 throw new IOException("On requestURL: " + url, e); 131 } 132 } catch (ServletException e) { 133 String url = httpRequest.getRequestURL().toString(); 134 if (DownloadHelper.isClientAbortError(e)) { 135 DownloadHelper.logClientAbort(e); 136 if (log.isDebugEnabled()) { 137 log.debug(String.format("Client disconnected from URL %s : %s", url, e.getMessage())); 138 } 139 } else { 140 throw new ServletException("On requestURL: " + url, e); 141 } 142 } 143 144 } 145 146 protected ServletResponse wrapResponse(HttpServletRequest request, HttpServletResponse response) { 147 return new FancyURLResponseWrapper(response, request); 148 } 149 150}