com.squareup.okhttp.Connection Java Examples

The following examples show how to use com.squareup.okhttp.Connection. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpTransport.java    From wildfly-samples with MIT License 6 votes vote down vote up
/**
 * Discards the response body so that the connection can be reused. This
 * needs to be done judiciously, since it delays the current request in
 * order to speed up a potential future request that may never occur.
 *
 * <p>A stream may be discarded to encourage response caching (a response
 * cannot be cached unless it is consumed completely) or to enable connection
 * reuse.
 */
private static boolean discardStream(HttpEngine httpEngine, InputStream responseBodyIn) {
  Connection connection = httpEngine.connection;
  if (connection == null) return false;
  Socket socket = connection.getSocket();
  if (socket == null) return false;
  try {
    int socketTimeout = socket.getSoTimeout();
    socket.setSoTimeout(DISCARD_STREAM_TIMEOUT_MILLIS);
    try {
      Util.skipAll(responseBodyIn);
      return true;
    } finally {
      socket.setSoTimeout(socketTimeout);
    }
  } catch (IOException e) {
    return false;
  }
}
 
Example #2
Source File: HttpTransport.java    From crosswalk-cordova-android with Apache License 2.0 6 votes vote down vote up
/**
 * Discards the response body so that the connection can be reused. This
 * needs to be done judiciously, since it delays the current request in
 * order to speed up a potential future request that may never occur.
 *
 * <p>A stream may be discarded to encourage response caching (a response
 * cannot be cached unless it is consumed completely) or to enable connection
 * reuse.
 */
private static boolean discardStream(HttpEngine httpEngine, InputStream responseBodyIn) {
  Connection connection = httpEngine.connection;
  if (connection == null) return false;
  Socket socket = connection.getSocket();
  if (socket == null) return false;
  try {
    int socketTimeout = socket.getSoTimeout();
    socket.setSoTimeout(DISCARD_STREAM_TIMEOUT_MILLIS);
    try {
      Util.skipAll(responseBodyIn);
      return true;
    } finally {
      socket.setSoTimeout(socketTimeout);
    }
  } catch (IOException e) {
    return false;
  }
}
 
Example #3
Source File: HttpEngine.java    From phonegapbootcampsite with MIT License 6 votes vote down vote up
/**
 * @param requestHeaders the client's supplied request headers. This class
 *     creates a private copy that it can mutate.
 * @param connection the connection used for an intermediate response
 *     immediately prior to this request/response pair, such as a same-host
 *     redirect. This engine assumes ownership of the connection and must
 *     release it when it is unneeded.
 */
public HttpEngine(OkHttpClient client, Policy policy, String method, RawHeaders requestHeaders,
    Connection connection, RetryableOutputStream requestBodyOut) throws IOException {
  this.client = client;
  this.policy = policy;
  this.method = method;
  this.connection = connection;
  this.requestBodyOut = requestBodyOut;

  try {
    uri = Platform.get().toUriLenient(policy.getURL());
  } catch (URISyntaxException e) {
    throw new IOException(e.getMessage());
  }

  this.requestHeaders = new RequestHeaders(uri, new RawHeaders(requestHeaders));
}
 
Example #4
Source File: HttpEngine.java    From crosswalk-cordova-android with Apache License 2.0 6 votes vote down vote up
/**
 * @param requestHeaders the client's supplied request headers. This class
 *     creates a private copy that it can mutate.
 * @param connection the connection used for an intermediate response
 *     immediately prior to this request/response pair, such as a same-host
 *     redirect. This engine assumes ownership of the connection and must
 *     release it when it is unneeded.
 */
public HttpEngine(OkHttpClient client, Policy policy, String method, RawHeaders requestHeaders,
    Connection connection, RetryableOutputStream requestBodyOut) throws IOException {
  this.client = client;
  this.policy = policy;
  this.method = method;
  this.connection = connection;
  this.requestBodyOut = requestBodyOut;

  try {
    uri = Platform.get().toUriLenient(policy.getURL());
  } catch (URISyntaxException e) {
    throw new IOException(e.getMessage());
  }

  this.requestHeaders = new RequestHeaders(uri, new RawHeaders(requestHeaders));
}
 
Example #5
Source File: HttpTransport.java    From cordova-android-chromeview with Apache License 2.0 6 votes vote down vote up
/**
 * Discards the response body so that the connection can be reused. This
 * needs to be done judiciously, since it delays the current request in
 * order to speed up a potential future request that may never occur.
 */
private static boolean discardStream(HttpEngine httpEngine, InputStream responseBodyIn) {
  Connection connection = httpEngine.connection;
  if (connection == null) return false;
  Socket socket = connection.getSocket();
  if (socket == null) return false;
  try {
    int socketTimeout = socket.getSoTimeout();
    socket.setSoTimeout(DISCARD_STREAM_TIMEOUT_MILLIS);
    try {
      Util.skipAll(responseBodyIn);
      return true;
    } finally {
      socket.setSoTimeout(socketTimeout);
    }
  } catch (IOException e) {
    return false;
  }
}
 
Example #6
Source File: HttpEngine.java    From L.TileLayer.Cordova with MIT License 6 votes vote down vote up
/**
 * @param requestHeaders the client's supplied request headers. This class
 *     creates a private copy that it can mutate.
 * @param connection the connection used for an intermediate response
 *     immediately prior to this request/response pair, such as a same-host
 *     redirect. This engine assumes ownership of the connection and must
 *     release it when it is unneeded.
 */
public HttpEngine(OkHttpClient client, Policy policy, String method, RawHeaders requestHeaders,
    Connection connection, RetryableOutputStream requestBodyOut) throws IOException {
  this.client = client;
  this.policy = policy;
  this.method = method;
  this.connection = connection;
  this.requestBodyOut = requestBodyOut;

  try {
    uri = Platform.get().toUriLenient(policy.getURL());
  } catch (URISyntaxException e) {
    throw new IOException(e.getMessage());
  }

  this.requestHeaders = new RequestHeaders(uri, new RawHeaders(requestHeaders));
}
 
Example #7
Source File: HttpEngine.java    From wildfly-samples with MIT License 6 votes vote down vote up
/**
 * @param requestHeaders the client's supplied request headers. This class
 *     creates a private copy that it can mutate.
 * @param connection the connection used for an intermediate response
 *     immediately prior to this request/response pair, such as a same-host
 *     redirect. This engine assumes ownership of the connection and must
 *     release it when it is unneeded.
 */
public HttpEngine(OkHttpClient client, Policy policy, String method, RawHeaders requestHeaders,
    Connection connection, RetryableOutputStream requestBodyOut) throws IOException {
  this.client = client;
  this.policy = policy;
  this.method = method;
  this.connection = connection;
  this.requestBodyOut = requestBodyOut;

  try {
    uri = Platform.get().toUriLenient(policy.getURL());
  } catch (URISyntaxException e) {
    throw new IOException(e.getMessage());
  }

  this.requestHeaders = new RequestHeaders(uri, new RawHeaders(requestHeaders));
}
 
Example #8
Source File: HttpTransport.java    From CordovaYoutubeVideoPlayer with MIT License 6 votes vote down vote up
/**
 * Discards the response body so that the connection can be reused. This
 * needs to be done judiciously, since it delays the current request in
 * order to speed up a potential future request that may never occur.
 *
 * <p>A stream may be discarded to encourage response caching (a response
 * cannot be cached unless it is consumed completely) or to enable connection
 * reuse.
 */
private static boolean discardStream(HttpEngine httpEngine, InputStream responseBodyIn) {
  Connection connection = httpEngine.connection;
  if (connection == null) return false;
  Socket socket = connection.getSocket();
  if (socket == null) return false;
  try {
    int socketTimeout = socket.getSoTimeout();
    socket.setSoTimeout(DISCARD_STREAM_TIMEOUT_MILLIS);
    try {
      Util.skipAll(responseBodyIn);
      return true;
    } finally {
      socket.setSoTimeout(socketTimeout);
    }
  } catch (IOException e) {
    return false;
  }
}
 
Example #9
Source File: HttpTransport.java    From bluemix-parking-meter with MIT License 6 votes vote down vote up
/**
 * Discards the response body so that the connection can be reused. This
 * needs to be done judiciously, since it delays the current request in
 * order to speed up a potential future request that may never occur.
 *
 * <p>A stream may be discarded to encourage response caching (a response
 * cannot be cached unless it is consumed completely) or to enable connection
 * reuse.
 */
private static boolean discardStream(HttpEngine httpEngine, InputStream responseBodyIn) {
  Connection connection = httpEngine.connection;
  if (connection == null) return false;
  Socket socket = connection.getSocket();
  if (socket == null) return false;
  try {
    int socketTimeout = socket.getSoTimeout();
    socket.setSoTimeout(DISCARD_STREAM_TIMEOUT_MILLIS);
    try {
      Util.skipAll(responseBodyIn);
      return true;
    } finally {
      socket.setSoTimeout(socketTimeout);
    }
  } catch (IOException e) {
    return false;
  }
}
 
Example #10
Source File: HttpTransport.java    From phonegap-plugin-loading-spinner with Apache License 2.0 6 votes vote down vote up
/**
 * Discards the response body so that the connection can be reused. This
 * needs to be done judiciously, since it delays the current request in
 * order to speed up a potential future request that may never occur.
 */
private static boolean discardStream(HttpEngine httpEngine, InputStream responseBodyIn) {
  Connection connection = httpEngine.connection;
  if (connection == null) return false;
  Socket socket = connection.getSocket();
  if (socket == null) return false;
  try {
    int socketTimeout = socket.getSoTimeout();
    socket.setSoTimeout(DISCARD_STREAM_TIMEOUT_MILLIS);
    try {
      Util.skipAll(responseBodyIn);
      return true;
    } finally {
      socket.setSoTimeout(socketTimeout);
    }
  } catch (IOException e) {
    return false;
  }
}
 
Example #11
Source File: HttpEngine.java    From bluemix-parking-meter with MIT License 6 votes vote down vote up
/**
 * @param requestHeaders the client's supplied request headers. This class
 *     creates a private copy that it can mutate.
 * @param connection the connection used for an intermediate response
 *     immediately prior to this request/response pair, such as a same-host
 *     redirect. This engine assumes ownership of the connection and must
 *     release it when it is unneeded.
 */
public HttpEngine(OkHttpClient client, Policy policy, String method, RawHeaders requestHeaders,
    Connection connection, RetryableOutputStream requestBodyOut) throws IOException {
  this.client = client;
  this.policy = policy;
  this.method = method;
  this.connection = connection;
  this.requestBodyOut = requestBodyOut;

  try {
    uri = Platform.get().toUriLenient(policy.getURL());
  } catch (URISyntaxException e) {
    throw new IOException(e.getMessage());
  }

  this.requestHeaders = new RequestHeaders(uri, new RawHeaders(requestHeaders));
}
 
Example #12
Source File: HttpTransport.java    From IoTgo_Android_App with MIT License 6 votes vote down vote up
/**
 * Discards the response body so that the connection can be reused. This
 * needs to be done judiciously, since it delays the current request in
 * order to speed up a potential future request that may never occur.
 *
 * <p>A stream may be discarded to encourage response caching (a response
 * cannot be cached unless it is consumed completely) or to enable connection
 * reuse.
 */
private static boolean discardStream(HttpEngine httpEngine, InputStream responseBodyIn) {
  Connection connection = httpEngine.connection;
  if (connection == null) return false;
  Socket socket = connection.getSocket();
  if (socket == null) return false;
  try {
    int socketTimeout = socket.getSoTimeout();
    socket.setSoTimeout(DISCARD_STREAM_TIMEOUT_MILLIS);
    try {
      Util.skipAll(responseBodyIn);
      return true;
    } finally {
      socket.setSoTimeout(socketTimeout);
    }
  } catch (IOException e) {
    return false;
  }
}
 
Example #13
Source File: HttpTransport.java    From android-discourse with Apache License 2.0 6 votes vote down vote up
/**
 * Discards the response body so that the connection can be reused. This
 * needs to be done judiciously, since it delays the current request in
 * order to speed up a potential future request that may never occur.
 * <p/>
 * <p>A stream may be discarded to encourage response caching (a response
 * cannot be cached unless it is consumed completely) or to enable connection
 * reuse.
 */
private static boolean discardStream(HttpEngine httpEngine, InputStream responseBodyIn) {
    Connection connection = httpEngine.connection;
    if (connection == null)
        return false;
    Socket socket = connection.getSocket();
    if (socket == null)
        return false;
    try {
        int socketTimeout = socket.getSoTimeout();
        socket.setSoTimeout(DISCARD_STREAM_TIMEOUT_MILLIS);
        try {
            Util.skipAll(responseBodyIn);
            return true;
        } finally {
            socket.setSoTimeout(socketTimeout);
        }
    } catch (IOException e) {
        return false;
    }
}
 
Example #14
Source File: HttpTransport.java    From phonegapbootcampsite with MIT License 6 votes vote down vote up
/**
 * Discards the response body so that the connection can be reused. This
 * needs to be done judiciously, since it delays the current request in
 * order to speed up a potential future request that may never occur.
 *
 * <p>A stream may be discarded to encourage response caching (a response
 * cannot be cached unless it is consumed completely) or to enable connection
 * reuse.
 */
private static boolean discardStream(HttpEngine httpEngine, InputStream responseBodyIn) {
  Connection connection = httpEngine.connection;
  if (connection == null) return false;
  Socket socket = connection.getSocket();
  if (socket == null) return false;
  try {
    int socketTimeout = socket.getSoTimeout();
    socket.setSoTimeout(DISCARD_STREAM_TIMEOUT_MILLIS);
    try {
      Util.skipAll(responseBodyIn);
      return true;
    } finally {
      socket.setSoTimeout(socketTimeout);
    }
  } catch (IOException e) {
    return false;
  }
}
 
Example #15
Source File: HttpTransport.java    From L.TileLayer.Cordova with MIT License 6 votes vote down vote up
/**
 * Discards the response body so that the connection can be reused. This
 * needs to be done judiciously, since it delays the current request in
 * order to speed up a potential future request that may never occur.
 *
 * <p>A stream may be discarded to encourage response caching (a response
 * cannot be cached unless it is consumed completely) or to enable connection
 * reuse.
 */
private static boolean discardStream(HttpEngine httpEngine, InputStream responseBodyIn) {
  Connection connection = httpEngine.connection;
  if (connection == null) return false;
  Socket socket = connection.getSocket();
  if (socket == null) return false;
  try {
    int socketTimeout = socket.getSoTimeout();
    socket.setSoTimeout(DISCARD_STREAM_TIMEOUT_MILLIS);
    try {
      Util.skipAll(responseBodyIn);
      return true;
    } finally {
      socket.setSoTimeout(socketTimeout);
    }
  } catch (IOException e) {
    return false;
  }
}
 
Example #16
Source File: HttpEngine.java    From cordova-android-chromeview with Apache License 2.0 6 votes vote down vote up
/**
 * @param requestHeaders the client's supplied request headers. This class
 * creates a private copy that it can mutate.
 * @param connection the connection used for an intermediate response
 * immediately prior to this request/response pair, such as a same-host
 * redirect. This engine assumes ownership of the connection and must
 * release it when it is unneeded.
 */
public HttpEngine(HttpURLConnectionImpl policy, String method, RawHeaders requestHeaders,
    Connection connection, RetryableOutputStream requestBodyOut) throws IOException {
  this.policy = policy;
  this.method = method;
  this.connection = connection;
  this.requestBodyOut = requestBodyOut;

  try {
    uri = Platform.get().toUriLenient(policy.getURL());
  } catch (URISyntaxException e) {
    throw new IOException(e.getMessage());
  }

  this.requestHeaders = new RequestHeaders(uri, new RawHeaders(requestHeaders));
}
 
Example #17
Source File: RouteSelector.java    From bluemix-parking-meter with MIT License 5 votes vote down vote up
/**
 * Clients should invoke this method when they encounter a connectivity
 * failure on a connection returned by this route selector.
 */
public void connectFailed(Connection connection, IOException failure) {
  Route failedRoute = connection.getRoute();
  if (failedRoute.getProxy().type() != Proxy.Type.DIRECT && proxySelector != null) {
    // Tell the proxy selector when we fail to connect on a fresh connection.
    proxySelector.connectFailed(uri, failedRoute.getProxy().address(), failure);
  }

  routeDatabase.failed(failedRoute, failure);
}
 
Example #18
Source File: RouteSelector.java    From reader with MIT License 5 votes vote down vote up
/**
 * Returns the next route address to attempt.
 *
 * @throws NoSuchElementException if there are no more routes to attempt.
 */
public Connection next(String method) throws IOException {
  // Always prefer pooled connections over new connections.
  for (Connection pooled; (pooled = pool.get(address)) != null; ) {
    if (method.equals("GET") || pooled.isReadable()) return pooled;
    pooled.close();
  }

  // Compute the next route to attempt.
  if (!hasNextTlsMode()) {
    if (!hasNextInetSocketAddress()) {
      if (!hasNextProxy()) {
        if (!hasNextPostponed()) {
          throw new NoSuchElementException();
        }
        return new Connection(nextPostponed());
      }
      lastProxy = nextProxy();
      resetNextInetSocketAddress(lastProxy);
    }
    lastInetSocketAddress = nextInetSocketAddress();
    resetNextTlsMode();
  }

  boolean modernTls = nextTlsMode() == TLS_MODE_MODERN;
  Route route = new Route(address, lastProxy, lastInetSocketAddress, modernTls);
  if (routeDatabase.shouldPostpone(route)) {
    postponedRoutes.add(route);
    // We will only recurse in order to skip previously failed routes. They will be
    // tried last.
    return next(method);
  }

  return new Connection(route);
}
 
Example #19
Source File: RouteSelector.java    From reader with MIT License 5 votes vote down vote up
/**
 * Clients should invoke this method when they encounter a connectivity
 * failure on a connection returned by this route selector.
 */
public void connectFailed(Connection connection, IOException failure) {
  Route failedRoute = connection.getRoute();
  if (failedRoute.getProxy().type() != Proxy.Type.DIRECT && proxySelector != null) {
    // Tell the proxy selector when we fail to connect on a fresh connection.
    proxySelector.connectFailed(uri, failedRoute.getProxy().address(), failure);
  }

  routeDatabase.failed(failedRoute, failure);
}
 
Example #20
Source File: RouteSelector.java    From cordova-amazon-fireos with Apache License 2.0 5 votes vote down vote up
/**
 * Clients should invoke this method when they encounter a connectivity
 * failure on a connection returned by this route selector.
 */
public void connectFailed(Connection connection, IOException failure) {
  Route failedRoute = connection.getRoute();
  if (failedRoute.getProxy().type() != Proxy.Type.DIRECT && proxySelector != null) {
    // Tell the proxy selector when we fail to connect on a fresh connection.
    proxySelector.connectFailed(uri, failedRoute.getProxy().address(), failure);
  }

  routeDatabase.failed(failedRoute, failure);
}
 
Example #21
Source File: RouteSelector.java    From CordovaYoutubeVideoPlayer with MIT License 5 votes vote down vote up
/**
 * Returns the next route address to attempt.
 *
 * @throws NoSuchElementException if there are no more routes to attempt.
 */
public Connection next(String method) throws IOException {
  // Always prefer pooled connections over new connections.
  for (Connection pooled; (pooled = pool.get(address)) != null; ) {
    if (method.equals("GET") || pooled.isReadable()) return pooled;
    pooled.close();
  }

  // Compute the next route to attempt.
  if (!hasNextTlsMode()) {
    if (!hasNextInetSocketAddress()) {
      if (!hasNextProxy()) {
        if (!hasNextPostponed()) {
          throw new NoSuchElementException();
        }
        return new Connection(nextPostponed());
      }
      lastProxy = nextProxy();
      resetNextInetSocketAddress(lastProxy);
    }
    lastInetSocketAddress = nextInetSocketAddress();
    resetNextTlsMode();
  }

  boolean modernTls = nextTlsMode() == TLS_MODE_MODERN;
  Route route = new Route(address, lastProxy, lastInetSocketAddress, modernTls);
  if (routeDatabase.shouldPostpone(route)) {
    postponedRoutes.add(route);
    // We will only recurse in order to skip previously failed routes. They will be
    // tried last.
    return next(method);
  }

  return new Connection(route);
}
 
Example #22
Source File: RouteSelector.java    From crosswalk-cordova-android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the next route address to attempt.
 *
 * @throws NoSuchElementException if there are no more routes to attempt.
 */
public Connection next(String method) throws IOException {
  // Always prefer pooled connections over new connections.
  for (Connection pooled; (pooled = pool.get(address)) != null; ) {
    if (method.equals("GET") || pooled.isReadable()) return pooled;
    pooled.close();
  }

  // Compute the next route to attempt.
  if (!hasNextTlsMode()) {
    if (!hasNextInetSocketAddress()) {
      if (!hasNextProxy()) {
        if (!hasNextPostponed()) {
          throw new NoSuchElementException();
        }
        return new Connection(nextPostponed());
      }
      lastProxy = nextProxy();
      resetNextInetSocketAddress(lastProxy);
    }
    lastInetSocketAddress = nextInetSocketAddress();
    resetNextTlsMode();
  }

  boolean modernTls = nextTlsMode() == TLS_MODE_MODERN;
  Route route = new Route(address, lastProxy, lastInetSocketAddress, modernTls);
  if (routeDatabase.shouldPostpone(route)) {
    postponedRoutes.add(route);
    // We will only recurse in order to skip previously failed routes. They will be
    // tried last.
    return next(method);
  }

  return new Connection(route);
}
 
Example #23
Source File: OkHttpInterceptor.java    From weex with Apache License 2.0 5 votes vote down vote up
public OkHttpInspectorResponse(
    String requestId,
    Request request,
    Response response,
    Connection connection) {
  mRequestId = requestId;
  mRequest = request;
  mResponse = response;
  mConnection = connection;
}
 
Example #24
Source File: HttpEngineConnectMethodInterceptor.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Override
protected void doInAfterTrace(SpanEventRecorder recorder, Object target, Object[] args, Object result, Throwable throwable) {
    recorder.recordApi(methodDescriptor);
    recorder.recordServiceType(OkHttpConstants.OK_HTTP_CLIENT_INTERNAL);
    recorder.recordException(throwable);

    if (target instanceof ConnectionGetter) {
        final Connection connection = ((ConnectionGetter)target)._$PINPOINT$_getConnection();
        if (connection != null) {
            final String hostAndPort = getHostAndPort(connection);
            recorder.recordAttribute(AnnotationKey.HTTP_INTERNAL_DISPLAY, hostAndPort);
        }
    }
}
 
Example #25
Source File: HttpURLConnectionImpl.java    From bluemix-parking-meter with MIT License 5 votes vote down vote up
private HttpEngine newHttpEngine(String method, RawHeaders requestHeaders,
    Connection connection, RetryableOutputStream requestBody) throws IOException {
  if (url.getProtocol().equals("http")) {
    return new HttpEngine(client, this, method, requestHeaders, connection, requestBody);
  } else if (url.getProtocol().equals("https")) {
    return new HttpsEngine(client, this, method, requestHeaders, connection, requestBody);
  } else {
    throw new AssertionError();
  }
}
 
Example #26
Source File: RouteSelector.java    From cordova-android-chromeview with Apache License 2.0 5 votes vote down vote up
/**
 * Clients should invoke this method when they encounter a connectivity
 * failure on a connection returned by this route selector.
 */
public void connectFailed(Connection connection, IOException failure) {
  Route failedRoute = connection.getRoute();
  if (failedRoute.getProxy().type() != Proxy.Type.DIRECT && proxySelector != null) {
    // Tell the proxy selector when we fail to connect on a fresh connection.
    proxySelector.connectFailed(uri, failedRoute.getProxy().address(), failure);
  }

  failedRoutes.add(failedRoute);
  if (!(failure instanceof SSLHandshakeException)) {
    // If the problem was not related to SSL then it will also fail with
    // a different Tls mode therefore we can be proactive about it.
    failedRoutes.add(failedRoute.flipTlsMode());
  }
}
 
Example #27
Source File: HttpURLConnectionImpl.java    From phonegapbootcampsite with MIT License 5 votes vote down vote up
private HttpEngine newHttpEngine(String method, RawHeaders requestHeaders,
    Connection connection, RetryableOutputStream requestBody) throws IOException {
  if (url.getProtocol().equals("http")) {
    return new HttpEngine(client, this, method, requestHeaders, connection, requestBody);
  } else if (url.getProtocol().equals("https")) {
    return new HttpsEngine(client, this, method, requestHeaders, connection, requestBody);
  } else {
    throw new AssertionError();
  }
}
 
Example #28
Source File: HttpURLConnectionImpl.java    From cordova-amazon-fireos with Apache License 2.0 5 votes vote down vote up
private HttpEngine newHttpEngine(String method, RawHeaders requestHeaders,
    Connection connection, RetryableOutputStream requestBody) throws IOException {
  if (url.getProtocol().equals("http")) {
    return new HttpEngine(client, this, method, requestHeaders, connection, requestBody);
  } else if (url.getProtocol().equals("https")) {
    return new HttpsEngine(client, this, method, requestHeaders, connection, requestBody);
  } else {
    throw new AssertionError();
  }
}
 
Example #29
Source File: RouteSelector.java    From phonegap-plugin-loading-spinner with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the next route address to attempt.
 *
 * @throws NoSuchElementException if there are no more routes to attempt.
 */
public Connection next() throws IOException {
  // Always prefer pooled connections over new connections.
  Connection pooled = pool.get(address);
  if (pooled != null) {
    return pooled;
  }

  // Compute the next route to attempt.
  if (!hasNextTlsMode()) {
    if (!hasNextInetSocketAddress()) {
      if (!hasNextProxy()) {
        if (!hasNextPostponed()) {
          throw new NoSuchElementException();
        }
        return new Connection(nextPostponed());
      }
      lastProxy = nextProxy();
      resetNextInetSocketAddress(lastProxy);
    }
    lastInetSocketAddress = nextInetSocketAddress();
    resetNextTlsMode();
  }

  boolean modernTls = nextTlsMode() == TLS_MODE_MODERN;
  Route route = new Route(address, lastProxy, lastInetSocketAddress, modernTls);
  if (failedRoutes.contains(route)) {
    postponedRoutes.add(route);
    // We will only recurse in order to skip previously failed routes. They will be
    // tried last.
    return next();
  }

  return new Connection(route);
}
 
Example #30
Source File: RouteSelector.java    From bluemix-parking-meter with MIT License 5 votes vote down vote up
/**
 * Returns the next route address to attempt.
 *
 * @throws NoSuchElementException if there are no more routes to attempt.
 */
public Connection next(String method) throws IOException {
  // Always prefer pooled connections over new connections.
  for (Connection pooled; (pooled = pool.get(address)) != null; ) {
    if (method.equals("GET") || pooled.isReadable()) return pooled;
    pooled.close();
  }

  // Compute the next route to attempt.
  if (!hasNextTlsMode()) {
    if (!hasNextInetSocketAddress()) {
      if (!hasNextProxy()) {
        if (!hasNextPostponed()) {
          throw new NoSuchElementException();
        }
        return new Connection(nextPostponed());
      }
      lastProxy = nextProxy();
      resetNextInetSocketAddress(lastProxy);
    }
    lastInetSocketAddress = nextInetSocketAddress();
    resetNextTlsMode();
  }

  boolean modernTls = nextTlsMode() == TLS_MODE_MODERN;
  Route route = new Route(address, lastProxy, lastInetSocketAddress, modernTls);
  if (routeDatabase.shouldPostpone(route)) {
    postponedRoutes.add(route);
    // We will only recurse in order to skip previously failed routes. They will be
    // tried last.
    return next(method);
  }

  return new Connection(route);
}