View Javadoc

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