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 public FancyURLResponseWrapper(HttpServletResponse response, HttpServletRequest request) { 056 super(response); 057 this.request = request; 058 } 059 060 protected String getViewId(String url, HttpServletRequest request) { 061 return null; 062 } 063 064 /** 065 * Rewrites url for given document view but extracts view id from the original url. 066 */ 067 protected String rewriteUrl(String url, DocumentView docView, URLPolicyService service) { 068 // try to get outcome that was saved in request by 069 // FancyNavigationHandler 070 String newViewId = (String) request.getAttribute(URLPolicyService.POST_OUTCOME_REQUEST_KEY); 071 if (newViewId != null) { 072 docView.setViewId(newViewId); 073 } else { 074 String jsfOutcome = service.getOutcomeFromUrl(url, request); 075 docView.setViewId(jsfOutcome); 076 } 077 078 int index = url.indexOf('?'); 079 if (index != -1) { 080 String uriString = url.substring(index + 1); 081 Map<String, String> parameters = URIUtils.getRequestParameters(uriString); 082 if (parameters != null) { 083 for (Map.Entry<String, String> entry : parameters.entrySet()) { 084 docView.addParameter(entry.getKey(), entry.getValue()); 085 } 086 } 087 } 088 String baseUrl = BaseURL.getBaseURL(request); 089 url = service.getUrlFromDocumentView(docView, baseUrl); 090 return url; 091 } 092 093 @Override 094 public void sendRedirect(String url) throws IOException { 095 if (request != null && "POST".equals(request.getMethod())) { 096 try { 097 URLPolicyService pservice = Framework.getService(URLPolicyService.class); 098 if (pservice.isCandidateForEncoding(request)) { 099 DocumentView docView = pservice.getDocumentViewFromRequest(request); 100 if (docView != null) { 101 url = rewriteUrl(url, docView, pservice); 102 } 103 } else { 104 URL serverURL = new URL(VirtualHostHelper.getServerURL(request)); 105 URL rewrittenURL = new URL(serverURL, url); 106 url = rewrittenURL.toString(); 107 } 108 } catch (MalformedURLException e) { 109 log.error("Could not redirect", e); 110 } 111 } 112 super.sendRedirect(url); 113 } 114}