BrightSide Workbench Full Report + Source Code
CookieUtil.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.elephant.impl.util;
19 
20 import java.io.IOException;
21 import javax.servlet.http.Cookie;
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24 import org.apache.commons.codec.DecoderException;
25 import org.apache.commons.codec.binary.Base64;
26 import org.turro.elephant.context.ElephantContext;
27 
32 public class CookieUtil {
33 
34  private CookieUtil() {}
35 
36  public static Cookie getCookie(HttpServletRequest request, String name) {
37  Cookie[] cookies = request.getCookies();
38  if(cookies == null) return null;
39  for(int i = 0; i < cookies.length; i++)
40  if(cookies[i].getName().equals(name))
41  return cookies[i];
42  return null;
43  }
44 
45  public static void setCookie(HttpServletResponse response, String name, String value, String path, int age) {
46  Cookie cookie = new Cookie(name, value);
47  if(ElephantContext.getUseSSL()) cookie.setSecure(true);
48  cookie.setPath(path);
49  cookie.setMaxAge(age);
50  response.addCookie(cookie);
51  }
52 
53  public static void deleteCookie(HttpServletResponse response, String name, String path) {
54  Cookie cookie = new Cookie(name, null);
55  cookie.setPath(path);
56  cookie.setMaxAge(0);
57  response.addCookie(cookie);
58  }
59 
60  public static void encryptCookie(HttpServletResponse response, String name, byte[] value, String path, int age) {
61  Cookie cookie = new Cookie(name, new String(new Base64().encode(value)));
62  if(ElephantContext.getUseSSL()) cookie.setSecure(true);
63  cookie.setVersion(1);
64  cookie.setPath(path);
65  cookie.setMaxAge(age);
66  response.addCookie(cookie);
67  }
68 
69  public static byte[] decryptCookieValue(Cookie cookie) throws IOException, DecoderException {
70  return cookie == null ? null : new Base64().decode(cookie.getValue().getBytes());
71  }
72 
73 }
static Cookie getCookie(HttpServletRequest request, String name)
Definition: CookieUtil.java:36
static void setCookie(HttpServletResponse response, String name, String value, String path, int age)
Definition: CookieUtil.java:45
static byte[] decryptCookieValue(Cookie cookie)
Definition: CookieUtil.java:69
static void encryptCookie(HttpServletResponse response, String name, byte[] value, String path, int age)
Definition: CookieUtil.java:60
static void deleteCookie(HttpServletResponse response, String name, String path)
Definition: CookieUtil.java:53