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.CharPool;
024 import com.liferay.portal.kernel.util.ContentTypes;
025 import com.liferay.portal.kernel.util.LocaleUtil;
026 import com.liferay.portal.kernel.util.StringPool;
027 import com.liferay.portal.kernel.util.StringUtil;
028 import com.liferay.portal.kernel.util.Validator;
029 import com.liferay.portal.security.auth.AuthTokenUtil;
030 import com.liferay.portal.util.PropsValues;
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 if (PropsValues.AUTH_TOKEN_CHECK_ENABLED) {
058 AuthTokenUtil.check(request);
059 }
060 }
061 catch (PortalException pe) {
062 _log.error("Invalid authentication token received");
063
064 return;
065 }
066
067 if (Validator.isNotNull(path) && path.startsWith(StringPool.SLASH)) {
068 path = path.substring(1);
069 }
070
071 String[] pathArray = StringUtil.split(path, CharPool.SLASH);
072
073 if (pathArray.length == 0) {
074 _log.error("Language id is not specified");
075
076 return;
077 }
078
079 if (pathArray.length == 1) {
080 _log.error("Language key is not specified");
081
082 return;
083 }
084
085 Locale locale = LocaleUtil.fromLanguageId(pathArray[0]);
086 String key = pathArray[1];
087
088 Object[] arguments = null;
089
090 if (pathArray.length > 2) {
091 arguments = new Object[pathArray.length - 2];
092
093 System.arraycopy(pathArray, 2, arguments, 0, arguments.length);
094 }
095
096 String value = key;
097
098 try {
099 if ((arguments == null) || (arguments.length == 0)) {
100 value = LanguageUtil.get(locale, key);
101 }
102 else {
103 value = LanguageUtil.format(locale, key, arguments);
104 }
105 }
106 catch (Exception e) {
107 if (_log.isWarnEnabled()) {
108 _log.warn(e, e);
109 }
110 }
111
112 if (!LanguageUtil.isValidLanguageKey(locale, key)) {
113 response.setDateHeader(HttpHeaders.EXPIRES, 0);
114 response.setHeader(
115 HttpHeaders.CACHE_CONTROL,
116 HttpHeaders.CACHE_CONTROL_NO_CACHE_VALUE);
117 response.setHeader(
118 HttpHeaders.PRAGMA, HttpHeaders.PRAGMA_NO_CACHE_VALUE);
119 }
120
121 response.setContentType(ContentTypes.TEXT_PLAIN_UTF8);
122 response.setHeader(
123 HttpHeaders.CONTENT_DISPOSITION, _CONTENT_DISPOSITION);
124
125 ServletResponseUtil.write(response, value.getBytes(StringPool.UTF8));
126 }
127
128 private static final String _CONTENT_DISPOSITION =
129 "attachment; filename=language.txt";
130
131 private static Log _log = LogFactoryUtil.getLog(LanguageServlet.class);
132
133 }