001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    import com.liferay.portal.util.PropsValues;
032    
033    import java.io.IOException;
034    
035    import java.util.Locale;
036    
037    import javax.servlet.http.HttpServlet;
038    import javax.servlet.http.HttpServletRequest;
039    import javax.servlet.http.HttpServletResponse;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     */
044    public class LanguageServlet extends HttpServlet {
045    
046            @Override
047            public void service(
048                            HttpServletRequest request, HttpServletResponse response)
049                    throws IOException {
050    
051                    String path = request.getPathInfo();
052    
053                    if (_log.isDebugEnabled()) {
054                            _log.debug("Path " + path);
055                    }
056    
057                    try {
058                            if (PropsValues.AUTH_TOKEN_CHECK_ENABLED) {
059                                    AuthTokenUtil.check(request);
060                            }
061                    }
062                    catch (PortalException pe) {
063                            _log.error("Invalid authentication token received");
064    
065                            return;
066                    }
067    
068                    if (Validator.isNotNull(path) && path.startsWith(StringPool.SLASH)) {
069                            path = path.substring(1);
070                    }
071    
072                    String[] pathArray = StringUtil.split(path, CharPool.SLASH);
073    
074                    if (pathArray.length == 0) {
075                            _log.error("Language id is not specified");
076    
077                            return;
078                    }
079    
080                    if (pathArray.length == 1) {
081                            _log.error("Language key is not specified");
082    
083                            return;
084                    }
085    
086                    Locale locale = LocaleUtil.fromLanguageId(pathArray[0]);
087                    String key = pathArray[1];
088    
089                    Object[] arguments = null;
090    
091                    if (pathArray.length > 2) {
092                            arguments = new Object[pathArray.length - 2];
093    
094                            System.arraycopy(pathArray, 2, arguments, 0, arguments.length);
095                    }
096    
097                    String value = key;
098    
099                    try {
100                            if (ArrayUtil.isEmpty(arguments)) {
101                                    value = LanguageUtil.get(locale, key);
102                            }
103                            else {
104                                    value = LanguageUtil.format(locale, key, arguments);
105                            }
106                    }
107                    catch (Exception e) {
108                            if (_log.isWarnEnabled()) {
109                                    _log.warn(e, e);
110                            }
111                    }
112    
113                    if (!LanguageUtil.isValidLanguageKey(locale, key)) {
114                            response.setDateHeader(HttpHeaders.EXPIRES, 0);
115                            response.setHeader(
116                                    HttpHeaders.CACHE_CONTROL,
117                                    HttpHeaders.CACHE_CONTROL_NO_CACHE_VALUE);
118                            response.setHeader(
119                                    HttpHeaders.PRAGMA, HttpHeaders.PRAGMA_NO_CACHE_VALUE);
120                    }
121    
122                    response.setContentType(ContentTypes.TEXT_PLAIN_UTF8);
123                    response.setHeader(
124                            HttpHeaders.CONTENT_DISPOSITION, _CONTENT_DISPOSITION);
125    
126                    ServletResponseUtil.write(response, value.getBytes(StringPool.UTF8));
127            }
128    
129            private static final String _CONTENT_DISPOSITION =
130                    "attachment; filename=language.txt";
131    
132            private static Log _log = LogFactoryUtil.getLog(LanguageServlet.class);
133    
134    }