KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.avalon.framework.configuration.Configuration;
19 import org.apache.avalon.framework.configuration.ConfigurationException;
20 import org.apache.avalon.framework.parameters.Parameters;
21 import org.apache.avalon.framework.thread.ThreadSafe;
22 import org.apache.cocoon.environment.ObjectModelHelper;
23 import org.apache.cocoon.environment.Redirector;
24 import org.apache.cocoon.environment.Response;
25 import org.apache.cocoon.environment.SourceResolver;
26
27 import java.util.Collections JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30
31 /**
32  * This action adds HTTP headers to the response.
33  *
34  * @author <a HREF="mailto:balld@apache.org">Donald Ball</a>
35  * @version CVS $Id: HttpHeaderAction.java 30932 2004-07-29 17:35:38Z vgritsenko $
36  */

37 public class HttpHeaderAction
38 extends AbstractConfigurableAction
39 implements ThreadSafe {
40
41     /**
42      * Stores keys of global configuration.
43      */

44     private Object JavaDoc[] defaults = {};
45
46     public void configure(Configuration conf) throws ConfigurationException {
47         super.configure(conf);
48         this.defaults = super.settings.keySet().toArray();
49     }
50
51     public Map JavaDoc act(Redirector redirector, SourceResolver resolver,
52                    Map JavaDoc objectModel, String JavaDoc source, Parameters parameters)
53     throws Exception JavaDoc {
54         final Map JavaDoc results = new HashMap JavaDoc();
55
56         final Response response = ObjectModelHelper.getResponse(objectModel);
57
58         // Process local configuration parameters
59
final String JavaDoc[] names = parameters.getNames();
60         for (int i = 0; i < names.length; i++) {
61             response.setHeader(names[i],parameters.getParameter(names[i]));
62             results.put(names[i], parameters.getParameter(names[i]));
63         }
64
65         // Process global defaults, that are not overridden
66
for (int i = 0; i < defaults.length; i++) {
67             if (!results.containsKey(this.defaults[i])) {
68                 response.setHeader((String JavaDoc) this.defaults[i], (String JavaDoc) this.settings.get(defaults[i]));
69                 results.put(this.defaults[i], this.settings.get(defaults[i]));
70             }
71         }
72
73         return Collections.unmodifiableMap(results);
74     }
75 }
76
Popular Tags