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 * <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> 018 * 019 * $Id: FancyNavigationHandler.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.util.Map; 026import java.util.Set; 027 028import javax.faces.FacesException; 029import javax.faces.application.ConfigurableNavigationHandler; 030import javax.faces.application.NavigationCase; 031import javax.faces.application.NavigationHandler; 032import javax.faces.application.ViewHandler; 033import javax.faces.component.UIViewRoot; 034import javax.faces.context.ExternalContext; 035import javax.faces.context.FacesContext; 036import javax.servlet.http.HttpServletRequest; 037 038import org.apache.commons.logging.Log; 039import org.apache.commons.logging.LogFactory; 040import org.jboss.seam.contexts.Contexts; 041import org.nuxeo.ecm.platform.ui.web.rest.api.URLPolicyService; 042import org.nuxeo.ecm.platform.ui.web.util.BaseURL; 043import org.nuxeo.runtime.api.Framework; 044 045import com.sun.faces.util.Util; 046 047/** 048 * Navigation handler that keeps outcome information available so that it can be used for a document view when 049 * redirecting to this context. 050 * 051 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> 052 */ 053public class FancyNavigationHandler extends ConfigurableNavigationHandler { 054 055 private static final Log log = LogFactory.getLog(FancyNavigationHandler.class); 056 057 private final NavigationHandler parent; 058 059 public static final String DISABLE_REDIRECT_FOR_URL_REWRITE = FancyNavigationHandler.class.getCanonicalName() 060 + ".DISABLE_REDIRECT_FOR_URL_REWRITE"; 061 062 public FancyNavigationHandler(NavigationHandler navigationHandler) { 063 parent = navigationHandler; 064 } 065 066 @Override 067 public void handleNavigation(FacesContext context, String fromAction, String outcome) { 068 ExternalContext eContext = context.getExternalContext(); 069 HttpServletRequest httpRequest = (HttpServletRequest) eContext.getRequest(); 070 // put outcome in request params 071 httpRequest.setAttribute(URLPolicyService.POST_OUTCOME_REQUEST_KEY, outcome); 072 URLPolicyService pservice = Framework.getService(URLPolicyService.class); 073 pservice.appendParametersToRequest(context); 074 // get old root to check if it's changed 075 UIViewRoot oldRoot = context.getViewRoot(); 076 parent.handleNavigation(context, fromAction, outcome); 077 UIViewRoot newRoot = context.getViewRoot(); 078 boolean rootChanged = !oldRoot.equals(newRoot); 079 if (outcome != null && !context.getResponseComplete() && !rootChanged && Framework.isDevModeSet()) { 080 // navigation was not done => maybe a hot reload issue: perform 081 // navigation again using local code because it uses 082 // information from the StaticNavigationHandler that is 083 // hot-reloaded correctly 084 // TODO: check if still relevant in JSF2 085 handleHotReloadNavigation(pservice, context, fromAction, outcome); 086 } 087 Object disable = httpRequest.getAttribute(DISABLE_REDIRECT_FOR_URL_REWRITE); 088 if (Boolean.TRUE.equals(disable)) { 089 // avoid redirect 090 return; 091 } 092 // force redirect if outcome is null so that url can be 093 // re-written except in an ajax request 094 boolean ajaxRequest = context.getPartialViewContext().isAjaxRequest(); 095 if (outcome == null && !ajaxRequest && !context.getResponseComplete()) { 096 String url = httpRequest.getRequestURL().toString(); 097 String localUrl = BaseURL.getServerURL(httpRequest, true); 098 String baseUrl = BaseURL.getServerURL(httpRequest, false); 099 if (localUrl != null && !localUrl.equals(baseUrl)) { 100 url = url.replaceFirst(localUrl, baseUrl); 101 } 102 if (Contexts.isEventContextActive()) { 103 // strip any jsession id before redirect 104 int jsessionidIndex = url.indexOf(";jsessionid"); 105 if (jsessionidIndex != -1) { 106 url = url.substring(0, jsessionidIndex); 107 } 108 // add conversation id before redirect 109 url = RestHelper.addMainConversationParameters(url); 110 } 111 try { 112 eContext.redirect(url); 113 } catch (IOException e) { 114 // do nothing... 115 log.error(e, e); 116 } 117 } 118 } 119 120 protected void handleHotReloadNavigation(URLPolicyService pservice, FacesContext context, String fromAction, 121 String outcome) { 122 String viewId = pservice.getViewIdFromOutcome(outcome, null); 123 ExternalContext extContext = context.getExternalContext(); 124 if (viewId != null) { 125 ViewHandler viewHandler = Util.getViewHandler(context); 126 // always redirect 127 String newPath = viewHandler.getActionURL(context, viewId); 128 try { 129 extContext.redirect(extContext.encodeActionURL(newPath)); 130 } catch (java.io.IOException ioe) { 131 throw new FacesException(ioe.getMessage(), ioe); 132 } 133 context.responseComplete(); 134 } 135 } 136 137 @Override 138 public NavigationCase getNavigationCase(FacesContext context, String fromAction, String outcome) { 139 if (parent instanceof ConfigurableNavigationHandler) { 140 return ((ConfigurableNavigationHandler) parent).getNavigationCase(context, fromAction, outcome); 141 } 142 return null; 143 } 144 145 @Override 146 public Map<String, Set<NavigationCase>> getNavigationCases() { 147 if (parent instanceof ConfigurableNavigationHandler) { 148 return ((ConfigurableNavigationHandler) parent).getNavigationCases(); 149 } 150 return null; 151 } 152}