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