001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.gwt;
029
030import org.opencms.i18n.CmsLocaleManager;
031import org.opencms.main.OpenCms;
032import org.opencms.util.CmsStringUtil;
033
034import java.io.IOException;
035import java.util.Locale;
036
037import javax.servlet.ServletRequest;
038import javax.servlet.ServletResponse;
039
040/**
041 * Exports the register client messages into a single JavaScript resource.<p>
042 */
043public class CmsMessagesService extends CmsGwtService {
044
045    /** The serial version id. */
046    private static final long serialVersionUID = 3072608993796119377L;
047
048    /** The client messages to export. */
049    private static final I_CmsClientMessageBundle[] CLIENT_MESSGAE_BUNDLES = new I_CmsClientMessageBundle[] {
050        ClientMessages.get(),
051        org.opencms.ade.containerpage.ClientMessages.get(),
052        org.opencms.ade.contenteditor.ClientMessages.get(),
053        org.opencms.ade.galleries.ClientMessages.get(),
054        org.opencms.ade.postupload.ClientMessages.get(),
055        org.opencms.ade.publish.ClientMessages.get(),
056        org.opencms.ade.sitemap.ClientMessages.get(),
057        org.opencms.ade.upload.ClientMessages.get(),
058        org.opencms.gwt.seo.ClientMessages.get()};
059
060    /**
061     * @see org.opencms.gwt.CmsGwtService#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
062     */
063    @Override
064    public void service(ServletRequest request, ServletResponse response) throws IOException {
065
066        try {
067            // Set response's character encoding to the default(*) to avoid ambiguous
068            // interpretations of the Servlet spec from different servlet containers.
069            // This complies with the Servlet spec.
070            // See for example the "Java Servlet Specification Version 3.0":
071            // "Servlets should set the locale and the character encoding of a response.
072            // [...]
073            // If the servlet does not specify a character encoding before the getWriter
074            // method of the ServletResponse interface is called or the response is committed,
075            // the default ISO-8859-1 is used."
076            // (*): the OpenCms configured encoding (defaulting to UTF-8) is favoured over
077            // ISO-8859-1 to allow for a wider charset support.
078            String characterEncoding = OpenCms.getSystemInfo().getDefaultEncoding();
079            response.setCharacterEncoding(characterEncoding);
080            response.setContentType("text/javascript");
081            Locale locale;
082            String localeString = request.getParameter(CmsLocaleManager.PARAMETER_LOCALE);
083            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(localeString)) {
084                locale = CmsLocaleManager.getLocale(localeString);
085            } else {
086                locale = OpenCms.getWorkplaceManager().getWorkplaceLocale(getCmsObject());
087            }
088            for (int i = 0; i < CLIENT_MESSGAE_BUNDLES.length; i++) {
089                response.getWriter().append(CLIENT_MESSGAE_BUNDLES[i].export(locale, false)).append("\n");
090            }
091            response.getWriter().flush();
092        } finally {
093            clearThreadStorage();
094        }
095    }
096}