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.ade.detailpage; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsResource; 032import org.opencms.file.CmsVfsResourceNotFoundException; 033import org.opencms.i18n.CmsMessageContainer; 034import org.opencms.main.CmsLog; 035import org.opencms.main.CmsResourceInitException; 036import org.opencms.main.I_CmsResourceInit; 037import org.opencms.main.OpenCms; 038import org.opencms.security.CmsPermissionViolationException; 039import org.opencms.security.CmsSecurityException; 040import org.opencms.util.CmsUUID; 041import org.opencms.workplace.CmsWorkplace; 042 043import org.apache.commons.logging.Log; 044 045import jakarta.servlet.ServletRequest; 046import jakarta.servlet.http.HttpServletRequest; 047import jakarta.servlet.http.HttpServletResponse; 048 049/** 050 * Resource init handler for detail-pages.<p> 051 * 052 * @since 8.0.0 053 */ 054public class CmsDetailPageResourceHandler implements I_CmsResourceInit { 055 056 /** The attribute containing the detail content resource. */ 057 public static final String ATTR_DETAIL_CONTENT_RESOURCE = "__opencms_detail_content_resource"; 058 059 /** The attribute containing the detail function page resource. */ 060 public static final String ATTR_DETAIL_FUNCTION_PAGE = "__opencms_detail_function_page"; 061 062 /** The log object for this class. */ 063 private static final Log LOG = CmsLog.getLog(CmsDetailPageResourceHandler.class); 064 065 /** 066 * Default constructor.<p> 067 */ 068 public CmsDetailPageResourceHandler() { 069 070 // empty 071 } 072 073 /** 074 * Returns the detail function page resource, if available.<p> 075 * 076 * @param req the current request 077 * 078 * @return the detail function page resource 079 */ 080 public static CmsResource getDetailFunctionPage(ServletRequest req) { 081 082 return (CmsResource)req.getAttribute(ATTR_DETAIL_FUNCTION_PAGE); 083 } 084 085 /** 086 * Returns the current detail content UUID, or <code>null</code> if this is not a request to a content detail page.<p> 087 * 088 * @param req the current request 089 * 090 * @return the current detail content UUID, or <code>null</code> if this is not a request to a content detail page 091 */ 092 public static CmsUUID getDetailId(ServletRequest req) { 093 094 CmsResource res = getDetailResource(req); 095 return res == null ? null : res.getStructureId(); 096 } 097 098 /** 099 * Returns the current detail content resource, or <code>null</code> if this is not a request to a content detail page.<p> 100 * 101 * @param req the current request 102 * 103 * @return the current detail content resource, or <code>null</code> if this is not a request to a content detail page 104 */ 105 public static CmsResource getDetailResource(ServletRequest req) { 106 107 return (CmsResource)req.getAttribute(ATTR_DETAIL_CONTENT_RESOURCE); 108 } 109 110 /** 111 * @see org.opencms.main.I_CmsResourceInit#initResource(org.opencms.file.CmsResource, org.opencms.file.CmsObject, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse) 112 */ 113 public CmsResource initResource( 114 CmsResource resource, 115 CmsObject cms, 116 HttpServletRequest req, 117 HttpServletResponse res) 118 throws CmsResourceInitException, CmsSecurityException { 119 120 // check if the resource was already found or the path starts with '/system/' 121 boolean abort = (resource != null) || cms.getRequestContext().getUri().startsWith(CmsWorkplace.VFS_PATH_SYSTEM); 122 if (abort) { 123 // skip in all cases above 124 return resource; 125 } 126 try { 127 CmsDetailResolution resolution = CmsDetailPageUtil.resolveDetail( 128 cms, 129 cms.getRequestContext().getUri(), 130 (page, detailRes) -> isValidDetailPage(cms, page, detailRes)); 131 if (resolution == null) { 132 return null; 133 } 134 if (res != null) { 135 // response will be null if this run through the init handler is only for determining the locale 136 boolean isFunction = resolution.isFunctionDetail(); 137 CmsResource detailResource = isFunction ? resolution.getFunctionPage() : resolution.getDetailContent(); 138 req.setAttribute(isFunction ? ATTR_DETAIL_FUNCTION_PAGE : ATTR_DETAIL_CONTENT_RESOURCE, detailResource); 139 cms.getRequestContext().setDetailResource(detailResource); 140 } 141 // set the resource path 142 cms.getRequestContext().setUri(cms.getSitePath(resolution.getDetailPage())); 143 return resolution.getDetailPage(); 144 } catch (CmsPermissionViolationException e) { 145 // trigger the permission denied handler 146 throw e; 147 } catch (CmsResourceInitException e) { 148 throw e; 149 } catch (CmsVfsResourceNotFoundException e) { 150 return null; 151 } catch (Throwable e) { 152 String uri = cms.getRequestContext().getUri(); 153 CmsMessageContainer msg = Messages.get().container(Messages.ERR_RESCOURCE_NOT_FOUND_1, uri); 154 if (LOG.isWarnEnabled()) { 155 LOG.warn(msg.key(), e); 156 } 157 throw new CmsResourceInitException(msg, e); 158 } 159 } 160 161 /** 162 * Checks whether the given detail page is valid for the given resource.<p> 163 * 164 * @param cms the CMS context 165 * @param page the detail page 166 * @param detailRes the detail resource 167 * 168 * @return true if the given detail page is valid 169 */ 170 protected boolean isValidDetailPage(CmsObject cms, CmsResource page, CmsResource detailRes) { 171 172 return OpenCms.getADEManager().getDetailPageHandler().isValidDetailPage(cms, page, detailRes); 173 174 } 175}