View Javadoc
1   /**
2    * This file Copyright (c) 2003-2018 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 final 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<>(getMappingComparator());
66      }
67  
68      protected Comparator<URI2RepositoryMapping> getMappingComparator() {
69          return (m0, m1) -> m1.getURIPrefix().compareTo(m0.getURIPrefix());
70      }
71  
72      /**
73       * The mapping to use for this uri.
74       */
75      public URI2RepositoryMapping getMapping(String uri) {
76          for (URI2RepositoryMapping mapping : mappings) {
77              if (mapping.matches(uri)) {
78                  return mapping;
79              }
80          }
81          return this.getDefaultMapping();
82      }
83  
84      public URI2RepositoryMapping getDefaultMapping() {
85          return DEFAULT_MAPPING;
86      }
87  
88      /**
89       * Get the handle for this uri.
90       */
91      public String getHandle(String uri) {
92          return this.getMapping(uri).getHandle(uri);
93      }
94  
95      /**
96       * Get the repository to use for this uri.
97       */
98      public String getRepository(String uri) {
99          return this.getMapping(uri).getRepository();
100     }
101 
102     /**
103      * Get the uri to use for this handle.
104      */
105     public String getURI(String repository, String handle) {
106         try {
107             return getURI(LinkUtil.createLinkInstance(repository, handle, null, null, null));
108         } catch (LinkException e) {
109             log.error("can't map [{}] to a uri", handle, e);
110         }
111         return handle;
112     }
113 
114     public String getURI(Link uuidLink) {
115         for (URI2RepositoryMapping mapping : mappings) {
116             if (StringUtils.equals(mapping.getRepository(), uuidLink.getWorkspace()) && (uuidLink.getPath().startsWith(mapping.getHandlePrefix()) || uuidLink.isEditorBinaryLink())) {
117                 return mapping.getURI(uuidLink);
118             }
119         }
120         return this.getDefaultMapping().getURI(uuidLink);
121     }
122 
123     public void setMappings(Collection<URI2RepositoryMapping> mappings) {
124         this.mappings.addAll(mappings);
125     }
126 
127     public void addMapping(URI2RepositoryMapping mapping) {
128         mappings.add(mapping);
129     }
130 
131     /**
132      * @deprecated since 4.5, use IoC
133      */
134     @Deprecated
135     public static URI2RepositoryManager getInstance() {
136         return Components.getSingleton(URI2RepositoryManager.class);
137     }
138 
139     /**
140      * @return the mappings
141      */
142     public Collection<URI2RepositoryMapping> getMappings() {
143         return this.mappings;
144     }
145 }