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 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
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 } 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 }