View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.beans.config;
35  
36  import info.magnolia.link.Link;
37  
38  import info.magnolia.link.LinkFactory;
39  import info.magnolia.link.LinkException;
40  import info.magnolia.objectfactory.Components;
41  
42  import java.util.Collection;
43  import java.util.Comparator;
44  import java.util.TreeSet;
45  
46  import org.apache.commons.lang.StringUtils;
47  import org.slf4j.Logger;
48  import org.slf4j.LoggerFactory;
49  
50  
51  /**
52   * Maps uri prefixes to repositories.
53   * 
54   * @author Philipp Bracher
55   * @version $Id: URI2RepositoryManager.java 36178 2010-08-03 16:34:52Z gjoseph $
56   */
57  public class URI2RepositoryManager {
58  
59      protected static final URI2RepositoryMapping DEFAULT_MAPPING = new URI2RepositoryMapping("", ContentRepository.WEBSITE,"");
60  
61      private static Logger log = LoggerFactory.getLogger(URI2RepositoryManager.class);
62  
63      /**
64       * The mappings.
65       */
66      private Collection<URI2RepositoryMapping> mappings;
67  
68      public URI2RepositoryManager() {
69          mappings = new TreeSet<URI2RepositoryMapping>(getMappingComparator());
70      }
71  
72      protected Comparator<URI2RepositoryMapping> getMappingComparator() {
73          return new Comparator<URI2RepositoryMapping>() {
74              public int compare(URI2RepositoryMapping m0, URI2RepositoryMapping m1) {
75                  return m1.getURIPrefix().compareTo(m0.getURIPrefix());
76              }
77          };
78      }
79  
80      /**
81       * The mapping to use for this uri.
82       */
83      public URI2RepositoryMapping getMapping(String uri) {
84          for (URI2RepositoryMapping mapping : mappings) {
85              if (mapping.matches(uri)) {
86                  return mapping;
87              }
88          }
89          return this.getDefaultMapping();
90      }
91  
92      public URI2RepositoryMapping getDefaultMapping() {
93          return DEFAULT_MAPPING;
94      }
95  
96      /**
97       * Get the handle for this uri.
98       */
99      public String getHandle(String uri) {
100         return this.getMapping(uri).getHandle(uri);
101     }
102 
103     /**
104      * Get the repository to use for this uri.
105      */
106     public String getRepository(String uri) {
107         return this.getMapping(uri).getRepository();
108     }
109 
110     /**
111      * Get the uri to use for this handle.
112      */
113     public String getURI(String repository, String handle) {
114         try {
115             return getURI(LinkFactory.createLink(repository, handle, null, null, null));
116         }
117         catch (LinkException e) {
118             log.error("can't map [" + handle + "] to a uri", e);
119         }
120         return handle;
121     }
122 
123     public String getURI(Link uuidLink) {
124         for (URI2RepositoryMapping mapping : mappings) {
125             if (StringUtils.equals(mapping.getRepository(), uuidLink.getRepository()) && uuidLink.getHandle().startsWith(mapping.getHandlePrefix())) {
126                 return mapping.getURI(uuidLink);
127             }
128         }
129         return this.getDefaultMapping().getURI(uuidLink);
130     }
131 
132     public void addMapping(URI2RepositoryMapping mapping) {
133         mappings.add(mapping);
134     }
135 
136     public static URI2RepositoryManager getInstance() {
137         return Components.getSingleton(URI2RepositoryManager.class);
138     }
139 
140     /**
141      * @return the mappings
142      */
143     public Collection<URI2RepositoryMapping> getMappings() {
144         return this.mappings;
145     }
146 
147 }