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.CmsStringUtil; 031 032import java.util.Locale; 033 034/** 035 * A cache policy for the shared cache delivery mode.<p> 036 * 037 * A policy without a content type is the default policy. Other policies match either an exact content type 038 * or a top-level content type wildcard such as {@code image/*}.<p> 039 */ 040public class CmsSharedCachePolicy { 041 042 /** Value used for an optional duration which has not been configured. */ 043 public static final long DURATION_UNSET = -1; 044 045 /** The maximum client cache age in seconds. */ 046 private long m_clientMaxAge = DURATION_UNSET; 047 048 /** The optional content type selector. */ 049 private String m_contentType; 050 051 /** The maximum shared cache age in seconds. */ 052 private long m_sharedMaxAge = DURATION_UNSET; 053 054 /** The optional stale-if-error duration in seconds. */ 055 private long m_staleIfError = DURATION_UNSET; 056 057 /** 058 * Returns the maximum client cache age in seconds.<p> 059 * 060 * @return the maximum client cache age 061 */ 062 public long getClientMaxAge() { 063 064 return m_clientMaxAge; 065 } 066 067 /** 068 * Returns the configured content type selector.<p> 069 * 070 * @return the content type selector, or {@code null} for the default policy 071 */ 072 public String getContentType() { 073 074 return m_contentType; 075 } 076 077 /** 078 * Returns the maximum shared cache age in seconds.<p> 079 * 080 * @return the maximum shared cache age 081 */ 082 public long getSharedMaxAge() { 083 084 return m_sharedMaxAge; 085 } 086 087 /** 088 * Returns the stale-if-error duration in seconds.<p> 089 * 090 * @return the stale-if-error duration, or {@link #DURATION_UNSET} if it is not configured 091 */ 092 public long getStaleIfError() { 093 094 return m_staleIfError; 095 } 096 097 /** 098 * Returns whether this is the default policy.<p> 099 * 100 * @return {@code true} if this is the default policy 101 */ 102 public boolean isDefault() { 103 104 return m_contentType == null; 105 } 106 107 /** 108 * Sets the maximum client cache age in seconds.<p> 109 * 110 * @param clientMaxAge the maximum client cache age 111 */ 112 public void setClientMaxAge(String clientMaxAge) { 113 114 m_clientMaxAge = parseDuration(clientMaxAge); 115 } 116 117 /** 118 * Sets the content type selector.<p> 119 * 120 * @param contentType the content type selector 121 */ 122 public void setContentType(String contentType) { 123 124 if (CmsStringUtil.isEmptyOrWhitespaceOnly(contentType)) { 125 m_contentType = null; 126 } else { 127 m_contentType = contentType.trim().toLowerCase(Locale.ROOT); 128 } 129 } 130 131 /** 132 * Sets the maximum shared cache age in seconds.<p> 133 * 134 * @param sharedMaxAge the maximum shared cache age 135 */ 136 public void setSharedMaxAge(String sharedMaxAge) { 137 138 m_sharedMaxAge = parseDuration(sharedMaxAge); 139 } 140 141 /** 142 * Sets the stale-if-error duration in seconds.<p> 143 * 144 * @param staleIfError the stale-if-error duration 145 */ 146 public void setStaleIfError(String staleIfError) { 147 148 m_staleIfError = parseDuration(staleIfError); 149 } 150 151 /** 152 * Parses a non-negative duration in seconds.<p> 153 * 154 * @param value the value to parse 155 * @return the parsed duration 156 */ 157 private long parseDuration(String value) { 158 159 if (CmsStringUtil.isEmptyOrWhitespaceOnly(value)) { 160 return DURATION_UNSET; 161 } 162 return Long.parseLong(value.trim()); 163 } 164}