KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > http > handler > SetResponseHeadersHandler


1 // ========================================================================
2
// $Id: SetResponseHeadersHandler.java,v 1.6 2005/08/13 00:01:26 gregwilkins Exp $
3
// Copyright 2003-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.http.handler;
17
18 import java.io.IOException JavaDoc;
19 import java.util.Arrays JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.mortbay.log.LogFactory;
28 import org.mortbay.http.HttpException;
29 import org.mortbay.http.HttpRequest;
30 import org.mortbay.http.HttpResponse;
31
32 /* ------------------------------------------------------------ */
33 /**
34  * Handler that allows arbitrary HTTP Header values to be set in the response.
35  *
36  * @version $Id: SetResponseHeadersHandler.java,v 1.6 2005/08/13 00:01:26 gregwilkins Exp $
37  * @author Brett Sealey
38  */

39 public class SetResponseHeadersHandler extends AbstractHttpHandler
40 {
41     private static Log log = LogFactory.getLog(SetResponseHeadersHandler.class);
42
43     /* ------------------------------------------------------------ */
44     /**
45      * The Map of _fields that will be asserted on outgoing responses.
46      * Key is the header name. Value is a List containing its values.
47      */

48     private Map JavaDoc _fields=new HashMap JavaDoc();
49
50     /* ------------------------------------------------------------ */
51     /** Set a header override, every response handled will have this header set.
52      * @param name The String name of the header.
53      * @param value The String value of the header.
54      */

55     public void setHeaderValue(String JavaDoc name,String JavaDoc value)
56     {
57         _fields.put(name,Collections.singletonList(value));
58     }
59
60     /* ------------------------------------------------------------ */
61     /** Set a multivalued header, every response handled will have
62      * this header set with the provided values.
63      *
64      * @param name The String name of the header.
65      * @param values An Array of String values to use as the values for a Header.
66      */

67     public void setHeaderValues(String JavaDoc name,String JavaDoc[] values)
68     {
69         _fields.put(name,Arrays.asList(values));
70     }
71
72     /* ------------------------------------------------------------ */
73     /** Handle a request by pre-populating the headers from the configured
74      * set of _fields.
75      *
76      * Settings made here can be overridden by subsequent handling of the
77      * request.
78      *
79      * @param pathInContext The context path. Ignored.
80      * @param pathParams Path parameters such as encoded Session ID. Ignored.
81      * @param request The HttpRequest request. Ignored.
82      * @param response The HttpResponse response. Updated with new Headers.
83      */

84     public void handle(String JavaDoc pathInContext,
85                        String JavaDoc pathParams,
86                        HttpRequest request,
87                        HttpResponse response)
88             throws HttpException,IOException JavaDoc
89     {
90         log.debug("SetResponseHeadersHandler.handle()");
91
92         for (Iterator JavaDoc iterator=_fields.entrySet().iterator();iterator.hasNext();)
93         {
94             Map.Entry JavaDoc entry=(Map.Entry JavaDoc)iterator.next();
95             String JavaDoc name=(String JavaDoc)entry.getKey();
96             List JavaDoc values=(List JavaDoc)entry.getValue();
97             response.setField(name,values);
98         }
99     }
100 }
101
Popular Tags