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.context.MgnlContext;
37  import info.magnolia.link.Link;
38  import info.magnolia.link.LinkException;
39  import info.magnolia.link.LinkUtil;
40  
41  import javax.jcr.RepositoryException;
42  import javax.jcr.Session;
43  
44  import org.apache.commons.lang3.StringUtils;
45  import org.slf4j.Logger;
46  import org.slf4j.LoggerFactory;
47  
48  
49  /**
50   * Describes a uri to repository mapping.
51   */
52  public class URI2RepositoryMapping {
53  
54      private static Logger log = LoggerFactory.getLogger(URI2RepositoryMapping.class);
55  
56      /**
57       * The prefix which triggers this mapping.
58       */
59      private String URIPrefix;
60  
61      /**
62       * The repository used for this mapping.
63       */
64      private String repository;
65  
66      /**
67       * The prefix added to the uri to create a full handle.
68       */
69      private String handlePrefix;
70  
71      public URI2RepositoryMapping(String uriPrefix, String repository, String handlePrefix) {
72          this.URIPrefix = uriPrefix;
73          this.repository = repository;
74          this.handlePrefix = handlePrefix;
75      }
76  
77      public URI2RepositoryMapping() {
78      }
79  
80      /**
81       * True if this mapping can get applied to the specified uri.
82       */
83      public boolean matches(String uri) {
84          if (uri == null) {
85              return false;
86          }
87          return uri.startsWith(URIPrefix);
88      }
89  
90      /**
91       * Create a node handle based on an uri.
92       */
93      public String getHandle(String uri) {
94          String handle;
95          handle = StringUtils.removeStart(uri, this.URIPrefix);
96          if (StringUtils.isNotEmpty(this.handlePrefix)) {
97              handle = StringUtils.removeStart(handle, "/");
98              handle = this.handlePrefix + "/" + handle;
99          }
100         //remove extension (ignore . anywhere else in the uri)
101         String fileName = StringUtils.substringAfterLast(handle, "/");
102         String extension = StringUtils.substringAfterLast(fileName, ".");
103         handle = StringUtils.removeEnd(handle, "." + extension);
104         handle = cleanHandle(handle);
105 
106         try {
107             final Session session = MgnlContext.getJCRSession(this.repository);
108             if (!session.itemExists(handle)) {
109                 String maybeHandle = (this.handlePrefix.endsWith("/") ? "/" : "") + StringUtils.removeStart(handle, this.handlePrefix);
110                 // prefix might have been prepended incorrectly. Second part of the condition is there to match links to binary nodes
111                 if (session.itemExists(maybeHandle) || (maybeHandle.lastIndexOf("/") > 0 && session.itemExists(StringUtils.substringBeforeLast(maybeHandle, "/")))) {
112                     return maybeHandle;
113                 }
114             }
115         } catch (RepositoryException e) {
116             //Log the exception and return handle
117             log.debug(e.getMessage(), e);
118         }
119         return handle;
120     }
121 
122     /**
123      * Clean a handle. Remove double / and add always a leading /.
124      */
125     protected String cleanHandle(String handle) {
126         if (!handle.startsWith("/")) {
127             handle = "/" + handle;
128         }
129         while (handle.indexOf("//") != -1) {
130             handle = StringUtils.replace(handle, "//", "/");
131         }
132         return handle;
133     }
134 
135     /**
136      * Create a uri based on a handle.
137      */
138     public String getURI(String handle) {
139         try {
140             return getURI(LinkUtil.createLinkInstance(this.getRepository(), handle, null, null, null));
141         } catch (LinkException e) {
142             return handle;
143         }
144     }
145 
146     public String getURI(Link uuidLink) {
147         String uri = uuidLink.getPath();
148         if (StringUtils.isNotEmpty(this.handlePrefix)) {
149             uri = StringUtils.removeStart(uri, this.handlePrefix);
150         }
151         if (StringUtils.isNotEmpty(this.URIPrefix)) {
152             uri = this.URIPrefix + "/" + uri;
153         }
154 
155         String nodeDataName = uuidLink.getNodeDataName();
156         String fileName = uuidLink.getFileName();
157         String extension = uuidLink.getExtension();
158 
159         if (StringUtils.isNotEmpty(nodeDataName)) {
160             uri += "/" + nodeDataName;
161         }
162         if (StringUtils.isNotEmpty(fileName)) {
163             uri += "/" + fileName;
164         }
165         if (StringUtils.isNotEmpty(uri) && StringUtils.isNotEmpty(extension) && !StringUtils.endsWith(uri, "/")) {
166             uri += "." + extension;
167         }
168 
169         return cleanHandle(uri);
170     }
171 
172     public String getHandlePrefix() {
173         return handlePrefix;
174     }
175 
176     public void setHandlePrefix(String handlePrefix) {
177         this.handlePrefix = handlePrefix;
178     }
179 
180     public String getRepository() {
181         return repository;
182     }
183 
184     public void setRepository(String repository) {
185         this.repository = repository;
186     }
187 
188     public String getURIPrefix() {
189         return URIPrefix;
190     }
191 
192     public void setURIPrefix(String uriPrefix) {
193         this.URIPrefix = uriPrefix;
194     }
195 
196     @Override
197     public String toString() {
198         return this.URIPrefix + " --> " + repository + ":" + this.handlePrefix;
199     }
200 }