001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (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.db;
029
030import org.opencms.gwt.shared.alias.CmsAliasMode;
031import org.opencms.util.CmsUUID;
032
033import java.util.regex.Pattern;
034
035import com.google.common.base.Objects;
036
037/**
038 * This class represents an alias from a virtual path to a resource in the VFS.<p>
039 */
040public class CmsAlias {
041
042    /** The regular expression which describes valid alias paths:
043     * one or more segments, each consisting of a slash at the front followed
044     * by one or more 'unreserved characters' for URIs (see RFC 2396).
045     */
046    public static final Pattern ALIAS_PATTERN = Pattern.compile("(?:/[a-zA-Z0-9-_\\.!~\\*\\'\\(\\)]+)+"); //$NON-NLS-1$
047
048    /** The alias path. */
049    protected String m_aliasPath;
050
051    /** The alias mode. */
052    protected CmsAliasMode m_mode;
053
054    /** The site root for the alias. */
055    protected String m_siteRoot;
056
057    /** The structure id of the aliased page. */
058    protected CmsUUID m_structureId;
059
060    /**
061     * Creates a new alias.<p>
062     *
063     * @param structureId the structure id of the aliased page
064     * @param siteRoot the site root of the alias
065     * @param aliasPath the alias path
066     * @param mode the alias mode
067     */
068    public CmsAlias(CmsUUID structureId, String siteRoot, String aliasPath, CmsAliasMode mode) {
069
070        m_aliasPath = aliasPath;
071        m_structureId = structureId;
072        m_siteRoot = siteRoot;
073        m_mode = mode;
074    }
075
076    /**
077     * @see java.lang.Object#equals(java.lang.Object)
078     */
079    @Override
080    public boolean equals(Object other) {
081
082        if (!(other instanceof CmsAlias)) {
083            return false;
084        }
085        CmsAlias otherAlias = (CmsAlias)other;
086        return Objects.equal(m_aliasPath, otherAlias.m_aliasPath)
087            && Objects.equal(m_siteRoot, otherAlias.m_siteRoot)
088            && Objects.equal(m_structureId, otherAlias.m_structureId)
089            && Objects.equal(m_mode, otherAlias.m_mode);
090    }
091
092    /**
093     * Gets the alias path.<p>
094     *
095     * @return the alias path
096     */
097    public String getAliasPath() {
098
099        return m_aliasPath;
100    }
101
102    /**
103     * Gets the alias mode.<p>
104     *
105     * @return the alias mode
106     */
107    public CmsAliasMode getMode() {
108
109        return m_mode;
110    }
111
112    /**
113     * Gets the alias site root.<p>
114     *
115     * @return the alias site root
116     */
117    public String getSiteRoot() {
118
119        return m_siteRoot;
120    }
121
122    /**
123     * Gets the structure id of the aliased resource.<p>
124     *
125     * @return the structure id of the aliased resource
126     */
127    public CmsUUID getStructureId() {
128
129        return m_structureId;
130    }
131
132    /**
133     * @see java.lang.Object#hashCode()
134     */
135    @Override
136    public int hashCode() {
137
138        return Objects.hashCode(m_aliasPath, m_siteRoot, m_mode, m_structureId);
139    }
140
141    /**
142     * Checks whether the mode of the alias is 'permanent redirect'.<p>
143     *
144     * @return true if the mode of the alias is 'permanent redirect'
145     */
146    public boolean isPermanentRedirect() {
147
148        return m_mode.equals(CmsAliasMode.permanentRedirect);
149    }
150
151    /**
152     * Checks whether the mode of the alias is a redirect type (permanent or temporary).<p>
153     *
154     * @return true if the mode of the alias is a redirect type
155     */
156    public boolean isRedirect() {
157
158        return m_mode.equals(CmsAliasMode.permanentRedirect) || m_mode.equals(CmsAliasMode.redirect);
159    }
160
161}