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)
098     */
099    @Override
100    protected String generateCacheKey(
101        CmsObject cms,
102        String targetSiteRoot,
103        String detailPagePart,
104        String absoluteLink) {
105
106        return cms.getRequestContext().getSiteRoot()
107            + ":"
108            + targetSiteRoot
109            + ":"
110            + detailPagePart
111            + absoluteLink
112            + ":"
113            + cms.getRequestContext().getLocale().toString();
114    }
115
116    /**
117     * @see org.opencms.staticexport.CmsDefaultLinkSubstitutionHandler#getRootPathForSite(org.opencms.file.CmsObject, java.lang.String, java.lang.String, boolean)
118     */
119    @Override
120    protected String getRootPathForSite(CmsObject cms, String path, String siteRoot, boolean isRootPath) {
121
122        CmsSite site = OpenCms.getSiteManager().getSiteForSiteRoot(siteRoot);
123        if ((site != null) && CmsSite.LocalizationMode.singleTree.equals(site.getLocalizationMode())) {
124            if (isRootPath) {
125                path = path.substring(site.getSiteRoot().length());
126            }
127            Locale locale = CmsSingleTreeLocaleHandler.getLocaleFromPath(path);
128            if (locale != null) {
129                path = path.substring(locale.toString().length() + 1);
130            }
131            return cms.getRequestContext().addSiteRoot(site.getSiteRoot(), path);
132        } else {
133            return super.getRootPathForSite(cms, path, siteRoot, isRootPath);
134        }
135    }
136
137    /**
138     * @see org.opencms.staticexport.CmsDefaultLinkSubstitutionHandler#prepareExportParameters(org.opencms.file.CmsObject, java.lang.String, java.lang.String)
139     */
140    @Override
141    protected String prepareExportParameters(CmsObject cms, String vfsName, String parameters) {
142
143        CmsSite site = OpenCms.getSiteManager().getSiteForSiteRoot(cms.getRequestContext().getSiteRoot());
144        if ((site != null) && CmsSite.LocalizationMode.singleTree.equals(site.getLocalizationMode())) {
145            if (!(OpenCms.getSiteManager().startsWithShared(vfsName)
146                || vfsName.startsWith(CmsWorkplace.VFS_PATH_SYSTEM))) {
147                if (parameters != null) {
148                    parameters += "&";
149                } else {
150                    parameters = "?";
151                }
152                parameters += CmsLocaleManager.PARAMETER_LOCALE + "=" + cms.getRequestContext().getLocale().toString();
153            }
154        }
155        return parameters;
156    }
157}