1 20 package org.enhydra.barracuda.plankton.http; 21 22 import java.text.*; 23 import java.util.*; 24 import javax.servlet.http.*; 25 26 32 public class HttpServices { 33 34 protected static final DateFormat cookieDF = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss zzz"); 35 36 46 public static Cookie parseCookie(String str) throws ParseException { 47 Cookie cookie; 48 StringTokenizer st = new StringTokenizer(str, ";"); 49 int length = 0; 51 String str1 = st.nextToken(); 52 length += str1.length(); 53 str1 = str1.trim(); 54 int index = str1.indexOf('='); 55 56 if(index < 0) throw new ParseException("Missing name=value pair", 0); 57 else if(index == 0) throw new ParseException("Missing name for name=value pair", 0); 58 else if(index == str1.length()) throw new ParseException("Missing value for name=value pair", 0); 59 else cookie = new Cookie(str1.substring(0, index), str1.substring(index+1)); 60 61 while(st.hasMoreTokens()) { 62 str1 = st.nextToken(); 63 index = str1.indexOf('='); 64 65 if(index < 0) { 66 if(str1.trim().equalsIgnoreCase("secure")) { 67 cookie.setSecure(true); 68 } else { 69 } 73 } else if(index > 0) { 74 String key = str1.substring(0, index).trim().toLowerCase(); 75 String val = str1.substring(index+1); 76 77 if(key.equals("comment")) { 78 cookie.setComment(val); 79 } else if(key.equals("domain")) { 80 cookie.setDomain(val); 81 } else if(key.equals("max-age")) { 82 try { cookie.setMaxAge(Integer.parseInt(val)); } 83 catch(NumberFormatException e) { 84 ParseException ee = new ParseException("Not an integer for 'max-age' field", length+8); 85 throw ee; 89 } 90 } else if(key.equals("path")) { 91 cookie.setPath(val); 92 } else if(key.equals("version")) { 93 try { cookie.setVersion(Integer.parseInt(val)); } 94 catch(NumberFormatException e) { 95 ParseException ee = new ParseException("Not an integer for 'version' field", length+8); 96 throw ee; 100 } 101 } else if(key.equals("expires")) { 102 try { cookie.setMaxAge( (int)(cookieDF.parse(val).getTime()/1000) ); } 104 catch(ParseException e) { 105 ParseException ee = new ParseException("Invalid date format for 'expires' field", length+8); 106 throw ee; 110 } 111 } else { 112 } 116 } else { 117 throw new ParseException("Missing option: "+str1, length); 118 } 119 120 length += 1+str1.length(); } 122 123 return cookie; 124 } 125 126 129 public static String formatCookie(Cookie cookie) { 130 StringBuffer sb = new StringBuffer (cookie.getName()+"="+cookie.getValue()); 131 if(cookie.getComment() != null) sb.append(";Comment=").append(cookie.getComment()); 132 if(cookie.getDomain() != null) sb.append(";Domain=").append(cookie.getDomain()); 133 if(cookie.getPath() != null) sb.append(";Path=").append(cookie.getPath()); 134 if(cookie.getSecure()) sb.append(";Secure"); 135 if(cookie.getVersion() == 0) { 136 if(cookie.getMaxAge() >= 0) sb.append(";Expires=").append(cookieDF.format(new Date(cookie.getMaxAge()))); 137 } else { 138 sb.append(";Version=").append(cookie.getVersion()); 139 sb.append(";Max-Age=").append(cookie.getMaxAge()); 140 } 141 142 return sb.toString(); 143 } 144 145 148 public static Cookie getCookie(String cookieName, HttpServletRequest req) { 149 if (cookieName==null || req==null) return null; 150 Cookie cookie = null; 151 Cookie[] cookies = req.getCookies(); 152 if (cookies!=null) { 153 for (int i=0; i<cookies.length; i++) { 154 if (cookies[i].getName().equals(cookieName)) { 155 cookie = cookies[i]; 156 break; 157 } 158 } 159 } 160 return cookie; 161 } 162 163 }; 164 | Popular Tags |