| 1 24 25 package com.rift.coad.lib.httpd; 27 28 import org.apache.log4j.Logger; 30 31 import java.net.InetAddress ; 33 import java.util.Date ; 34 import java.util.Map ; 35 import java.util.HashMap ; 36 import java.util.StringTokenizer ; 37 import java.util.TimeZone ; 38 import java.text.SimpleDateFormat ; 39 40 import com.rift.coad.lib.configuration.Configuration; 42 import com.rift.coad.lib.configuration.ConfigurationFactory; 43 44 50 public class CookieWrapper { 51 52 private final static String PATH = "Path"; 54 private final static String DOMAIN = "Domain"; 55 public final static String VERSION = "Version"; 56 public final static String DOLLAR = "$"; 57 private final static String VERSION_NUMBER = "1"; 58 private final static String COOKIE_HOST = "cookie_host"; 59 60 private Logger log = 62 Logger.getLogger(CookieWrapper.class.getName()); 63 private String name = null; 64 private String value = null; 65 private String domain = null; 66 private String path = null; 67 68 69 75 public CookieWrapper(String rawSource) 76 throws HttpdException { 77 if (rawSource.contains("=") == false) { 79 throw new HttpdException( 80 "Incorrectly formated cookie [" + rawSource + "]"); 81 } 82 name = rawSource.substring(0,rawSource.indexOf('=')) 83 .trim().toLowerCase(); 84 value = stripInvertedCommas( 85 rawSource.substring(rawSource.indexOf('=') + 1).trim()) 86 .trim(); 87 } 88 89 90 99 public CookieWrapper(String name, String value) throws HttpdException { 100 try { 101 Configuration config = ConfigurationFactory.getInstance(). 102 getConfig(CookieWrapper.class); 103 this.name = name.toLowerCase().trim(); 104 this.value = value; 105 } catch (Exception ex) { 106 throw new HttpdException("Failed to init the cookie wrapper : " + 107 ex.getMessage(),ex); 108 } 109 } 110 111 112 117 public String getName() { 118 return name; 119 } 120 121 122 127 public String getValue() { 128 return value; 129 } 130 131 132 137 public void setValue(String value) { 138 this.value = value; 139 } 140 141 142 147 public String getDomain() { 148 return domain; 149 } 150 151 152 157 public void setDomain(String domain) { 158 this.domain = domain; 159 } 160 161 162 167 public String getPath() { 168 return path; 169 } 170 171 172 177 public void setPath(String path) { 178 this.path = path; 179 } 180 181 182 187 public String getSetCookieString() { 188 StringBuffer cookieBuffer = new StringBuffer (); 189 cookieBuffer.append(name).append("=").append(value).append(";"). 190 append(VERSION).append("=").append(VERSION_NUMBER); 191 if (domain != null) { 192 cookieBuffer.append(";").append(DOMAIN).append("=").append(domain); 193 } 194 if (path != null) { 195 cookieBuffer.append(";").append(PATH).append("=").append(path); 196 } 197 return cookieBuffer.toString(); 198 } 199 200 201 207 private String stripInvertedCommas(String value) { 208 int beginPos = value.indexOf('"'); 209 if (beginPos == -1) { 210 return value; 211 } 212 int endPos = value.indexOf('"',beginPos); 213 return value.substring(beginPos,endPos); 214 } 215 } 216 | Popular Tags |