001
014
015 package com.liferay.portal.servlet;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.language.LanguageUtil;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.servlet.HttpHeaders;
022 import com.liferay.portal.kernel.servlet.ServletResponseUtil;
023 import com.liferay.portal.kernel.util.ArrayUtil;
024 import com.liferay.portal.kernel.util.CharPool;
025 import com.liferay.portal.kernel.util.ContentTypes;
026 import com.liferay.portal.kernel.util.LocaleUtil;
027 import com.liferay.portal.kernel.util.StringPool;
028 import com.liferay.portal.kernel.util.StringUtil;
029 import com.liferay.portal.kernel.util.Validator;
030 import com.liferay.portal.security.auth.AuthTokenUtil;
031
032 import java.io.IOException;
033
034 import java.util.Locale;
035
036 import javax.servlet.http.HttpServlet;
037 import javax.servlet.http.HttpServletRequest;
038 import javax.servlet.http.HttpServletResponse;
039
040
043 public class LanguageServlet extends HttpServlet {
044
045 @Override
046 public void service(
047 HttpServletRequest request, HttpServletResponse response)
048 throws IOException {
049
050 String path = request.getPathInfo();
051
052 if (_log.isDebugEnabled()) {
053 _log.debug("Path " + path);
054 }
055
056 try {
057 AuthTokenUtil.checkCSRFToken(
058 request, LanguageServlet.class.getName());
059 }
060 catch (PortalException pe) {
061 _log.error("Invalid authentication token received");
062
063 return;
064 }
065
066 if (Validator.isNotNull(path) && path.startsWith(StringPool.SLASH)) {
067 path = path.substring(1);
068 }
069
070 String[] pathArray = StringUtil.split(path, CharPool.SLASH);
071
072 if (pathArray.length == 0) {
073 _log.error("Language id is not specified");
074
075 return;
076 }
077
078 if (pathArray.length == 1) {
079 _log.error("Language key is not specified");
080
081 return;
082 }
083
084 Locale locale = LocaleUtil.fromLanguageId(pathArray[0]);
085 String key = pathArray[1];
086
087 Object[] arguments = null;
088
089 if (pathArray.length > 2) {
090 arguments = new Object[pathArray.length - 2];
091
092 System.arraycopy(pathArray, 2, arguments, 0, arguments.length);
093 }
094
095 String value = key;
096
097 try {
098 if (ArrayUtil.isEmpty(arguments)) {
099 value = LanguageUtil.get(locale, key);
100 }
101 else {
102 value = LanguageUtil.format(locale, key, arguments);
103 }
104 }
105 catch (Exception e) {
106 if (_log.isWarnEnabled()) {
107 _log.warn(e, e);
108 }
109 }
110
111 if (!LanguageUtil.isValidLanguageKey(locale, key)) {
112 response.setDateHeader(HttpHeaders.EXPIRES, 0);
113 response.setHeader(
114 HttpHeaders.CACHE_CONTROL,
115 HttpHeaders.CACHE_CONTROL_NO_CACHE_VALUE);
116 response.setHeader(
117 HttpHeaders.PRAGMA, HttpHeaders.PRAGMA_NO_CACHE_VALUE);
118 }
119
120 response.setContentType(ContentTypes.TEXT_PLAIN_UTF8);
121 response.setHeader(
122 HttpHeaders.CONTENT_DISPOSITION, _CONTENT_DISPOSITION);
123
124 ServletResponseUtil.write(response, value.getBytes(StringPool.UTF8));
125 }
126
127 private static final String _CONTENT_DISPOSITION =
128 "attachment; filename=language.txt";
129
130 private static Log _log = LogFactoryUtil.getLog(LanguageServlet.class);
131
132 }