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.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;
039import org.opencms.site.CmsSite;
040import org.opencms.util.CmsStringUtil;
041
042import java.util.Locale;
043
044import javax.servlet.http.HttpServletRequest;
045
046import org.apache.commons.logging.Log;
047
048/**
049 * Advanced locale handler allowing single tree localization.<p>
050 */
051public class CmsSingleTreeLocaleHandler extends CmsDefaultLocaleHandler {
052
053    /** The log object for this class. */
054    private static final Log LOG = CmsLog.getLog(CmsSingleTreeLocaleHandler.class);
055
056    /** A cms object that has been initialized with Admin permissions. */
057    private CmsObject m_adminCmsObject;
058
059    /**
060     * Reads the locale from the first path element.<p>
061     *
062     * @param sitePath the site path with the locale prefix
063     *
064     * @return the locale or <code>null</code> if no matching locale was found
065     */
066    public static Locale getLocaleFromPath(String sitePath) {
067
068        Locale result = null;
069        if (sitePath.indexOf("/") == 0) {
070            sitePath = sitePath.substring(1);
071        }
072        if (sitePath.length() > 1) {
073            String localePrefix;
074            if (!sitePath.contains("/")) {
075                // this may be the case for paths pointing to the root folder of a site
076                // check for parameters
077                int separator = -1;
078                int param = sitePath.indexOf("?");
079                int hash = sitePath.indexOf("#");
080                if (param >= 0) {
081                    if (hash != 0) {
082                        separator = param < hash ? param : hash;
083                    } else {
084                        separator = param;
085                    }
086                } else {
087                    separator = hash;
088                }
089                if (separator >= 0) {
090                    localePrefix = sitePath.substring(0, separator);
091                } else {
092                    localePrefix = sitePath;
093                }
094            } else {
095                localePrefix = sitePath.substring(0, sitePath.indexOf("/"));
096            }
097            Locale locale = CmsLocaleManager.getLocale(localePrefix);
098            if (localePrefix.equals(locale.toString())
099                && OpenCms.getLocaleManager().getAvailableLocales().contains(locale)) {
100                result = locale;
101            }
102        }
103        return result;
104    }
105
106    /**
107     * @see org.opencms.i18n.CmsDefaultLocaleHandler#getI18nInfo(javax.servlet.http.HttpServletRequest, org.opencms.file.CmsUser, org.opencms.file.CmsProject, java.lang.String)
108     */
109    @Override
110    public CmsI18nInfo getI18nInfo(HttpServletRequest req, CmsUser user, CmsProject project, String resourceName) {
111
112        CmsSite site = OpenCms.getSiteManager().getSiteForRootPath(resourceName);
113        if ((site != null) && CmsSite.LocalizationMode.singleTree.equals(site.getLocalizationMode())) {
114
115            String sitePath = resourceName.substring(site.getSiteRoot().length());
116            if (sitePath.startsWith("/")) {
117                sitePath = sitePath.substring(1);
118            }
119            Locale locale = getLocaleFromPath(sitePath);
120
121            if (locale == null) {
122                return super.getI18nInfo(req, user, project, resourceName);
123            }
124
125            sitePath = sitePath.substring(locale.toString().length());
126            String encoding = null;
127            resourceName = CmsStringUtil.joinPaths(site.getSiteRoot(), sitePath);
128            CmsObject adminCms = null;
129            try {
130                // create a copy of the Admin context to avoid concurrent modification
131                adminCms = OpenCms.initCmsObject(m_adminCmsObject);
132            } catch (CmsException e) {
133                // unable to copy Admin context - this should never happen
134            }
135
136            if (adminCms != null) {
137
138                // must switch project id in stored Admin context to match current project
139                adminCms.getRequestContext().setCurrentProject(project);
140                adminCms.getRequestContext().setUri(resourceName);
141
142                // now get default m_locale names
143                CmsResource res = null;
144                try {
145                    res = adminCms.readResource(resourceName, CmsResourceFilter.IGNORE_EXPIRATION);
146                } catch (CmsException e) {
147                    // unable to read the resource - maybe we need the init handlers
148                }
149                if (res == null) {
150                    try {
151                        res = OpenCms.initResource(adminCms, resourceName, req, null);
152                    } catch (CmsException e) {
153                        // ignore
154                    }
155                }
156
157                if (res != null) {
158                    // get the encoding
159                    try {
160                        encoding = adminCms.readPropertyObject(
161                            res,
162                            CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING,
163                            true).getValue(OpenCms.getSystemInfo().getDefaultEncoding());
164                    } catch (CmsException e) {
165                        if (LOG.isInfoEnabled()) {
166                            LOG.info(
167                                Messages.get().getBundle().key(Messages.ERR_READ_ENCODING_PROP_1, resourceName),
168                                e);
169                        }
170                    }
171                }
172                if (encoding == null) {
173                    // no special encoding could be determined
174                    encoding = OpenCms.getSystemInfo().getDefaultEncoding();
175                }
176                return new CmsI18nInfo(locale, encoding);
177            }
178        }
179        return super.getI18nInfo(req, user, project, resourceName);
180    }
181
182    /**
183     * @see org.opencms.i18n.I_CmsLocaleHandler#initHandler(org.opencms.file.CmsObject)
184     */
185    @Override
186    public void initHandler(CmsObject cms) {
187
188        m_adminCmsObject = cms;
189        super.initHandler(cms);
190    }
191}