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) throws CmsResourceInitException, CmsSecurityException { 079 080 // check if the resource was already found or the path starts with '/system/' 081 boolean abort = (resource != null) || cms.getRequestContext().getUri().startsWith(CmsWorkplace.VFS_PATH_SYSTEM); 082 if (abort) { 083 // skip in all cases above 084 return resource; 085 } 086 087 String path = cms.getRequestContext().getUri(); 088 path = CmsFileUtil.removeTrailingSeparator(path); 089 String siteRoot = cms.getRequestContext().getSiteRoot(); 090 if ("".equals(siteRoot)) { 091 siteRoot = OpenCms.getSiteManager().getSiteRoot(path); 092 if (siteRoot == null) { 093 siteRoot = ""; 094 } 095 } 096 try { 097 String sitePath = path; 098 String oldSiteRoot = cms.getRequestContext().getSiteRoot(); 099 try { 100 cms.getRequestContext().setSiteRoot(siteRoot); 101 sitePath = cms.getRequestContext().removeSiteRoot(path); 102 } finally { 103 cms.getRequestContext().setSiteRoot(oldSiteRoot); 104 } 105 CmsRewriteAliasMatcher rewriteAliases = OpenCms.getAliasManager().getRewriteAliasMatcher(cms, siteRoot); 106 CmsRewriteAliasMatcher.RewriteResult rewriteResult = rewriteAliases.match(sitePath); 107 if ((rewriteResult != null) && (res != null)) { 108 String link = OpenCms.getLinkManager().substituteLink(cms, rewriteResult.getNewPath()); 109 if (rewriteResult.getAlias().getMode().isRedirect()) { 110 redirectToTarget( 111 req, 112 res, 113 link, 114 rewriteResult.getAlias().getMode() == CmsAliasMode.permanentRedirect); 115 } else { 116 cms.getRequestContext().setUri(rewriteResult.getNewPath()); 117 } 118 return null; 119 } 120 List<CmsAlias> aliases = OpenCms.getAliasManager().getAliasesForPath(cms, siteRoot, sitePath); 121 assert aliases.size() < 2; 122 if (aliases.size() == 1) { 123 CmsAlias alias = aliases.get(0); 124 CmsResource aliasTarget = cms.readResource(alias.getStructureId()); 125 126 if (alias.isRedirect()) { 127 String link = OpenCms.getLinkManager().substituteLink(cms, aliasTarget); 128 boolean isPermanent = alias.isPermanentRedirect(); 129 // response may be null if we're coming from the locale manager 130 redirectToTarget(req, res, link, isPermanent); 131 } else { 132 // not a redirect, just proceed with the aliased resource 133 cms.getRequestContext().setUri(cms.getSitePath(aliasTarget)); 134 return aliasTarget; 135 } 136 } 137 } catch (CmsPermissionViolationException e) { 138 // trigger the permission denied handler 139 throw e; 140 } catch (CmsResourceInitException e) { 141 // just rethrow so the catch(Throwable e) code isn't executed 142 throw e; 143 } catch (Throwable e) { 144 String uri = cms.getRequestContext().getUri(); 145 CmsMessageContainer msg = org.opencms.ade.detailpage.Messages.get().container( 146 org.opencms.ade.detailpage.Messages.ERR_RESCOURCE_NOT_FOUND_1, 147 uri); 148 LOG.error(e.getLocalizedMessage(), e); 149 throw new CmsResourceInitException(msg, e); 150 } 151 152 return null; 153 } 154 155 /** 156 * Helper method for sending a redirect to a new URI.<p> 157 * 158 * @param req the current request 159 * @param res the current response 160 * @param link the redirect target 161 * @param isPermanent if true, sends a 'moved permanently' redirect 162 * 163 * @throws IOException 164 * @throws CmsResourceInitException 165 */ 166 private void redirectToTarget(HttpServletRequest req, HttpServletResponse res, String link, boolean isPermanent) 167 throws IOException, CmsResourceInitException { 168 169 CmsResourceInitException resInitException = new CmsResourceInitException(getClass()); 170 if (res != null) { 171 // preserve request parameters for the redirect 172 String query = req.getQueryString(); 173 if (query != null) { 174 link += "?" + query; 175 } 176 // disable 404 handler 177 resInitException.setClearErrors(true); 178 if (isPermanent) { 179 res.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); 180 res.setHeader("Location", link); 181 } else { 182 res.sendRedirect(link); 183 } 184 } 185 throw resInitException; 186 } 187 188}