1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.springframework.extensions.surf.servlet;
20
21 import java.io.IOException;
22 import java.util.Iterator;
23 import java.util.Map;
24 import java.util.StringTokenizer;
25
26 import javax.servlet.ServletException;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import org.json.JSONArray;
31 import org.json.JSONException;
32 import org.json.JSONObject;
33 import org.springframework.extensions.surf.site.servlet.BaseServlet;
34 import org.springframework.extensions.surf.studio.BrowserStateBean;
35 import org.springframework.extensions.surf.studio.WebStudioService;
36 import org.springframework.extensions.surf.studio.WebStudioStateBean;
37 import org.springframework.extensions.surf.studio.WebStudioStateProvider;
38
39
40
41
42 public class ClientStatePersistenceServlet extends BaseServlet
43 {
44 public static final String PARAM_CONFIG = "config";
45 public static final String RESULT_STATUS_OK = "ok";
46 public static final String RESULT_STATUS = "status";
47
48 public static final String OBJECT_TYPE_APPLET = "applet";
49 public static final String OBJECT_TYPE_APPLICATION = "application";
50
51 public static final String COMMAND_ENABLE = "enable";
52 public static final String COMMAND_DISABLE = "disable";
53 public static final String COMMAND_PUT = "put";
54 public static final String COMMAND_GET = "get";
55 public static final String COMMAND_REMOVE = "remove";
56 public static final String COMMAND_DEPS = "deps";
57
58 public void init() throws ServletException
59 {
60 super.init();
61 }
62
63 protected void service(HttpServletRequest request,
64 HttpServletResponse response) throws ServletException, IOException
65 {
66 String uri = request.getRequestURI();
67
68
69
70 uri = uri.substring(request.getContextPath().length());
71
72
73
74 StringTokenizer t = new StringTokenizer(uri, "/");
75 String servletName = t.nextToken();
76 if (!t.hasMoreTokens())
77 {
78 throw new ServletException("Invalid URL: " + uri);
79 }
80
81
82 String objectType = t.nextToken();
83
84
85 String objectId = t.nextToken();
86
87
88 String command = t.nextToken();
89
90
91
92 WebStudioStateProvider clientStateProvider = WebStudioService.getInstance().getClientStateProvider();
93 WebStudioStateBean state = clientStateProvider.provide(request);
94
95
96 BrowserStateBean bean = null;
97 if (OBJECT_TYPE_APPLICATION.equals(objectType))
98 {
99 bean = state.getApplicationState(objectId);
100 }
101 else if (OBJECT_TYPE_APPLET.equals(objectType))
102 {
103 bean = state.getAppletState(objectId);
104 }
105
106 if (bean != null)
107 {
108
109 boolean processed = false;
110 JSONObject json = new JSONObject();
111
112 try
113 {
114
115 if (COMMAND_REMOVE.equals(command))
116 {
117 if (t.hasMoreTokens())
118 {
119
120 String key = (String) t.nextToken();
121 bean.remove(key);
122
123
124 json.put(RESULT_STATUS, RESULT_STATUS_OK);
125 processed = true;
126 }
127 else
128 {
129
130 bean.removeProperties();
131
132
133 json.put(RESULT_STATUS, RESULT_STATUS_OK);
134 processed = true;
135 }
136 }
137
138
139 if (COMMAND_PUT.equals(command))
140 {
141 if (t.hasMoreTokens())
142 {
143 String key = (String) t.nextToken();
144 if (t.hasMoreTokens())
145 {
146 String value = t.nextToken();
147 bean.put(key, value);
148
149
150 json.put(RESULT_STATUS, RESULT_STATUS_OK);
151 processed = true;
152 }
153 }
154 }
155
156
157 if (COMMAND_GET.equals(command))
158 {
159 if (t.hasMoreTokens())
160 {
161
162 String key = (String) t.nextToken();
163 String value = bean.get(key);
164
165
166 json.put(RESULT_STATUS, RESULT_STATUS_OK);
167 json.put(key, value);
168 processed = true;
169 }
170 else
171 {
172 Map<String, String> properties = bean.getProperties();
173 Iterator it = properties.keySet().iterator();
174 while (it.hasNext())
175 {
176 String key = (String) it.next();
177 String value = (String) properties.get(key);
178
179 json.put(key, value);
180 }
181
182 json.put(RESULT_STATUS, RESULT_STATUS_OK);
183 processed = true;
184 }
185 }
186
187 if (COMMAND_ENABLE.equals(command))
188 {
189 bean.enable();
190 json.put(RESULT_STATUS, RESULT_STATUS_OK);
191 processed = true;
192 }
193
194 if (COMMAND_DISABLE.equals(command))
195 {
196 bean.disable();
197 json.put(RESULT_STATUS, RESULT_STATUS_OK);
198 processed = true;
199 }
200
201 if (COMMAND_DEPS.equals(command))
202 {
203 JSONObject config = null;
204
205 String options = request.getParameter(PARAM_CONFIG);
206 if (options != null)
207 {
208 config = new JSONObject(options);
209
210
211 bean.clearDependencies();
212
213
214 JSONArray jsArray = config.getJSONArray("js");
215 for (int i = 0; i < jsArray.length(); i++)
216 {
217 String file = jsArray.getString(i);
218 bean.addJsFile(file);
219 }
220
221
222 JSONArray cssArray = config.getJSONArray("css");
223 for (int i = 0; i < cssArray.length(); i++)
224 {
225 String file = cssArray.getString(i);
226 bean.addCssFile(file);
227 }
228
229
230 JSONArray domArray = config.getJSONArray("dom");
231 for (int i = 0; i < domArray.length(); i++)
232 {
233 String file = domArray.getString(i);
234 bean.addDomFile(file);
235 }
236
237 json.put(RESULT_STATUS, RESULT_STATUS_OK);
238 processed = true;
239 }
240 }
241
242
243 if (processed)
244 {
245 response.getWriter().println(json.toString());
246 }
247 else
248 {
249 response.getWriter().println(
250 "Unable to execute command: " + command);
251 }
252 }
253 catch (JSONException jsonException)
254 {
255 throw new ServletException(jsonException);
256 }
257 }
258 }
259 }