BrightSide Workbench Full Report + Source Code
Cookies.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2019 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 
19 package org.turro.marker;
20 
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import javax.servlet.http.Cookie;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26 import org.turro.elephant.context.ElephantContext;
27 import org.turro.elephant.impl.util.CookieUtil;
28 
33 public class Cookies {
34 
35  private final HttpServletRequest request;
36  private final HttpServletResponse response;
37 
38  public Cookies(HttpServletRequest request, HttpServletResponse response) {
39  this.request = request;
40  this.response = response;
41  }
42 
43  public Cookie get(String name) {
44  return CookieUtil.getCookie(request, name);
45  }
46 
47  public String getValue(String name) {
48  Cookie c = get(name);
49  return c != null ? c.getValue() : null;
50  }
51 
52  public void set(String name, String value, String path, int age) {
53  CookieUtil.setCookie(response, name, value, path, age);
54  }
55 
56  public void encrypt(String name, String value, String path, int age) {
57  try {
58  byte[] b = ElephantContext.encrypt(value.getBytes());
59  CookieUtil.encryptCookie(response, name, b, path, age);
60  } catch (Exception ex) {
61  Logger.getLogger(Cookies.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
62  }
63  }
64 
65  public String decrypt(String name) {
66  try {
67  byte[] v = CookieUtil.decryptCookieValue(get(name));
68  return new String(ElephantContext.decrypt(v));
69  } catch (Exception ex) {
70  Logger.getLogger(Cookies.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
71  }
72  return null;
73  }
74 
75  public void delete(String name, String path) {
76  CookieUtil.deleteCookie(response, name, path);
77  }
78 
79 }
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
Cookies(HttpServletRequest request, HttpServletResponse response)
Definition: Cookies.java:38
String decrypt(String name)
Definition: Cookies.java:65
String getValue(String name)
Definition: Cookies.java:47
void encrypt(String name, String value, String path, int age)
Definition: Cookies.java:56