KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > modules > input > HeaderAttributeModule


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 package org.apache.cocoon.components.modules.input;
18
19 import org.apache.avalon.framework.configuration.Configuration;
20 import org.apache.avalon.framework.configuration.ConfigurationException;
21 import org.apache.avalon.framework.thread.ThreadSafe;
22 import org.apache.cocoon.environment.ObjectModelHelper;
23 import org.apache.cocoon.environment.Request;
24
25 import java.util.Enumeration JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.LinkedList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.SortedSet JavaDoc;
31 import java.util.TreeSet JavaDoc;
32
33 /**
34  * HeaderAttributeModule accesses request header attributes. If the
35  * attribute name contains an askerisk "*" this is considered a
36  * wildcard and all attributes that would match this wildcard are
37  * considered to be part of an array of that name for
38  * getAttributeValues. Only one "*" is allowed.
39  *
40  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
41  * @version CVS $Id: HeaderAttributeModule.java 30932 2004-07-29 17:35:38Z vgritsenko $ */

42 public class HeaderAttributeModule extends AbstractInputModule implements ThreadSafe {
43     
44     public Object JavaDoc getAttribute( String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel )
45         throws ConfigurationException {
46
47         String JavaDoc pname = (String JavaDoc) this.settings.get("parameter",name);
48         if ( modeConf != null ) {
49             pname = modeConf.getAttribute( "parameter", pname );
50             // preferred
51
pname = modeConf.getChild("parameter").getValue(pname);
52         }
53         return ObjectModelHelper.getRequest(objectModel).getHeader( pname );
54     }
55
56
57     public Iterator JavaDoc getAttributeNames( Configuration modeConf, Map JavaDoc objectModel )
58         throws ConfigurationException {
59
60         return new IteratorHelper(ObjectModelHelper.getRequest(objectModel).getHeaderNames());
61     }
62
63
64     public Object JavaDoc[] getAttributeValues( String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel )
65         throws ConfigurationException {
66
67         Request request = ObjectModelHelper.getRequest(objectModel);
68         String JavaDoc wildcard = (String JavaDoc) this.settings.get("parameter", name);
69         if ( modeConf != null ) {
70             wildcard = modeConf.getAttribute( "parameter", wildcard );
71             // preferred
72
wildcard = modeConf.getChild("parameter").getValue(wildcard);
73         }
74         int wildcardIndex = wildcard.indexOf( "*" );
75         if ( wildcardIndex != -1 ) {
76             // "*" contained in attribute name => combine all
77
// attributes' values that match prefix, suffix
78

79             // split the attribute's name so that the "*" could be
80
// determined by looking at the attributes' names that
81
// start with the prefix and end with the suffix
82
//
83
String JavaDoc prefix = wildcard.substring( 0, wildcardIndex );
84             String JavaDoc suffix;
85             if ( wildcard.length() >= wildcardIndex + 1 ) {
86                 suffix = wildcard.substring( wildcardIndex + 1 );
87             } else {
88                 suffix = "";
89             }
90             SortedSet JavaDoc names = new TreeSet JavaDoc();
91             Enumeration JavaDoc allNames = request.getHeaderNames();
92
93             while (allNames.hasMoreElements()) {
94                 String JavaDoc pname = (String JavaDoc) allNames.nextElement();
95                 if ( pname.startsWith( prefix ) && pname.endsWith( suffix ) ) {
96                     names.add(pname);
97                 }
98             }
99
100             List JavaDoc values = new LinkedList JavaDoc();
101             Iterator JavaDoc j = names.iterator();
102             while (j.hasNext()){
103                 String JavaDoc pname = (String JavaDoc) j.next();
104                 values.add( request.getHeader( pname ) );
105             }
106             
107             return values.toArray();
108
109         } else {
110             // no "*" in attribute name => just return all values of
111
// this one attribute. Make sure, it's an array.
112

113             Object JavaDoc value = request.getHeader( wildcard );
114             if ( value != null && !value.getClass().isArray() ) {
115                 Object JavaDoc[] values = new Object JavaDoc[1];
116                 values[0] = value;
117                 return values;
118             } else {
119                 return (Object JavaDoc[]) value;
120             }
121             
122         }
123     
124     }
125
126 }
127
Popular Tags