org.onap migration
[vid.git] / vid-app-common / src / main / java / org / onap / vid / client / FakeHttpSession.java
1 package org.onap.vid.client;
2
3 import org.apache.commons.io.IOUtils;
4 import org.json.JSONArray;
5 import org.json.JSONObject;
6 import org.json.JSONTokener;
7
8 import javax.servlet.ServletContext;
9 import javax.servlet.http.HttpSession;
10 import java.io.File;
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;
16 import java.util.Map;
17
18 /**
19  * Created by pickjonathan on 03/07/2017.
20  */
21 public class FakeHttpSession implements HttpSession {
22
23     /**
24      * Setup the creation time
25      */
26     public FakeHttpSession() {
27         File file = new File("resources/roles.json");
28
29         String rolesInputStream = null;
30         try {
31             rolesInputStream = IOUtils.toString(FakeHttpSession.class.getClassLoader().getResourceAsStream("roles.json"),"UTF8");
32         } catch (IOException e) {
33             e.printStackTrace();
34         }
35         JSONTokener tokener = new JSONTokener(rolesInputStream);
36         JSONObject roles = new JSONObject(tokener);
37
38         JSONArray rolesArray = roles.getJSONArray("roles");
39
40         //set permissions to the roles from file.
41         this.setAttribute("role", rolesArray);
42
43         creationTime = System.currentTimeMillis();
44     }
45
46
47     /**
48      * Setup the creation time
49      * @param id The new session id
50      */
51     public FakeHttpSession(String id)
52     {
53         this.id = id;
54         creationTime = System.currentTimeMillis();
55     }
56
57     /* (non-Javadoc)
58      * @see javax.servlet.http.HttpSession#getCreationTime()
59      */
60     public long getCreationTime()
61     {
62         return creationTime;
63     }
64
65     /* (non-Javadoc)
66      * @see javax.servlet.http.HttpSession#getId()
67      */
68     public String getId()
69     {
70         if (id == null)
71         {
72             System.out.println("Inventing data in FakeHttpSession.getId() to remain plausible.");
73             id = "fake";
74         }
75
76         return id;
77     }
78
79     /* (non-Javadoc)
80      * @see javax.servlet.http.HttpSession#getLastAccessedTime()
81      */
82     public long getLastAccessedTime()
83     {
84         return creationTime;
85     }
86
87     /* (non-Javadoc)
88      * @see javax.servlet.http.HttpSession#getServletContext()
89      */
90     public ServletContext getServletContext()
91     {
92         return null;
93     }
94
95     /* (non-Javadoc)
96      * @see javax.servlet.http.HttpSession#setMaxInactiveInterval(int)
97      */
98     public void setMaxInactiveInterval(int maxInactiveInterval)
99     {
100         this.maxInactiveInterval = maxInactiveInterval;
101     }
102
103     /* (non-Javadoc)
104      * @see javax.servlet.http.HttpSession#getMaxInactiveInterval()
105      */
106     public int getMaxInactiveInterval()
107     {
108         return maxInactiveInterval;
109     }
110
111     /**
112      * @see javax.servlet.http.HttpSession#getSessionContext()
113      * @deprecated
114      */
115     @SuppressWarnings({"UnnecessaryFullyQualifiedName"})
116     @Deprecated
117     public javax.servlet.http.HttpSessionContext getSessionContext()
118     {
119         return null;
120     }
121
122     /* (non-Javadoc)
123      * @see javax.servlet.http.HttpSession#getAttribute(java.lang.String)
124      */
125     public Object getAttribute(String name)
126     {
127         return attributes.get(name);
128     }
129
130     /* (non-Javadoc)
131      * @see javax.servlet.http.HttpSession#getValue(java.lang.String)
132      */
133     @Deprecated
134     public Object getValue(String name)
135     {
136         return attributes.get(name);
137     }
138
139     /* (non-Javadoc)
140      * @see javax.servlet.http.HttpSession#getAttributeNames()
141      */
142     public Enumeration<String> getAttributeNames()
143     {
144         return Collections.enumeration(attributes.keySet());
145     }
146
147     /* (non-Javadoc)
148      * @see javax.servlet.http.HttpSession#getValueNames()
149      */
150     @Deprecated
151     public String[] getValueNames()
152     {
153         return attributes.keySet().toArray(new String[attributes.keySet().size()]);
154     }
155
156     /* (non-Javadoc)
157      * @see javax.servlet.http.HttpSession#setAttribute(java.lang.String, java.lang.Object)
158      */
159     public void setAttribute(String name, Object value)
160     {
161         attributes.put(name, value);
162     }
163
164     /* (non-Javadoc)
165      * @see javax.servlet.http.HttpSession#putValue(java.lang.String, java.lang.Object)
166      */
167     @Deprecated
168     public void putValue(String name, Object value)
169     {
170         attributes.put(name, value);
171     }
172
173     /* (non-Javadoc)
174      * @see javax.servlet.http.HttpSession#removeAttribute(java.lang.String)
175      */
176     public void removeAttribute(String name)
177     {
178         attributes.remove(name);
179     }
180
181     /* (non-Javadoc)
182      * @see javax.servlet.http.HttpSession#removeValue(java.lang.String)
183      */
184     @Deprecated
185     public void removeValue(String name)
186     {
187         attributes.remove(name);
188     }
189
190     /* (non-Javadoc)
191      * @see javax.servlet.http.HttpSession#invalidate()
192      */
193     public void invalidate()
194     {
195     }
196
197     /* (non-Javadoc)
198      * @see javax.servlet.http.HttpSession#isNew()
199      */
200     public boolean isNew()
201     {
202         return true;
203     }
204
205     /**
206      * The session id
207      */
208     private String id = null;
209
210     /**
211      * The list of attributes
212      */
213     private Map<String, Object> attributes = new HashMap<String, Object>();
214
215     /**
216      * When were we created
217      */
218     private long creationTime;
219
220     /**
221      * How long before we timeout?
222      */
223     private int maxInactiveInterval = 30 * 60 * 1000;
224 }