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.StringTokenizer;
23  
24  import javax.servlet.ServletException;
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  import org.springframework.extensions.surf.site.servlet.BaseServlet;
29  import org.springframework.extensions.surf.util.OverlayUtil;
30  
31  /**
32   * Helpful servlet that lets you switch various request-side features
33   * on for the current Web Studio session.
34   * 
35   * Generally, these are invoked by the web studio client but they
36   * could also be invoked by hand.
37   * 
38   * The general format of incoming requests will be:
39   *  - /studio/debug/<command>/<value>
40   * 
41   * The following is supported:
42   * 
43   *    /studio/debug/overlay/enable
44   *    /studio/debug/overlay/disable
45   *    /studio/debug/refresh
46   * 
47   * @author muzquiano
48   */
49  public class WebStudioDebugServlet extends BaseServlet
50  {
51      public void init() throws ServletException
52      {
53          super.init();
54      }
55  
56      protected void service(HttpServletRequest request,
57              HttpServletResponse response) throws ServletException, IOException
58      {
59          String uri = request.getRequestURI();
60  
61          // skip server context path and build the path to the resource
62          // we are looking for
63          uri = uri.substring(request.getContextPath().length());
64  
65          // validate and return the resource path - stripping the
66          // servlet context
67          StringTokenizer t = new StringTokenizer(uri, "/");
68          String servletName = t.nextToken();
69          if (!t.hasMoreTokens())
70          {
71              throw new ServletException("Invalid URL: " + uri);
72          }
73          String command = (String) t.nextToken();
74          if (!t.hasMoreTokens())
75          {
76              throw new ServletException("Invalid URL: " + uri);
77          }
78          String value = (String) t.nextToken();
79  
80          if (command != null)
81          {
82              if (command.equals("overlay"))
83              {
84                  if ("enable".equals(value))
85                  {
86                      // enable the overlays
87                      //WebStudioUtil.setOverlayEnabled(request, true);
88                  }
89  
90                  if ("disable".equals(value))
91                  {
92                      // disable the overlays
93                      //WebStudioUtil.setOverlayEnabled(request, false);
94                  }
95              }
96  
97              if (command.equals("refresh"))
98              {
99                  // refresh the js and css cache
100                 OverlayUtil.removeCachedResources(request, "JS_");
101                 OverlayUtil.removeCachedResources(request, "CSS_");
102             }
103         }
104     }
105 }