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.io.IOException;
22  import java.util.StringTokenizer;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.springframework.extensions.surf.FrameworkUtil;
30  import org.springframework.extensions.surf.ThreadLocalPreviewContext;
31  import org.springframework.extensions.surf.WebFrameworkConstants;
32  import org.springframework.web.servlet.ModelAndView;
33  
34  /**
35   * Controller for managing requests for preview.
36   * 
37   * This allows the previewed sandbox and webapp to be specified via a
38   * URL for the current session.  It then performs a redirect to the
39   * Surf application.
40   * 
41   * Incoming URLs can be of the form:
42   * 
43   *    /preview/{sandboxId}
44   *    /preview/{sandboxId}/{webappId}
45   *    /preview?alfStoreId={sandboxId}
46   *    /preview?alfStoreId={sandboxId}&alfWebappId={webappId}
47   *    /preview?s={sandboxId}
48   *    /preview?s={sandboxId}&w={webappId}
49   * 
50   * @author muzquiano
51   */
52  public class PreviewController extends AbstractWebFrameworkController
53  {
54      private static Log logger = LogFactory.getLog(PreviewController.class);
55      
56      /* (non-Javadoc)
57       * @see org.alfresco.web.framework.mvc.AbstractWebFrameworkController#getLogger()
58       */
59      public Log getLogger()
60      {
61          return logger;        
62      }
63      
64      /* (non-Javadoc)
65       * @see org.alfresco.web.framework.mvc.AbstractWebFrameworkController#createModelAndView(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
66       */
67      public ModelAndView createModelAndView(HttpServletRequest request, HttpServletResponse response)
68      {
69          // This servlet will only operate if the Web Framework instance
70          // is in preview mode
71          if (this.getWebFrameworkConfiguration().isPreviewEnabled())
72          {
73              boolean updated = false;
74              
75              // values we want to determine
76              String storeId = null;
77              String webappId = null;
78              
79              // try to determine values by parsing tokens
80              String uri = request.getRequestURI();
81              
82              // skip server context path and build the path to the resource we are looking for
83              uri = uri.substring(request.getContextPath().length());
84              
85              // validate and return the resource path - stripping the servlet context
86              StringTokenizer t = new StringTokenizer(uri, "/");
87              String servletName = t.nextToken();
88              if (t.hasMoreTokens())
89              {
90                  storeId = t.nextToken();
91              }
92              if (t.hasMoreTokens())
93              {
94                  webappId = t.nextToken();
95              }
96              
97              
98              // try to determine values by looking at request parameters
99              String _storeId = (String) request.getParameter(WebFrameworkConstants.STORE_ID_REQUEST_PARAM_NAME);
100             if (_storeId == null)
101             {
102                 _storeId = (String) request.getParameter("s");                
103             }
104             if (_storeId != null)
105             {
106                 storeId = _storeId;
107             }
108             String _webappId = (String) request.getParameter(WebFrameworkConstants.WEBAPP_ID_REQUEST_PARAM_NAME);
109             if (_webappId == null)
110             {
111                 _webappId = (String) request.getParameter("w");
112             }
113             if (_webappId != null)
114             {
115                 webappId = _webappId;
116             }
117                         
118             // set values onto session
119             if (storeId != null)
120             {
121                 updated = true;
122                 request.getSession(true).setAttribute(WebFrameworkConstants.STORE_ID_SESSION_ATTRIBUTE_NAME, storeId);                
123             }
124             if (storeId != null && webappId != null)
125             {
126                 updated = true;
127                 request.getSession(true).setAttribute(WebFrameworkConstants.WEBAPP_ID_SESSION_ATTRIBUTE_NAME, webappId);
128             }
129             
130             
131             // if updated, then lets do a full refresh
132             if (updated)
133             {
134                 // set onto thread local
135                 ThreadLocalPreviewContext sandboxContext = new ThreadLocalPreviewContext(storeId, webappId);
136                 
137                 // refresh the web scripts
138                 FrameworkUtil.resetWebScripts();
139                 
140                 /*
141                 ApplicationContext appContext = FrameworkHelper.getApplicationContext();
142                 if (appContext instanceof ConfigurableApplicationContext)
143                 {
144                     ((ConfigurableApplicationContext) appContext).refresh();
145                 }
146                 */
147 
148                 // TODO: reinstate this?
149                 // refresh web flow
150                 //FrameworkUtil.resetWebFlow();
151             }
152 
153             // send redirect
154             try {
155                 response.sendRedirect(request.getContextPath());
156             }
157             catch (IOException ioe){ }
158         }
159         
160         return null;
161     }
162 }