KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > util > ServletHelper


1 /*
2  * Copyright 1999-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 at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * 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 and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: ServletHelper.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.util;
21
22 import java.util.Enumeration JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.cocoon.environment.Request;
27 import org.apache.log4j.Category;
28
29 /**
30  * Servlet utility class.
31  */

32 public final class ServletHelper {
33     
34     private static Category log = Category.getInstance(ServletHelper.class);
35
36     /**
37      * Ctor.
38      */

39     private ServletHelper() {
40
41     }
42
43     /**
44      * Returns the URL inside the web application (without the context prefix).
45      * @param request The request.
46      * @return A string.
47      */

48     public static String JavaDoc getWebappURI(Request request) {
49         String JavaDoc context = request.getContextPath();
50         String JavaDoc requestUri = request.getRequestURI();
51         return getWebappURI(context, requestUri);
52     }
53
54     /**
55      * Returns the URL inside the web application (without the context prefix).
56      * @param context The context prefix.
57      * @param requestUri The complete request URI.
58      * @return A string.
59      */

60     public static String JavaDoc getWebappURI(String JavaDoc context, String JavaDoc requestUri) {
61         if (context == null) {
62             context = "";
63         }
64         String JavaDoc url = requestUri.substring(context.length());
65         if (url.length() > 0 && !url.startsWith("/")) {
66             url = "/" + url;
67         }
68         
69         log.debug(" Context prefix: [" + context + "]");
70         log.debug(" Webapp URL: [" + url + "]");
71         
72         return url;
73     }
74
75     /**
76      * Converts the request parameters to a map.
77      * If a key is mapped to multiple parameters, a string array is used as the value.
78      * @param request The request.
79      * @return A map.
80      */

81     public static Map JavaDoc getParameterMap(Request request) {
82         Map JavaDoc requestParameters = new HashMap JavaDoc();
83         for (Enumeration JavaDoc e = request.getParameterNames(); e.hasMoreElements();) {
84             String JavaDoc key = (String JavaDoc) e.nextElement();
85             String JavaDoc[] values = request.getParameterValues(key);
86             Object JavaDoc value;
87             if (values.length == 1) {
88                 value = values[0];
89             }
90             else {
91                 value = values;
92             }
93             requestParameters.put(key, value);
94         }
95         return requestParameters;
96     }
97
98 }
99
Popular Tags