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.link.Link;
37 import info.magnolia.link.LinkException;
38 import info.magnolia.link.LinkUtil;
39
40 import org.apache.commons.lang3.StringUtils;
41
42
43
44
45
46 public class URI2RepositoryMapping {
47
48
49
50
51 private String URIPrefix;
52
53
54
55
56 private String repository;
57
58
59
60
61 private String handlePrefix;
62
63 public URI2RepositoryMapping(String uriPrefix, String repository, String handlePrefix) {
64 this.URIPrefix = uriPrefix;
65 this.repository = repository;
66 this.handlePrefix = handlePrefix;
67 }
68
69 public URI2RepositoryMapping() {
70 }
71
72
73
74
75 public boolean matches(String uri) {
76 if (uri == null) {
77 return false;
78 }
79 return uri.startsWith(URIPrefix);
80 }
81
82
83
84
85 public String getHandle(String uri) {
86 String handle;
87 handle = StringUtils.removeStart(uri, this.URIPrefix);
88 if (StringUtils.isNotEmpty(this.handlePrefix)) {
89 handle = StringUtils.removeStart(handle, "/");
90 handle = this.handlePrefix + "/" + handle;
91 }
92
93 String fileName = StringUtils.substringAfterLast(handle, "/");
94 String extension = StringUtils.substringAfterLast(fileName, ".");
95 handle = StringUtils.removeEnd(handle, "." + extension);
96 return cleanHandle(handle);
97 }
98
99
100
101
102 protected String cleanHandle(String handle) {
103 if (!handle.startsWith("/")) {
104 handle = "/" + handle;
105 }
106 while (handle.indexOf("//") != -1) {
107 handle = StringUtils.replace(handle, "//", "/");
108 }
109 return handle;
110 }
111
112
113
114
115 public String getURI(String handle) {
116 try {
117 return getURI(LinkUtil.createLinkInstance(this.getRepository(), handle, null, null, null));
118 } catch (LinkException e) {
119 return handle;
120 }
121 }
122
123 public String getURI(Link uuidLink) {
124 String uri = uuidLink.getPath();
125 if (StringUtils.isNotEmpty(this.handlePrefix)) {
126 uri = StringUtils.removeStart(uri, this.handlePrefix);
127 }
128 if (StringUtils.isNotEmpty(this.URIPrefix)) {
129 uri = this.URIPrefix + "/" + uri;
130 }
131
132 String nodeDataName = uuidLink.getNodeDataName();
133 String fileName = uuidLink.getFileName();
134 String extension = uuidLink.getExtension();
135
136 if (StringUtils.isNotEmpty(nodeDataName)) {
137 uri += "/" + nodeDataName;
138 }
139 if (StringUtils.isNotEmpty(fileName)) {
140 uri += "/" + fileName;
141 }
142 if (StringUtils.isNotEmpty(uri) && StringUtils.isNotEmpty(extension) && !StringUtils.endsWith(uri, "/")) {
143 uri += "." + extension;
144 }
145
146 return cleanHandle(uri);
147 }
148
149 public String getHandlePrefix() {
150 return handlePrefix;
151 }
152
153 public void setHandlePrefix(String handlePrefix) {
154 this.handlePrefix = handlePrefix;
155 }
156
157 public String getRepository() {
158 return repository;
159 }
160
161 public void setRepository(String repository) {
162 this.repository = repository;
163 }
164
165 public String getURIPrefix() {
166 return URIPrefix;
167 }
168
169 public void setURIPrefix(String uriPrefix) {
170 this.URIPrefix = uriPrefix;
171 }
172
173 @Override
174 public String toString() {
175 return this.URIPrefix + " --> " + repository + ":" + this.handlePrefix;
176 }
177 }