BrightSide Workbench Full Report + Source Code
CookieCtrl.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 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.elephant.cookies;
20 
21 import java.util.Set;
22 import org.turro.collections.KeyValueMap;
23 import org.turro.elephant.context.Application;
24 import org.turro.elephant.context.IConstructor;
25 import org.turro.elephant.direct.AbstractDirectContentCtrl;
26 import org.turro.elephant.direct.DirectContent;
27 import org.turro.elephant.direct.DirectContents;
28 import org.turro.i18n.I_;
29 import org.turro.marker.ElephantMarker;
30 
35 @DirectContent(identifier="cookie-management")
36 public class CookieCtrl extends AbstractDirectContentCtrl {
37 
38  public CookieCtrl() {
39  super("cookies");
40  }
41 
42  public String createPost(String type, boolean accept) {
43  return DirectContents.doCreatePOST(getIdentifier(),
44  "type=" + type +
45  ";accept=" + accept);
46  }
47 
48  @Override
49  public String getIdentifier() {
50  return CookieCtrl.class.getAnnotation(DirectContent.class).identifier();
51  }
52 
53  @Override
54  protected void prepareCleanMarker(ElephantMarker em, KeyValueMap kvm) {
55  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
56  }
57 
58  @Override
59  protected void prepareMarker(ElephantMarker marker) {
60  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
61  }
62 
63  @Override
64  public void doExecute(IConstructor constructor, KeyValueMap map) {
65  CookieManager.load().keySet().stream().forEach(type -> {
66  setType((String) type, "on".equals(map.get(type)));
67  });
68  }
69 
70  public Set<String> getTypes() {
71  return CookieManager.load().stringPropertyNames();
72  }
73 
74  public String getMessage(String type) {
75  return I_.get(CookieManager.load().getProperty(type));
76  }
77 
79  return CookieManager.load();
80  }
81 
82  public boolean anyUnset() {
83  return CookieManager.load().keySet().stream().anyMatch(type -> {
84  return !decline((String) type) && !allow((String) type);
85  });
86  }
87 
88  public boolean unset(String type) {
89  return !decline(type) && !allow(type);
90  }
91 
92  public boolean decline(String type) {
93  return (type + "_decline").equals(Application.decryptCookieValue(type + "_decline"));
94  }
95 
96  public boolean allow(String type) {
97  return (type + "_allow").equals(Application.decryptCookieValue(type + "_allow"));
98  }
99 
100  private void setType(String type, boolean value) {
101  Application.deleteCookie(type + "_allow", "/");
102  Application.deleteCookie(type + "_decline", "/");
103  String name = type + (value ? "_allow" : "_decline");
104  Application.encryptCookie(name, name, "/", 60 * 60 * 24 * 365 * 10);
105  }
106 
107 }
static String decryptCookieValue(String name)
static void encryptCookie(String name, String value, String path, int age)
static void deleteCookie(String name, String path)
void prepareCleanMarker(ElephantMarker em, KeyValueMap kvm)
Definition: CookieCtrl.java:54
String createPost(String type, boolean accept)
Definition: CookieCtrl.java:42
void doExecute(IConstructor constructor, KeyValueMap map)
Definition: CookieCtrl.java:64
void prepareMarker(ElephantMarker marker)
Definition: CookieCtrl.java:59
static String doCreatePOST(String id, String values)
static String get(String msg)
Definition: I_.java:41