1   /**
2    * Copyright (c) 2000-2010 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   *
12   * 
13   */
14  
15  package com.liferay.portal.servlet.filters.language;
16  
17  import com.liferay.portal.kernel.language.LanguageUtil;
18  import com.liferay.portal.kernel.language.UnicodeLanguageUtil;
19  import com.liferay.portal.kernel.util.LocaleUtil;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.servlet.filters.BasePortalFilter;
22  import com.liferay.util.servlet.filters.CacheResponse;
23  import com.liferay.util.servlet.filters.CacheResponseData;
24  import com.liferay.util.servlet.filters.CacheResponseUtil;
25  
26  import java.util.Locale;
27  import java.util.regex.Matcher;
28  import java.util.regex.Pattern;
29  
30  import javax.servlet.FilterChain;
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  
34  /**
35   * <a href="LanguageFilter.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Eduardo Lundgren
38   */
39  public class LanguageFilter extends BasePortalFilter {
40  
41      protected void processFilter(
42              HttpServletRequest request, HttpServletResponse response,
43              FilterChain filterChain)
44          throws Exception {
45  
46          CacheResponse cacheResponse = new CacheResponse(
47              response, StringPool.UTF8);
48  
49          processFilter(
50              LanguageFilter.class, request, cacheResponse, filterChain);
51  
52          byte[] bytes = translateResponse(
53              request, cacheResponse, cacheResponse.unsafeGetData(),
54              cacheResponse.getContentLength());
55  
56          CacheResponseData cacheResponseData = new CacheResponseData(
57              bytes, bytes.length, cacheResponse.getContentType(),
58              cacheResponse.getHeaders());
59  
60          CacheResponseUtil.write(response, cacheResponseData);
61      }
62  
63      protected byte[] translateResponse(
64          HttpServletRequest request, HttpServletResponse response,
65          byte[] bytes, int length) {
66  
67          String languageId = LanguageUtil.getLanguageId(request);
68          Locale locale = LocaleUtil.fromLanguageId(languageId);
69  
70          String content = new String(bytes, 0 ,length);
71  
72          Matcher matcher = _pattern.matcher(content);
73  
74          while (matcher.find()) {
75              String match = matcher.group(0);
76              String key = matcher.group(1);
77  
78              StringBuffer sb = new StringBuffer();
79  
80              sb.append(StringPool.APOSTROPHE);
81              sb.append(UnicodeLanguageUtil.get(locale, key));
82              sb.append(StringPool.APOSTROPHE);
83  
84              content = content.replace(match, sb.toString());
85          }
86  
87          return content.getBytes();
88      }
89  
90      private static Pattern _pattern = Pattern.compile(
91          "Liferay\\.Language\\.get\\([\"']([^)]+)[\"']\\)");
92  
93  }