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.webscripts.servlet.mvc;
20  
21  import java.util.Map;
22  import java.util.StringTokenizer;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.springframework.extensions.config.Config;
28  import org.springframework.extensions.config.ConfigService;
29  import org.springframework.extensions.config.ServerConfigElement;
30  import org.springframework.extensions.config.ServerProperties;
31  import org.springframework.extensions.surf.util.I18NUtil;
32  import org.springframework.extensions.webscripts.RuntimeContainer;
33  import org.springframework.extensions.webscripts.servlet.ServletAuthenticatorFactory;
34  import org.springframework.extensions.webscripts.servlet.WebScriptServletRuntime;
35  import org.springframework.web.servlet.view.AbstractUrlBasedView;
36  import org.springframework.web.util.WebUtils;
37  
38  /**
39   * Web Script view implementation
40   * 
41   * @author muzquiano
42   */
43  public class WebScriptView extends AbstractUrlBasedView 
44  {   
45      protected RuntimeContainer container;
46      protected ServletAuthenticatorFactory authenticatorFactory;
47      protected ConfigService configService;
48      
49      /** Host Server Configuration */
50      protected static ServerProperties serverProperties;    
51      
52      public WebScriptView(RuntimeContainer container, ServletAuthenticatorFactory authenticatorFactory, ConfigService configService) 
53      {
54          this.container = container;
55          this.authenticatorFactory = authenticatorFactory;
56          this.configService = configService;
57          
58          // retrieve host server configuration 
59          Config config = configService.getConfig("Server");
60          serverProperties = (ServerConfigElement)config.getConfigElement(ServerConfigElement.CONFIG_ELEMENT_ID);        
61      }
62      
63      /* (non-Javadoc)
64       * @see org.springframework.web.servlet.view.AbstractView#renderMergedOutputModel(java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
65       */
66      protected void renderMergedOutputModel(
67              Map model, HttpServletRequest request, HttpServletResponse response) throws Exception 
68      {
69          // the web script url
70          String uri = this.getUrl();
71          
72          // Expose the model object as request attributes.
73          exposeModelAsRequestAttributes(model, request);
74          
75          if (logger.isDebugEnabled())
76              logger.debug("Processing request ("  + request.getMethod() + ") " + request.getRequestURL() + (request.getQueryString() != null ? "?" + request.getQueryString() : ""));
77          
78          if (request.getCharacterEncoding() == null)
79          {
80              request.setCharacterEncoding("UTF-8");
81          }
82          
83          setLanguageFromRequestHeader(request);
84          
85          // handle off to web script servlet runtime
86          WebScriptServletRuntime runtime = new WebScriptServletRuntime(container, authenticatorFactory, request, response, serverProperties);
87          runtime.executeScript();
88      }
89  
90      /**
91       * Expose forward request attributes.
92       * 
93       * @param request the request
94       */
95      protected void exposeForwardRequestAttributes(HttpServletRequest request) {
96          WebUtils.exposeForwardRequestAttributes(request);
97      }    
98      
99      /**
100      * Apply Client and Repository language locale based on the 'Accept-Language' request header
101      */
102     public static void setLanguageFromRequestHeader(HttpServletRequest req)
103     {
104         // set language locale from browser header
105         String acceptLang = req.getHeader("Accept-Language");
106         if (acceptLang != null && acceptLang.length() != 0)
107         {
108             StringTokenizer t = new StringTokenizer(acceptLang, ",; ");
109             // get language and convert to java locale format
110             String language = t.nextToken().replace('-', '_');
111             I18NUtil.setLocale(I18NUtil.parseLocale(language));
112         }
113     }
114     
115 }