001    /**
002     * Copyright (c) 2000-2011 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.servlet.ServletResponseUtil;
022    import com.liferay.portal.kernel.util.CharPool;
023    import com.liferay.portal.kernel.util.ContentTypes;
024    import com.liferay.portal.kernel.util.LocaleUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.kernel.util.Validator;
028    
029    import java.io.IOException;
030    
031    import java.util.Locale;
032    
033    import javax.servlet.http.HttpServlet;
034    import javax.servlet.http.HttpServletRequest;
035    import javax.servlet.http.HttpServletResponse;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class LanguageServlet extends HttpServlet {
041    
042            @Override
043            public void service(
044                            HttpServletRequest request, HttpServletResponse response)
045                    throws IOException {
046    
047                    String path = request.getPathInfo();
048    
049                    if (_log.isDebugEnabled()) {
050                            _log.debug("Path " + path);
051                    }
052    
053                    if (Validator.isNotNull(path) && path.startsWith(StringPool.SLASH)) {
054                            path = path.substring(1, path.length());
055                    }
056    
057                    String[] pathArray = StringUtil.split(path, CharPool.SLASH);
058    
059                    if (pathArray.length == 0) {
060                            _log.error("Language id is not specified");
061    
062                            return;
063                    }
064    
065                    if (pathArray.length == 1) {
066                            _log.error("Language key is not specified");
067    
068                            return;
069                    }
070    
071                    Locale locale = LocaleUtil.fromLanguageId(pathArray[0]);
072                    String key = pathArray[1];
073    
074                    Object[] arguments = null;
075    
076                    if (pathArray.length > 2) {
077                            arguments = new Object[pathArray.length - 2];
078    
079                            System.arraycopy(pathArray, 2, arguments, 0, arguments.length);
080                    }
081    
082                    String value = key;
083    
084                    try {
085                            if ((arguments == null) || (arguments.length == 0)) {
086                                    value = LanguageUtil.get(locale, key);
087                            }
088                            else {
089                                    value = LanguageUtil.format(locale, key, arguments);
090                            }
091                    }
092                    catch (Exception e) {
093                            if (_log.isWarnEnabled()) {
094                                    _log.warn(e, e);
095                            }
096                    }
097    
098                    response.setContentType(ContentTypes.TEXT_PLAIN_UTF8);
099                    response.setHeader(
100                            HttpHeaders.CONTENT_DISPOSITION, _CONTENT_DISPOSITION);
101    
102                    ServletResponseUtil.write(response, value.getBytes(StringPool.UTF8));
103            }
104    
105            private static final String _CONTENT_DISPOSITION =
106                    "attachment; filename=language.txt";
107    
108            private static Log _log = LogFactoryUtil.getLog(LanguageServlet.class);
109    
110    }