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.main.CmsLog;
031
032import java.util.ArrayList;
033import java.util.Collection;
034import java.util.List;
035import java.util.regex.Matcher;
036import java.util.regex.Pattern;
037import java.util.regex.PatternSyntaxException;
038
039import org.apache.commons.logging.Log;
040
041/**
042 * Helper class used for matching rewrite aliases to incoming request URIs.<p>
043 */
044public class CmsRewriteAliasMatcher {
045
046    /**
047     * The result of a match operation.<p>
048     */
049    public static class RewriteResult {
050
051        /** The rewrite alias which matched the given path. */
052        private CmsRewriteAlias m_alias;
053
054        /** The path resulting from the rewrite. */
055        private String m_newPath;
056
057        /**
058         * Creates a new instance.<p>
059         *
060         * @param newPath the path resulting from the rewrite
061         * @param alias the alias that matched the path
062         */
063        public RewriteResult(String newPath, CmsRewriteAlias alias) {
064
065            m_newPath = newPath;
066            m_alias = alias;
067
068        }
069
070        /**
071         * Gets the alias which matched the given path.<p>
072         *
073         * @return the matching alias
074         */
075        public CmsRewriteAlias getAlias() {
076
077            return m_alias;
078        }
079
080        /**
081         * Gets the path resulting from the rewrite.<p>
082         *
083         * @return the new path
084         */
085        public String getNewPath() {
086
087            return m_newPath;
088        }
089
090    }
091
092    /** The logger instance for this class. */
093    private static final Log LOG = CmsLog.getLog(CmsRewriteAliasMatcher.class);
094
095    /** The list of rewrite aliases to use for matching. */
096    private List<CmsRewriteAlias> m_aliases;
097
098    /**
099     * Creates a new matcher instance for the given list of rewrite aliases.<p>
100     *
101     * @param aliases the list of rewrite aliases to be used for matching
102     */
103    public CmsRewriteAliasMatcher(Collection<CmsRewriteAlias> aliases) {
104
105        m_aliases = new ArrayList<CmsRewriteAlias>(aliases);
106    }
107
108    /**
109     * Tries to rewrite a given path, and either returns the rewrite result or null if no
110     * rewrite alias matched the path.<p>
111     *
112     * @param path the path to match
113     * @return the rewrite result or null if no rewrite alias matched
114     */
115    public RewriteResult match(String path) {
116
117        for (CmsRewriteAlias alias : m_aliases) {
118            try {
119                Pattern pattern = Pattern.compile(alias.getPatternString());
120                Matcher matcher = pattern.matcher(path);
121                if (matcher.matches()) {
122                    String newPath = matcher.replaceFirst(alias.getReplacementString());
123                    return new RewriteResult(newPath, alias);
124                }
125            } catch (PatternSyntaxException e) {
126                LOG.warn(e.getLocalizedMessage(), e);
127            } catch (IndexOutOfBoundsException e) {
128                LOG.warn(e.getLocalizedMessage(), e);
129            }
130        }
131        return null;
132    }
133}