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.staticexport;
029
030import org.opencms.file.CmsObject;
031import org.opencms.i18n.CmsLocaleManager;
032import org.opencms.i18n.CmsSingleTreeLocaleHandler;
033import org.opencms.main.OpenCms;
034import org.opencms.site.CmsSite;
035import org.opencms.util.CmsPair;
036import org.opencms.util.CmsStringUtil;
037import org.opencms.workplace.CmsWorkplace;
038
039import java.util.Locale;
040import java.util.regex.Matcher;
041import java.util.regex.Pattern;
042
043/**
044 * Link substitution handler required to render single tree localized sites.<p>
045 */
046public class CmsLocalePrefixLinkSubstitutionHandler extends CmsDefaultLinkSubstitutionHandler {
047
048    /**
049     * @see org.opencms.staticexport.CmsDefaultLinkSubstitutionHandler#addVfsPrefix(org.opencms.file.CmsObject, java.lang.String, org.opencms.site.CmsSite, java.lang.String)
050     */
051    @Override
052    protected CmsPair<String, String> addVfsPrefix(
053        CmsObject cms,
054        String vfsName,
055        CmsSite targetSite,
056        String parameters) {
057
058        if (CmsSite.LocalizationMode.singleTree.equals(targetSite.getLocalizationMode())) {
059            // check if locale is specified via parameters
060            Locale localeFromParameter = null;
061            if (null != parameters) {
062                Pattern pattern = Pattern.compile("(.*)" + CmsLocaleManager.PARAMETER_LOCALE + "=([^&]*)(.*)");
063                Matcher matcher = pattern.matcher(parameters);
064                if (matcher.find()) {
065                    String localeFromParameterString = matcher.group(2);
066                    if ((localeFromParameterString != null) && !localeFromParameterString.isEmpty()) {
067                        Locale l = CmsLocaleManager.getLocale(localeFromParameterString);
068                        if (OpenCms.getLocaleManager().getAvailableLocales(cms, vfsName).contains(l)) {
069                            localeFromParameter = l;
070                            if (matcher.group(3).isEmpty()) {
071                                parameters = matcher.group(1).substring(0, matcher.group(1).length() - 1);
072                                if (parameters.isEmpty()) {
073                                    parameters = null;
074                                }
075                            } else {
076                                parameters = matcher.group(1) + matcher.group(3).substring(1);
077                            }
078                        }
079                    }
080                }
081            }
082            // inject the current locale as a virtual path element
083            return new CmsPair<String, String>(
084                CmsStringUtil.joinPaths(
085                    OpenCms.getStaticExportManager().getVfsPrefix(),
086                    null != localeFromParameter
087                    ? localeFromParameter.toString()
088                    : cms.getRequestContext().getLocale().toString(),
089                    vfsName),
090                parameters);
091        } else {
092            return super.addVfsPrefix(cms, vfsName, targetSite, parameters);
093        }
094    }
095
096    /**
097     * @see org.opencms.staticexport.CmsDefaultLinkSubstitutionHandler#generateCacheKey(org.opencms.file.CmsObject, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
098     */
099    @Override
100    protected String generateCacheKey(
101        CmsObject cms,
102        String sourceSiteRoot,
103        String targetSiteRoot,
104        String detailPagePart,
105        String absoluteLink) {
106
107        return ""
108            + cms.getRequestContext().getCurrentUser().getId()
109            + ":"
110            + cms.getRequestContext().getSiteRoot()
111            + ":"
112            + sourceSiteRoot
113            + ":"
114            + targetSiteRoot
115            + ":"
116            + detailPagePart
117            + absoluteLink
118            + ":"
119            + cms.getRequestContext().getLocale().toString();
120    }
121
122    /**
123     * @see org.opencms.staticexport.CmsDefaultLinkSubstitutionHandler#getRootPathForSite(org.opencms.file.CmsObject, java.lang.String, java.lang.String, boolean)
124     */
125    @Override
126    protected String getRootPathForSite(CmsObject cms, String path, String siteRoot, boolean isRootPath) {
127
128        CmsSite site = OpenCms.getSiteManager().getSiteForSiteRoot(siteRoot);
129        if ((site != null) && CmsSite.LocalizationMode.singleTree.equals(site.getLocalizationMode())) {
130            if (isRootPath) {
131                path = path.substring(site.getSiteRoot().length());
132            }
133            Locale locale = CmsSingleTreeLocaleHandler.getLocaleFromPath(path);
134            if (locale != null) {
135                path = path.substring(locale.toString().length() + 1);
136            }
137            return cms.getRequestContext().addSiteRoot(site.getSiteRoot(), path);
138        } else {
139            return super.getRootPathForSite(cms, path, siteRoot, isRootPath);
140        }
141    }
142
143    /**
144     * @see org.opencms.staticexport.CmsDefaultLinkSubstitutionHandler#prepareExportParameters(org.opencms.file.CmsObject, java.lang.String, java.lang.String)
145     */
146    @Override
147    protected String prepareExportParameters(CmsObject cms, String vfsName, String parameters) {
148
149        CmsSite site = OpenCms.getSiteManager().getSiteForSiteRoot(cms.getRequestContext().getSiteRoot());
150        if ((site != null) && CmsSite.LocalizationMode.singleTree.equals(site.getLocalizationMode())) {
151            if (!(OpenCms.getSiteManager().startsWithShared(vfsName)
152                || vfsName.startsWith(CmsWorkplace.VFS_PATH_SYSTEM))) {
153                if (parameters != null) {
154                    parameters += "&";
155                } else {
156                    parameters = "?";
157                }
158                parameters += CmsLocaleManager.PARAMETER_LOCALE + "=" + cms.getRequestContext().getLocale().toString();
159            }
160        }
161        return parameters;
162    }
163}