1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.servlet;
24  
25  import com.liferay.portal.kernel.language.LanguageUtil;
26  import com.liferay.portal.kernel.util.ContentTypes;
27  import com.liferay.portal.kernel.util.LocaleUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.util.PortalInstances;
32  import com.liferay.util.servlet.ServletResponseUtil;
33  
34  import java.io.IOException;
35  
36  import java.util.Locale;
37  
38  import javax.servlet.http.HttpServlet;
39  import javax.servlet.http.HttpServletRequest;
40  import javax.servlet.http.HttpServletResponse;
41  
42  import org.apache.commons.logging.Log;
43  import org.apache.commons.logging.LogFactory;
44  
45  /**
46   * <a href="LanguageServlet.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   *
50   */
51  public class LanguageServlet extends HttpServlet {
52  
53      public void service(
54              HttpServletRequest request, HttpServletResponse response)
55          throws IOException {
56  
57          String path = request.getPathInfo();
58  
59          if (_log.isDebugEnabled()) {
60              _log.debug("Path " + path);
61          }
62  
63          if (Validator.isNotNull(path) && path.startsWith(StringPool.SLASH)) {
64              path = path.substring(1, path.length());
65          }
66  
67          String[] pathArray = StringUtil.split(path, StringPool.SLASH);
68  
69          if (pathArray.length == 0) {
70              _log.error("Language id is not specified");
71  
72              return;
73          }
74  
75          if (pathArray.length == 1) {
76              _log.error("Language key is not specified");
77  
78              return;
79          }
80  
81          Locale locale = LocaleUtil.fromLanguageId(pathArray[0]);
82          String key = pathArray[1];
83  
84          Object[] arguments = null;
85  
86          if (pathArray.length > 2) {
87              arguments = new Object[pathArray.length - 2];
88  
89              System.arraycopy(pathArray, 2, arguments, 0, arguments.length);
90          }
91  
92          String value = key;
93  
94          try {
95              long companyId = PortalInstances.getCompanyId(request);
96  
97              if ((arguments == null) || (arguments.length == 0)) {
98                  value = LanguageUtil.get(companyId, locale, key);
99              }
100             else {
101                 value = LanguageUtil.format(companyId, locale, key, arguments);
102             }
103         }
104         catch (Exception e) {
105             if (_log.isWarnEnabled()) {
106                 _log.warn(e, e);
107             }
108         }
109 
110         response.setContentType(ContentTypes.TEXT_PLAIN_UTF8);
111 
112         ServletResponseUtil.write(response, value.getBytes(StringPool.UTF8));
113     }
114 
115     private static Log _log = LogFactory.getLog(LanguageServlet.class);
116 
117 }