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 GmbH & Co. KG, 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.staticexport; 029 030import org.opencms.util.CmsRequestUtil; 031 032import java.io.IOException; 033 034import jakarta.servlet.http.HttpServletResponse; 035import jakarta.servlet.http.HttpServletResponseWrapper; 036 037/** 038 * Response wrapper for static export requests, required to access the status code of the response.<p> 039 * 040 * The <code>{@link org.opencms.loader.I_CmsResourceLoader#export(org.opencms.file.CmsObject, org.opencms.file.CmsResource, jakarta.servlet.http.HttpServletRequest, HttpServletResponse)}</code> 041 * method is called by the static export manager. Many implementations set the http status codes for the response. 042 * This wrapper enables the export manager to return the status code set on the response 043 * in <code>{@link org.opencms.staticexport.CmsStaticExportManager#export(jakarta.servlet.http.HttpServletRequest, HttpServletResponse, org.opencms.file.CmsObject, CmsStaticExportData)}</code>.<p> 044 * 045 * @since 6.0.0 046 */ 047public class CmsStaticExportResponseWrapper extends HttpServletResponseWrapper { 048 049 /** The enforced cache control value, or {@code null} if no value is enforced. */ 050 private String m_cacheControl; 051 052 /** The status code. */ 053 protected int m_status; 054 055 /** 056 * Creates a new export response wrapper.<p> 057 * 058 * @param res the original response to wrap 059 */ 060 public CmsStaticExportResponseWrapper(HttpServletResponse res) { 061 062 super(res); 063 m_status = -1; 064 } 065 066 /** 067 * @see jakarta.servlet.http.HttpServletResponseWrapper#addHeader(java.lang.String, java.lang.String) 068 */ 069 @Override 070 public void addHeader(String name, String value) { 071 072 if ((m_cacheControl != null) && CmsRequestUtil.HEADER_CACHE_CONTROL.equalsIgnoreCase(name)) { 073 super.setHeader(CmsRequestUtil.HEADER_CACHE_CONTROL, m_cacheControl); 074 } else { 075 super.addHeader(name, value); 076 } 077 } 078 079 /** 080 * Enforces the given Cache-Control value for this response.<p> 081 * 082 * Later attempts by a resource loader to replace or add Cache-Control headers retain this value. If this method 083 * is not called, the wrapper keeps its classic behavior and delegates all header changes unchanged.<p> 084 * 085 * @param cacheControl the Cache-Control value to enforce 086 */ 087 public void enforceCacheControl(String cacheControl) { 088 089 m_cacheControl = cacheControl; 090 super.setHeader(CmsRequestUtil.HEADER_CACHE_CONTROL, cacheControl); 091 } 092 093 /** 094 * Returns the status code of this export response, if no status code was set so far, 095 * <code>-1</code> is returned.<p> 096 * 097 * @return the status code of this export response 098 */ 099 @Override 100 public int getStatus() { 101 102 return m_status; 103 } 104 105 /** 106 * @see jakarta.servlet.ServletResponseWrapper#reset() 107 */ 108 @Override 109 public void reset() { 110 111 super.reset(); 112 m_status = -1; 113 if (m_cacheControl != null) { 114 super.setHeader(CmsRequestUtil.HEADER_CACHE_CONTROL, m_cacheControl); 115 } 116 } 117 118 /** 119 * @see jakarta.servlet.http.HttpServletResponse#sendError(int) 120 */ 121 @Override 122 public void sendError(int status) throws IOException { 123 124 m_status = status; 125 super.sendError(status); 126 } 127 128 /** 129 * @see jakarta.servlet.http.HttpServletResponse#sendError(int, java.lang.String) 130 */ 131 @Override 132 public void sendError(int status, String message) throws IOException { 133 134 m_status = status; 135 super.sendError(status, message); 136 } 137 138 /** 139 * @see jakarta.servlet.http.HttpServletResponseWrapper#setHeader(java.lang.String, java.lang.String) 140 */ 141 @Override 142 public void setHeader(String name, String value) { 143 144 if ((m_cacheControl != null) && CmsRequestUtil.HEADER_CACHE_CONTROL.equalsIgnoreCase(name)) { 145 super.setHeader(CmsRequestUtil.HEADER_CACHE_CONTROL, m_cacheControl); 146 } else { 147 super.setHeader(name, value); 148 } 149 } 150 151 /** 152 * @see jakarta.servlet.http.HttpServletResponseWrapper#setStatus(int) 153 */ 154 @Override 155 public void setStatus(int status) { 156 157 m_status = status; 158 super.setStatus(status); 159 } 160 161}