View Javadoc
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  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.net.URI;
27  import java.net.URISyntaxException;
28  
29  /**
30   * Http implementation of {@link DocumentSource}.
31   */
32  public class HTTPDocumentSource implements DocumentSource {
33  
34      private static final Logger LOG = LoggerFactory.getLogger(HTTPDocumentSource.class);
35  
36      private final HTTPClient client;
37  
38      private String uri;
39  
40      private InputStream unusedInputStream = null;
41  
42      private boolean loaded = false;
43  
44      public HTTPDocumentSource(HTTPClient client, String uri) throws URISyntaxException {
45          this.client = client;
46          this.uri = normalize(uri);
47      }
48  
49      private String normalize(String uri) throws URISyntaxException {
50          try {
51              URI normalized = new URI(uri).normalize();
52              return normalized.toString();
53          } catch (URISyntaxException e) {
54              LOG.warn("Invalid uri: {}", uri);
55              LOG.error("Can not convert URL", e);
56              throw e;
57          }
58      }
59  
60      private void ensureOpen() throws IOException {
61          if (loaded) return;
62          loaded = true;
63          unusedInputStream = client.openInputStream(uri);
64          if (client.getActualDocumentIRI() != null) {
65              uri = client.getActualDocumentIRI();
66          }
67      }
68  
69      public InputStream openInputStream() throws IOException {
70          ensureOpen();
71          if (unusedInputStream != null) {
72              InputStream temp = unusedInputStream;
73              unusedInputStream = null;
74              return temp;
75          }
76          return client.openInputStream(uri);
77      }
78  
79      public long getContentLength() {
80          return client.getContentLength();
81      }
82  
83      public String getDocumentIRI() {
84          return uri;
85      }
86  
87      public String getContentType() {
88          return client.getContentType();
89      }
90  
91      public boolean isLocal() {
92          return false;
93      }
94  
95  }