View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 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  import info.magnolia.repository.RepositoryConstants;
42  
43  import java.util.Collection;
44  import java.util.Comparator;
45  import java.util.TreeSet;
46  
47  import org.apache.commons.lang.StringUtils;
48  import org.slf4j.Logger;
49  import org.slf4j.LoggerFactory;
50  
51  
52  /**
53   * Maps uri prefixes to repositories.
54   *
55   * @author Philipp Bracher
56   * @version $Id$
57   */
58  public class URI2RepositoryManager {
59  
60      protected static final URI2RepositoryMapping DEFAULT_MAPPING = new URI2RepositoryMapping("", RepositoryConstants.WEBSITE,"");
61  
62      private static Logger log = LoggerFactory.getLogger(URI2RepositoryManager.class);
63  
64      /**
65       * The mappings.
66       */
67      private final Collection<URI2RepositoryMapping> mappings;
68  
69      public URI2RepositoryManager() {
70          mappings = new TreeSet<URI2RepositoryMapping>(getMappingComparator());
71      }
72  
73      protected Comparator<URI2RepositoryMapping> getMappingComparator() {
74          return new Comparator<URI2RepositoryMapping>() {
75              @Override
76              public int compare(URI2RepositoryMapping m0, URI2RepositoryMapping m1) {
77                  return m1.getURIPrefix().compareTo(m0.getURIPrefix());
78              }
79          };
80      }
81  
82      /**
83       * The mapping to use for this uri.
84       */
85      public URI2RepositoryMapping getMapping(String uri) {
86          for (URI2RepositoryMapping mapping : mappings) {
87              if (mapping.matches(uri)) {
88                  return mapping;
89              }
90          }
91          return this.getDefaultMapping();
92      }
93  
94      public URI2RepositoryMapping getDefaultMapping() {
95          return DEFAULT_MAPPING;
96      }
97  
98      /**
99       * Get the handle for this uri.
100      */
101     public String getHandle(String uri) {
102         return this.getMapping(uri).getHandle(uri);
103     }
104 
105     /**
106      * Get the repository to use for this uri.
107      */
108     public String getRepository(String uri) {
109         return this.getMapping(uri).getRepository();
110     }
111 
112     /**
113      * Get the uri to use for this handle.
114      */
115     public String getURI(String repository, String handle) {
116         try {
117             return getURI(LinkFactory.createLink(repository, handle, null, null, null));
118         }
119         catch (LinkException e) {
120             log.error("can't map [" + handle + "] to a uri", e);
121         }
122         return handle;
123     }
124 
125     public String getURI(Link uuidLink) {
126         for (URI2RepositoryMapping mapping : mappings) {
127             if (StringUtils.equals(mapping.getRepository(), uuidLink.getRepository()) && (uuidLink.getHandle().startsWith(mapping.getHandlePrefix()) || uuidLink.isEditorBinaryLink())) {
128                 return mapping.getURI(uuidLink);
129             }
130         }
131         return this.getDefaultMapping().getURI(uuidLink);
132     }
133 
134     public void setMappings(Collection<URI2RepositoryMapping> mappings) {
135         this.mappings.addAll(mappings);
136     }
137 
138     public void addMapping(URI2RepositoryMapping mapping) {
139         mappings.add(mapping);
140     }
141 
142     /**
143      * @deprecated since 4.5, use IoC
144      */
145     public static URI2RepositoryManager getInstance() {
146         return Components.getSingleton(URI2RepositoryManager.class);
147     }
148 
149     /**
150      * @return the mappings
151      */
152     public Collection<URI2RepositoryMapping> getMappings() {
153         return this.mappings;
154     }
155 }