Home » displaytag-1.1.1-src » org » displaytag » util » [javadoc | source]

    1   /**
    2    * Licensed under the Artistic License; you may not use this file
    3    * except in compliance with the License.
    4    * You may obtain a copy of the License at
    5    *
    6    *      http://displaytag.sourceforge.net/license.html
    7    *
    8    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
    9    * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
   10    * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
   11    */
   12   package org.displaytag.util;
   13   
   14   import java.io.UnsupportedEncodingException;
   15   import java.net.URLEncoder;
   16   import java.util.Enumeration;
   17   import java.util.HashMap;
   18   import java.util.Map;
   19   
   20   import javax.servlet.http.HttpServletRequest;
   21   import javax.servlet.http.HttpServletResponse;
   22   
   23   import org.apache.commons.lang.ObjectUtils;
   24   import org.apache.commons.lang.StringUtils;
   25   import org.apache.commons.lang.UnhandledException;
   26   import org.apache.commons.logging.Log;
   27   import org.apache.commons.logging.LogFactory;
   28   import org.displaytag.Messages;
   29   
   30   
   31   /**
   32    * Default RequestHelper implementation.
   33    * @author Fabrizio Giustina
   34    * @version $Revision: 1104 $ ($Author: fgiust $)
   35    * @see org.displaytag.util.Href
   36    * @see org.displaytag.util.RequestHelper
   37    */
   38   public class DefaultRequestHelper implements RequestHelper
   39   {
   40   
   41       /**
   42        * logger.
   43        */
   44       private static Log log = LogFactory.getLog(DefaultRequestHelper.class);
   45   
   46       /**
   47        * original HttpServletRequest.
   48        */
   49       private HttpServletRequest request;
   50   
   51       /**
   52        * original HttpServletResponse.
   53        */
   54       private HttpServletResponse response;
   55   
   56       /**
   57        * Construct a new RequestHelper for the given request.
   58        * @param servletRequest HttpServletRequest needed to generate the base href
   59        * @param servletResponse HttpServletResponse needed to encode generated urls
   60        */
   61       public DefaultRequestHelper(HttpServletRequest servletRequest, HttpServletResponse servletResponse)
   62       {
   63           this.request = servletRequest;
   64           this.response = servletResponse;
   65       }
   66   
   67       /**
   68        * @see org.displaytag.util.RequestHelper#getHref()
   69        */
   70       public Href getHref()
   71       {
   72           String requestURI = this.request.getRequestURI();
   73           // call encodeURL to preserve session id when cookies are disabled
   74           Href href = new DefaultHref(this.response.encodeURL(requestURI));
   75           href.setParameterMap(getParameterMap());
   76           return href;
   77       }
   78   
   79       /**
   80        * @see org.displaytag.util.RequestHelper#getParameter(java.lang.String)
   81        */
   82       public String getParameter(String key)
   83       {
   84           // actually simply return the parameter, this behaviour could be changed
   85           return this.request.getParameter(key);
   86       }
   87   
   88       /**
   89        * @see org.displaytag.util.RequestHelper#getIntParameter(java.lang.String)
   90        */
   91       public Integer getIntParameter(String key)
   92       {
   93           String value = this.request.getParameter(key);
   94   
   95           if (value != null)
   96           {
   97               try
   98               {
   99                   return new Integer(value);
  100               }
  101               catch (NumberFormatException e)
  102               {
  103                   // It's ok to ignore, simply return null
  104                   log.debug(Messages.getString("RequestHelper.invalidparameter", //$NON-NLS-1$
  105                       new Object[]{key, value}));
  106               }
  107           }
  108   
  109           return null;
  110       }
  111   
  112       /**
  113        * @see org.displaytag.util.RequestHelper#getParameterMap()
  114        */
  115       public Map getParameterMap()
  116       {
  117   
  118           Map map = new HashMap();
  119   
  120           // get the parameters names
  121           Enumeration parametersName = this.request.getParameterNames();
  122   
  123           while (parametersName.hasMoreElements())
  124           {
  125               // ... get the value
  126               String paramName = (String) parametersName.nextElement();
  127   
  128               request.getParameter(paramName);
  129               // put key/value in the map
  130               String[] originalValues = (String[]) ObjectUtils.defaultIfNull(
  131                   this.request.getParameterValues(paramName),
  132                   new String[0]);
  133               String[] values = new String[originalValues.length];
  134   
  135               for (int i = 0; i < values.length; i++)
  136               {
  137                   try
  138                   {
  139                       values[i] = URLEncoder.encode(StringUtils.defaultString(originalValues[i]), StringUtils
  140                           .defaultString(response.getCharacterEncoding(), "UTF8")); //$NON-NLS-1$
  141                   }
  142                   catch (UnsupportedEncodingException e)
  143                   {
  144                       throw new UnhandledException(e);
  145                   }
  146               }
  147               map.put(paramName, values);
  148   
  149           }
  150   
  151           // return the Map
  152           return map;
  153       }
  154   
  155   }

Home » displaytag-1.1.1-src » org » displaytag » util » [javadoc | source]