View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * @author muzquiano
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          // skip server context path and build the path to the resource
69          // we are looking for
70          uri = uri.substring(request.getContextPath().length());
71  
72          // validate and return the resource path - stripping the
73          // servlet context
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          // the type of thing we're looking at (application, applet)
82          String objectType = t.nextToken();
83  
84          // the id of the thing
85          String objectId = t.nextToken();
86  
87          // the command (put, get, remove, deps)
88          String command = t.nextToken();
89  
90          // get the web studio state for the connecting browser
91          // TODO: rework all of this
92          WebStudioStateProvider clientStateProvider = WebStudioService.getInstance().getClientStateProvider();
93          WebStudioStateBean state = clientStateProvider.provide(request);
94  
95          // determine what we're acting upon
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             // JSON return
109             boolean processed = false;
110             JSONObject json = new JSONObject();
111 
112             try
113             {
114                 // remove command
115                 if (COMMAND_REMOVE.equals(command))
116                 {
117                     if (t.hasMoreTokens())
118                     {
119                         // remove a single property
120                         String key = (String) t.nextToken();
121                         bean.remove(key);
122 
123                         // update status
124                         json.put(RESULT_STATUS, RESULT_STATUS_OK);
125                         processed = true;
126                     }
127                     else
128                     {
129                         // remove all properties
130                         bean.removeProperties();
131 
132                         // update status
133                         json.put(RESULT_STATUS, RESULT_STATUS_OK);
134                         processed = true;
135                     }
136                 }
137 
138                 // puts a single property
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                             // update status
150                             json.put(RESULT_STATUS, RESULT_STATUS_OK);
151                             processed = true;
152                         }
153                     }
154                 }
155 
156                 // gets properties
157                 if (COMMAND_GET.equals(command))
158                 {
159                     if (t.hasMoreTokens())
160                     {
161                         // get a single property
162                         String key = (String) t.nextToken();
163                         String value = bean.get(key);
164 
165                         // update status
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                         // clear existing dependencies
211                         bean.clearDependencies();
212 
213                         // add in js files
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                         // add in css files
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                         // add in dom files
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                 // write out the json
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 }