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;
20  
21  import java.io.IOException;
22  import java.util.StringTokenizer;
23  
24  import javax.servlet.ServletException;
25  import javax.servlet.http.HttpServlet;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.springframework.context.ApplicationContext;
32  import org.springframework.extensions.config.Config;
33  import org.springframework.extensions.config.ConfigService;
34  import org.springframework.extensions.config.ServerConfigElement;
35  import org.springframework.extensions.config.ServerProperties;
36  import org.springframework.extensions.surf.util.I18NUtil;
37  import org.springframework.extensions.webscripts.RuntimeContainer;
38  import org.springframework.web.context.support.WebApplicationContextUtils;
39  
40  
41  /**
42   * Entry point for Web Scripts
43   * 
44   * @author davidc
45   */
46  public class WebScriptServlet extends HttpServlet
47  {
48      private static final long serialVersionUID = 4209892938069597860L;
49  
50      // Logger
51      private static final Log logger = LogFactory.getLog(WebScriptServlet.class);
52  
53      // Component Dependencies
54      protected RuntimeContainer container;
55      protected ServletAuthenticatorFactory authenticatorFactory;
56      protected ConfigService configService;
57  
58      /** Host Server Configuration */
59      protected static ServerProperties serverProperties;
60  
61      /* (non-Javadoc)
62       * @see javax.servlet.GenericServlet#init()
63       */
64      @Override
65      public void init() throws ServletException
66      {
67          super.init();
68          ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
69          configService = (ConfigService)context.getBean("web.config");
70          String containerName = getServletConfig().getInitParameter("container");
71          if (containerName == null)
72          {
73              containerName = "webscripts.container";
74          }
75          container = (RuntimeContainer)context.getBean(containerName);
76          
77          // retrieve authenticator factory
78          String authenticatorId = getInitParameter("authenticator");
79          if (authenticatorId != null && authenticatorId.length() > 0)
80          {
81              Object bean = context.getBean(authenticatorId);
82              if (bean == null || !(bean instanceof ServletAuthenticatorFactory))
83              {
84                  throw new ServletException("Initialisation parameter 'authenticator' does not refer to a servlet authenticator factory (" + authenticatorId + ")");
85              }
86              authenticatorFactory = (ServletAuthenticatorFactory)bean;
87          }
88          
89          // retrieve host server configuration 
90          Config config = configService.getConfig("Server");
91          serverProperties = (ServerConfigElement)config.getConfigElement(ServerConfigElement.CONFIG_ELEMENT_ID);
92          
93          // servlet specific initialisation
94          initServlet(context);
95          
96          if (logger.isDebugEnabled())
97              logger.debug("Initialised Web Script Servlet (authenticator='" + authenticatorId + "')");
98      }
99  
100     /* (non-Javadoc) 
101      * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
102      */
103     protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
104     {
105         if (logger.isDebugEnabled())
106             logger.debug("Processing request ("  + req.getMethod() + ") " + req.getRequestURL() + (req.getQueryString() != null ? "?" + req.getQueryString() : ""));
107         
108         if (req.getCharacterEncoding() == null)
109         {
110             req.setCharacterEncoding("UTF-8");
111         }
112         
113         setLanguageFromRequestHeader(req);
114         
115         WebScriptServletRuntime runtime = new WebScriptServletRuntime(container, authenticatorFactory, req, res, serverProperties);
116         runtime.executeScript();
117     }
118     
119     /**
120      * Servlet specific initialisation
121      * 
122      * @param context
123      */
124     protected void initServlet(ApplicationContext context)
125     {
126         // NOOP
127     }
128     
129     /**
130      * Apply Client and Repository language locale based on the 'Accept-Language' request header
131      */
132     public static void setLanguageFromRequestHeader(HttpServletRequest req)
133     {
134         // set language locale from browser header
135         String acceptLang = req.getHeader("Accept-Language");
136         if (acceptLang != null && acceptLang.length() != 0)
137         {
138             StringTokenizer t = new StringTokenizer(acceptLang, ",; ");
139             // get language and convert to java locale format
140             String language = t.nextToken().replace('-', '_');
141             I18NUtil.setLocale(I18NUtil.parseLocale(language));
142         }
143     }
144 }