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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */16packageorg.apache.jetspeed.util.parser;
1718import javax.servlet.http.HttpServletRequest;
1920import org.apache.turbine.util.parser.DefaultParameterParser;
2122import org.apache.jetspeed.om.registry.MediaTypeEntry;
23import org.apache.jetspeed.services.Registry;
24import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
25import org.apache.jetspeed.services.logging.JetspeedLogger;
26import org.apache.jetspeed.capability.CapabilityMap;
27import org.apache.jetspeed.capability.CapabilityMapFactory;
28import org.apache.jetspeed.services.resources.JetspeedResources;
2930/***31 * DefaultJetspeedParameterParser is a utility object to handle parsing and32 * 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 converted35 * to lowercase or uppercase when the object is initialized and when36 * new data is added. This behaviour is determined by the url.case.folding37 * property in TurbineResources.properties. Adding a name/value pair may38 * 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*/52publicclassDefaultJetspeedParameterParserextends DefaultParameterParser
53 {
54/***55 * Static initialization of the logger for this class56 */57privatestaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(DefaultJetspeedParameterParser.class.getName());
5859publicDefaultJetspeedParameterParser()
60 {
61super();
62 }
6364publicDefaultJetspeedParameterParser(String characterEncoding)
65 {
66super (characterEncoding);
67 }
6869/***70 * Sets the servlet request to be parser. This requires a71 * valid HttpServletRequest object. It will attempt to parse out72 * the GET/POST/PATH_INFO data and store the data into a Hashtable.73 * There are convenience methods for retrieving the data as a74 * number of different datatypes. The PATH_INFO data must be a75 * URLEncoded() string.76 *77 * <p>To add name/value pairs to this set of parameters, use the78 * <code>add()</code> methods.79 *80 * @param req An HttpServletRequest.81 */82publicvoid setRequest(HttpServletRequest req)
83 {
84super.setRequest(req);
8586 String enc = JetspeedResources.getString(JetspeedResources.CONTENT_ENCODING_KEY,"US-ASCII");
87CapabilityMap cm = CapabilityMapFactory.getCapabilityMap( req.getHeader("User-Agent") );
88 String mimeCode = cm.getPreferredType().getCode();
89if ( mimeCode != null )
90 {
91MediaTypeEntry media = (MediaTypeEntry)Registry.getEntry(Registry.MEDIA_TYPE, mimeCode);
92if ( media != null && media.getCharacterSet() != null)
93 {
94 enc = media.getCharacterSet();
95 }
9697 }
98if ( req.getCharacterEncoding() != null )
99 {
100 enc = req.getCharacterEncoding();
101 }
102 setCharacterEncoding( enc );
103 }
104105/***106 * Return a String for the given name. If the name does not107 * exist, return null.108 *109 * @param name A String with the name.110 * @return A String.111 */112public String getString(String name)
113 {
114 String str = super.getString(name);
115if (str == null) returnnull;
116117try118 {
119returnnew String(str.getBytes("8859_1"), getCharacterEncoding());
120 }
121catch (Exception e)
122 {
123 logger.warn("DefaultJetspeedParameterParser: Exception: " + e.toString());
124return str;
125 }
126127 }
128129 }
130