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.mvc;
20  
21  import java.util.ArrayList;
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.FrameworkUtil;
29  import org.springframework.extensions.surf.RequestContext;
30  import org.springframework.extensions.surf.site.CacheUtil;
31  import org.springframework.web.servlet.ModelAndView;
32  import org.springframework.web.servlet.mvc.AbstractController;
33  
34  /**
35   * Default Spring controller for processing Surf remote control calls.
36   * 
37   * Surf remote control calls are essentially REST-style calls which tell
38   * the Surf server to perform operations against its cache or its
39   * internal state.
40   * 
41   * The following URLs are supported:
42   * 
43   *     /cache/invalidate
44   *     /webscripts/reset
45   *     
46   * These are generally of the form:
47   * 
48   *     /<system>/<action>
49   * 
50   * @author muzquiano
51   */
52  public class RemoteController extends AbstractController
53  {
54      private static final String MODE_CACHE = "cache";
55      private static final String MODE_CACHE_COMMAND_INVALIDATE = "invalidate";
56      
57      private static final String MODE_WEBSCRIPTS = "webscripts";
58      private static final String MODE_WEBSCRIPTS_COMMAND_RESET = "reset";
59      
60      public ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception
61      {
62          // get the request context
63          RequestContext context = FrameworkUtil.getCurrentRequestContext();
64          
65          String uri = request.getRequestURI();
66          
67          // skip server context path and build the path to the resource we are looking for
68          uri = uri.substring(request.getContextPath().length());
69          
70          // validate and return the resource path - stripping the servlet context
71          StringTokenizer t = new StringTokenizer(uri, "/");
72          String servletName = t.nextToken();
73          if (!t.hasMoreTokens())
74          {
75              throw new ServletException("Invalid URL: " + uri);
76          }
77          String mode = t.nextToken();
78          if ( !t.hasMoreTokens())
79          {
80              throw new ServletException("Invalid URL: " + uri);
81          }
82          String command = t.nextToken();
83          
84          // load additional arguments, if any
85          ArrayList<String> args = new ArrayList<String>();
86          if (t.hasMoreTokens())
87          {
88              args.add(t.nextToken());            
89          }
90                  
91          // CACHE
92          if (MODE_CACHE.equalsIgnoreCase(mode))
93          {
94              if (MODE_CACHE_COMMAND_INVALIDATE.equalsIgnoreCase(command))
95              {
96                  // invalidate the model service object cache
97                  CacheUtil.invalidateModelObjectServiceCache(context);
98              }
99          }
100         
101         // WEBSCRIPTS
102         if (MODE_WEBSCRIPTS.equalsIgnoreCase(mode))
103         {
104             if (MODE_WEBSCRIPTS_COMMAND_RESET.equalsIgnoreCase(command))
105             {
106                 FrameworkUtil.resetWebScripts();
107             }
108         }
109         
110         return null;
111     }
112 }