001: /*
002: * CoadunationLib: The coaduntion implementation library.
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * HttpCookieManager.java
020: *
021: * This class is responsible for managing the cookies sent between server and
022: * browser and browser and server.
023: */
024:
025: // package path
026: package com.rift.coad.lib.httpd;
027:
028: // java imports
029: import java.util.Map;
030: import java.util.HashMap;
031:
032: // logging import
033: import org.apache.log4j.Logger;
034:
035: // apache imports
036: import org.apache.http.HttpServerConnection;
037: import org.apache.http.protocol.HttpService;
038: import org.apache.http.HttpException;
039: import org.apache.http.HttpRequest;
040: import org.apache.http.HttpResponse;
041: import org.apache.http.Header;
042:
043: /**
044: * This class is responsible for managing the cookies sent between server and
045: * browser and browser and server.
046: *
047: * @author Brett Chaldecott
048: */
049: public class HttpRequestCookieManager {
050:
051: private final static String COOKIE = "Cookie";
052: private final static String COOKIE_2 = "Cookie2";
053: private final static String SET_COOKIE = "Set-Cookie";
054: private final static String SET_COOKIE_2 = "Set-Cookie2";
055:
056: // the classes member variables
057: private Logger log = Logger
058: .getLogger(HttpRequestCookieManager.class.getName());
059: private Map cookies = new HashMap();
060: private HttpRequest request = null;
061: private HttpResponse response = null;
062:
063: /**
064: * Creates a new instance of HttpCookieManager
065: *
066: * @param request The object containing the request value.
067: * @param response The method that encloses the http response value.
068: * @exception HttpdException
069: */
070: public HttpRequestCookieManager(HttpRequest request,
071: HttpResponse response) throws HttpdException {
072: this .request = request;
073: this .response = response;
074:
075: // check for basic auth
076: if (request.containsHeader(COOKIE)) {
077: processHeaders(request.getHeaders(COOKIE));
078: }
079: if (request.containsHeader(COOKIE_2)) {
080: processHeaders(request.getHeaders(COOKIE_2));
081: }
082:
083: }
084:
085: /**
086: * This method is responsible for adding a cookie to the response
087: *
088: * @param cookie The cookie wrapper to add.
089: */
090: public void addCookie(CookieWrapper cookie) {
091: cookies.put(cookie.getName(), cookie);
092: log.debug("Set cookie [" + cookie.getSetCookieString() + "]");
093: response.addHeader(new Header(SET_COOKIE, cookie
094: .getSetCookieString()));
095: }
096:
097: /**
098: * This method returns the cookie reference matching the name.
099: *
100: * @return The name of the cookie.
101: * @param name The name of the cookie to retrieve.
102: */
103: public CookieWrapper getCookie(String name) {
104: return (CookieWrapper) cookies.get(name.trim().toLowerCase());
105: }
106:
107: /**
108: * This method is responsible for processing the headers passed to it.
109: *
110: * @param headers The reference to the headers
111: * @exception HttpException
112: */
113: private void processHeaders(Header[] headers) throws HttpdException {
114:
115: log.debug("There are [" + headers.length + "] cookies");
116: for (int index = 0; index < headers.length; index++) {
117: Header header = headers[index];
118: String value = header.getValue().trim();
119: log.debug("Process cookie : " + value);
120: if (value.length() == 0) {
121: continue;
122: }
123: CookieWrapper cookie = new CookieWrapper(value);
124: cookies.put(cookie.getName(), cookie);
125: }
126: }
127: }
|