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(String uri) {
86          String handle;
87          handle = StringUtils.removeStart(uri, this.URIPrefix);
88          if (StringUtils.isNotEmpty(this.handlePrefix)) {
89              handle = StringUtils.removeStart(handle, "/");
90              handle = this.handlePrefix + "/" + handle;
91          }
92          //remove extension (ignore . anywhere else in the uri)
93          String fileName = StringUtils.substringAfterLast(handle, "/");
94          String extension = StringUtils.substringAfterLast(fileName, ".");
95          handle = StringUtils.removeEnd(handle, "." + extension);
96          return cleanHandle(handle);
97      }
98  
99      /**
100      * Clean a handle. Remove double / and add always a leading /.
101      */
102     protected String cleanHandle(String handle) {
103         if (!handle.startsWith("/")) {
104             handle = "/" + handle;
105         }
106         while (handle.indexOf("//") != -1) {
107             handle = StringUtils.replace(handle, "//", "/");
108         }
109         return handle;
110     }
111 
112     /**
113      * Create a uri based on a handle.
114      */
115     public String getURI(String handle) {
116         try {
117             return getURI(LinkUtil.createLinkInstance(this.getRepository(), handle, null, null, null));
118         } catch (LinkException e) {
119             return handle;
120         }
121     }
122 
123     public String getURI(Link uuidLink) {
124         String uri = uuidLink.getPath();
125         if (StringUtils.isNotEmpty(this.handlePrefix)) {
126             uri = StringUtils.removeStart(uri, this.handlePrefix);
127         }
128         if (StringUtils.isNotEmpty(this.URIPrefix)) {
129             uri = this.URIPrefix + "/" + uri;
130         }
131 
132         String nodeDataName = uuidLink.getNodeDataName();
133         String fileName = uuidLink.getFileName();
134         String extension = uuidLink.getExtension();
135 
136         if (StringUtils.isNotEmpty(nodeDataName)) {
137             uri += "/" + nodeDataName;
138         }
139         if (StringUtils.isNotEmpty(fileName)) {
140             uri += "/" + fileName;
141         }
142         if (StringUtils.isNotEmpty(uri) && StringUtils.isNotEmpty(extension) && !StringUtils.endsWith(uri, "/")) {
143             uri += "." + extension;
144         }
145 
146         return cleanHandle(uri);
147     }
148 
149     public String getHandlePrefix() {
150         return handlePrefix;
151     }
152 
153     public void setHandlePrefix(String handlePrefix) {
154         this.handlePrefix = handlePrefix;
155     }
156 
157     public String getRepository() {
158         return repository;
159     }
160 
161     public void setRepository(String repository) {
162         this.repository = repository;
163     }
164 
165     public String getURIPrefix() {
166         return URIPrefix;
167     }
168 
169     public void setURIPrefix(String uriPrefix) {
170         this.URIPrefix = uriPrefix;
171     }
172 
173     @Override
174     public String toString() {
175         return this.URIPrefix + " --> " + repository + ":" + this.handlePrefix;
176     }
177 }