1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.beans.config;
35
36 import javax.jcr.RepositoryException;
37 import javax.jcr.Session;
38
39 import info.magnolia.context.MgnlContext;
40 import info.magnolia.link.Link;
41 import info.magnolia.link.LinkException;
42 import info.magnolia.link.LinkUtil;
43
44 import org.apache.commons.lang3.StringUtils;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48
49
50
51
52 public class URI2RepositoryMapping {
53
54 private static Logger log = LoggerFactory.getLogger(URI2RepositoryMapping.class);
55
56
57
58
59 private String URIPrefix;
60
61
62
63
64 private String repository;
65
66
67
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
82
83 public boolean matches(String uri) {
84 if (uri == null) {
85 return false;
86 }
87 return uri.startsWith(URIPrefix);
88 }
89
90
91
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
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
111 if (session.itemExists(maybeHandle) || (maybeHandle.lastIndexOf("/") > 0 && session.itemExists(StringUtils.substringBeforeLast(maybeHandle, "/")))) {
112 return maybeHandle;
113 }
114 }
115 }catch(RepositoryException e){
116
117 log.debug(e.getMessage(), e);
118 }
119 return handle;
120 }
121
122
123
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
137
138 public String getURI(String handle) {
139 try {
140 return getURI(LinkUtil.createLinkInstance(this.getRepository(), handle, null, null, null));
141 }
142 catch (LinkException e) {
143 return handle;
144 }
145 }
146
147 public String getURI(Link uuidLink){
148 String uri = uuidLink.getPath();
149 if (StringUtils.isNotEmpty(this.handlePrefix)) {
150 uri = StringUtils.removeStart(uri, this.handlePrefix);
151 }
152 if (StringUtils.isNotEmpty(this.URIPrefix)) {
153 uri = this.URIPrefix + "/" + uri;
154 }
155
156 String nodeDataName = uuidLink.getNodeDataName();
157 String fileName = uuidLink.getFileName();
158 String extension = uuidLink.getExtension();
159
160 if(StringUtils.isNotEmpty(nodeDataName)){
161 uri += "/" + nodeDataName;
162 }
163 if(StringUtils.isNotEmpty(fileName)){
164 uri += "/" + fileName;
165 }
166 if(StringUtils.isNotEmpty(uri) && StringUtils.isNotEmpty(extension) && !StringUtils.endsWith(uri, "/")){
167 uri += "." + extension ;
168 }
169
170 return cleanHandle(uri);
171 }
172
173 public String getHandlePrefix() {
174 return handlePrefix;
175 }
176
177 public void setHandlePrefix(String handlePrefix) {
178 this.handlePrefix = handlePrefix;
179 }
180
181 public String getRepository() {
182 return repository;
183 }
184
185 public void setRepository(String repository) {
186 this.repository = repository;
187 }
188
189 public String getURIPrefix() {
190 return URIPrefix;
191 }
192
193 public void setURIPrefix(String uriPrefix) {
194 this.URIPrefix = uriPrefix;
195 }
196
197 @Override
198 public String toString() {
199 return this.URIPrefix + " --> " + repository + ":" + this.handlePrefix;
200 }
201 }