001/* 002 * (C) Copyright 2006-2007 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 * $Id: FancyURLResponseWrapper.java 28924 2008-01-10 14:04:05Z sfermigier $ 020 */ 021 022package org.nuxeo.ecm.platform.ui.web.rest; 023 024import java.io.IOException; 025import java.net.MalformedURLException; 026import java.net.URL; 027import java.util.Map; 028 029import javax.servlet.http.HttpServletRequest; 030import javax.servlet.http.HttpServletResponse; 031import javax.servlet.http.HttpServletResponseWrapper; 032 033import org.apache.commons.logging.Log; 034import org.apache.commons.logging.LogFactory; 035import org.nuxeo.common.utils.URIUtils; 036import org.nuxeo.ecm.platform.ui.web.rest.api.URLPolicyService; 037import org.nuxeo.ecm.platform.ui.web.util.BaseURL; 038import org.nuxeo.ecm.platform.url.api.DocumentView; 039import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper; 040import org.nuxeo.runtime.api.Framework; 041 042/** 043 * Used in post methods to rewrite the url with needed get params. Encapsulates response into a wrapper. 044 */ 045public class FancyURLResponseWrapper extends HttpServletResponseWrapper { 046 047 private static final Log log = LogFactory.getLog(FancyURLResponseWrapper.class); 048 049 protected HttpServletRequest request = null; 050 051 public FancyURLResponseWrapper(HttpServletResponse response) { 052 super(response); 053 } 054 055 /** 056 * @deprecated since 5.5: use constructor without the {@link StaticNavigationHandler} that is now wrapped into the 057 * {@link URLPolicyService} 058 */ 059 @Deprecated 060 public FancyURLResponseWrapper(HttpServletResponse response, HttpServletRequest request, 061 StaticNavigationHandler navigationHandler) { 062 this(response, request); 063 } 064 065 public FancyURLResponseWrapper(HttpServletResponse response, HttpServletRequest request) { 066 super(response); 067 this.request = request; 068 } 069 070 protected String getViewId(String url, HttpServletRequest request) { 071 return null; 072 } 073 074 /** 075 * Rewrites url for given document view but extracts view id from the original url. 076 */ 077 protected String rewriteUrl(String url, DocumentView docView, URLPolicyService service) { 078 // try to get outcome that was saved in request by 079 // FancyNavigationHandler 080 String newViewId = (String) request.getAttribute(URLPolicyService.POST_OUTCOME_REQUEST_KEY); 081 if (newViewId != null) { 082 docView.setViewId(newViewId); 083 } else { 084 String jsfOutcome = service.getOutcomeFromUrl(url, request); 085 docView.setViewId(jsfOutcome); 086 } 087 088 int index = url.indexOf('?'); 089 if (index != -1) { 090 String uriString = url.substring(index + 1); 091 Map<String, String> parameters = URIUtils.getRequestParameters(uriString); 092 if (parameters != null) { 093 for (Map.Entry<String, String> entry : parameters.entrySet()) { 094 docView.addParameter(entry.getKey(), entry.getValue()); 095 } 096 } 097 } 098 String baseUrl = BaseURL.getBaseURL(request); 099 url = service.getUrlFromDocumentView(docView, baseUrl); 100 return url; 101 } 102 103 @Override 104 public void sendRedirect(String url) throws IOException { 105 if (request != null) { 106 try { 107 URLPolicyService pservice = Framework.getService(URLPolicyService.class); 108 if (pservice.isCandidateForEncoding(request)) { 109 DocumentView docView = pservice.getDocumentViewFromRequest(request); 110 if (docView != null) { 111 url = rewriteUrl(url, docView, pservice); 112 } 113 } else { 114 URL serverURL = new URL(VirtualHostHelper.getServerURL(request)); 115 URL rewrittenURL = new URL(serverURL, url); 116 url = rewrittenURL.toString(); 117 } 118 } catch (MalformedURLException e) { 119 log.error("Could not redirect", e); 120 } 121 } 122 super.sendRedirect(url); 123 } 124}