1 24 25 package com.rift.coad.lib.httpd; 27 28 import java.util.Map ; 30 import java.util.HashMap ; 31 32 import org.apache.log4j.Logger; 34 35 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 50 public class HttpRequestCookieManager { 51 52 private final static String COOKIE = "Cookie"; 53 private final static String COOKIE_2 = "Cookie2"; 54 private final static String SET_COOKIE = "Set-Cookie"; 55 private final static String SET_COOKIE_2 = "Set-Cookie2"; 56 57 58 private Logger log = 60 Logger.getLogger(HttpRequestCookieManager.class.getName()); 61 private Map cookies = new HashMap (); 62 private HttpRequest request = null; 63 private HttpResponse response = null; 64 65 72 public HttpRequestCookieManager(HttpRequest request, HttpResponse response) 73 throws HttpdException { 74 this.request = request; 75 this.response = response; 76 77 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 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 106 public CookieWrapper getCookie(String name) { 107 return (CookieWrapper)cookies.get(name.trim().toLowerCase()); 108 } 109 110 111 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 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 |