Example #1
0
  @Override
  public InputStream getContent() throws IOException {
    InputStream in;

    try {
      successful = true;
      in = connection.getInputStream();
    } catch (IOException e) {
      successful = false;
      in = connection.getErrorStream();
      if (in == null) {
        throw e;
      }
    }

    String encoding = connection.getHeaderField("Content-Encoding");

    if (config.getMaxResponseSize() > 0) {
      in = new LimitingInputStream(config.getMaxResponseSize(), in);
    }

    if ("gzip".equals(encoding)) {
      in = new GZIPInputStream(in);
    }

    if (config.hasMessageHandlers() || config.isTraceMessage()) {
      byte[] bytes = FileUtil.toBytes(in);
      in = new ByteArrayInputStream(bytes);

      if (config.hasMessageHandlers()) {
        Iterator<MessageHandler> it = config.getMessagerHandlers();
        while (it.hasNext()) {
          MessageHandler handler = it.next();
          if (handler instanceof MessageHandlerWithHeaders) {
            ((MessageHandlerWithHeaders) handler)
                .handleResponse(url, bytes, connection.getHeaderFields());
          } else {
            handler.handleResponse(url, bytes);
          }
        }
      }

      if (config.isTraceMessage()) {
        new TeeInputStream(config, bytes);
      }
    }

    return in;
  }