All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ocpsoft.pretty.PrettyContext Maven / Gradle / Ivy

There is a newer version: 3.3.3
Show newest version
package com.ocpsoft.pretty;

import java.io.Serializable;

import javax.faces.context.FacesContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ocpsoft.pretty.faces.config.PrettyConfig;
import com.ocpsoft.pretty.faces.config.PrettyConfigurator;
import com.ocpsoft.pretty.faces.config.mapping.UrlMapping;
import com.ocpsoft.pretty.faces.url.QueryString;
import com.ocpsoft.pretty.faces.url.URL;

/**
 * @author Lincoln Baxter, III 
 */
public class PrettyContext implements Serializable
{

   private static final String DEFAULT_ENCODING = "UTF-8";
   public static final String CONFIG_FILES_ATTR = "com.ocpsoft.pretty.CONFIG_FILES";

   private static final Log log = LogFactory.getLog(PrettyContext.class);
   private static final long serialVersionUID = -4593906924975844541L;

   public static final String PRETTY_PREFIX = "pretty:";
   private static final String CONTEXT_REQUEST_KEY = "prettyContext";

   private PrettyConfig config;

   private final String contextPath;
   private final URL url;
   private final QueryString query;

   private UrlMapping currentMapping;

   private boolean dynaviewProcessed = false;
   private boolean inNavigation = false;

   /**
    * Must create instance through the initialize() method
    */
   protected PrettyContext(final HttpServletRequest request)
   {
      contextPath = request.getContextPath();
      url = new URL(stripContextPath(request.getRequestURI()));
      String encoding = request.getCharacterEncoding() == null ? DEFAULT_ENCODING : request.getCharacterEncoding();
      url.setEncoding(encoding);
      query = QueryString.build(request.getQueryString());

      log.trace("Initialized PrettyContext");
   }

   /**
    * Get the current PrettyFaces context object, or construct a new one if it does not yet exist for this request.
    * (Delegates to FacesContext to retrieve the current HttpServletRequest object)
    * 
    * @return current context instance
    */
   public static PrettyContext getCurrentInstance()
   {
      FacesContext context = FacesContext.getCurrentInstance();
      return getCurrentInstance(context);
   }

   /**
    * Get the current PrettyFaces context object, or construct a new one if it does not yet exist for this request.
    * (Delegates to FacesContext to retrieve the current HttpServletRequest object)
    * 
    * @return current context instance
    */
   public static PrettyContext getCurrentInstance(final FacesContext context)
   {
      return getCurrentInstance((HttpServletRequest) context.getExternalContext().getRequest());
   }

   /**
    * Get the current PrettyFaces context object, or construct a new one if it does not yet exist for this request.
    * 
    * @return current context instance
    */
   public static PrettyContext getCurrentInstance(final HttpServletRequest request)
   {
      PrettyContext prettyContext = (PrettyContext) request.getAttribute(CONTEXT_REQUEST_KEY);
      if (prettyContext instanceof PrettyContext)
      {
         log.trace("Retrieved PrettyContext from Request");
         return prettyContext;
      }
      else
      {
         return newInstance(request);
      }
   }

   public static boolean isInstantiated(final ServletRequest request)
   {
      PrettyContext prettyContext = (PrettyContext) request.getAttribute(CONTEXT_REQUEST_KEY);
      if (prettyContext instanceof PrettyContext)
      {
         return true;
      }
      else
      {
         return false;
      }
   }

   /**
    * Package private -- only PrettyFilter should be calling this method -- it overwrites existing contexts in Request
    * object
    * 
    * @param request
    * @return
    */
   static PrettyContext newInstance(final HttpServletRequest request)
   {
      PrettyContext prettyContext;
      log.trace("PrettyContext not found in Request - building new instance");
      prettyContext = new PrettyContext(request);
      PrettyConfig prettyConfig = (PrettyConfig) request.getSession().getServletContext()
               .getAttribute(CONFIG_FILES_ATTR);
      prettyContext.setConfig(prettyConfig);
      request.setAttribute(CONTEXT_REQUEST_KEY, prettyContext);
      return prettyContext;
   }

   /**
    * Return the {@link URL} representing the path with which this request was populated. (Does not include
    * context-path.)
    */
   public URL getRequestURL()
   {
      return url;
   }

   /**
    * Return the {@link QueryString} representing the query-string with which this request was populated.
    * 
    * @return
    */
   public QueryString getRequestQueryString()
   {
      return query;
   }

   /**
    * Determine if this request URL is mapped by PrettyFaces
    */
   public boolean isPrettyRequest()
   {
      return getCurrentMapping() != null;
   }

   public String getContextPath()
   {
      return contextPath;
   }

   /**
    * If the given URL is prefixed with this request's context-path, return the URI without the context path. Otherwise
    * return the URI unchanged.
    */
   public String stripContextPath(String uri)
   {
      if (uri.startsWith(contextPath))
      {
         uri = uri.substring(contextPath.length());
      }
      return uri;
   }

   /**
    * Get the pretty-config.xml configurations as loaded by {@link PrettyConfigurator} (This can be dynamically
    * manipulated at runtime in order to change or add any mappings.
    */
   public PrettyConfig getConfig()
   {
      return config;
   }

   void setConfig(final PrettyConfig config)
   {
      this.config = config;
   }

   /**
    * Get the PrettyUrlMapping representing the current request.
    */
   public UrlMapping getCurrentMapping()
   {
      if (currentMapping == null)
      {
         currentMapping = config.getMappingForUrl(url);
      }
      return currentMapping;
   }

   /**
    * Return the current viewId to which the current request will be forwarded to JSF.
    */
   public String getCurrentViewId()
   {
      if (getCurrentMapping() != null)
      {
         return currentMapping.getViewId();
      }
      return "";
   }

   /**
    * Return whether or not this faces application is in the Navigation State
    */
   public boolean isInNavigation()
   {
      return inNavigation;
   }

   /**
    * Set whether or not to treat this request as if it is in the Navigation State
    */
   public void setInNavigation(final boolean value)
   {
      inNavigation = value;
   }

   /**
    * @return True if the current mapping and request should trigger DynaView capabilities.
    */
   public boolean shouldProcessDynaview()
   {
      if (isPrettyRequest())
      {
         return getCurrentMapping().isDynaView() && !isDynaviewProcessed();
      }
      return false;
   }

   /**
    * Return true if the current request has already processed the DynaView life-cycle.
    */
   public boolean isDynaviewProcessed()
   {
      return dynaviewProcessed;
   }

   public void setDynaviewProcessed(final boolean value)
   {
      dynaviewProcessed = value;
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy