dev@javaserverfaces.java.net

Re: [REVIEW] ConfigureListener - check for FacesServlet before performing faces configuration steps

From: Ryan Lubke <Ryan.Lubke_at_Sun.COM>
Date: Fri, 18 Feb 2005 09:19:43 -0500

Jacob Hookom wrote:

> I thought there would be a possiblity to determine the mapping of the
> request, just by examining the request path info, this could possibly
> remove the need to inspect any web.xmls for purposes of serving the
> ViewHandler. The only caveat is that the servlet must be hit before
> action uri's are generated.
>
> Basically, if extCtx.getRequestPathInfo() == null, then we know it's a
> suffix mapping. We can then take the requested URI and grab the
> suffix of the incoming request (uri.substring(uri.lastIndexOf('.')))
> and replace it with ViewHandler.DEFAULT_SUFFIX. Otherwise, the
> requested servlet is using a prefix mapping and you can grab the
> extCtx.getRequestServletPath(). This information is cached within the
> ViewHandler for conversion back into Action Ids (.jsp->.jsf).
>
> I've attached support code for a ViewHandler implementation that I
> attempted before participating in the RI, it might be something to
> look at?
>
> -- Jacob Hookom (McKesson Medical)


Actually the current ViewHandlerImpl does something similar to what
you've suggested.

>
> Adam Winer wrote:
>
>> Ed Burns wrote:
>>
>>>>>>>> On Thu, 17 Feb 2005 15:42:01 -0500, Ryan Lubke
>>>>>>>> <Ryan.Lubke_at_Sun.COM> said:
>>>>>>>
>>>>>>>
>>>
>>>
>>> RL> Consider the case where JSF is installed in the common
>>> classloader of RL> Tomcat, or SJSAS.
>>> RL> With this configuration, the ConfigureListener will be invoked
>>> for every RL> web application
>>> RL> whether or not it uses JSF.
>>>
>>> RL> This modification to ConfigureListener will process the web.xml
>>> of the RL> current web application
>>> RL> scanning for the presence of javax.faces.webapp.FacesServlet.
>>> If found, RL> perform the normal
>>> RL> application processing, otherwise return.
>>>
>>> As it turns out, an app is not required to map the FacesServlet by the
>>> spec.
>>
>>
>>
>> IIRC, ViewHandlerImpl in the RI does in fact look for
>> FacesServlet to figure out servlet mappings, so there is
>> currently a requirement to use FacesServlet. That hard
>> dependency is not necessary, because ViewHandlerImpl could simply
>> look for *all* servlet mappings, and whichever servlet
>> mapping corresponds to the current request is necessarily
>> the correct servlet, whether or not it's FacesServlet.
>>
>>> Check out
>>> <http://www.theserverside.com/news/thread.tss?thread_id=31626#155689>.
>>> So, I think we should consider developing a very robust heuristic, of
>>> which "does it map the FacesServlet?" is a part. How about:
>>>
>>> If any of the following are true, we know this app definately uses
>>> Faces.
>>>
>>> * It maps the FacesServlet
>>>
>>> * It has a faces-config (or similar) file (check the init param)
>>>
>>> If neither of these are true, you probably don't have a faces app.
>>>
>>> What do you all think?
>>
>>
>>
>> Seems viable.
>>
>> -- Adam
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe_at_javaserverfaces.dev.java.net
>> For additional commands, e-mail: dev-help_at_javaserverfaces.dev.java.net
>>
>>
>
>
>------------------------------------------------------------------------
>
>/*
> * Copyright 1999,2004 The Apache Software Foundation.
> *
> * Licensed 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 net.java.strux.application.support;
>
>import java.io.Serializable;
>import java.util.Map;
>
>import javax.faces.FacesException;
>import javax.faces.application.ViewHandler;
>import javax.faces.context.ExternalContext;
>import javax.faces.context.FacesContext;
>
>import net.java.strux.Jsf;
>
>
>
>/**
> * @author Jacob Hookom
> */
>public class ActionMapping implements Serializable
>{
> public static final String ACTION_MAPPING_KEY = ActionMapping.class.getName();
> public static final String DEFAULT_MAPPING = ".jsf";
> public static final int TYPE_UNKNOWN = -1;
> public static final int TYPE_SUFFIX = 1;
> public static final int TYPE_PREFIX = 0;
>
> protected int type = TYPE_SUFFIX;
> protected String mapping = DEFAULT_MAPPING;
> protected String defaultSuffix = null;
>
> public ActionMapping()
> {
> // do nothing
> }
>
> public static ActionMapping getCurrentInstance(FacesContext context, int scope) throws FacesException
> {
> Map scopeMap = Jsf.getScope(context, scope);
> ActionMapping actionMapping = (ActionMapping) scopeMap.get(ACTION_MAPPING_KEY);
> if (actionMapping == null)
> {
> actionMapping = new ActionMapping();
> scopeMap.put(ACTION_MAPPING_KEY, actionMapping);
> }
> return actionMapping;
> }
>
> public String getDefaultSuffix(FacesContext context) throws FacesException
> {
> if (this.defaultSuffix == null)
> {
> ExternalContext extCtx = context.getExternalContext();
> String viewSuffix = extCtx.getInitParameter(ViewHandler.DEFAULT_SUFFIX_PARAM_NAME);
> this.defaultSuffix = (viewSuffix != null) ? viewSuffix : ViewHandler.DEFAULT_SUFFIX;
> }
> return this.defaultSuffix;
> }
>
> /**
> * @param context
> * @param viewId
> * @return
> * @throws FacesException
> */
> public String convertToActionId(FacesContext context, String viewId) throws FacesException
> {
> String actionId = null;
> if (TYPE_PREFIX == this.type)
> {
> if (viewId.startsWith(this.mapping))
> {
> actionId = viewId;
> }
> else
> {
> actionId = this.mapping + viewId;
> }
> }
> else if (TYPE_SUFFIX == this.type)
> {
> actionId = viewId.replaceFirst(this.getDefaultSuffix(context), this.mapping);
> }
> else
> {
> throw new FacesException(Jsf.msg("view.error.actionId",viewId));
> }
> return actionId;
> }
>
> /**
> * Looks at the current context and updates its state while converting the actionId
> * to a viewId. Previous state is ignored when this is called since we can't determine the
> * mapping for the life of the request.
> *
> * @param context
> * @param actionId
> * @return
> * @throws FacesException
> */
> public String convertToViewId(FacesContext context, String actionId) throws FacesException
> {
> ExternalContext extCtx = context.getExternalContext();
> String viewId = extCtx.getRequestPathInfo();
> if (extCtx.getRequestPathInfo() == null)
> {
> // we know it's a suffix mapping
> String facesSuffix = actionId.substring(actionId.lastIndexOf('.'));
> String viewSuffix = this.getDefaultSuffix(context);
> viewId = actionId.replaceFirst(facesSuffix, viewSuffix);
>
> synchronized (this)
> {
> this.type = TYPE_SUFFIX;
> this.mapping = facesSuffix;
> }
> }
> else
> {
> // else it's a prefix mapping
> synchronized (this)
> {
> this.type = TYPE_PREFIX;
> this.mapping = extCtx.getRequestServletPath();
> }
> }
> return viewId;
> }
>
> public String getMapping()
> {
> return this.mapping;
> }
>
> public synchronized void setMapping(String mapping)
> {
> this.mapping = mapping;
> }
>
> public int getType()
> {
> return this.type;
> }
>
> public synchronized void setType(int type)
> {
> this.type = type;
> }
>}
>
>
>
>------------------------------------------------------------------------
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: dev-unsubscribe_at_javaserverfaces.dev.java.net
>For additional commands, e-mail: dev-help_at_javaserverfaces.dev.java.net
>
>