KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > internal > server > ServletUtil


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
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 JavaDoc;
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,v 1.1 2004/05/22 11:34:45 vmassol Exp $
30  */

31 public class ServletUtil
32 {
33     /**
34      * A substitute method for <code>HttpServletRequest.getParameter()</code>.
35      * Contrary to <code>getParameter()</code>, this method does not
36      * access the request input stream (only the query string of the url).
37      *
38      * Note: We use this method internally to retrieve Cactus parameters passed
39      * by the client side. The issue with <code>getParameter()</code> is that
40      * if you use it, then you cannot call <code>getReader()</code> or
41      * <code>getInputStream()</code> (see the Servlet spec). However, if we
42      * want to allow for testing code that uses these 2 methods (and we do !)
43      * we need to use this method to get the internal Cactus parameters.
44      *
45      * @param theQueryString the query string to parse
46      * @param theParameter the name of the parameter to locate
47      * @return the value for theParameter in theQueryString, null if
48      * theParameter does not exist and "" if the parameter exists but
49      * has no value defined in the query string
50      */

51     public static String JavaDoc getQueryStringParameter(String JavaDoc theQueryString,
52         String JavaDoc theParameter)
53     {
54         if (theQueryString == null)
55         {
56             return null;
57         }
58
59         String JavaDoc value = null;
60
61         int startIndex = theQueryString.indexOf(theParameter + "=");
62
63         if (startIndex >= 0)
64         {
65             // add 1 for '='
66
startIndex += (theParameter.length() + 1);
67
68             int endIndex = theQueryString.indexOf('&', startIndex);
69
70             if (endIndex > startIndex)
71             {
72                 value = theQueryString.substring(startIndex, endIndex);
73             }
74             else if (endIndex == startIndex)
75             {
76                 value = "";
77             }
78             else
79             {
80                 value = theQueryString.substring(startIndex);
81             }
82
83             // In JDK 1.2 URLDecoder.decode throws an Exception. This is not
84
// needed for JDK 1.3+ but needed to keep JDK 1.2.2 compatibility
85
try
86             {
87                 value = URLDecoder.decode(value);
88             }
89             catch (Exception JavaDoc e)
90             {
91                 throw new ChainedRuntimeException("Error URL decoding ["
92                     + value + "]", e);
93             }
94         }
95
96         return value;
97     }
98 }
99
Popular Tags