Project Tiles has retired. For details please refer to its Attic page.
SelectLocaleServlet xref
View Javadoc

1   /*
2    * $Id: SelectLocaleServlet.java 892369 2009-12-18 20:21:36Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.tiles.test.servlet;
22  
23  import java.util.Locale;
24  
25  import javax.servlet.ServletConfig;
26  import javax.servlet.ServletException;
27  import javax.servlet.http.HttpServlet;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  import javax.servlet.http.HttpSession;
31  
32  import org.apache.tiles.TilesContainer;
33  import org.apache.tiles.access.TilesAccess;
34  import org.apache.tiles.locale.impl.DefaultLocaleResolver;
35  import org.apache.tiles.request.ApplicationContext;
36  import org.apache.tiles.request.Request;
37  import org.apache.tiles.request.servlet.ServletRequest;
38  
39  /**
40   * Servlet able to let a user choose a locale.
41   *
42   * @version $Rev: 892369 $ $Date: 2009-12-19 07:21:36 +1100 (Sat, 19 Dec 2009) $
43   */
44  public class SelectLocaleServlet extends HttpServlet {
45  
46      /**
47       * The key of the container to use.
48       */
49      private String containerKey;
50  
51      /**
52       * The name of the definition to render.
53       */
54      private String definitionName;
55  
56      /** {@inheritDoc} */
57      @Override
58      public void init(ServletConfig config) throws ServletException {
59          super.init(config);
60          containerKey = config
61                  .getInitParameter("org.apache.tiles.test.servlet.ServletConfig.CONTAINER_KEY");
62          definitionName = config
63                  .getInitParameter("org.apache.tiles.test.servlet.ServletConfig.DEFINITION_NAME");
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      protected void doGet(HttpServletRequest request,
69              HttpServletResponse response) {
70          process(request, response);
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      protected void doPost(HttpServletRequest request,
76              HttpServletResponse response) {
77          process(request, response);
78      }
79  
80      /**
81       * Processes the request.
82       *
83       * @param request The request object.
84       * @param response The response object.
85       */
86      private void process(HttpServletRequest request,
87              HttpServletResponse response) {
88          String localeParameter = request.getParameter("locale");
89          HttpSession session = request.getSession();
90          Locale locale = null;
91          if (localeParameter != null && localeParameter.trim().length() > 0) {
92              String[] localeStrings = localeParameter.split("_");
93              if (localeStrings.length == 1) {
94                  locale = new Locale(localeStrings[0]);
95              } else if (localeStrings.length == 2) {
96                  locale = new Locale(localeStrings[0], localeStrings[1]);
97              } else if (localeStrings.length > 2) {
98                  locale = new Locale(localeStrings[0], localeStrings[1], localeStrings[2]);
99              }
100         }
101         session.setAttribute(DefaultLocaleResolver.LOCALE_KEY, locale);
102         ApplicationContext applicationContext = org.apache.tiles.request.servlet.ServletUtil
103                 .getApplicationContext(getServletContext());
104         Request currentRequest = new ServletRequest(applicationContext, request, response);
105         TilesAccess.setCurrentContainer(currentRequest, containerKey);
106         TilesContainer container = TilesAccess.getCurrentContainer(currentRequest);
107         container.render(definitionName, currentRequest);
108     }
109 }