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 GmbH & Co. KG, 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.i18n;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsProject;
032import org.opencms.file.CmsPropertyDefinition;
033import org.opencms.file.CmsResource;
034import org.opencms.file.CmsResourceFilter;
035import org.opencms.file.CmsUser;
036import org.opencms.main.CmsException;
037import org.opencms.main.CmsLog;
038import org.opencms.main.OpenCms;
039
040import java.io.UnsupportedEncodingException;
041import java.util.List;
042import java.util.Locale;
043
044import javax.servlet.http.HttpServletRequest;
045
046import org.apache.commons.logging.Log;
047
048/**
049 * Default implementation of the locale handler.<p>
050 *
051 * @since 6.0.0
052 */
053public class CmsDefaultLocaleHandler implements I_CmsLocaleHandler {
054
055    /** The log object for this class. */
056    private static final Log LOG = CmsLog.getLog(CmsDefaultLocaleHandler.class);
057
058    /** A cms object that has been initialized with Admin permissions. */
059    private CmsObject m_adminCmsObject;
060
061    /**
062     * Constructor, no action is required.<p>
063     */
064    public CmsDefaultLocaleHandler() {
065
066        // noop
067    }
068
069    /**
070     * @see org.opencms.i18n.I_CmsLocaleHandler#getI18nInfo(javax.servlet.http.HttpServletRequest, org.opencms.file.CmsUser, org.opencms.file.CmsProject, java.lang.String)
071     */
072    public CmsI18nInfo getI18nInfo(HttpServletRequest req, CmsUser user, CmsProject project, String resourceName) {
073
074        CmsLocaleManager localeManager = OpenCms.getLocaleManager();
075        List<Locale> defaultLocales = null;
076        String encoding = null;
077
078        CmsObject adminCms = null;
079        try {
080            // create a copy of the Admin context to avoid concurrent modification
081            adminCms = OpenCms.initCmsObject(m_adminCmsObject);
082        } catch (CmsException e) {
083            // unable to copy Admin context - this should never happen
084        }
085
086        if (adminCms != null) {
087
088            // must switch project id in stored Admin context to match current project
089            adminCms.getRequestContext().setCurrentProject(project);
090            adminCms.getRequestContext().setUri(resourceName);
091
092            // now get default m_locale names
093            CmsResource res = null;
094            try {
095                res = adminCms.readResource(resourceName, CmsResourceFilter.IGNORE_EXPIRATION);
096            } catch (CmsException e) {
097                // unable to read the resource - maybe we need the init handlers
098            }
099            if (res == null) {
100                try {
101                    res = OpenCms.initResource(adminCms, resourceName, req, null);
102                } catch (CmsException e) {
103                    // unable to resolve the resource, use default locale
104                }
105            }
106
107            String defaultNames = null;
108
109            if (res != null) {
110                // the resource may not exist at all (e.g. if an unknown resource was requested by the user in the browser)
111                try {
112                    defaultNames = adminCms.readPropertyObject(
113                        res,
114                        CmsPropertyDefinition.PROPERTY_LOCALE,
115                        true).getValue();
116                } catch (CmsException e) {
117                    LOG.warn(Messages.get().getBundle().key(Messages.ERR_READ_ENCODING_PROP_1, resourceName), e);
118                }
119                if (defaultNames != null) {
120                    defaultLocales = localeManager.getAvailableLocales(defaultNames);
121                }
122
123                // get the encoding
124                try {
125                    encoding = adminCms.readPropertyObject(
126                        res,
127                        CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING,
128                        true).getValue(OpenCms.getSystemInfo().getDefaultEncoding());
129                } catch (CmsException e) {
130                    if (LOG.isInfoEnabled()) {
131                        LOG.info(Messages.get().getBundle().key(Messages.ERR_READ_ENCODING_PROP_1, resourceName), e);
132                    }
133                }
134            }
135        }
136
137        if ((defaultLocales == null) || (defaultLocales.isEmpty())) {
138            // no default locales could be determined
139            defaultLocales = localeManager.getDefaultLocales();
140        }
141        if (encoding == null) {
142            // no special encoding could be determined
143            encoding = OpenCms.getSystemInfo().getDefaultEncoding();
144        }
145
146        // set the request character encoding
147        if (req != null) {
148            try {
149                req.setCharacterEncoding(encoding);
150            } catch (UnsupportedEncodingException e) {
151                LOG.error(Messages.get().getBundle().key(Messages.ERR_UNSUPPORTED_REQUEST_ENCODING_1, encoding), e);
152            }
153        }
154
155        Locale locale;
156        // return the first default locale name
157        if ((defaultLocales != null) && (defaultLocales.size() > 0)) {
158            locale = defaultLocales.get(0);
159        } else {
160            locale = CmsLocaleManager.getDefaultLocale();
161        }
162
163        return new CmsI18nInfo(locale, encoding);
164    }
165
166    /**
167     * @see org.opencms.i18n.I_CmsLocaleHandler#initHandler(org.opencms.file.CmsObject)
168     */
169    public void initHandler(CmsObject cms) {
170
171        m_adminCmsObject = cms;
172    }
173}