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)
    62              return;
    63          loaded = true;
    64          unusedInputStream = client.openInputStream(uri);
    65          if (client.getActualDocumentIRI() != null) {
    66              uri = client.getActualDocumentIRI();
    67          }
    68      }
    69
    70      public InputStream openInputStream() throws IOException {
    71          ensureOpen();
    72          if (unusedInputStream != null) {
    73              InputStream temp = unusedInputStream;
    74              unusedInputStream = null;
    75              return temp;
    76          }
    77          return client.openInputStream(uri);
    78      }
    79
    80      public long getContentLength() {
    81          return client.getContentLength();
    82      }
    83
    84      public String getDocumentIRI() {
    85          return uri;
    86      }
    87
    88      public String getContentType() {
    89          return client.getContentType();
    90      }
    91
    92      public boolean isLocal() {
    93          return false;
    94      }
    95
    96  }