1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.any23.source; |
19 | |
|
20 | |
import org.apache.any23.http.HTTPClient; |
21 | |
|
22 | |
import java.io.IOException; |
23 | |
import java.io.InputStream; |
24 | |
import java.net.URI; |
25 | |
import java.net.URISyntaxException; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
public class HTTPDocumentSource implements DocumentSource { |
31 | |
|
32 | |
private final HTTPClient client; |
33 | |
|
34 | |
private String uri; |
35 | |
|
36 | 0 | private InputStream unusedInputStream = null; |
37 | |
|
38 | 0 | private boolean loaded = false; |
39 | |
|
40 | 0 | public HTTPDocumentSource(HTTPClient client, String uri) throws URISyntaxException { |
41 | 0 | this.client = client; |
42 | 0 | this.uri = normalize(uri); |
43 | 0 | } |
44 | |
|
45 | |
private String normalize(String uri) throws URISyntaxException { |
46 | 0 | return new URI(uri).normalize().toString(); |
47 | |
} |
48 | |
|
49 | |
private void ensureOpen() throws IOException { |
50 | 0 | if (loaded) return; |
51 | 0 | loaded = true; |
52 | 0 | unusedInputStream = client.openInputStream(uri); |
53 | 0 | if (client.getActualDocumentURI() != null) { |
54 | 0 | uri = client.getActualDocumentURI(); |
55 | |
} |
56 | 0 | } |
57 | |
|
58 | |
public InputStream openInputStream() throws IOException { |
59 | 0 | ensureOpen(); |
60 | 0 | if (unusedInputStream != null) { |
61 | 0 | InputStream temp = unusedInputStream; |
62 | 0 | unusedInputStream = null; |
63 | 0 | return temp; |
64 | |
} |
65 | 0 | return client.openInputStream(uri); |
66 | |
} |
67 | |
|
68 | |
public long getContentLength() { |
69 | 0 | return client.getContentLength(); |
70 | |
} |
71 | |
|
72 | |
public String getDocumentURI() { |
73 | 0 | return uri; |
74 | |
} |
75 | |
|
76 | |
public String getContentType() { |
77 | 0 | return client.getContentType(); |
78 | |
} |
79 | |
|
80 | |
public boolean isLocal() { |
81 | 0 | return false; |
82 | |
} |
83 | |
|
84 | |
} |