org.apache.myfaces.shared_tomahawk.application.DefaultViewHandlerSupport Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tomahawk21 Show documentation
Show all versions of tomahawk21 Show documentation
JSF components and utilities that can be used with any JSF implementation.
This library is based on the JSF1.1 version of Tomahawk, but with minor source code and build
changes to take advantage of JSF2.1 features. A JSF2.1 implementation is required to use this
version of the Tomahawk library.
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.myfaces.shared_tomahawk.application;
import java.net.MalformedURLException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.ProjectStage;
import javax.faces.application.ViewHandler;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
import org.apache.myfaces.shared_tomahawk.renderkit.html.util.SharedStringBuilder;
import org.apache.myfaces.shared_tomahawk.util.ConcurrentLRUCache;
import org.apache.myfaces.shared_tomahawk.util.ExternalContextUtils;
import org.apache.myfaces.shared_tomahawk.util.StringUtils;
import org.apache.myfaces.shared_tomahawk.util.WebConfigParamUtils;
/**
* A ViewHandlerSupport implementation for use with standard Java Servlet engines,
* ie an engine that supports javax.servlet, and uses a standard web.xml file.
*
* @author Mathias Broekelmann (latest modification by $Author: lu4242 $)
* @version $Revision: 1401386 $ $Date: 2012-10-23 13:59:22 -0500 (Tue, 23 Oct 2012) $
*/
public class DefaultViewHandlerSupport implements ViewHandlerSupport
{
/**
* Identifies the FacesServlet mapping in the current request map.
*/
private static final String CACHED_SERVLET_MAPPING =
DefaultViewHandlerSupport.class.getName() + ".CACHED_SERVLET_MAPPING";
//private static final Log log = LogFactory.getLog(DefaultViewHandlerSupport.class);
private static final Logger log = Logger.getLogger(DefaultViewHandlerSupport.class.getName());
/**
* Controls the size of the cache used to "remember" if a view exists or not.
*/
@JSFWebConfigParam(defaultValue = "500", since = "2.0.2", group="viewhandler", tags="performance",
classType="java.lang.Integer",
desc="Controls the size of the cache used to 'remember' if a view exists or not.")
private static final String CHECKED_VIEWID_CACHE_SIZE_ATTRIBUTE = "org.apache.myfaces.CHECKED_VIEWID_CACHE_SIZE";
private static final int CHECKED_VIEWID_CACHE_DEFAULT_SIZE = 500;
/**
* Enable or disable a cache used to "remember" if a view exists or not and reduce the impact of
* sucesive calls to ExternalContext.getResource().
*/
@JSFWebConfigParam(defaultValue = "true", since = "2.0.2", expectedValues="true, false", group="viewhandler",
tags="performance",
desc="Enable or disable a cache used to 'remember' if a view exists or not and reduce the impact " +
"of sucesive calls to ExternalContext.getResource().")
private static final String CHECKED_VIEWID_CACHE_ENABLED_ATTRIBUTE =
"org.apache.myfaces.CHECKED_VIEWID_CACHE_ENABLED";
private static final boolean CHECKED_VIEWID_CACHE_ENABLED_DEFAULT = true;
private static final String VIEW_HANDLER_SUPPORT_SB = "oam.viewhandler.SUPPORT_SB";
private volatile ConcurrentLRUCache _checkedViewIdMap = null;
private Boolean _checkedViewIdCacheEnabled = null;
private final String[] _faceletsViewMappings;
private final String[] _contextSuffixes;
private final String _faceletsContextSufix;
private final boolean _initialized;
public DefaultViewHandlerSupport()
{
_faceletsViewMappings = null;
_contextSuffixes = null;
_faceletsContextSufix = null;
_initialized = false;
}
public DefaultViewHandlerSupport(FacesContext facesContext)
{
_faceletsViewMappings = getFaceletsViewMappings(facesContext);
_contextSuffixes = getContextSuffix(facesContext);
_faceletsContextSufix = getFaceletsContextSuffix(facesContext);
_initialized = true;
}
public String calculateViewId(FacesContext context, String viewId)
{
//If no viewId found, don't try to derive it, just continue.
if (viewId == null)
{
return null;
}
FacesServletMapping mapping = getFacesServletMapping(context);
if (mapping == null || mapping.isExtensionMapping())
{
viewId = handleSuffixMapping(context, viewId);
}
else if(mapping.isPrefixMapping())
{
viewId = handlePrefixMapping(viewId,mapping.getPrefix());
// A viewId that is equals to the prefix mapping on servlet mode is
// considered invalid, because jsp vdl will use RequestDispatcher and cause
// a loop that ends in a exception. Note in portlet mode the view
// could be encoded as a query param, so the viewId could be valid.
if (viewId != null && viewId.equals(mapping.getPrefix()) &&
!ExternalContextUtils.isPortlet(context.getExternalContext()))
{
throw new InvalidViewIdException();
}
}
else if (viewId != null && mapping.getUrlPattern().startsWith(viewId))
{
throw new InvalidViewIdException(viewId);
}
//if(viewId != null)
//{
// return (checkResourceExists(context,viewId) ? viewId : null);
//}
return viewId; // return null if no physical resource exists
}
public String calculateAndCheckViewId(FacesContext context, String viewId)
{
//If no viewId found, don't try to derive it, just continue.
if (viewId == null)
{
return null;
}
FacesServletMapping mapping = getFacesServletMapping(context);
if (mapping == null || mapping.isExtensionMapping())
{
viewId = handleSuffixMapping(context, viewId);
}
else if(mapping.isPrefixMapping())
{
viewId = handlePrefixMapping(viewId,mapping.getPrefix());
if(viewId != null)
{
// A viewId that is equals to the prefix mapping on servlet mode is
// considered invalid, because jsp vdl will use RequestDispatcher and cause
// a loop that ends in a exception. Note in portlet mode the view
// could be encoded as a query param, so the viewId could be valid.
if (viewId != null && viewId.equals(mapping.getPrefix()) &&
!ExternalContextUtils.isPortlet(context.getExternalContext()))
{
throw new InvalidViewIdException();
}
return (checkResourceExists(context,viewId) ? viewId : null);
}
}
else if (viewId != null && mapping.getUrlPattern().startsWith(viewId))
{
throw new InvalidViewIdException(viewId);
}
else
{
if(viewId != null)
{
return (checkResourceExists(context,viewId) ? viewId : null);
}
}
return viewId; // return null if no physical resource exists
}
public String calculateActionURL(FacesContext context, String viewId)
{
if (viewId == null || !viewId.startsWith("/"))
{
throw new IllegalArgumentException("ViewId must start with a '/': " + viewId);
}
FacesServletMapping mapping = getFacesServletMapping(context);
ExternalContext externalContext = context.getExternalContext();
String contextPath = externalContext.getRequestContextPath();
//StringBuilder builder = new StringBuilder(contextPath);
StringBuilder builder = SharedStringBuilder.get(context, VIEW_HANDLER_SUPPORT_SB);
builder.append(contextPath);
if (mapping != null)
{
if (mapping.isExtensionMapping())
{
//See JSF 2.0 section 7.5.2
String[] contextSuffixes = _initialized ? _contextSuffixes : getContextSuffix(context);
boolean founded = false;
for (String contextSuffix : contextSuffixes)
{
if (viewId.endsWith(contextSuffix))
{
builder.append(viewId.substring(0, viewId.indexOf(contextSuffix)));
builder.append(mapping.getExtension());
founded = true;
break;
}
}
if (!founded)
{
//See JSF 2.0 section 7.5.2
// - If the argument viewId has an extension, and this extension is mapping,
// the result is contextPath + viewId
//
// -= Leonardo Uribe =- It is evident that when the page is generated, the derived
// viewId will end with the
// right contextSuffix, and a navigation entry on faces-config.xml should use such id,
// this is just a workaroud
// for usability. There is a potential risk that change the mapping in a webapp make
// the same application fail,
// so use viewIds ending with mapping extensions is not a good practice.
if (viewId.endsWith(mapping.getExtension()))
{
builder.append(viewId);
}
else if(viewId.lastIndexOf(".") != -1 )
{
builder.append(viewId.substring(0,viewId.lastIndexOf(".")));
builder.append(contextSuffixes[0]);
}
else
{
builder.append(viewId);
builder.append(contextSuffixes[0]);
}
}
}
else
{
builder.append(mapping.getPrefix());
builder.append(viewId);
}
}
else
{
builder.append(viewId);
}
String calculatedActionURL = builder.toString();
if (log.isLoggable(Level.FINEST))
{
log.finest("Calculated actionURL: '" + calculatedActionURL + "' for viewId: '" + viewId + "'");
}
return calculatedActionURL;
}
/**
* Read the web.xml file that is in the classpath and parse its internals to
* figure out how the FacesServlet is mapped for the current webapp.
*/
protected FacesServletMapping getFacesServletMapping(FacesContext context)
{
Map