예제 #1
0
  /**
   * Opens a new HTTP stream for reading, i.e. a GET request.
   *
   * @param path the URL for the stream
   * @return the opened stream
   */
  static HttpStreamWrapper openRead(HttpPath path) throws IOException {
    HttpStream stream = createStream(path);
    stream._isPost = false;

    HttpStreamWrapper wrapper = new HttpStreamWrapper(stream);

    String status = (String) wrapper.getAttribute("status");

    // ioc/23p0
    if ("404".equals(status)) {
      throw new FileNotFoundException(L.l("'{0}' returns a HTTP 404.", path.getURL()));
    }

    return wrapper;
  }
예제 #2
0
파일: HttpPath.java 프로젝트: dlitz/resin
 /** Returns a read/write pair for a POST request. */
 public StreamImpl openReadWriteImpl() throws IOException {
   return HttpStream.openReadWrite(this);
 }
예제 #3
0
파일: HttpPath.java 프로젝트: dlitz/resin
 /** Returns a read stream for a GET request. */
 @Override
 public StreamImpl openReadImpl() throws IOException {
   return HttpStream.openRead(this);
 }
예제 #4
0
  /** Close the connection. */
  public void close() throws IOException {
    if (_isKeepalive) {
      // If recycling, read any unread data
      if (!_didGet) getConnInput();

      if (!_isRequestDone) {
        if (_tempBuffer == null) _tempBuffer = new byte[256];

        try {
          while (read(_tempBuffer, 0, _tempBuffer.length) > 0) {}
        } catch (IOException e) {
          _isKeepalive = false;
        }
      }
    }

    if (_isKeepalive) {
      HttpStream oldSaved;

      long now;

      now = CurrentTime.getCurrentTime();

      synchronized (LOCK) {
        oldSaved = _savedStream;
        _savedStream = this;
        _saveTime = now;
      }

      if (oldSaved != null && oldSaved != this) {
        oldSaved._isKeepalive = false;
        oldSaved.close();
      }

      return;
    }

    try {
      try {
        if (_ws != null) _ws.close();
      } catch (Throwable e) {
      }
      _ws = null;

      try {
        if (_rs != null) _rs.close();
      } catch (Throwable e) {
      }
      _rs = null;

      try {
        if (_os != null) _os.close();
      } catch (Throwable e) {
      }
      _os = null;

      try {
        if (_is != null) _is.close();
      } catch (Throwable e) {
      }
      _is = null;
    } finally {
      if (_s != null) _s.close();
      _s = null;
    }
  }
예제 #5
0
  /**
   * Creates a new HTTP stream. If there is a saved connection to the same host, use it.
   *
   * @param path the URL for the stream
   * @return the opened stream
   */
  private static HttpStream createStream(HttpPath path) throws IOException {
    String host = path.getHost();
    int port = path.getPort();

    HttpStream stream = null;
    long streamTime = 0;
    synchronized (LOCK) {
      if (_savedStream != null
          && host.equals(_savedStream.getHost())
          && port == _savedStream.getPort()) {
        stream = _savedStream;
        streamTime = _saveTime;
        _savedStream = null;
      }
    }

    if (stream != null) {
      long now;

      now = CurrentTime.getCurrentTime();

      if (now < streamTime + 5000) {
        // if the stream is still valid, use it
        stream.init(path);
        return stream;
      } else {
        // if the stream has timed out, close it
        try {
          stream._isKeepalive = false;
          stream.close();
        } catch (IOException e) {
          log.log(Level.FINE, e.toString(), e);
        }
      }
    }

    Socket s;

    try {
      s = new Socket(host, port);

      if (path instanceof HttpsPath) {
        SSLContext context = SSLContext.getInstance("TLSv1");

        javax.net.ssl.TrustManager tm =
            new javax.net.ssl.X509TrustManager() {
              public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
              }

              public void checkClientTrusted(
                  java.security.cert.X509Certificate[] cert, String foo) {}

              public void checkServerTrusted(
                  java.security.cert.X509Certificate[] cert, String foo) {}
            };

        context.init(null, new javax.net.ssl.TrustManager[] {tm}, null);
        SSLSocketFactory factory = context.getSocketFactory();

        SSLSocket sslSock = (SSLSocket) factory.createSocket(s, host, port, true);

        s = sslSock;
      }
    } catch (ConnectException e) {
      throw new ConnectException(path.getURL() + ": " + e.getMessage());
    } catch (Exception e) {
      throw new ConnectException(path.getURL() + ": " + e.toString());
    }

    int socketTimeout = 300 * 1000;

    try {
      s.setSoTimeout(socketTimeout);
    } catch (Exception e) {
    }

    return new HttpStream(path, host, port, s);
  }
예제 #6
0
  /**
   * Opens a new HTTP stream for reading and writing, i.e. a POST request.
   *
   * @param path the URL for the stream
   * @return the opened stream
   */
  static HttpStreamWrapper openReadWrite(HttpPath path) throws IOException {
    HttpStream stream = createStream(path);
    stream._isPost = true;

    return new HttpStreamWrapper(stream);
  }