01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.acting;
18:
19: import org.apache.avalon.framework.configuration.Configuration;
20: import org.apache.avalon.framework.configuration.ConfigurationException;
21: import org.apache.avalon.framework.parameters.Parameters;
22: import org.apache.avalon.framework.thread.ThreadSafe;
23: import org.apache.cocoon.environment.ObjectModelHelper;
24: import org.apache.cocoon.environment.Redirector;
25: import org.apache.cocoon.environment.Response;
26: import org.apache.cocoon.environment.SourceResolver;
27:
28: import java.util.Collections;
29: import java.util.HashMap;
30: import java.util.Map;
31:
32: /**
33: * This action adds HTTP headers to the response.
34: *
35: * @author <a href="mailto:balld@apache.org">Donald Ball</a>
36: * @version CVS $Id: HttpHeaderAction.java 433543 2006-08-22 06:22:54Z crossley $
37: */
38: public class HttpHeaderAction extends AbstractConfigurableAction
39: implements ThreadSafe {
40:
41: /**
42: * Stores keys of global configuration.
43: */
44: private Object[] defaults = {};
45:
46: public void configure(Configuration conf)
47: throws ConfigurationException {
48: super .configure(conf);
49: this .defaults = super .settings.keySet().toArray();
50: }
51:
52: public Map act(Redirector redirector, SourceResolver resolver,
53: Map objectModel, String source, Parameters parameters)
54: throws Exception {
55: final Map results = new HashMap();
56:
57: final Response response = ObjectModelHelper
58: .getResponse(objectModel);
59:
60: // Process local configuration parameters
61: final String[] names = parameters.getNames();
62: for (int i = 0; i < names.length; i++) {
63: response.setHeader(names[i], parameters
64: .getParameter(names[i]));
65: results.put(names[i], parameters.getParameter(names[i]));
66: }
67:
68: // Process global defaults, that are not overridden
69: for (int i = 0; i < defaults.length; i++) {
70: if (!results.containsKey(this .defaults[i])) {
71: response.setHeader((String) this .defaults[i],
72: (String) this.settings.get(defaults[i]));
73: results.put(this.defaults[i], this.settings
74: .get(defaults[i]));
75: }
76: }
77:
78: return Collections.unmodifiableMap(results);
79: }
80: }
|