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.NoSuchLayoutException;
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.util.CharPool;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.LocaleUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.kernel.xml.Element;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portal.util.WebKeys;
029    
030    import java.io.IOException;
031    
032    import java.util.Collections;
033    import java.util.HashSet;
034    import java.util.Iterator;
035    import java.util.Locale;
036    import java.util.Set;
037    
038    import javax.servlet.RequestDispatcher;
039    import javax.servlet.ServletContext;
040    import javax.servlet.ServletException;
041    import javax.servlet.http.HttpServlet;
042    import javax.servlet.http.HttpServletRequest;
043    import javax.servlet.http.HttpServletResponse;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     */
048    public class I18nServlet extends HttpServlet {
049    
050            public static Set<String> getLanguageIds() {
051                    return _languageIds;
052            }
053    
054            public static void setLanguageIds(Element root) {
055                    Iterator<Element> itr = root.elements("servlet-mapping").iterator();
056    
057                    while (itr.hasNext()) {
058                            Element el = itr.next();
059    
060                            String servletName = el.elementText("servlet-name");
061    
062                            if (servletName.equals("I18n Servlet")) {
063                                    String urlPattern = el.elementText("url-pattern");
064    
065                                    String languageId = urlPattern.substring(
066                                            0, urlPattern.lastIndexOf(CharPool.SLASH));
067    
068                                    _languageIds.add(languageId);
069                            }
070                    }
071    
072                    _languageIds = Collections.unmodifiableSet(_languageIds);
073            }
074    
075            public void service(
076                            HttpServletRequest request, HttpServletResponse response)
077                    throws IOException, ServletException {
078    
079                    try {
080                            String[] i18nData = getI18nData(request);
081    
082                            if ((i18nData == null) ||
083                                    !PortalUtil.isValidResourceId(i18nData[2])) {
084    
085                                    PortalUtil.sendError(
086                                            HttpServletResponse.SC_NOT_FOUND,
087                                            new NoSuchLayoutException(), request, response);
088                            }
089                            else {
090                                    String i18nLanguageId = i18nData[0];
091                                    String i18nPath = i18nData[1];
092                                    String redirect = i18nData[2];
093    
094                                    request.setAttribute(WebKeys.I18N_LANGUAGE_ID, i18nLanguageId);
095                                    request.setAttribute(WebKeys.I18N_PATH, i18nPath);
096    
097                                    ServletContext servletContext = getServletContext();
098    
099                                    RequestDispatcher requestDispatcher =
100                                            servletContext.getRequestDispatcher(redirect);
101    
102                                    requestDispatcher.forward(request, response);
103                            }
104                    }
105                    catch (Exception e) {
106                            _log.error(e, e);
107    
108                            PortalUtil.sendError(
109                                    HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
110                                    response);
111                    }
112            }
113    
114            protected String[] getI18nData(HttpServletRequest request) {
115                    String path = GetterUtil.getString(request.getPathInfo());
116    
117                    if (Validator.isNull(path)) {
118                            return null;
119                    }
120    
121                    String i18nLanguageId = request.getServletPath();
122    
123                    int pos = i18nLanguageId.lastIndexOf(CharPool.SLASH);
124    
125                    i18nLanguageId = i18nLanguageId.substring(pos + 1);
126    
127                    if (_log.isDebugEnabled()) {
128                            _log.debug("Language ID " + i18nLanguageId);
129                    }
130    
131                    if (Validator.isNull(i18nLanguageId)) {
132                            return null;
133                    }
134    
135                    String i18nPath = StringPool.SLASH + i18nLanguageId;
136    
137                    Locale locale = LocaleUtil.fromLanguageId(i18nLanguageId);
138    
139                    if (Validator.isNull(locale.getCountry())) {
140    
141                            // Locales must contain the country code
142    
143                            locale = LanguageUtil.getLocale(locale.getLanguage());
144    
145                            i18nLanguageId = LocaleUtil.toLanguageId(locale);
146                    }
147    
148                    String redirect = path;
149    
150                    if (_log.isDebugEnabled()) {
151                            _log.debug("Redirect " + redirect);
152                    }
153    
154                    return new String[] {i18nLanguageId, i18nPath, redirect};
155            }
156    
157            private static Log _log = LogFactoryUtil.getLog(I18nServlet.class);
158    
159            private static Set<String> _languageIds = new HashSet<String>();
160    
161    }