RepositoryAddress.java
7.18 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Credentials
* javax.jcr.SimpleCredentials
*/
package com.day.jcr.vault.fs.api;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.BitSet;
import javax.jcr.Credentials;
import javax.jcr.SimpleCredentials;
public class RepositoryAddress {
public static final String JCR_ROOT = "/jcr:root";
private final URI uri;
private final URI specific;
private final String workspace;
private final String path;
private static BitSet URISaveEx;
private static final char[] hexTable;
public RepositoryAddress(String uri) throws URISyntaxException {
this(new URI(uri));
}
public RepositoryAddress(URI uri) throws URISyntaxException {
int idx1;
String workspace;
String path = uri.getPath();
String prefix = "/";
String localPath = "/";
if (path.length() == 0 || path.equals("/")) {
workspace = "-";
localPath = "/";
} else if (!uri.isAbsolute()) {
idx1 = path.indexOf(47, 1);
if (idx1 < 0) {
workspace = path.substring(1);
} else {
workspace = path.substring(1, idx1);
localPath = path.substring(idx1);
}
} else {
String segment;
if (path.charAt(path.length() - 1) != '/') {
path = path + "/";
}
idx1 = -1;
int idx2 = 0;
int idx3 = path.indexOf(47, 1);
while (idx3 > 0 && !(segment = path.substring(idx2, idx3)).equals("/jcr:root")) {
idx1 = idx2;
idx2 = idx3;
idx3 = path.indexOf(47, idx3 + 1);
}
if (idx3 < 0) {
if (uri.getScheme() != null && uri.getScheme().equals("rmi")) {
idx1 = path.indexOf(47, 1);
idx2 = path.indexOf(47, idx1 + 1);
if (idx2 < 0) {
workspace = "-";
prefix = path.substring(0, idx1);
localPath = "/";
} else {
workspace = path.substring(idx1 + 1, idx2);
prefix = path.substring(0, idx1);
int end = path.length();
if (end != idx2 + 1) {
--end;
}
localPath = path.substring(idx2, end);
}
} else {
workspace = idx1 < 0 ? "-" : path.substring(idx1 + 1, idx2);
prefix = idx1 <= 0 ? "/" : path.substring(0, idx1);
localPath = "/";
}
} else {
workspace = path.substring(idx1 + 1, idx2);
prefix = path.substring(0, idx1);
int end = path.length();
if (end - idx3 > 1) {
--end;
}
localPath = path.substring(idx3, end);
}
}
if (uri.getScheme() != null && uri.getScheme().startsWith("http") && (prefix.equals("/") || prefix.equals("/crx"))) {
prefix = "/crx/server";
workspace = "-";
}
if (prefix.length() == 0) {
prefix = "/";
}
this.path = localPath;
this.workspace = workspace;
this.specific = uri.resolve(prefix);
StringBuffer buf = new StringBuffer(this.specific.toString());
if (buf.charAt(buf.length() - 1) != '/') {
buf.append('/');
}
buf.append(workspace);
buf.append("/jcr:root");
if (!localPath.equals("/")) {
buf.append(RepositoryAddress.escapePath(localPath));
}
this.uri = new URI(buf.toString());
}
private RepositoryAddress(URI uri, URI specific, String workspace, String path) {
this.uri = uri;
this.specific = specific;
this.workspace = workspace;
this.path = path;
}
public URI getURI() {
return this.uri;
}
public RepositoryAddress resolve(String path) {
if (path == null || path.length() == 0 || path.equals(".") || path.equals("./")) {
return this;
}
StringBuffer newPath = new StringBuffer(this.specific.getPath());
newPath.append("/");
newPath.append(this.workspace);
newPath.append("/jcr:root");
if (path.charAt(0) != '/') {
path = this.path.endsWith("/") ? this.path + path : this.path + "/" + path;
}
newPath.append(RepositoryAddress.escapePath(path));
URI uri = this.specific.resolve(newPath.toString());
return new RepositoryAddress(uri, this.specific, this.workspace, path);
}
public String getWorkspace() {
return "-".equals(this.workspace) ? null : this.workspace;
}
public URI getSpecificURI() {
return this.specific;
}
public String getPath() {
return this.path;
}
public Credentials getCredentials() {
String userinfo = this.uri.getUserInfo();
if (userinfo == null) {
return null;
}
int idx = userinfo.indexOf(58);
if (idx < 0) {
return new SimpleCredentials(userinfo, new char[0]);
}
return new SimpleCredentials(userinfo.substring(0, idx), userinfo.substring(idx + 1).toCharArray());
}
public String toString() {
return this.getURI().toString();
}
public int hashCode() {
return this.getURI().hashCode();
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof RepositoryAddress) {
return this.getURI().equals(((RepositoryAddress)obj).getURI());
}
return false;
}
private static String escapePath(String string) {
try {
byte[] bytes = string.getBytes("utf-8");
StringBuffer out = new StringBuffer(bytes.length);
for (byte aByte : bytes) {
int c = aByte & 255;
if (URISaveEx.get(c) && c != 37) {
out.append((char)c);
continue;
}
out.append('%');
out.append(hexTable[c >> 4 & 15]);
out.append(hexTable[c & 15]);
}
return out.toString();
}
catch (UnsupportedEncodingException e) {
throw new InternalError(e.toString());
}
}
static {
int i;
URISaveEx = new BitSet(256);
for (i = 97; i <= 122; ++i) {
URISaveEx.set(i);
}
for (i = 65; i <= 90; ++i) {
URISaveEx.set(i);
}
for (i = 48; i <= 57; ++i) {
URISaveEx.set(i);
}
URISaveEx.set(45);
URISaveEx.set(95);
URISaveEx.set(46);
URISaveEx.set(33);
URISaveEx.set(126);
URISaveEx.set(42);
URISaveEx.set(39);
URISaveEx.set(40);
URISaveEx.set(41);
URISaveEx.set(47);
hexTable = "0123456789abcdef".toCharArray();
}
}