2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.

View Javadoc

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