|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| package org.apache.xindice.core.request; |
|
21 |
| |
|
22 |
| import org.apache.commons.logging.Log; |
|
23 |
| import org.apache.commons.logging.LogFactory; |
|
24 |
| import org.apache.xindice.core.Collection; |
|
25 |
| import org.apache.xindice.core.Container; |
|
26 |
| import org.apache.xindice.core.DBException; |
|
27 |
| import org.apache.xindice.core.Database; |
|
28 |
| import org.apache.xindice.core.FaultCodes; |
|
29 |
| import org.apache.xindice.util.ObjectPool; |
|
30 |
| import org.apache.xindice.util.Poolable; |
|
31 |
| import org.apache.xindice.util.XindiceException; |
|
32 |
| import org.apache.xindice.xml.TextWriter; |
|
33 |
| |
|
34 |
| import org.w3c.dom.Document; |
|
35 |
| |
|
36 |
| import java.io.ByteArrayInputStream; |
|
37 |
| import java.io.IOException; |
|
38 |
| import java.io.InputStream; |
|
39 |
| import java.net.URL; |
|
40 |
| import java.net.URLConnection; |
|
41 |
| import java.util.Properties; |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| public final class URIMapper extends URLConnection implements Poolable { |
|
52 |
| |
|
53 |
| private static final Log log = LogFactory.getLog(URIMapper.class); |
|
54 |
| |
|
55 |
| public static final int UNKNOWN = -1; |
|
56 |
| public static final int APPLICATION = 1; |
|
57 |
| public static final int COLLECTION = 2; |
|
58 |
| public static final int DOCUMENT = 3; |
|
59 |
| |
|
60 |
| public static final String DEFAULT_ENCODING = "UTF-8"; |
|
61 |
| |
|
62 |
| private ObjectPool pool = null; |
|
63 |
| private String uri = null; |
|
64 |
| private int type = 0; |
|
65 |
| |
|
66 |
| private byte[] buf = null; |
|
67 |
| private int pos = 0; |
|
68 |
| private char lastChar = 0; |
|
69 |
| |
|
70 |
| private Database db = null; |
|
71 |
| private Collection collection = null; |
|
72 |
| private Document document = null; |
|
73 |
| private String method = null; |
|
74 |
| private Container container = null; |
|
75 |
| |
|
76 |
| private Properties params = null; |
|
77 |
| private String[] args = null; |
|
78 |
| |
|
79 |
| private String urlresult = null; |
|
80 |
| private boolean inputstreamset = false; |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
0
| public URIMapper(URL u) {
|
|
86 |
0
| super(u);
|
|
87 |
0
| try {
|
|
88 |
0
| setURI(u.toString());
|
|
89 |
| } catch (Exception e) { |
|
90 |
| |
|
91 |
0
| if (log.isWarnEnabled()) {
|
|
92 |
0
| log.warn("ignored exception", e);
|
|
93 |
| } |
|
94 |
| } |
|
95 |
| } |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
0
| public URIMapper(String uri) throws XindiceException {
|
|
101 |
0
| super(null);
|
|
102 |
0
| setURI(uri);
|
|
103 |
| } |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
0
| public URIMapper() {
|
|
109 |
0
| super(null);
|
|
110 |
| } |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
0
| public void connect() {
|
|
117 |
0
| this.connected = true;
|
|
118 |
| } |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
0
| public InputStream getInputStream() throws IOException {
|
|
124 |
| |
|
125 |
0
| String output = null;
|
|
126 |
| |
|
127 |
| |
|
128 |
0
| if (!inputstreamset) {
|
|
129 |
0
| try {
|
|
130 |
0
| switch (type) {
|
|
131 |
| |
|
132 |
0
| case URIMapper.DOCUMENT:
|
|
133 |
0
| output = TextWriter.toString(getDocument());
|
|
134 |
0
| break;
|
|
135 |
| |
|
136 |
0
| default :
|
|
137 |
| |
|
138 |
0
| throw new Exception("Content type unsupported");
|
|
139 |
| } |
|
140 |
| } catch (Exception e) { |
|
141 |
0
| if (log.isWarnEnabled()) {
|
|
142 |
0
| log.warn("ignored exception", e);
|
|
143 |
| } |
|
144 |
| } |
|
145 |
| } |
|
146 |
| |
|
147 |
| else { |
|
148 |
0
| output = urlresult;
|
|
149 |
| } |
|
150 |
| |
|
151 |
| |
|
152 |
0
| urlresult = output;
|
|
153 |
| |
|
154 |
0
| inputstreamset = true;
|
|
155 |
| |
|
156 |
| |
|
157 |
0
| if (output == null) {
|
|
158 |
0
| return new ByteArrayInputStream(new byte[0]);
|
|
159 |
| } else { |
|
160 |
0
| return new ByteArrayInputStream(output.getBytes(DEFAULT_ENCODING));
|
|
161 |
| } |
|
162 |
| } |
|
163 |
| |
|
164 |
| |
|
165 |
| |
|
166 |
| |
|
167 |
| |
|
168 |
0
| public String getContentEncoding() {
|
|
169 |
0
| return DEFAULT_ENCODING;
|
|
170 |
| } |
|
171 |
| |
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
0
| public String getContentType() {
|
|
177 |
| |
|
178 |
0
| return "text/xml";
|
|
179 |
| } |
|
180 |
| |
|
181 |
| |
|
182 |
| |
|
183 |
| |
|
184 |
| |
|
185 |
0
| public int getContentLength() {
|
|
186 |
| |
|
187 |
0
| if (!inputstreamset) {
|
|
188 |
0
| try {
|
|
189 |
0
| this.getInputStream();
|
|
190 |
| } catch (IOException e) { |
|
191 |
0
| return 0;
|
|
192 |
| } |
|
193 |
| } |
|
194 |
| |
|
195 |
| |
|
196 |
0
| return urlresult.length();
|
|
197 |
| } |
|
198 |
| |
|
199 |
| |
|
200 |
| |
|
201 |
| |
|
202 |
| |
|
203 |
0
| public long getLastModified() {
|
|
204 |
| |
|
205 |
0
| return 0;
|
|
206 |
| } |
|
207 |
| |
|
208 |
0
| public void setPool(ObjectPool pool) {
|
|
209 |
0
| this.pool = pool;
|
|
210 |
| } |
|
211 |
| |
|
212 |
0
| public void reclaim() {
|
|
213 |
0
| reset();
|
|
214 |
0
| if (pool != null) {
|
|
215 |
0
| pool.putObject(this);
|
|
216 |
| } |
|
217 |
| } |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
0
| private void reset() {
|
|
223 |
0
| type = UNKNOWN;
|
|
224 |
0
| lastChar = 0;
|
|
225 |
0
| method = "";
|
|
226 |
0
| db = null;
|
|
227 |
0
| collection = null;
|
|
228 |
0
| document = null;
|
|
229 |
0
| params = null;
|
|
230 |
0
| args = null;
|
|
231 |
0
| container = null;
|
|
232 |
| |
|
233 |
| |
|
234 |
0
| urlresult = null;
|
|
235 |
0
| inputstreamset = false;
|
|
236 |
| } |
|
237 |
| |
|
238 |
| |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
0
| public void setURI(String uri) throws XindiceException {
|
|
246 |
0
| this.uri = uri;
|
|
247 |
0
| reset();
|
|
248 |
0
| parse();
|
|
249 |
| } |
|
250 |
| |
|
251 |
| |
|
252 |
| |
|
253 |
| |
|
254 |
| |
|
255 |
| |
|
256 |
| |
|
257 |
0
| private String parseName(String delims) {
|
|
258 |
0
| int start = pos;
|
|
259 |
0
| while (pos < buf.length) {
|
|
260 |
0
| lastChar = (char) buf[pos++];
|
|
261 |
0
| if (delims.indexOf(lastChar) != -1) {
|
|
262 |
0
| break;
|
|
263 |
| } |
|
264 |
| } |
|
265 |
0
| if (pos == buf.length && delims.indexOf(lastChar) == -1) {
|
|
266 |
0
| pos++;
|
|
267 |
| } |
|
268 |
| |
|
269 |
0
| return pos > start ? new String(buf, start, pos - start - 1) : "";
|
|
270 |
| } |
|
271 |
| |
|
272 |
| |
|
273 |
| |
|
274 |
| |
|
275 |
| |
|
276 |
0
| private void parseParams() {
|
|
277 |
0
| if (lastChar == '?') {
|
|
278 |
| |
|
279 |
0
| params = new Properties();
|
|
280 |
0
| String name;
|
|
281 |
0
| String value;
|
|
282 |
0
| String temp;
|
|
283 |
0
| while (true) {
|
|
284 |
0
| name = parseName("=");
|
|
285 |
0
| if (name.length() == 0) {
|
|
286 |
0
| break;
|
|
287 |
| } |
|
288 |
0
| value = parseName("?&;");
|
|
289 |
0
| temp = params.getProperty(name);
|
|
290 |
0
| if (temp != null) {
|
|
291 |
0
| StringBuffer sb = new StringBuffer(32);
|
|
292 |
0
| sb.append(temp);
|
|
293 |
0
| sb.append('\u0001');
|
|
294 |
0
| sb.append(value);
|
|
295 |
0
| value = sb.toString();
|
|
296 |
| } |
|
297 |
0
| params.setProperty(name, value);
|
|
298 |
| } |
|
299 |
| } else |
|
300 |
0
| params = new Properties();
|
|
301 |
| } |
|
302 |
| |
|
303 |
| |
|
304 |
| |
|
305 |
| |
|
306 |
0
| private void parse() throws XindiceException {
|
|
307 |
0
| buf = uri.getBytes();
|
|
308 |
0
| pos = 0;
|
|
309 |
0
| String tmp;
|
|
310 |
| |
|
311 |
0
| if (buf.length == 0) {
|
|
312 |
0
| throw new DBException(FaultCodes.URI_EMPTY);
|
|
313 |
| } |
|
314 |
| |
|
315 |
| |
|
316 |
0
| if ((char) buf[0] != '/') {
|
|
317 |
0
| parseName(":");
|
|
318 |
0
| parseName("/");
|
|
319 |
0
| parseName("/");
|
|
320 |
0
| parseName("/:");
|
|
321 |
0
| if (lastChar == ':') {
|
|
322 |
0
| parseName("/");
|
|
323 |
| } |
|
324 |
| } else { |
|
325 |
0
| pos = 1;
|
|
326 |
| } |
|
327 |
| |
|
328 |
| |
|
329 |
0
| tmp = parseName("/");
|
|
330 |
0
| if (tmp == null) {
|
|
331 |
0
| return;
|
|
332 |
| } |
|
333 |
| |
|
334 |
0
| db = Database.getDatabase(tmp);
|
|
335 |
0
| if (db == null) {
|
|
336 |
0
| return;
|
|
337 |
| } |
|
338 |
| |
|
339 |
0
| type = APPLICATION;
|
|
340 |
0
| tmp = parseName("/(?");
|
|
341 |
| |
|
342 |
0
| int objType = getParsedObjectType(db, tmp);
|
|
343 |
| |
|
344 |
| |
|
345 |
0
| if (objType != UNKNOWN) {
|
|
346 |
0
| type = walkURI(db, tmp, objType);
|
|
347 |
| } |
|
348 |
| |
|
349 |
0
| if (lastChar == '?') {
|
|
350 |
0
| parseParams();
|
|
351 |
0
| return;
|
|
352 |
| } |
|
353 |
| } |
|
354 |
| |
|
355 |
| |
|
356 |
| |
|
357 |
| |
|
358 |
| |
|
359 |
0
| protected int walkURI(Collection col, String name, int objType) throws XindiceException {
|
|
360 |
0
| switch (objType) {
|
|
361 |
0
| case DOCUMENT:
|
|
362 |
0
| container = col.getContainer(name);
|
|
363 |
0
| document = container.getDocument();
|
|
364 |
0
| return DOCUMENT;
|
|
365 |
| |
|
366 |
0
| case COLLECTION:
|
|
367 |
0
| Collection c = col.getCollection(name);
|
|
368 |
0
| if (c != null) {
|
|
369 |
0
| collection = c;
|
|
370 |
0
| String tmp = parseName("/(?");
|
|
371 |
| |
|
372 |
0
| if (!tmp.equals("")) {
|
|
373 |
0
| return walkURI(c, tmp, getParsedObjectType(c, tmp));
|
|
374 |
| } |
|
375 |
| |
|
376 |
0
| return COLLECTION;
|
|
377 |
| } |
|
378 |
| |
|
379 |
0
| default:
|
|
380 |
0
| if (log.isWarnEnabled()) {
|
|
381 |
0
| log.warn("invalid object type : " + objType);
|
|
382 |
| } |
|
383 |
| } |
|
384 |
| |
|
385 |
0
| return UNKNOWN;
|
|
386 |
| } |
|
387 |
| |
|
388 |
| |
|
389 |
| |
|
390 |
| |
|
391 |
| |
|
392 |
0
| protected int getParsedObjectType(Collection col, String name) throws XindiceException {
|
|
393 |
| |
|
394 |
0
| if (col.getCollection(name) != null) {
|
|
395 |
0
| return COLLECTION;
|
|
396 |
0
| } else if ((col.getFiler() != null) && (col.getContainer(name) != null)) {
|
|
397 |
0
| return DOCUMENT;
|
|
398 |
| } else { |
|
399 |
0
| return UNKNOWN;
|
|
400 |
| } |
|
401 |
| } |
|
402 |
| |
|
403 |
| |
|
404 |
| |
|
405 |
| |
|
406 |
| |
|
407 |
| |
|
408 |
| |
|
409 |
| |
|
410 |
| |
|
411 |
0
| public int getObjectType() {
|
|
412 |
0
| return type;
|
|
413 |
| } |
|
414 |
| |
|
415 |
| |
|
416 |
| |
|
417 |
| |
|
418 |
| |
|
419 |
| |
|
420 |
| |
|
421 |
0
| public Database getDatabase() {
|
|
422 |
0
| return db;
|
|
423 |
| } |
|
424 |
| |
|
425 |
| |
|
426 |
| |
|
427 |
| |
|
428 |
| |
|
429 |
| |
|
430 |
| |
|
431 |
| |
|
432 |
0
| public Collection getCollection() {
|
|
433 |
0
| return collection;
|
|
434 |
| } |
|
435 |
| |
|
436 |
| |
|
437 |
| |
|
438 |
| |
|
439 |
| |
|
440 |
| |
|
441 |
| |
|
442 |
| |
|
443 |
0
| public Document getDocument() {
|
|
444 |
0
| return document;
|
|
445 |
| } |
|
446 |
| |
|
447 |
| |
|
448 |
| |
|
449 |
| |
|
450 |
| |
|
451 |
| |
|
452 |
| |
|
453 |
| |
|
454 |
0
| public Container getContainer() {
|
|
455 |
0
| return container;
|
|
456 |
| } |
|
457 |
| |
|
458 |
| |
|
459 |
| |
|
460 |
| |
|
461 |
| |
|
462 |
| |
|
463 |
| |
|
464 |
| |
|
465 |
0
| public String getMethod() {
|
|
466 |
0
| return method;
|
|
467 |
| } |
|
468 |
| |
|
469 |
| |
|
470 |
| |
|
471 |
| |
|
472 |
| |
|
473 |
| |
|
474 |
| |
|
475 |
| |
|
476 |
| |
|
477 |
0
| public Properties getProperties() {
|
|
478 |
0
| return params;
|
|
479 |
| } |
|
480 |
| |
|
481 |
| |
|
482 |
| |
|
483 |
| |
|
484 |
| |
|
485 |
| |
|
486 |
| |
|
487 |
| |
|
488 |
| |
|
489 |
0
| public String[] getArguments() {
|
|
490 |
0
| return args;
|
|
491 |
| } |
|
492 |
| } |