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