001/* 002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 * $Id: FancyURLResponseWrapper.java 28924 2008-01-10 14:04:05Z sfermigier $ 018 */ 019 020package org.nuxeo.ecm.platform.ui.web.rest; 021 022import java.io.IOException; 023import java.net.MalformedURLException; 024import java.net.URL; 025import java.util.Map; 026 027import javax.servlet.http.HttpServletRequest; 028import javax.servlet.http.HttpServletResponse; 029import javax.servlet.http.HttpServletResponseWrapper; 030 031import org.apache.commons.logging.Log; 032import org.apache.commons.logging.LogFactory; 033import org.nuxeo.common.utils.URIUtils; 034import org.nuxeo.ecm.platform.ui.web.rest.api.URLPolicyService; 035import org.nuxeo.ecm.platform.ui.web.util.BaseURL; 036import org.nuxeo.ecm.platform.url.api.DocumentView; 037import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper; 038import org.nuxeo.runtime.api.Framework; 039 040/** 041 * Used in post methods to rewrite the url with needed get params. Encapsulates response into a wrapper. 042 */ 043public class FancyURLResponseWrapper extends HttpServletResponseWrapper { 044 045 private static final Log log = LogFactory.getLog(FancyURLResponseWrapper.class); 046 047 protected HttpServletRequest request = null; 048 049 public FancyURLResponseWrapper(HttpServletResponse response) { 050 super(response); 051 } 052 053 /** 054 * @deprecated since 5.5: use constructor without the {@link StaticNavigationHandler} that is now wrapped into the 055 * {@link URLPolicyService} 056 */ 057 @Deprecated 058 public FancyURLResponseWrapper(HttpServletResponse response, HttpServletRequest request, 059 StaticNavigationHandler navigationHandler) { 060 this(response, request); 061 } 062 063 public FancyURLResponseWrapper(HttpServletResponse response, HttpServletRequest request) { 064 super(response); 065 this.request = request; 066 } 067 068 protected String getViewId(String url, HttpServletRequest request) { 069 return null; 070 } 071 072 /** 073 * Rewrites url for given document view but extracts view id from the original url. 074 */ 075 protected String rewriteUrl(String url, DocumentView docView, URLPolicyService service) { 076 // try to get outcome that was saved in request by 077 // FancyNavigationHandler 078 String newViewId = (String) request.getAttribute(URLPolicyService.POST_OUTCOME_REQUEST_KEY); 079 if (newViewId != null) { 080 docView.setViewId(newViewId); 081 } else { 082 String jsfOutcome = service.getOutcomeFromUrl(url, request); 083 docView.setViewId(jsfOutcome); 084 } 085 086 int index = url.indexOf('?'); 087 if (index != -1) { 088 String uriString = url.substring(index + 1); 089 Map<String, String> parameters = URIUtils.getRequestParameters(uriString); 090 if (parameters != null) { 091 for (Map.Entry<String, String> entry : parameters.entrySet()) { 092 docView.addParameter(entry.getKey(), entry.getValue()); 093 } 094 } 095 } 096 String baseUrl = BaseURL.getBaseURL(request); 097 url = service.getUrlFromDocumentView(docView, baseUrl); 098 return url; 099 } 100 101 @Override 102 public void sendRedirect(String url) throws IOException { 103 if (request != null) { 104 try { 105 URLPolicyService pservice = Framework.getService(URLPolicyService.class); 106 if (pservice.isCandidateForEncoding(request)) { 107 DocumentView docView = pservice.getDocumentViewFromRequest(request); 108 if (docView != null) { 109 url = rewriteUrl(url, docView, pservice); 110 } 111 } else { 112 URL serverURL = new URL(VirtualHostHelper.getServerURL(request)); 113 URL rewrittenURL = new URL(serverURL, url); 114 url = rewrittenURL.toString(); 115 } 116 } catch (MalformedURLException e) { 117 log.error("Could not redirect", e); 118 } 119 } 120 super.sendRedirect(url); 121 } 122}