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.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
47
48
49
50
51 public class URI2RepositoryMapping {
52
53
54
55
56 private String URIPrefix;
57
58
59
60
61 private String repository;
62
63
64
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
79
80 public boolean matches(String uri) {
81 if (uri == null) {
82 return false;
83 }
84 return uri.startsWith(URIPrefix);
85 }
86
87
88
89
90 public String getHandle(String uri) {
91 String handle;
92 handle = StringUtils.removeStart(uri, this.URIPrefix);
93 if (StringUtils.isNotEmpty(this.handlePrefix)) {
94 handle = StringUtils.removeStart(handle, "/");
95 handle = this.handlePrefix + "/" + handle;
96 }
97
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
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
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
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 }