001 package com.khubla.pragmatach.framework.api; 002 003 import java.util.Hashtable; 004 005 import javax.servlet.http.Cookie; 006 import javax.servlet.http.HttpServletRequest; 007 import javax.servlet.http.HttpServletResponse; 008 009 import com.khubla.pragmatach.framework.application.Application; 010 import com.khubla.pragmatach.framework.crypto.AES; 011 012 /** 013 * @author tome 014 */ 015 public class Cookies { 016 /** 017 * for long term cookies 018 */ 019 private static final int SECONDS_PER_YEAR = 60 * 60 * 24 * 365; 020 /** 021 * HttpServletRequest 022 */ 023 private final HttpServletRequest httpServletRequest; 024 /** 025 * HttpServletResponse 026 */ 027 private final HttpServletResponse httpServletResponse; 028 /** 029 * config key 030 */ 031 private final static String COOKIE_CONFIG_KEY = "pragmatach.cookie.cryptokey"; 032 033 /** 034 * ctor 035 */ 036 public Cookies(HttpServletRequest httpServletRequest, 037 HttpServletResponse httpServletResponse) { 038 this.httpServletRequest = httpServletRequest; 039 this.httpServletResponse = httpServletResponse; 040 } 041 042 /** 043 * clear all cookies 044 */ 045 public void clearAll() throws PragmatachException { 046 final Cookie[] cookies = httpServletRequest.getCookies(); 047 if ((null != cookies) && (cookies.length > 0)) { 048 for (final Cookie cookie : cookies) { 049 removeCookie(cookie.getName()); 050 } 051 } 052 } 053 054 /** 055 * decrypt cookie 056 */ 057 private String decryptCookie(String cookie) throws Exception { 058 final String key = getCryptoKey(); 059 if (null != key) { 060 return AES.decrypt(cookie, key); 061 } else { 062 return cookie; 063 } 064 } 065 066 /** 067 * encrypt cookie 068 */ 069 private String encryptCookie(String cookie) throws Exception { 070 final String key = getCryptoKey(); 071 if (null != key) { 072 return AES.encrypt(cookie, key); 073 } else { 074 return cookie; 075 } 076 } 077 078 /** 079 * get a cookie by name 080 */ 081 public String getCookie(String name) throws PragmatachException { 082 try { 083 if ((null != name) && (name.length() > 0)) { 084 final Hashtable<String, String> cookies = getCookies(); 085 if (null != cookies) { 086 return cookies.get(name); 087 } 088 } 089 return null; 090 } catch (final Exception e) { 091 throw new PragmatachException("Exception in getCookie", e); 092 } 093 } 094 095 /** 096 * get all the cookies 097 */ 098 public Hashtable<String, String> getCookies() throws PragmatachException { 099 try { 100 final Cookie[] cookies = httpServletRequest.getCookies(); 101 if ((null != cookies) && (cookies.length > 0)) { 102 final Hashtable<String, String> ret = new Hashtable<String, String>(); 103 for (final Cookie cookie : cookies) { 104 ret.put(cookie.getName(), cookie.getValue()); 105 } 106 return ret; 107 } 108 return null; 109 } catch (final Exception e) { 110 throw new PragmatachException("Exception in getCookies", e); 111 } 112 } 113 114 private String getCryptoKey() throws PragmatachException { 115 return Application.getConfiguration().getParameter(COOKIE_CONFIG_KEY); 116 } 117 118 /** 119 * get a encrypter cookie by name 120 */ 121 public String getEncryptedCookie(String name) throws PragmatachException { 122 try { 123 if ((null != name) && (name.length() > 0)) { 124 final Hashtable<String, String> cookies = getCookies(); 125 if (null != cookies) { 126 final String k = cookies.get(name); 127 if (null != k) { 128 return decryptCookie(k); 129 } 130 } 131 } 132 return null; 133 } catch (final Exception e) { 134 throw new PragmatachException("Exception in getCookie", e); 135 } 136 } 137 138 /** 139 * remove a cookie 140 */ 141 public void removeCookie(String name) throws PragmatachException { 142 final Cookie[] cookies = httpServletRequest.getCookies(); 143 if ((null != cookies) && (cookies.length > 0)) { 144 for (final Cookie cookie : cookies) { 145 if (cookie.getName().compareTo(name) == 0) { 146 cookie.setMaxAge(0); 147 cookie.setPath("/"); 148 httpServletResponse.addCookie(cookie); 149 break; 150 } 151 } 152 } 153 } 154 155 /** 156 * set a cookie 157 */ 158 public void setCookie(String name, String value) throws PragmatachException { 159 try { 160 final Cookie cookie = new Cookie(name, value); 161 cookie.setMaxAge(SECONDS_PER_YEAR); 162 cookie.setPath("/"); 163 httpServletResponse.addCookie(cookie); 164 } catch (final Exception e) { 165 throw new PragmatachException("Exception in setCookie", e); 166 } 167 } 168 169 /** 170 * set a cookie 171 */ 172 public void setEncryptedCookie(String name, String value) 173 throws PragmatachException { 174 try { 175 final Cookie cookie = new Cookie(name, encryptCookie(value)); 176 cookie.setMaxAge(SECONDS_PER_YEAR); 177 cookie.setPath("/"); 178 httpServletResponse.addCookie(cookie); 179 } catch (final Exception e) { 180 throw new PragmatachException("Exception in setCookie", e); 181 } 182 } 183 }