1   /**
2    * Copyright (c) 2000-2009 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.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.ContentTypes;
29  import com.liferay.portal.kernel.util.LocaleUtil;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.kernel.util.StringUtil;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.portal.util.PortalInstances;
34  import com.liferay.util.servlet.ServletResponseUtil;
35  
36  import java.io.IOException;
37  
38  import java.util.Locale;
39  
40  import javax.servlet.http.HttpServlet;
41  import javax.servlet.http.HttpServletRequest;
42  import javax.servlet.http.HttpServletResponse;
43  
44  /**
45   * <a href="LanguageServlet.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class LanguageServlet extends HttpServlet {
51  
52      public void service(
53              HttpServletRequest request, HttpServletResponse response)
54          throws IOException {
55  
56          String path = request.getPathInfo();
57  
58          if (_log.isDebugEnabled()) {
59              _log.debug("Path " + path);
60          }
61  
62          if (Validator.isNotNull(path) && path.startsWith(StringPool.SLASH)) {
63              path = path.substring(1, path.length());
64          }
65  
66          String[] pathArray = StringUtil.split(path, StringPool.SLASH);
67  
68          if (pathArray.length == 0) {
69              _log.error("Language id is not specified");
70  
71              return;
72          }
73  
74          if (pathArray.length == 1) {
75              _log.error("Language key is not specified");
76  
77              return;
78          }
79  
80          Locale locale = LocaleUtil.fromLanguageId(pathArray[0]);
81          String key = pathArray[1];
82  
83          Object[] arguments = null;
84  
85          if (pathArray.length > 2) {
86              arguments = new Object[pathArray.length - 2];
87  
88              System.arraycopy(pathArray, 2, arguments, 0, arguments.length);
89          }
90  
91          String value = key;
92  
93          try {
94              long companyId = PortalInstances.getCompanyId(request);
95  
96              if ((arguments == null) || (arguments.length == 0)) {
97                  value = LanguageUtil.get(companyId, locale, key);
98              }
99              else {
100                 value = LanguageUtil.format(companyId, locale, key, arguments);
101             }
102         }
103         catch (Exception e) {
104             if (_log.isWarnEnabled()) {
105                 _log.warn(e, e);
106             }
107         }
108 
109         response.setContentType(ContentTypes.TEXT_PLAIN_UTF8);
110 
111         ServletResponseUtil.write(response, value.getBytes(StringPool.UTF8));
112     }
113 
114     private static Log _log = LogFactoryUtil.getLog(LanguageServlet.class);
115 
116 }