1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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  }