Rev 1312 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | fvelez | 1 | package pt.estgp.estgweb.domain; |
2 | |||
1148 | jmachado | 3 | import jomm.utils.DesUtils; |
1 | fvelez | 4 | import org.apache.commons.io.output.ByteArrayOutputStream; |
974 | jmachado | 5 | import pt.estgp.estgweb.domain.dao.DaoFactory; |
1148 | jmachado | 6 | import pt.estgp.estgweb.services.ftpservices.FtpService; |
974 | jmachado | 7 | import pt.estgp.estgweb.web.utils.RequestUtils; |
1 | fvelez | 8 | import pt.utl.ist.berserk.logic.serviceManager.IServiceManager; |
9 | import pt.utl.ist.berserk.logic.serviceManager.ServiceManager; |
||
10 | |||
11 | import javax.servlet.http.HttpServletRequest; |
||
142 | jmachado | 12 | import javax.servlet.http.HttpServletResponse; |
974 | jmachado | 13 | import java.io.Serializable; |
14 | import java.util.Date; |
||
15 | import java.util.HashSet; |
||
16 | import java.util.Iterator; |
||
17 | import java.util.Set; |
||
1 | fvelez | 18 | |
19 | /** |
||
20 | * @author Jorge Machado |
||
21 | * @date 2/Mar/2008 |
||
22 | * @time 10:27:25 |
||
23 | * @see pt.estgp.estgweb.domain |
||
24 | */ |
||
25 | public class UserSessionImpl extends UserSession |
||
26 | { |
||
27 | |||
28 | private Set<SessionObject> removeList; |
||
1097 | jmachado | 29 | private 1.5.0/docs/api/javax/security/auth/login/Configuration.html">Configuration nowConfiguration; |
1 | fvelez | 30 | |
31 | /** |
||
32 | * @return |
||
33 | */ |
||
34 | public 1.5.0/docs/api/java/io/Serializable.html">Serializable getSerializable() |
||
35 | { |
||
36 | return getCookie(); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * put an object in cache |
||
41 | * |
||
42 | * @param key |
||
43 | * @param obj |
||
44 | */ |
||
76 | jmachado | 45 | public void put(1.5.0/docs/api/java/lang/String.html">String key, 1.5.0/docs/api/java/io/Serializable.html">Serializable obj) |
1 | fvelez | 46 | { |
76 | jmachado | 47 | if (getObjects() == null || getObjects().size() == 0) |
1 | fvelez | 48 | { |
49 | setObjects(new HashSet<SessionObject>()); |
||
50 | } |
||
51 | SessionObject sessionObject = getSessionObject(key); |
||
76 | jmachado | 52 | if (sessionObject == null) |
1 | fvelez | 53 | { |
54 | sessionObject = DomainObjectFactory.createSessionObjectImpl(); |
||
55 | SessionObjectId sessionObjectId = DomainObjectFactory.createSessionObjectIdImpl(); |
||
56 | sessionObjectId.setKeyId(key); |
||
88 | jmachado | 57 | sessionObject.setSaveDate(new 5+0%2Fdocs%2Fapi+Date">Date()); |
1 | fvelez | 58 | sessionObjectId.setUserSession(this); |
59 | sessionObject.setId(sessionObjectId); |
||
48 | fvelez | 60 | getObjects().add(sessionObject); |
1 | fvelez | 61 | } |
62 | 1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream os = new 1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream(); |
||
162 | jmachado | 63 | // try |
64 | // { |
||
65 | // ObjectOutputStream oos = new ObjectOutputStream(os); |
||
66 | // oos.writeObject(obj); |
||
67 | // oos.close(); |
||
68 | // } |
||
69 | // catch (IOException e) |
||
70 | // { |
||
71 | // e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. |
||
72 | // } |
||
73 | // XMLEncoder xenc = new XMLEncoder(os); |
||
74 | // xenc.writeObject(obj); |
||
75 | // xenc.close(); |
||
76 | // sessionObject.setXmlObject(os.toString()); |
||
77 | sessionObject.setObject(obj); |
||
1 | fvelez | 78 | } |
79 | |||
80 | /** |
||
81 | * put an object in cache |
||
82 | * only will be persistent when serialize is called |
||
83 | * |
||
84 | * @param key |
||
85 | */ |
||
86 | public void remove(1.5.0/docs/api/java/lang/String.html">String key) |
||
87 | { |
||
76 | jmachado | 88 | if (getObjects() == null || getObjects().size() == 0) |
1 | fvelez | 89 | { |
90 | setObjects(new HashSet<SessionObject>()); |
||
91 | } |
||
92 | SessionObject sessionObject = getAndRemoveSessionObject(key); |
||
76 | jmachado | 93 | if (sessionObject != null) |
1 | fvelez | 94 | { |
76 | jmachado | 95 | if (removeList == null) |
1 | fvelez | 96 | removeList = new HashSet<SessionObject>(); |
97 | removeList.add(sessionObject); |
||
98 | } |
||
99 | } |
||
100 | |||
76 | jmachado | 101 | /** |
102 | * put an object in cache |
||
103 | * only will be persistent when serialize is called |
||
104 | * |
||
105 | * @param key |
||
106 | */ |
||
107 | |||
108 | public void removeWithOpenTransaction(1.5.0/docs/api/java/lang/String.html">String key) |
||
109 | { |
||
110 | if (getObjects() == null || getObjects().size() == 0) |
||
111 | { |
||
112 | setObjects(new HashSet<SessionObject>()); |
||
113 | } |
||
114 | SessionObject sessionObject = getAndRemoveSessionObject(key); |
||
115 | DaoFactory.getSessionObjectDaoImpl().evictAndDelete(sessionObject); |
||
116 | } |
||
117 | |||
118 | |||
1 | fvelez | 119 | public void clearObjects() |
120 | { |
||
978 | jmachado | 121 | |
76 | jmachado | 122 | if (getObjects() == null || getObjects().size() == 0) |
1 | fvelez | 123 | { |
124 | return; |
||
125 | } |
||
978 | jmachado | 126 | 1.5.0/docs/api/java/lang/System.html">System.out.println("Clearing session from " + getObjects().size() + " objects"); |
127 | for(SessionObject obj: getObjects()) |
||
128 | 1.5.0/docs/api/java/lang/System.html">System.out.println(obj.getId().getKeyId()); |
||
76 | jmachado | 129 | if(removeList != null) |
130 | removeList.addAll(getObjects()); |
||
131 | else |
||
132 | removeList = getObjects(); |
||
975 | jmachado | 133 | //todo NOVO Authenticate |
134 | getObjects().clear(); |
||
1 | fvelez | 135 | } |
136 | |||
76 | jmachado | 137 | public void clearObjectsWithOpenTransaction() |
138 | { |
||
139 | clearObjects(); |
||
181 | jmachado | 140 | if(removeList != null && removeList.size() > 0) |
974 | jmachado | 141 | { |
181 | jmachado | 142 | DaoFactory.getSessionObjectDaoImpl().evictAndDelete(removeList); |
974 | jmachado | 143 | |
144 | } |
||
76 | jmachado | 145 | } |
146 | |||
147 | |||
1 | fvelez | 148 | public void reset() |
149 | { |
||
150 | setUser(null); |
||
151 | setUsername(null); |
||
152 | setName(null); |
||
153 | clear(); |
||
154 | } |
||
155 | |||
76 | jmachado | 156 | public void resetWithOpenTransaction() |
157 | { |
||
158 | setUser(null); |
||
159 | setUsername(null); |
||
160 | setName(null); |
||
161 | clearWithOpenTransaction(); |
||
162 | } |
||
163 | |||
1 | fvelez | 164 | public void clear() |
165 | { |
||
76 | jmachado | 166 | clearObjects(); |
1 | fvelez | 167 | setUser(null); |
168 | setUsername(null); |
||
169 | setName(null); |
||
170 | } |
||
171 | |||
76 | jmachado | 172 | public void clearWithOpenTransaction() |
173 | { |
||
174 | clearObjectsWithOpenTransaction(); |
||
175 | setUser(null); |
||
176 | setUsername(null); |
||
177 | setName(null); |
||
178 | } |
||
179 | |||
1 | fvelez | 180 | /** |
181 | * Return a seralized object for a given key |
||
76 | jmachado | 182 | * |
1 | fvelez | 183 | * @param key |
184 | * @return |
||
185 | */ |
||
186 | public 1.5.0/docs/api/java/io/Serializable.html">Serializable get(1.5.0/docs/api/java/lang/String.html">String key) |
||
187 | { |
||
76 | jmachado | 188 | if (getObjects() == null) |
1 | fvelez | 189 | { |
190 | return null; |
||
191 | } |
||
76 | jmachado | 192 | for (SessionObject sessionObject : getObjects()) |
1 | fvelez | 193 | { |
76 | jmachado | 194 | if (sessionObject.getId().getKeyId().equals(key)) |
1 | fvelez | 195 | { |
162 | jmachado | 196 | //XMLDecoder xmlDecoder = new XMLDecoder(new ByteArrayInputStream(sessionObject.getXmlObject().getBytes())); |
197 | //return (Serializable) xmlDecoder.readObject(); |
||
198 | return sessionObject.getObject(); |
||
1 | fvelez | 199 | } |
200 | } |
||
201 | return null; |
||
202 | } |
||
203 | |||
204 | /** |
||
76 | jmachado | 205 | * Return an session object with a given key |
1 | fvelez | 206 | * |
207 | * @param key |
||
208 | * @return |
||
209 | */ |
||
995 | jmachado | 210 | public SessionObject getSessionObject(1.5.0/docs/api/java/lang/String.html">String key) |
1 | fvelez | 211 | { |
76 | jmachado | 212 | if (getObjects() == null) |
1 | fvelez | 213 | { |
214 | return null; |
||
215 | } |
||
76 | jmachado | 216 | for (SessionObject sessionObject : getObjects()) |
1 | fvelez | 217 | { |
76 | jmachado | 218 | if (sessionObject.getId().getKeyId().equals(key)) |
1 | fvelez | 219 | { |
220 | return sessionObject; |
||
221 | } |
||
222 | } |
||
223 | return null; |
||
224 | } |
||
225 | |||
76 | jmachado | 226 | /** |
227 | * Return an session object with a given key removing it from list |
||
1 | fvelez | 228 | * |
229 | * @param key |
||
230 | * @return |
||
231 | */ |
||
232 | private SessionObject getAndRemoveSessionObject(1.5.0/docs/api/java/lang/String.html">String key) |
||
233 | { |
||
76 | jmachado | 234 | if (getObjects() == null) |
1 | fvelez | 235 | { |
236 | return null; |
||
237 | } |
||
238 | Iterator<SessionObject> iter = getObjects().iterator(); |
||
76 | jmachado | 239 | while (iter.hasNext()) |
240 | { |
||
1 | fvelez | 241 | SessionObject sessionObject = iter.next(); |
76 | jmachado | 242 | if (sessionObject.getId().getKeyId().equals(key)) |
1 | fvelez | 243 | { |
244 | iter.remove(); |
||
245 | return sessionObject; |
||
246 | } |
||
247 | } |
||
248 | return null; |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Serialize Session to Database |
||
76 | jmachado | 253 | * |
1 | fvelez | 254 | * @param request |
255 | * @throws Throwable |
||
256 | */ |
||
142 | jmachado | 257 | public void serialize(HttpServletRequest request, HttpServletResponse response) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable |
1 | fvelez | 258 | { |
142 | jmachado | 259 | serialize(RequestUtils.getRequester(request, response)); |
1 | fvelez | 260 | } |
261 | |||
262 | /** |
||
263 | * Serialize Session to Database |
||
264 | * |
||
265 | * @param requester |
||
266 | * @throws Throwable |
||
267 | */ |
||
268 | public void serialize(1.5.0/docs/api/java/lang/String.html">String requester) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable |
||
269 | { |
||
270 | try |
||
271 | { |
||
77 | jmachado | 272 | |
1 | fvelez | 273 | IServiceManager sm = ServiceManager.getInstance(); |
76 | jmachado | 274 | 5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{this, removeList}; |
275 | sm.execute(requester, "UpdateUserSession", args); |
||
77 | jmachado | 276 | if(removeList != null) |
277 | removeList.clear(); |
||
1 | fvelez | 278 | } |
279 | catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e) |
||
280 | { |
||
281 | throw e; |
||
282 | } |
||
283 | } |
||
76 | jmachado | 284 | |
1097 | jmachado | 285 | public 1.5.0/docs/api/javax/security/auth/login/Configuration.html">Configuration getNowConfiguration() { |
286 | return nowConfiguration; |
||
287 | } |
||
288 | |||
289 | public void setNowConfiguration(1.5.0/docs/api/javax/security/auth/login/Configuration.html">Configuration nowConfiguration) { |
||
290 | this.nowConfiguration = nowConfiguration; |
||
291 | } |
||
1148 | jmachado | 292 | |
293 | public 1.5.0/docs/api/java/lang/String.html">String getUserPasswordInSession() |
||
294 | { |
||
295 | 1.5.0/docs/api/java/lang/String.html">String passCrypt = (1.5.0/docs/api/java/lang/String.html">String) get(FtpService.FTP_PASSWORD); |
||
296 | if(passCrypt != null) |
||
297 | return DesUtils.getInstance().decrypt(passCrypt); |
||
298 | return null; |
||
299 | } |
||
300 | |||
1312 | jmachado | 301 | public boolean isAuthenticated() |
302 | { |
||
303 | return getUser() != null; |
||
304 | } |
||
305 | |||
306 | public boolean isAnonymous() |
||
307 | { |
||
308 | return getUser() == null; |
||
309 | } |
||
310 | |||
1405 | jmachado | 311 | public 1.5.0/docs/api/java/lang/String.html">String getShortName() |
312 | { |
||
313 | if(getName() != null) |
||
314 | return getName().split(" ")[0]; |
||
315 | return getName(); |
||
316 | } |
||
317 | |||
1 | fvelez | 318 | } |