01: /*
02: * ========================================================================
03: *
04: * Copyright 2001-2004 The Apache Software Foundation.
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: *
18: * ========================================================================
19: */
20: package org.apache.cactus.internal.server;
21:
22: import java.net.URLDecoder;
23:
24: import org.apache.cactus.util.ChainedRuntimeException;
25:
26: /**
27: * All prupose utility methods for manipulating the Servlet API.
28: *
29: * @version $Id: ServletUtil.java 238991 2004-05-22 11:34:50Z vmassol $
30: */
31: public class ServletUtil {
32: /**
33: * A substitute method for <code>HttpServletRequest.getParameter()</code>.
34: * Contrary to <code>getParameter()</code>, this method does not
35: * access the request input stream (only the query string of the url).
36: *
37: * Note: We use this method internally to retrieve Cactus parameters passed
38: * by the client side. The issue with <code>getParameter()</code> is that
39: * if you use it, then you cannot call <code>getReader()</code> or
40: * <code>getInputStream()</code> (see the Servlet spec). However, if we
41: * want to allow for testing code that uses these 2 methods (and we do !)
42: * we need to use this method to get the internal Cactus parameters.
43: *
44: * @param theQueryString the query string to parse
45: * @param theParameter the name of the parameter to locate
46: * @return the value for theParameter in theQueryString, null if
47: * theParameter does not exist and "" if the parameter exists but
48: * has no value defined in the query string
49: */
50: public static String getQueryStringParameter(String theQueryString,
51: String theParameter) {
52: if (theQueryString == null) {
53: return null;
54: }
55:
56: String value = null;
57:
58: int startIndex = theQueryString.indexOf(theParameter + "=");
59:
60: if (startIndex >= 0) {
61: // add 1 for '='
62: startIndex += (theParameter.length() + 1);
63:
64: int endIndex = theQueryString.indexOf('&', startIndex);
65:
66: if (endIndex > startIndex) {
67: value = theQueryString.substring(startIndex, endIndex);
68: } else if (endIndex == startIndex) {
69: value = "";
70: } else {
71: value = theQueryString.substring(startIndex);
72: }
73:
74: // In JDK 1.2 URLDecoder.decode throws an Exception. This is not
75: // needed for JDK 1.3+ but needed to keep JDK 1.2.2 compatibility
76: try {
77: value = URLDecoder.decode(value);
78: } catch (Exception e) {
79: throw new ChainedRuntimeException(
80: "Error URL decoding [" + value + "]", e);
81: }
82: }
83:
84: return value;
85: }
86: }
|