KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > httpd > HttpRequestCookieManager


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * HttpCookieManager.java
20  *
21  * This class is responsible for managing the cookies sent between server and
22  * browser and browser and server.
23  */

24
25 // package path
26
package com.rift.coad.lib.httpd;
27
28 // java imports
29
import java.util.Map JavaDoc;
30 import java.util.HashMap JavaDoc;
31
32 // logging import
33
import org.apache.log4j.Logger;
34
35 // apache imports
36
import org.apache.http.HttpServerConnection;
37 import org.apache.http.protocol.HttpService;
38 import org.apache.http.HttpException;
39 import org.apache.http.HttpRequest;
40 import org.apache.http.HttpResponse;
41 import org.apache.http.Header;
42
43
44 /**
45  * This class is responsible for managing the cookies sent between server and
46  * browser and browser and server.
47  *
48  * @author Brett Chaldecott
49  */

50 public class HttpRequestCookieManager {
51     
52     private final static String JavaDoc COOKIE = "Cookie";
53     private final static String JavaDoc COOKIE_2 = "Cookie2";
54     private final static String JavaDoc SET_COOKIE = "Set-Cookie";
55     private final static String JavaDoc SET_COOKIE_2 = "Set-Cookie2";
56     
57     
58     // the classes member variables
59
private Logger log =
60             Logger.getLogger(HttpRequestCookieManager.class.getName());
61     private Map JavaDoc cookies = new HashMap JavaDoc();
62     private HttpRequest request = null;
63     private HttpResponse response = null;
64     
65     /**
66      * Creates a new instance of HttpCookieManager
67      *
68      * @param request The object containing the request value.
69      * @param response The method that encloses the http response value.
70      * @exception HttpdException
71      */

72     public HttpRequestCookieManager(HttpRequest request, HttpResponse response)
73     throws HttpdException {
74         this.request = request;
75         this.response = response;
76         
77         // check for basic auth
78
if (request.containsHeader(COOKIE)) {
79             processHeaders(request.getHeaders(COOKIE));
80         }
81         if (request.containsHeader(COOKIE_2)) {
82             processHeaders(request.getHeaders(COOKIE_2));
83         }
84         
85     }
86     
87     
88     /**
89      * This method is responsible for adding a cookie to the response
90      *
91      * @param cookie The cookie wrapper to add.
92      */

93     public void addCookie(CookieWrapper cookie) {
94         cookies.put(cookie.getName(),cookie);
95         log.debug("Set cookie [" + cookie.getSetCookieString() + "]");
96         response.addHeader(new Header(SET_COOKIE,cookie.getSetCookieString()));
97     }
98     
99     
100     /**
101      * This method returns the cookie reference matching the name.
102      *
103      * @return The name of the cookie.
104      * @param name The name of the cookie to retrieve.
105      */

106     public CookieWrapper getCookie(String JavaDoc name) {
107         return (CookieWrapper)cookies.get(name.trim().toLowerCase());
108     }
109     
110     
111     /**
112      * This method is responsible for processing the headers passed to it.
113      *
114      * @param headers The reference to the headers
115      * @exception HttpException
116      */

117     private void processHeaders(Header[] headers) throws HttpdException {
118         
119         log.debug("There are [" + headers.length + "] cookies");
120         for (int index = 0; index < headers.length; index++) {
121             Header header = headers[index];
122             String JavaDoc value = header.getValue().trim();
123             log.debug("Process cookie : " + value);
124             if (value.length() == 0) {
125                 continue;
126             }
127             CookieWrapper cookie = new CookieWrapper(value);
128             cookies.put(cookie.getName(),cookie);
129         }
130     }
131 }
132
Popular Tags