##### Coverage Report - org.apache.any23.source.HTTPDocumentSource

Classes in this File | Line Coverage | Branch Coverage | Complexity
HTTPDocumentSource |
0% | 0/23
0% | 0/6
| 1.625;1.625

1 |  |
     /*
2 |  |
      * Licensed to the Apache Software Foundation (ASF) under one or more
3 |  |
      * contributor license agreements.  See the NOTICE file distributed with
4 |  |
      * this work for additional information regarding copyright ownership.
5 |  |
      * The ASF licenses this file to You under the Apache License, Version 2.0
6 |  |
      * (the "License"); you may not use this file except in compliance with
7 |  |
      * the License.  You may obtain a copy of the License at
8 |  |
      *
9 |  |
      *  http://www.apache.org/licenses/LICENSE-2.0
10 |  |
      *
11 |  |
      * Unless required by applicable law or agreed to in writing, software
12 |  |
      * distributed under the License is distributed on an "AS IS" BASIS,
13 |  |
      * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 |  |
      * See the License for the specific language governing permissions and
15 |  |
      * limitations under the License.
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 |  |
      * Http implementation of {@link DocumentSource}.
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 |  |
     }