001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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.main;
029
030import org.opencms.ade.detailpage.CmsDetailPageResourceHandler;
031import org.opencms.db.CmsAlias;
032import org.opencms.db.CmsRewriteAliasMatcher;
033import org.opencms.file.CmsObject;
034import org.opencms.file.CmsResource;
035import org.opencms.gwt.shared.alias.CmsAliasMode;
036import org.opencms.i18n.CmsMessageContainer;
037import org.opencms.security.CmsPermissionViolationException;
038import org.opencms.security.CmsSecurityException;
039import org.opencms.util.CmsFileUtil;
040import org.opencms.workplace.CmsWorkplace;
041
042import java.io.IOException;
043import java.util.List;
044
045import javax.servlet.http.HttpServletRequest;
046import javax.servlet.http.HttpServletResponse;
047
048import org.apache.commons.logging.Log;
049
050/**
051 * Resource init handler for detail-pages.<p>
052 *
053 * @since 8.0.0
054 */
055public class CmsAliasResourceHandler implements I_CmsResourceInit {
056
057    /** The attribute containing the detail content resource. */
058    public static final String ATTR_DETAIL_CONTENT_RESOURCE = "__opencms_detail_content_resource";
059
060    /** The log object for this class. */
061    private static final Log LOG = CmsLog.getLog(CmsDetailPageResourceHandler.class);
062
063    /**
064     * Default constructor.<p>
065     */
066    public CmsAliasResourceHandler() {
067
068        // empty
069    }
070
071    /**
072     * @see org.opencms.main.I_CmsResourceInit#initResource(org.opencms.file.CmsResource, org.opencms.file.CmsObject, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
073     */
074    public CmsResource initResource(
075        CmsResource resource,
076        CmsObject cms,
077        HttpServletRequest req,
078        HttpServletResponse res)
079    throws CmsResourceInitException, CmsSecurityException {
080
081        // check if the resource was already found or the path starts with '/system/'
082        boolean abort = (resource != null) || cms.getRequestContext().getUri().startsWith(CmsWorkplace.VFS_PATH_SYSTEM);
083        if (abort) {
084            // skip in all cases above
085            return resource;
086        }
087
088        String path = cms.getRequestContext().getUri();
089        path = CmsFileUtil.removeTrailingSeparator(path);
090        String siteRoot = cms.getRequestContext().getSiteRoot();
091        if ("".equals(siteRoot)) {
092            siteRoot = OpenCms.getSiteManager().getSiteRoot(path);
093            if (siteRoot == null) {
094                siteRoot = "";
095            }
096        }
097        try {
098            String sitePath = path;
099            String oldSiteRoot = cms.getRequestContext().getSiteRoot();
100            try {
101                cms.getRequestContext().setSiteRoot(siteRoot);
102                sitePath = cms.getRequestContext().removeSiteRoot(path);
103            } finally {
104                cms.getRequestContext().setSiteRoot(oldSiteRoot);
105            }
106            CmsRewriteAliasMatcher rewriteAliases = OpenCms.getAliasManager().getRewriteAliasMatcher(cms, siteRoot);
107            CmsRewriteAliasMatcher.RewriteResult rewriteResult = rewriteAliases.match(sitePath);
108            if ((rewriteResult != null) && (res != null)) {
109                if (rewriteResult.getAlias().getMode().isRedirect()) {
110                    String link = OpenCms.getLinkManager().substituteLink(cms, rewriteResult.getNewPath());
111                    redirectToTarget(
112                        cms,
113                        req,
114                        res,
115                        link,
116                        rewriteResult.getAlias().getMode() == CmsAliasMode.permanentRedirect);
117                    return null;
118                } else {
119                    CmsResource rewriteTarget = cms.readResource(rewriteResult.getNewPath());
120                    String uri = cms.getRequestContext().getUri();
121                    String query = req.getQueryString();
122                    res.setHeader("X-Forwarded-URI", (query == null ? uri : uri + "?" + query));
123                    cms.getRequestContext().setUri(rewriteResult.getNewPath());
124                    return rewriteTarget;
125                }
126            }
127            List<CmsAlias> aliases = OpenCms.getAliasManager().getAliasesForPath(cms, siteRoot, sitePath);
128            assert aliases.size() < 2;
129            if (aliases.size() == 1) {
130                CmsAlias alias = aliases.get(0);
131                CmsResource aliasTarget = cms.readResource(alias.getStructureId());
132                if (alias.isRedirect()) {
133                    String link = OpenCms.getLinkManager().substituteLink(cms, aliasTarget);
134                    boolean isPermanent = alias.isPermanentRedirect();
135                    // response may be null if we're coming from the locale manager
136                    redirectToTarget(cms, req, res, link, isPermanent);
137                } else {
138                    // not a redirect, just proceed with the aliased resource
139                    cms.getRequestContext().setUri(cms.getSitePath(aliasTarget));
140                    return aliasTarget;
141                }
142            }
143        } catch (CmsPermissionViolationException e) {
144            // trigger the permission denied handler
145            throw e;
146        } catch (CmsResourceInitException e) {
147            // just rethrow so the catch(Throwable e) code isn't executed
148            throw e;
149        } catch (Throwable e) {
150            String uri = cms.getRequestContext().getUri();
151            CmsMessageContainer msg = org.opencms.ade.detailpage.Messages.get().container(
152                org.opencms.ade.detailpage.Messages.ERR_RESCOURCE_NOT_FOUND_1,
153                uri);
154            LOG.error(e.getLocalizedMessage(), e);
155            throw new CmsResourceInitException(msg, e);
156        }
157
158        return null;
159    }
160
161    /**
162     * Helper method for sending a redirect to a new URI.<p>
163     *
164     * @param cms the current CMS context
165     * @param req the current request
166     * @param res the current response
167     * @param link the redirect target
168     * @param isPermanent if true, sends a 'moved permanently' redirect
169     *
170     * @throws IOException
171     * @throws CmsResourceInitException
172     */
173    private void redirectToTarget(
174        CmsObject cms,
175        HttpServletRequest req,
176        HttpServletResponse res,
177        String link,
178        boolean isPermanent)
179    throws IOException, CmsResourceInitException {
180
181        CmsResourceInitException resInitException = new CmsResourceInitException(getClass());
182        if (res != null) {
183            // preserve request parameters for the redirect
184            String query = req.getQueryString();
185            if (query != null) {
186                link += "?" + query;
187            }
188            // disable 404 handler
189            resInitException.setClearErrors(true);
190            if (isPermanent && cms.getRequestContext().getCurrentProject().isOnlineProject()) {
191                // offline permanent redirects are confusing and not useful because the user can switch sites while staying on the same domain
192                res.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
193                res.setHeader("Location", link);
194            } else {
195                res.sendRedirect(link);
196            }
197        }
198        throw resInitException;
199    }
200
201}