1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.servlet;
21  
22  import com.liferay.portal.kernel.language.LanguageUtil;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.portal.kernel.util.ContentTypes;
26  import com.liferay.portal.kernel.util.LocaleUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.util.PortalInstances;
31  import com.liferay.util.servlet.ServletResponseUtil;
32  
33  import java.io.IOException;
34  
35  import java.util.Locale;
36  
37  import javax.servlet.http.HttpServlet;
38  import javax.servlet.http.HttpServletRequest;
39  import javax.servlet.http.HttpServletResponse;
40  
41  /**
42   * <a href="LanguageServlet.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class LanguageServlet extends HttpServlet {
48  
49      public void service(
50              HttpServletRequest request, HttpServletResponse response)
51          throws IOException {
52  
53          String path = request.getPathInfo();
54  
55          if (_log.isDebugEnabled()) {
56              _log.debug("Path " + path);
57          }
58  
59          if (Validator.isNotNull(path) && path.startsWith(StringPool.SLASH)) {
60              path = path.substring(1, path.length());
61          }
62  
63          String[] pathArray = StringUtil.split(path, StringPool.SLASH);
64  
65          if (pathArray.length == 0) {
66              _log.error("Language id is not specified");
67  
68              return;
69          }
70  
71          if (pathArray.length == 1) {
72              _log.error("Language key is not specified");
73  
74              return;
75          }
76  
77          Locale locale = LocaleUtil.fromLanguageId(pathArray[0]);
78          String key = pathArray[1];
79  
80          Object[] arguments = null;
81  
82          if (pathArray.length > 2) {
83              arguments = new Object[pathArray.length - 2];
84  
85              System.arraycopy(pathArray, 2, arguments, 0, arguments.length);
86          }
87  
88          String value = key;
89  
90          try {
91              long companyId = PortalInstances.getCompanyId(request);
92  
93              if ((arguments == null) || (arguments.length == 0)) {
94                  value = LanguageUtil.get(companyId, locale, key);
95              }
96              else {
97                  value = LanguageUtil.format(companyId, locale, key, arguments);
98              }
99          }
100         catch (Exception e) {
101             if (_log.isWarnEnabled()) {
102                 _log.warn(e, e);
103             }
104         }
105 
106         response.setContentType(ContentTypes.TEXT_PLAIN_UTF8);
107 
108         ServletResponseUtil.write(response, value.getBytes(StringPool.UTF8));
109     }
110 
111     private static Log _log = LogFactoryUtil.getLog(LanguageServlet.class);
112 
113 }