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

com.ocpsoft.pretty.faces.servlet.PrettyFacesWrappedResponse Maven / Gradle / Ivy

/*
 * PrettyFaces is an OpenSource JSF library to create bookmarkable URLs.
 * Copyright (C) 2010 - Lincoln Baxter, III  This program
 * is free software: you can redistribute it and/or modify it under the terms of
 * the GNU Lesser General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version. This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. You should have received a copy of the GNU Lesser General
 * Public License along with this program. If not, see the file COPYING.LESSER
 * or visit the GNU website at .
 */

package com.ocpsoft.pretty.faces.servlet;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.faces.component.UIParameter;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

import com.ocpsoft.pretty.PrettyContext;
import com.ocpsoft.pretty.PrettyException;
import com.ocpsoft.pretty.faces.config.PrettyConfig;
import com.ocpsoft.pretty.faces.config.mapping.PathParameter;
import com.ocpsoft.pretty.faces.config.mapping.UrlMapping;
import com.ocpsoft.pretty.faces.config.rewrite.RewriteRule;
import com.ocpsoft.pretty.faces.rewrite.RewriteEngine;
import com.ocpsoft.pretty.faces.url.QueryString;
import com.ocpsoft.pretty.faces.util.PrettyURLBuilder;

/**
 * @author Lincoln Baxter, III 
 */
public class PrettyFacesWrappedResponse extends HttpServletResponseWrapper
{
   private final RewriteEngine rewriteEngine = new RewriteEngine();

   public PrettyFacesWrappedResponse(final HttpServletResponse response, final PrettyConfig config)
   {
      super(response);
   }

   @Override
   @SuppressWarnings("deprecation")
   public String encodeRedirectUrl(final String url)
   {
      return super.encodeRedirectUrl(url);
   }

   @Override
   public String encodeRedirectURL(final String url)
   {
      return super.encodeRedirectURL(url);
   }

   @Override
   @SuppressWarnings("deprecation")
   public String encodeUrl(final String url)
   {
      return super.encodeUrl(url);
   }

   @Override
   public String encodeURL(final String url)
   {
      FacesContext context = FacesContext.getCurrentInstance();
      PrettyContext prettyContext = PrettyContext.getCurrentInstance();

      String result = rewritePrettyMappings(url, context, prettyContext);

      result = rewrite(result, prettyContext);

      return super.encodeURL(result);
   }

   private String rewritePrettyMappings(final String url, final FacesContext context, final PrettyContext prettyContext)
   {
      String contextPath = "";
      String result = url;

      if ((url != null) && (context != null) && !context.getResponseComplete())
      {
         String strippedUrl = prettyContext.stripContextPath(url);
         if (!url.equals(strippedUrl))
         {
            contextPath = prettyContext.getContextPath();
         }

         List matches = new ArrayList();
         for (UrlMapping m : prettyContext.getConfig().getMappings())
         {
            if (!"".equals(m.getViewId()) && strippedUrl.startsWith(m.getViewId()))
            {
               matches.add(m);
            }
         }

         Collections.sort(matches, UrlMapping.ORDINAL_COMPARATOR);

         Iterator iterator = matches.iterator();
         while (iterator.hasNext())
         {
            UrlMapping m = iterator.next();

            if (m.isOutbound())
            {
               List uiParams = new ArrayList();

               QueryString qs = QueryString.build("");
               if (url.contains("?"))
               {
                  qs.addParameters(url);
               }
               Map queryParams = qs.getParameterMap();

               List pathParams = m.getPatternParser().getPathParameters();

               int pathParamsFound = 0;
               for (PathParameter p : pathParams)
               {
                  UIParameter uip = new UIParameter();
                  String[] values = queryParams.get(p.getName());
                  if ((values != null) && (values.length > 0))
                  {
                     String value = values[0];
                     uip.setValue(value);
                     if ((value != null) && !"".equals(value))
                     {
                        pathParamsFound++;
                     }
                  }
                  queryParams.remove(p.getName());
                  uiParams.add(uip);
               }

               for (Entry entry : queryParams.entrySet())
               {
                  UIParameter uip = new UIParameter();
                  uip.setName(entry.getKey());
                  uip.setValue(entry.getValue());
                  uiParams.add(uip);
               }

               if (pathParams.size() == pathParamsFound)
               {
                  PrettyURLBuilder builder = new PrettyURLBuilder();
                  result = contextPath + builder.build(m, uiParams);
                  break;
               }
            }
         }
      }
      return result;
   }

   private String rewrite(final String url, final PrettyContext context)
   {
      String result = "";
      if (url != null)
      {
         String strippedUrl = context.stripContextPath(url);

         if (!strippedUrl.equals(url))
         {
            result = PrettyContext.getCurrentInstance().getContextPath();
         }

         try
         {
            for (RewriteRule c : context.getConfig().getGlobalRewriteRules())
            {
               strippedUrl = rewriteEngine.processOutbound(c, strippedUrl);
            }
            result += strippedUrl;
         }
         catch (Exception e)
         {
            throw new PrettyException("Error occurred during canonicalization of request <[" + url + "]>", e);
         }
      }
      return result;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy