KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > go > teaservlet > SpiderableRequest


1 /* ====================================================================
2  * TeaServlet - Copyright (c) 1999-2000 Walt Disney Internet Group
3  * ====================================================================
4  * The Tea Software License, Version 1.1
5  *
6  * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Walt Disney Internet Group (http://opensource.go.com/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact opensource@dig.com.
31  *
32  * 5. Products derived from this software may not be called "Tea",
33  * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet",
34  * "Kettle", "Trove" or "BeanDoc" appear in their name, without prior
35  * written permission of the Walt Disney Internet Group.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS
41  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
42  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
43  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
44  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
45  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * For more information about Tea, please see http://opensource.go.com/.
51  */

52
53 package com.go.teaservlet;
54
55 import java.util.*;
56 import javax.servlet.http.*;
57 import com.go.teaservlet.util.FilteredHttpServletRequest;
58
59 /******************************************************************************
60  * Allows HTTP requests to be 'spiderable', that is, support parameters
61  * without using '?', '&' and '=' characters so that search engines will
62  * index the request URL. The request URI is broken down, providing additional
63  * parameters, and possibly altering the PathInfo. If the URL contains the
64  * normal query separator, '?', then any parameters specified after it are
65  * preserved.
66  *
67  * @author Brian S O'Neill
68  * @version
69  * <!--$$Revision:--> 10 <!-- $-->, <!--$$JustDate:--> 9/07/00 <!-- $-->
70  */

71 class SpiderableRequest extends FilteredHttpServletRequest {
72     private final String JavaDoc mQuerySeparator;
73     private final String JavaDoc mParameterSeparator;
74     private final String JavaDoc mValueSeparator;
75
76     private final int mQuerySepLen;
77     private final int mParamSepLen;
78     private final int mValSepLen;
79
80     private String JavaDoc mPathInfo;
81     private Map mParameters;
82
83     public SpiderableRequest(HttpServletRequest request,
84                              String JavaDoc querySeparator,
85                              String JavaDoc parameterSeparator,
86                              String JavaDoc valueSeparator) {
87         super(request);
88         mQuerySeparator = querySeparator;
89         mParameterSeparator = parameterSeparator;
90         mValueSeparator = valueSeparator;
91
92         mQuerySepLen = querySeparator.length();
93         mParamSepLen = parameterSeparator.length();
94         mValSepLen = valueSeparator.length();
95
96         mPathInfo = request.getPathInfo();
97         mParameters = new HashMap(37);
98         
99         fillParameters();
100         fillDefaultParameters();
101     }
102
103     public String JavaDoc getPathInfo() {
104         return mPathInfo;
105     }
106     
107     public String JavaDoc getParameter(String JavaDoc name) {
108         String JavaDoc[] values = getParameterValues(name);
109         if (values != null && values.length > 0) {
110             return values[0];
111         }
112         return null;
113     }
114
115     public String JavaDoc[] getParameterValues(String JavaDoc name) {
116         return (String JavaDoc[])mParameters.get(name);
117     }
118
119     public Enumeration getParameterNames() {
120         return Collections.enumeration(mParameters.keySet());
121     }
122
123     private void fillParameters() {
124         String JavaDoc str;
125         int startIndex, midIndex, endIndex;
126         
127         if ("?".equals(mQuerySeparator)) {
128             if ((str = mRequest.getQueryString()) == null) {
129                 return;
130             }
131             startIndex = 0;
132         }
133         else {
134             startIndex = mPathInfo.indexOf(mQuerySeparator);
135             if (startIndex >= 0) {
136                 mPathInfo = mPathInfo.substring(0, startIndex);
137             }
138
139             str = mRequest.getRequestURI();
140             startIndex = str.indexOf(mQuerySeparator);
141             if (startIndex < 0) {
142                 return;
143             }
144             startIndex += mQuerySepLen;
145         }
146         
147         int length = str.length();
148         
149         for (; startIndex < length; startIndex = endIndex + mParamSepLen) {
150             endIndex = str.indexOf(mParameterSeparator, startIndex);
151             
152             if (endIndex < 0) {
153                 endIndex = length;
154             }
155             
156             midIndex = str.indexOf(mValueSeparator, startIndex);
157             
158             String JavaDoc key;
159             String JavaDoc value;
160             
161             if (midIndex < 0 || midIndex > endIndex) {
162                 if (endIndex - startIndex > 1) {
163                     key = str.substring(startIndex, endIndex);
164                     value = "";
165                 }
166                 else {
167                     continue;
168                 }
169             }
170             else if (midIndex - startIndex > 1) {
171                 key = str.substring(startIndex, midIndex);
172                 value = str.substring(midIndex + mValSepLen, endIndex);
173             }
174             else {
175                 continue;
176             }
177             
178             putParameter(key, value);
179         }
180     }
181
182     private void fillDefaultParameters() {
183         Enumeration names = mRequest.getParameterNames();
184         while (names.hasMoreElements()) {
185             String JavaDoc name = (String JavaDoc)names.nextElement();
186             putParameters(name, mRequest.getParameterValues(name));
187         }
188     }
189
190     private void putParameter(String JavaDoc key, String JavaDoc value) {
191         String JavaDoc[] currentValues = (String JavaDoc[])mParameters.get(key);
192         if (currentValues == null) {
193             currentValues = new String JavaDoc[] {value};
194         }
195         else {
196             String JavaDoc[] newValues = new String JavaDoc[currentValues.length + 1];
197             int i;
198             for (i=0; i<currentValues.length; i++) {
199                 newValues[i] = currentValues[i];
200             }
201             currentValues = newValues;
202             currentValues[i] = value;
203         }
204
205         mParameters.put(key, currentValues);
206     }
207
208
209     private void putParameters(String JavaDoc key, String JavaDoc[] values) {
210         if (values == null || values.length == 0) {
211             return;
212         }
213         else if (values.length == 1) {
214             putParameter(key, values[0]);
215             return;
216         }
217
218         String JavaDoc[] currentValues = (String JavaDoc[])mParameters.get(key);
219         if (currentValues == null) {
220             currentValues = values;
221         }
222         else {
223             String JavaDoc[] newValues =
224                 new String JavaDoc[currentValues.length + values.length];
225             int i, j;
226             for (i=0; i<currentValues.length; i++) {
227                 newValues[i] = currentValues[i];
228             }
229             for (j=0; j<values.length; j++) {
230                 newValues[i + j] = values[j];
231             }
232             currentValues = newValues;
233         }
234         
235         mParameters.put(key, currentValues);
236     }
237 }
238
Popular Tags