001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.servlet;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.servlet.HttpHeaders;
021    import com.liferay.portal.kernel.util.ContentTypes;
022    import com.liferay.portal.kernel.util.LocaleUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.util.servlet.ServletResponseUtil;
027    
028    import java.io.IOException;
029    
030    import java.util.Locale;
031    
032    import javax.servlet.http.HttpServlet;
033    import javax.servlet.http.HttpServletRequest;
034    import javax.servlet.http.HttpServletResponse;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     */
039    public class LanguageServlet extends HttpServlet {
040    
041            public void service(
042                            HttpServletRequest request, HttpServletResponse response)
043                    throws IOException {
044    
045                    String path = request.getPathInfo();
046    
047                    if (_log.isDebugEnabled()) {
048                            _log.debug("Path " + path);
049                    }
050    
051                    if (Validator.isNotNull(path) && path.startsWith(StringPool.SLASH)) {
052                            path = path.substring(1, path.length());
053                    }
054    
055                    String[] pathArray = StringUtil.split(path, StringPool.SLASH);
056    
057                    if (pathArray.length == 0) {
058                            _log.error("Language id is not specified");
059    
060                            return;
061                    }
062    
063                    if (pathArray.length == 1) {
064                            _log.error("Language key is not specified");
065    
066                            return;
067                    }
068    
069                    Locale locale = LocaleUtil.fromLanguageId(pathArray[0]);
070                    String key = pathArray[1];
071    
072                    Object[] arguments = null;
073    
074                    if (pathArray.length > 2) {
075                            arguments = new Object[pathArray.length - 2];
076    
077                            System.arraycopy(pathArray, 2, arguments, 0, arguments.length);
078                    }
079    
080                    String value = key;
081    
082                    try {
083                            if ((arguments == null) || (arguments.length == 0)) {
084                                    value = LanguageUtil.get(locale, key);
085                            }
086                            else {
087                                    value = LanguageUtil.format(locale, key, arguments);
088                            }
089                    }
090                    catch (Exception e) {
091                            if (_log.isWarnEnabled()) {
092                                    _log.warn(e, e);
093                            }
094                    }
095    
096                    response.setContentType(ContentTypes.TEXT_PLAIN_UTF8);
097                    response.setHeader(
098                            HttpHeaders.CONTENT_DISPOSITION, _CONTENT_DISPOSITION);
099    
100                    ServletResponseUtil.write(response, value.getBytes(StringPool.UTF8));
101            }
102    
103            private static final String _CONTENT_DISPOSITION =
104                    "attachment; filename=language.txt";
105    
106            private static Log _log = LogFactoryUtil.getLog(LanguageServlet.class);
107    
108    }