1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class LanguageServlet extends HttpServlet {
50  
51      public void service(
52              HttpServletRequest request, HttpServletResponse response)
53          throws IOException {
54  
55          String path = request.getPathInfo();
56  
57          if (_log.isDebugEnabled()) {
58              _log.debug("Path " + path);
59          }
60  
61          if (Validator.isNotNull(path) && path.startsWith(StringPool.SLASH)) {
62              path = path.substring(1, path.length());
63          }
64  
65          String[] pathArray = StringUtil.split(path, StringPool.SLASH);
66  
67          if (pathArray.length == 0) {
68              _log.error("Language id is not specified");
69  
70              return;
71          }
72  
73          if (pathArray.length == 1) {
74              _log.error("Language key is not specified");
75  
76              return;
77          }
78  
79          Locale locale = LocaleUtil.fromLanguageId(pathArray[0]);
80          String key = pathArray[1];
81  
82          Object[] arguments = null;
83  
84          if (pathArray.length > 2) {
85              arguments = new Object[pathArray.length - 2];
86  
87              System.arraycopy(pathArray, 2, arguments, 0, arguments.length);
88          }
89  
90          String value = key;
91  
92          try {
93              long companyId = PortalInstances.getCompanyId(request);
94  
95              if ((arguments == null) || (arguments.length == 0)) {
96                  value = LanguageUtil.get(companyId, locale, key);
97              }
98              else {
99                  value = LanguageUtil.format(companyId, locale, key, arguments);
100             }
101         }
102         catch (Exception e) {
103             if (_log.isWarnEnabled()) {
104                 _log.warn(e, e);
105             }
106         }
107 
108         response.setContentType(ContentTypes.TEXT_PLAIN_UTF8);
109 
110         ServletResponseUtil.write(response, value.getBytes(StringPool.UTF8));
111     }
112 
113     private static Log _log = LogFactoryUtil.getLog(LanguageServlet.class);
114 
115 }