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.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 String link = OpenCms.getLinkManager().substituteLink(cms, rewriteResult.getNewPath()); 110 if (rewriteResult.getAlias().getMode().isRedirect()) { 111 redirectToTarget( 112 cms, 113 req, 114 res, 115 link, 116 rewriteResult.getAlias().getMode() == CmsAliasMode.permanentRedirect); 117 } else { 118 cms.getRequestContext().setUri(rewriteResult.getNewPath()); 119 } 120 return null; 121 } 122 List<CmsAlias> aliases = OpenCms.getAliasManager().getAliasesForPath(cms, siteRoot, sitePath); 123 assert aliases.size() < 2; 124 if (aliases.size() == 1) { 125 CmsAlias alias = aliases.get(0); 126 CmsResource aliasTarget = cms.readResource(alias.getStructureId()); 127 128 if (alias.isRedirect()) { 129 String link = OpenCms.getLinkManager().substituteLink(cms, aliasTarget); 130 boolean isPermanent = alias.isPermanentRedirect(); 131 // response may be null if we're coming from the locale manager 132 redirectToTarget(cms, req, res, link, isPermanent); 133 } else { 134 // not a redirect, just proceed with the aliased resource 135 cms.getRequestContext().setUri(cms.getSitePath(aliasTarget)); 136 return aliasTarget; 137 } 138 } 139 } catch (CmsPermissionViolationException e) { 140 // trigger the permission denied handler 141 throw e; 142 } catch (CmsResourceInitException e) { 143 // just rethrow so the catch(Throwable e) code isn't executed 144 throw e; 145 } catch (Throwable e) { 146 String uri = cms.getRequestContext().getUri(); 147 CmsMessageContainer msg = org.opencms.ade.detailpage.Messages.get().container( 148 org.opencms.ade.detailpage.Messages.ERR_RESCOURCE_NOT_FOUND_1, 149 uri); 150 LOG.error(e.getLocalizedMessage(), e); 151 throw new CmsResourceInitException(msg, e); 152 } 153 154 return null; 155 } 156 157 /** 158 * Helper method for sending a redirect to a new URI.<p> 159 * 160 * @param cms the current CMS context 161 * @param req the current request 162 * @param res the current response 163 * @param link the redirect target 164 * @param isPermanent if true, sends a 'moved permanently' redirect 165 * 166 * @throws IOException 167 * @throws CmsResourceInitException 168 */ 169 private void redirectToTarget( 170 CmsObject cms, 171 HttpServletRequest req, 172 HttpServletResponse res, 173 String link, 174 boolean isPermanent) 175 throws IOException, CmsResourceInitException { 176 177 CmsResourceInitException resInitException = new CmsResourceInitException(getClass()); 178 if (res != null) { 179 // preserve request parameters for the redirect 180 String query = req.getQueryString(); 181 if (query != null) { 182 link += "?" + query; 183 } 184 // disable 404 handler 185 resInitException.setClearErrors(true); 186 if (isPermanent && cms.getRequestContext().getCurrentProject().isOnlineProject()) { 187 // offline permanent redirects are confusing and not useful because the user can switch sites while staying on the same domain 188 res.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); 189 res.setHeader("Location", link); 190 } else { 191 res.sendRedirect(link); 192 } 193 } 194 throw resInitException; 195 } 196 197}