001    /**
002     * Copyright (c) 2000-2012 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.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    }