View Javadoc

1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.jetspeed.util.parser;
17  
18  import javax.servlet.http.HttpServletRequest;
19  
20  import org.apache.turbine.util.parser.DefaultParameterParser;
21  
22  import org.apache.jetspeed.om.registry.MediaTypeEntry;
23  import org.apache.jetspeed.services.Registry;
24  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
25  import org.apache.jetspeed.services.logging.JetspeedLogger;
26  import org.apache.jetspeed.capability.CapabilityMap;
27  import org.apache.jetspeed.capability.CapabilityMapFactory;
28  import org.apache.jetspeed.services.resources.JetspeedResources;
29  
30  /***
31   * DefaultJetspeedParameterParser is a utility object to handle parsing and
32   * retrieving the data passed via the GET/POST/PATH_INFO arguments.
33   *
34   * <p>NOTE: The name= portion of a name=value pair may be converted
35   * to lowercase or uppercase when the object is initialized and when
36   * new data is added.  This behaviour is determined by the url.case.folding
37   * property in TurbineResources.properties.  Adding a name/value pair may
38   * overwrite existing name=value pairs if the names match:
39   *
40   * <pre>
41   * ParameterParser pp = data.getParameters();
42   * pp.add("ERROR",1);
43   * pp.add("eRrOr",2);
44   * int result = pp.getInt("ERROR");
45   * </pre>
46   *
47   * In the above example, result is 2.
48   *
49   * @author <a href="mailto:shinsuke@yahoo.co.jp">Shinsuke SUGAYA</a>
50   * @version $Id: DefaultJetspeedParameterParser.java,v 1.4 2004/02/23 03:18:08 jford Exp $ 
51  */
52  public class DefaultJetspeedParameterParser extends DefaultParameterParser 
53  {
54      /***
55       * Static initialization of the logger for this class
56       */    
57      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(DefaultJetspeedParameterParser.class.getName());
58      
59      public DefaultJetspeedParameterParser()
60      {      
61          super();
62      }
63  
64      public DefaultJetspeedParameterParser(String characterEncoding)
65      {
66          super (characterEncoding);
67      }
68  
69      /***
70       * Sets the servlet request to be parser.  This requires a
71       * valid HttpServletRequest object.  It will attempt to parse out
72       * the GET/POST/PATH_INFO data and store the data into a Hashtable.
73       * There are convenience methods for retrieving the data as a
74       * number of different datatypes.  The PATH_INFO data must be a
75       * URLEncoded() string.
76       *
77       * <p>To add name/value pairs to this set of parameters, use the
78       * <code>add()</code> methods.
79       *
80       * @param req An HttpServletRequest.
81       */
82      public void setRequest(HttpServletRequest req)
83      {
84          super.setRequest(req);
85  
86          String enc = JetspeedResources.getString(JetspeedResources.CONTENT_ENCODING_KEY,"US-ASCII");
87          CapabilityMap cm = CapabilityMapFactory.getCapabilityMap( req.getHeader("User-Agent") );
88          String mimeCode = cm.getPreferredType().getCode();
89          if ( mimeCode != null )
90          {
91              MediaTypeEntry media = (MediaTypeEntry)Registry.getEntry(Registry.MEDIA_TYPE, mimeCode);
92              if ( media != null && media.getCharacterSet() != null)
93              {
94                  enc = media.getCharacterSet();
95              }
96  
97          }
98          if ( req.getCharacterEncoding() != null )
99          {
100             enc = req.getCharacterEncoding();
101         }
102         setCharacterEncoding( enc );
103     }
104 
105     /***
106      * Return a String for the given name.  If the name does not
107      * exist, return null.
108      *
109      * @param name A String with the name.
110      * @return A String.
111      */
112     public String getString(String name)
113     {
114         String str = super.getString(name);
115         if (str == null) return null;
116 
117         try
118         {
119             return new String(str.getBytes("8859_1"), getCharacterEncoding());
120         }
121         catch (Exception e)
122         {
123             logger.warn("DefaultJetspeedParameterParser: Exception: " + e.toString());
124             return str;
125         }
126 
127     }
128 
129 }
130