View Javadoc
1   /**
2    * This file Copyright (c) 2003-2017 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  
40  import org.apache.commons.lang3.StringUtils;
41  
42  
43  /**
44   * Describes a uri to repository mapping.
45   */
46  public class URI2RepositoryMapping {
47  
48      /**
49       * The prefix which triggers this mapping.
50       */
51      private String URIPrefix;
52  
53      /**
54       * The repository used for this mapping.
55       */
56      private String repository;
57  
58      /**
59       * The prefix added to the uri to create a full handle.
60       */
61      private String handlePrefix;
62  
63      public URI2RepositoryMapping(String uriPrefix, String repository, String handlePrefix) {
64          this.URIPrefix = uriPrefix;
65          this.repository = repository;
66          this.handlePrefix = handlePrefix;
67      }
68  
69      public URI2RepositoryMapping() {
70      }
71  
72      /**
73       * True if this mapping can get applied to the specified uri.
74       */
75      public boolean matches(String uri) {
76          if (uri == null) {
77              return false;
78          }
79          return uri.startsWith(URIPrefix);
80      }
81  
82      /**
83       * Create a node handle based on an uri.
84       */
85      public String getHandle(final String uri) {
86          String cleanedUri = StringUtils.removeStart(uri, this.URIPrefix);
87          if (StringUtils.isNotEmpty(this.handlePrefix)) {
88              // We remove any occurrences of the handle prefix from given URI
89              cleanedUri = StringUtils.removeStart(cleanedUri, this.handlePrefix);
90              // So that we do not double it up afterwards
91              cleanedUri = StringUtils.removeStart(cleanedUri, "/");
92              cleanedUri = this.handlePrefix + "/" + cleanedUri;
93          }
94          // Remove extension (ignore . anywhere else in the uri)
95          String fileName = StringUtils.substringAfterLast(cleanedUri, "/");
96          String extension = StringUtils.substringAfterLast(fileName, ".");
97          cleanedUri = StringUtils.removeEnd(cleanedUri, "." + extension);
98          return cleanHandle(cleanedUri);
99      }
100 
101     /**
102      * Clean a handle. Remove double / and add always a leading /.
103      */
104     protected String cleanHandle(String handle) {
105         if (!handle.startsWith("/")) {
106             handle = "/" + handle;
107         }
108         while (handle.indexOf("//") != -1) {
109             handle = StringUtils.replace(handle, "//", "/");
110         }
111         return handle;
112     }
113 
114     /**
115      * Create a uri based on a handle.
116      */
117     public String getURI(String handle) {
118         try {
119             return getURI(LinkUtil.createLinkInstance(this.getRepository(), handle, null, null, null));
120         } catch (LinkException e) {
121             return handle;
122         }
123     }
124 
125     public String getURI(Link uuidLink) {
126         String uri = uuidLink.getPath();
127         if (StringUtils.isNotEmpty(this.handlePrefix)) {
128             uri = StringUtils.removeStart(uri, this.handlePrefix);
129         }
130         if (StringUtils.isNotEmpty(this.URIPrefix)) {
131             uri = this.URIPrefix + "/" + uri;
132         }
133 
134         String nodeDataName = uuidLink.getNodeDataName();
135         String fileName = uuidLink.getFileName();
136         String extension = uuidLink.getExtension();
137 
138         if (StringUtils.isNotEmpty(nodeDataName)) {
139             uri += "/" + nodeDataName;
140         }
141         if (StringUtils.isNotEmpty(fileName)) {
142             uri += "/" + fileName;
143         }
144         if (StringUtils.isNotEmpty(uri) && StringUtils.isNotEmpty(extension) && !StringUtils.endsWith(uri, "/")) {
145             uri += "." + extension;
146         }
147 
148         return cleanHandle(uri);
149     }
150 
151     public String getHandlePrefix() {
152         return handlePrefix;
153     }
154 
155     public void setHandlePrefix(String handlePrefix) {
156         this.handlePrefix = handlePrefix;
157     }
158 
159     public String getRepository() {
160         return repository;
161     }
162 
163     public void setRepository(String repository) {
164         this.repository = repository;
165     }
166 
167     public String getURIPrefix() {
168         return URIPrefix;
169     }
170 
171     public void setURIPrefix(String uriPrefix) {
172         this.URIPrefix = uriPrefix;
173     }
174 
175     @Override
176     public String toString() {
177         return this.URIPrefix + " --> " + repository + ":" + this.handlePrefix;
178     }
179 }