KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > acting > RequestParameterExistsAction


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 package org.apache.cocoon.acting;
17
18 import java.util.Collections JavaDoc;
19 import java.util.Enumeration JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import org.apache.avalon.framework.parameters.Parameters;
25 import org.apache.cocoon.environment.ObjectModelHelper;
26 import org.apache.cocoon.environment.Redirector;
27 import org.apache.cocoon.environment.Request;
28 import org.apache.cocoon.environment.SourceResolver;
29
30 /**
31  * This action simply checks to see if a given request parameter
32  * exists. It takes an arbitrary number of default parameters to check
33  * named 'parameter-name'. Non-default parameters need to be separated
34  * by spaces and passed as value of a sitemap parameter named
35  * 'parameters'. The action returns a map with all parameters if all
36  * of them exist and null otherwise. Parameter names can only be added
37  * to this list but no default parameters can be overridden by
38  * specific ones.
39  *
40  * <p>This action is very closely related to @link{RequestParamAction}
41  * and {@link FormValidatorAction}. However this action is considerably
42  * simpler in that it tests only for existence of a parameter and it
43  * doesn't need a descriptor. Besides it doesn't propagate all request
44  * parameters to the sitemap but only those that are marked as
45  * required.</p> <p> One special feature is, however, that parameters
46  * can contain <strong>one</strong> wildcard ("*"). It will be
47  * checked, whether all parameters with a wildcard have the same
48  * matches. E.g. "id_* name_*" enforces, that if "id_1" exists,
49  * "name_1" must also exist and vice versa.</p>
50  *
51  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
52  * @version CVS $Id: RequestParameterExistsAction.java 124685 2005-01-08 22:20:56Z antonio $
53  */

54 public class RequestParameterExistsAction extends AbstractConfigurableAction
55 {
56
57     protected static class StringParts {
58         String JavaDoc prefix = null;
59         String JavaDoc pstfix = null;
60         int count = 0;
61
62         public StringParts (String JavaDoc pre, String JavaDoc post) {
63             prefix = pre;
64             pstfix = post;
65         }
66     }
67
68
69
70     public Map JavaDoc act( Redirector redirector,
71             SourceResolver resolver,
72             Map JavaDoc objectModel,
73             String JavaDoc source,
74             Parameters parameters
75             )
76     throws Exception JavaDoc {
77         Request request = ObjectModelHelper.getRequest(objectModel);
78         HashMap JavaDoc results = new HashMap JavaDoc();
79         HashMap JavaDoc items = new HashMap JavaDoc();
80         int wildcards = 0;
81
82         // check default parameters for existence
83
if (this.getLogger().isDebugEnabled()) {
84             getLogger().debug("checking default parameters");
85         }
86         Iterator JavaDoc reqParams = settings.values().iterator();
87         while (reqParams.hasNext()) {
88             String JavaDoc paramName = (String JavaDoc) reqParams.next();
89             StringParts sp = splitParameter(paramName);
90             if (sp != null) {
91                 // wildcard: special care required (deferred until later)
92
items.put(new Integer JavaDoc(wildcards++), sp);
93                 if (this.getLogger().isDebugEnabled()) {
94                     getLogger().debug("(default) deferring " + paramName);
95                 }
96             } else {
97                 String JavaDoc paramValue = request.getParameter(paramName);
98                 if (paramValue == null) {
99                     return null;
100                 }
101                 results.put(paramName, paramValue);
102             }
103         }
104
105         // check parameters for existence
106
if (this.getLogger().isDebugEnabled()) {
107             getLogger().debug("checking sitemap parameters");
108         }
109         String JavaDoc params = parameters.getParameter("parameters", null);
110         if (params != null) {
111             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(params);
112             while (st.hasMoreTokens()) {
113                 String JavaDoc paramName = st.nextToken();
114                 StringParts sp = splitParameter(paramName);
115                 if (sp != null) {
116                     // wildcard: special care required (deferred until later)
117
items.put(new Integer JavaDoc(wildcards++), sp);
118                     if (this.getLogger().isDebugEnabled()) {
119                         getLogger().debug("deferring " + paramName);
120                     }
121                 } else {
122
123                     String JavaDoc paramValue = request.getParameter(paramName);
124                     if (paramValue == null) {
125                         return null;
126                     }
127                     results.put(paramName, paramValue);
128                 }
129             }
130         }
131
132         if (wildcards != 0) {
133             // special care for parameters with wildcard
134
//
135
if (this.getLogger().isDebugEnabled()) {
136                 getLogger().debug("deferred checking for parameters: " + wildcards);
137             }
138
139             // first one
140
//
141
if (this.getLogger().isDebugEnabled()) {
142                 getLogger().debug(" checking first");
143             }
144             HashMap JavaDoc values = new HashMap JavaDoc();
145             StringParts sp1 = (StringParts) items.get(new Integer JavaDoc(0));
146             if (this.getLogger().isDebugEnabled()) {
147                 getLogger().debug(
148                     " Parameter is [" + sp1.prefix + " * " + sp1.pstfix + "] ");
149             }
150             Enumeration JavaDoc requestParams = request.getParameterNames();
151             Boolean JavaDoc dummy = Boolean.TRUE;
152             while (requestParams.hasMoreElements()) {
153                 String JavaDoc paramName = (String JavaDoc) requestParams.nextElement();
154                 String JavaDoc match = getMatch(paramName, sp1);
155                 if (match != null) {
156                     if (this.getLogger().isDebugEnabled()) {
157                         getLogger().debug(
158                             " value is >"
159                             + match
160                             + "< "
161                             + sp1.prefix.length()
162                             + " "
163                             + paramName.length()
164                             + " "
165                             + sp1.pstfix.length());
166                     }
167                     values.put(match, dummy);
168                     sp1.count++;
169                     if (this.getLogger().isDebugEnabled()) {
170                         getLogger().debug(
171                             " Parameter "
172                             + sp1.prefix
173                             + "*"
174                             + sp1.pstfix
175                             + " matches "
176                             + paramName
177                             + " ("
178                             + sp1.count
179                             + " so far)");
180                     }
181                     String JavaDoc paramValue = request.getParameter(paramName);
182                     if (paramValue == null) {
183                         return null;
184                     }
185                     results.put(paramName, paramValue);
186                 }
187             }
188
189             if (sp1.count == 0) {
190                 if (this.getLogger().isDebugEnabled()) {
191                     getLogger().debug(
192                        " Parameter "
193                         + sp1.prefix
194                         + "*"
195                         + sp1.pstfix
196                         + " matches "
197                         + sp1.count);
198                 }
199                 return null;
200             }
201
202             // all other ones
203
//
204
if (this.getLogger().isDebugEnabled()) {
205                 getLogger().debug(" checking others");
206             }
207             requestParams = request.getParameterNames();
208             while (requestParams.hasMoreElements()) {
209                 String JavaDoc paramName = (String JavaDoc) requestParams.nextElement();
210                 if (this.getLogger().isDebugEnabled()) {
211                     getLogger().debug(" checking request parameter " + paramName);
212                 }
213                 for (int i = wildcards - 1; i > 0; i--) {
214                     if (this.getLogger().isDebugEnabled()) {
215                         getLogger().debug(" checking against " + i);
216                     }
217                     StringParts sp = (StringParts) items.get(new Integer JavaDoc(i));
218                     String JavaDoc match = getMatch(paramName, sp);
219                     if (this.getLogger().isDebugEnabled()) {
220                         getLogger().debug(
221                             " Parameter is ["
222                             + sp.prefix
223                             + " * "
224                             + sp.pstfix
225                             + "] ");
226                     }
227                     if (match != null) {
228                         if (this.getLogger().isDebugEnabled()) {
229                             getLogger().debug(
230                                 " Parameter "
231                                     + sp.prefix
232                                     + "*"
233                                     + sp.pstfix
234                                     + " matches "
235                                     + paramName
236                                     + " ("
237                                     + sp.count
238                                     + " so far)");
239                         }
240                         if (values.containsKey(match)) {
241                             sp.count++;
242                             if (this.getLogger().isDebugEnabled()) {
243                                 getLogger().debug(
244                                 " " + paramName + " (verified)");
245                             }
246                             String JavaDoc paramValue = request.getParameter(paramName);
247                             if (paramValue == null) {
248                                 return null;
249                             }
250                             results.put(paramName, paramValue);
251
252                         } else {
253                             if (this.getLogger().isDebugEnabled()) {
254                                 getLogger().debug(
255                                     "Match "
256                                     + match
257                                     + "not found for "
258                                     + sp1.prefix
259                                     + "*"
260                                     + sp1.pstfix
261                                     + " but for "
262                                     + sp.prefix
263                                     + "*"
264                                     + sp.pstfix);
265                             }
266                             return null;
267                         }
268                     }
269                 }
270             }
271
272             // since we enforce that only matches are counted, that exist for
273
// the first parameter as well, check if every parameter has an
274
// equal number of matches.
275
//
276
if (this.getLogger().isDebugEnabled()) {
277                 getLogger().debug("checking number of matches");
278             }
279             for (int i = wildcards - 1; i > 0; i--) {
280                 StringParts sp = (StringParts) items.get(new Integer JavaDoc(i));
281                 if (sp.count != sp1.count) {
282                     if (this.getLogger().isDebugEnabled()) {
283                         getLogger().debug(
284                             "Found "
285                             + sp.count
286                             + " matches for "
287                             + sp.prefix
288                             + "*"
289                             + sp.pstfix
290                             + " but expected "
291                             + sp1.count);
292                     }
293                     return null;
294                 } else {
295                     if (this.getLogger().isDebugEnabled()) {
296                         getLogger().debug(
297                             "Found "
298                             + sp.count
299                             + " matches for "
300                             + sp.prefix
301                             + "*"
302                             + sp.pstfix
303                             + " as expected");
304                     }
305                 }
306             }
307
308         }
309
310         return Collections.unmodifiableMap(results);
311     }
312
313
314     /**
315      * Find first "*" in a String and split it into the substring
316      * before and after the "*". Returns null if no "*" is present.
317      */

318     protected StringParts splitParameter( String JavaDoc paramName )
319     {
320     int idx = paramName.indexOf("*");
321     if ( idx != -1 ) {
322         return new StringParts(paramName.substring(0,idx), paramName.substring(idx+1));
323     } else {
324         return null;
325     }
326     }
327
328     /**
329      * If a String matches a StringPart spec, return the substring
330      * between the specified prefix and postfix. Returns null if it
331      * doesn't match.
332      */

333     protected String JavaDoc getMatch( String JavaDoc paramName,
334                    StringParts sp
335                    )
336     {
337     if ( paramName.startsWith( sp.prefix ) && paramName.endsWith( sp.pstfix ) ) {
338         return paramName.substring( sp.prefix.length(), ( paramName.length() - sp.pstfix.length() ) );
339     } else {
340         return null;
341     }
342     }
343
344 }
345
Popular Tags