|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ServletUtil.java | 75% | 81.2% | 100% | 80% |
|
||||||||||||||
| 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;
|
|
| 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 |
/**
|
|
| 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 | 313 |
public static String getQueryStringParameter(String theQueryString, |
| 52 |
String theParameter) |
|
| 53 |
{
|
|
| 54 | 313 |
if (theQueryString == null) |
| 55 |
{
|
|
| 56 | 0 |
return null; |
| 57 |
} |
|
| 58 |
|
|
| 59 | 313 |
String value = null;
|
| 60 |
|
|
| 61 | 313 |
int startIndex = theQueryString.indexOf(theParameter + "="); |
| 62 |
|
|
| 63 | 313 |
if (startIndex >= 0)
|
| 64 |
{
|
|
| 65 |
// add 1 for '='
|
|
| 66 | 117 |
startIndex += (theParameter.length() + 1); |
| 67 |
|
|
| 68 | 117 |
int endIndex = theQueryString.indexOf('&', startIndex);
|
| 69 |
|
|
| 70 | 117 |
if (endIndex > startIndex)
|
| 71 |
{
|
|
| 72 | 70 |
value = theQueryString.substring(startIndex, endIndex); |
| 73 |
} |
|
| 74 | 47 |
else if (endIndex == startIndex) |
| 75 |
{
|
|
| 76 | 0 |
value = "";
|
| 77 |
} |
|
| 78 |
else
|
|
| 79 |
{
|
|
| 80 | 47 |
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 | 117 |
try
|
| 86 |
{
|
|
| 87 | 117 |
value = URLDecoder.decode(value); |
| 88 |
} |
|
| 89 |
catch (Exception e)
|
|
| 90 |
{
|
|
| 91 | 0 |
throw new ChainedRuntimeException("Error URL decoding [" |
| 92 |
+ value + "]", e);
|
|
| 93 |
} |
|
| 94 |
} |
|
| 95 |
|
|
| 96 | 313 |
return value;
|
| 97 |
} |
|
| 98 |
} |
|
| 99 |
|
|
||||||||||