0
|
1 package nabble.view.web.util;
|
|
2
|
|
3 import nabble.view.lib.Jtp;
|
|
4 import nabble.model.Executors;
|
|
5
|
|
6 import javax.servlet.ServletException;
|
|
7 import javax.servlet.http.HttpServlet;
|
|
8 import javax.servlet.http.HttpServletRequest;
|
|
9 import javax.servlet.http.HttpServletResponse;
|
|
10 import java.io.IOException;
|
|
11 import java.io.PrintWriter;
|
|
12 import java.util.Map;
|
|
13 import java.util.HashMap;
|
|
14 import java.util.Collections;
|
|
15 import java.util.concurrent.TimeUnit;
|
|
16
|
|
17
|
|
18 public final class SessionService extends HttpServlet {
|
|
19
|
|
20 protected void service(HttpServletRequest request, HttpServletResponse response)
|
|
21 throws ServletException, IOException
|
|
22 {
|
|
23 response.setHeader("Content-Type","application/x-javascript");
|
|
24 Jtp.dontCache(response);
|
|
25 String action = request.getParameter("action");
|
|
26 String clientID = request.getParameter("cid");
|
|
27 if (clientID == null)
|
|
28 return;
|
|
29
|
|
30 SessionData sessionData = getSessionData(clientID);
|
|
31 if ("set".equals(action)) {
|
|
32 String[] vs = request.getParameterValues("v");
|
|
33 for (String v : vs) {
|
|
34 int posPipe = v.indexOf('|');
|
|
35 String key = v.substring(0, posPipe);
|
|
36 String oldValue = sessionData.attributes.get(key);
|
|
37 String value = v.substring(posPipe+1);
|
|
38 value = oldValue == null? value : oldValue + value; // concat
|
|
39 sessionData.attributes.put(key, value);
|
|
40 }
|
|
41 } else if ("get".equals(action)) {
|
|
42 PrintWriter out = response.getWriter();
|
|
43 String[] keys = request.getParameterValues("key");
|
|
44 for (String key : keys) {
|
|
45 String value = sessionData.attributes.get(key);
|
|
46 if (value != null) {
|
|
47 out.print(value);
|
|
48 sessionData.attributes.remove(key);
|
|
49 }
|
|
50 }
|
|
51 }
|
|
52 }
|
|
53
|
|
54
|
|
55 private static final Map<String, SessionData> sessions = Collections.synchronizedMap(new HashMap<String, SessionData>());
|
|
56
|
|
57 private static synchronized SessionData getSessionData(String clientID) {
|
|
58 SessionData sessionData = sessions.get(clientID);
|
|
59 if (sessionData == null) {
|
|
60 sessionData = new SessionData();
|
|
61 sessions.put(clientID, sessionData);
|
|
62 }
|
|
63 sessionData.lastUsage = System.currentTimeMillis();
|
|
64 return sessionData;
|
|
65 }
|
|
66
|
|
67 private static class SessionData {
|
|
68 private long lastUsage;
|
|
69 private final Map<String, String> attributes = new HashMap<String, String>();
|
|
70 }
|
|
71
|
|
72 private static final long TIME_LIMIT = 2L * 60L * 1000L; // two minutes
|
|
73 static {
|
|
74 // Custom Garbage Collection algorithm:
|
|
75 // Removes SessionData objects that are old in memory.
|
|
76
|
|
77 // A - If the sessionData has no cookies, then it was used just for the iframe resize.
|
|
78 // So we can delete them after two minutes without problems. If the user tries to resize
|
|
79 // the iframe after that, another object will be created for the transfer.
|
|
80
|
|
81 Executors.scheduleWithFixedDelay(new Runnable(){public void run(){
|
|
82 synchronized(SessionService.class) {
|
|
83 long now = System.currentTimeMillis();
|
|
84 String[] keys = sessions.keySet().toArray(new String[0]);
|
|
85 for( String key : keys ) {
|
|
86 if( now - sessions.get(key).lastUsage > TIME_LIMIT )
|
|
87 sessions.remove(key);
|
|
88 }
|
|
89 }
|
|
90 }}, 5*60, 5*60, TimeUnit.SECONDS); // Every 5 minutes
|
|
91 }
|
|
92 }
|