1 package org.onap.vid.client;
3 import org.apache.commons.io.IOUtils;
4 import org.json.JSONArray;
5 import org.json.JSONObject;
6 import org.json.JSONTokener;
8 import javax.servlet.ServletContext;
9 import javax.servlet.http.HttpSession;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.util.Collections;
14 import java.util.Enumeration;
15 import java.util.HashMap;
19 * Created by pickjonathan on 03/07/2017.
21 public class FakeHttpSession implements HttpSession {
24 * Setup the creation time
26 public FakeHttpSession() {
27 File file = new File("resources/roles.json");
29 String rolesInputStream = null;
31 rolesInputStream = IOUtils.toString(FakeHttpSession.class.getClassLoader().getResourceAsStream("roles.json"),"UTF8");
32 } catch (IOException e) {
35 JSONTokener tokener = new JSONTokener(rolesInputStream);
36 JSONObject roles = new JSONObject(tokener);
38 JSONArray rolesArray = roles.getJSONArray("roles");
40 //set permissions to the roles from file.
41 this.setAttribute("role", rolesArray);
43 creationTime = System.currentTimeMillis();
48 * Setup the creation time
49 * @param id The new session id
51 public FakeHttpSession(String id)
54 creationTime = System.currentTimeMillis();
58 * @see javax.servlet.http.HttpSession#getCreationTime()
60 public long getCreationTime()
66 * @see javax.servlet.http.HttpSession#getId()
72 System.out.println("Inventing data in FakeHttpSession.getId() to remain plausible.");
80 * @see javax.servlet.http.HttpSession#getLastAccessedTime()
82 public long getLastAccessedTime()
88 * @see javax.servlet.http.HttpSession#getServletContext()
90 public ServletContext getServletContext()
96 * @see javax.servlet.http.HttpSession#setMaxInactiveInterval(int)
98 public void setMaxInactiveInterval(int maxInactiveInterval)
100 this.maxInactiveInterval = maxInactiveInterval;
104 * @see javax.servlet.http.HttpSession#getMaxInactiveInterval()
106 public int getMaxInactiveInterval()
108 return maxInactiveInterval;
112 * @see javax.servlet.http.HttpSession#getSessionContext()
115 @SuppressWarnings({"UnnecessaryFullyQualifiedName"})
117 public javax.servlet.http.HttpSessionContext getSessionContext()
123 * @see javax.servlet.http.HttpSession#getAttribute(java.lang.String)
125 public Object getAttribute(String name)
127 return attributes.get(name);
131 * @see javax.servlet.http.HttpSession#getValue(java.lang.String)
134 public Object getValue(String name)
136 return attributes.get(name);
140 * @see javax.servlet.http.HttpSession#getAttributeNames()
142 public Enumeration<String> getAttributeNames()
144 return Collections.enumeration(attributes.keySet());
148 * @see javax.servlet.http.HttpSession#getValueNames()
151 public String[] getValueNames()
153 return attributes.keySet().toArray(new String[attributes.keySet().size()]);
157 * @see javax.servlet.http.HttpSession#setAttribute(java.lang.String, java.lang.Object)
159 public void setAttribute(String name, Object value)
161 attributes.put(name, value);
165 * @see javax.servlet.http.HttpSession#putValue(java.lang.String, java.lang.Object)
168 public void putValue(String name, Object value)
170 attributes.put(name, value);
174 * @see javax.servlet.http.HttpSession#removeAttribute(java.lang.String)
176 public void removeAttribute(String name)
178 attributes.remove(name);
182 * @see javax.servlet.http.HttpSession#removeValue(java.lang.String)
185 public void removeValue(String name)
187 attributes.remove(name);
191 * @see javax.servlet.http.HttpSession#invalidate()
193 public void invalidate()
198 * @see javax.servlet.http.HttpSession#isNew()
200 public boolean isNew()
208 private String id = null;
211 * The list of attributes
213 private Map<String, Object> attributes = new HashMap<String, Object>();
216 * When were we created
218 private long creationTime;
221 * How long before we timeout?
223 private int maxInactiveInterval = 30 * 60 * 1000;