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.Collections;
033import java.util.LinkedHashSet;
034import java.util.Locale;
035import java.util.Set;
036
037/**
038 * Configuration for direct delivery of externally stored VFS content.<p>
039 */
040public class CmsStoredContentDeliveryConfiguration {
041
042    /** If direct stored content delivery is enabled. */
043    private boolean m_enabled;
044
045    /** Suffixes for which direct stored content delivery is enabled. */
046    private Set<String> m_enabledSuffixes = new LinkedHashSet<String>();
047
048    /** If this configuration was explicitly configured. */
049    private boolean m_configured;
050
051    /**
052     * Adds a suffix for direct stored content delivery.<p>
053     *
054     * @param suffix the suffix
055     */
056    public void addEnabledSuffix(String suffix) {
057
058        String normalizedSuffix = normalizeSuffix(suffix);
059        if (normalizedSuffix != null) {
060            m_enabledSuffixes.add(normalizedSuffix);
061            m_configured = true;
062        }
063    }
064
065    /**
066     * Returns the enabled suffixes.<p>
067     *
068     * @return the enabled suffixes
069     */
070    public Set<String> getEnabledSuffixes() {
071
072        return Collections.unmodifiableSet(m_enabledSuffixes);
073    }
074
075    /**
076     * Returns if suffixes for direct stored content delivery have been configured.<p>
077     *
078     * @return <code>true</code> if suffixes have been configured
079     */
080    public boolean hasEnabledSuffixes() {
081
082        return !m_enabledSuffixes.isEmpty();
083    }
084
085    /**
086     * Returns if stored content delivery settings have been configured explicitly.<p>
087     *
088     * @return <code>true</code> if stored content delivery settings have been configured
089     */
090    public boolean isConfigured() {
091
092        return m_configured;
093    }
094
095    /**
096     * Returns if direct stored content delivery is enabled.<p>
097     *
098     * @return <code>true</code> if direct stored content delivery is enabled
099     */
100    public boolean isEnabled() {
101
102        return m_enabled;
103    }
104
105    /**
106     * Returns if the suffix of the given resource name is enabled for direct stored content delivery.<p>
107     *
108     * @param resourceName the resource name
109     * @return <code>true</code> if the suffix is enabled
110     */
111    public boolean isSuffixEnabled(String resourceName) {
112
113        if (!hasEnabledSuffixes()) {
114            return true;
115        }
116        if (CmsStringUtil.isEmptyOrWhitespaceOnly(resourceName)) {
117            return false;
118        }
119        String normalizedName = resourceName.toLowerCase(Locale.ROOT);
120        for (String suffix : m_enabledSuffixes) {
121            if (normalizedName.endsWith(suffix)) {
122                return true;
123            }
124        }
125        return false;
126    }
127
128    /**
129     * Sets if direct stored content delivery is enabled.<p>
130     *
131     * @param enabled if direct stored content delivery is enabled
132     */
133    public void setEnabled(String enabled) {
134
135        m_enabled = Boolean.valueOf(enabled).booleanValue();
136        m_configured = true;
137    }
138
139    /**
140     * Normalizes a suffix.<p>
141     *
142     * @param suffix the suffix
143     * @return the normalized suffix
144     */
145    private String normalizeSuffix(String suffix) {
146
147        if (CmsStringUtil.isEmptyOrWhitespaceOnly(suffix)) {
148            return null;
149        }
150        String result = suffix.trim().toLowerCase(Locale.ROOT);
151        return result.startsWith(".") ? result : "." + result;
152    }
153}