Java Code Examples for java.net.HttpURLConnection#HTTP_GATEWAY_TIMEOUT

The following examples show how to use java.net.HttpURLConnection#HTTP_GATEWAY_TIMEOUT . 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: GrpcUtil.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
private static Status.Code httpStatusToGrpcCode(int httpStatusCode) {
  if (httpStatusCode >= 100 && httpStatusCode < 200) {
    // 1xx. These headers should have been ignored.
    return Status.Code.INTERNAL;
  }
  switch (httpStatusCode) {
    case HttpURLConnection.HTTP_BAD_REQUEST:  // 400
    case 431: // Request Header Fields Too Large
      // TODO(carl-mastrangelo): this should be added to the http-grpc-status-mapping.md doc.
      return Status.Code.INTERNAL;
    case HttpURLConnection.HTTP_UNAUTHORIZED:  // 401
      return Status.Code.UNAUTHENTICATED;
    case HttpURLConnection.HTTP_FORBIDDEN:  // 403
      return Status.Code.PERMISSION_DENIED;
    case HttpURLConnection.HTTP_NOT_FOUND:  // 404
      return Status.Code.UNIMPLEMENTED;
    case 429:  // Too Many Requests
    case HttpURLConnection.HTTP_BAD_GATEWAY:  // 502
    case HttpURLConnection.HTTP_UNAVAILABLE:  // 503
    case HttpURLConnection.HTTP_GATEWAY_TIMEOUT:  // 504
      return Status.Code.UNAVAILABLE;
    default:
      return Status.Code.UNKNOWN;
  }
}
 
Example 2
Source File: DefaultSettingsSpiCallTest.java    From firebase-android-sdk with Apache License 2.0 6 votes vote down vote up
public void testRequestWasSuccessful_unsuccessfulStatusCodes() {
  // 204 and 205 are considered unsuccessful in this case, and 206 should never happen. Also
  // test with some of the other common status codes (these are ones that our backend has
  // been known to return).
  final int[] statusCodes = {
    HttpURLConnection.HTTP_NO_CONTENT,
    HttpURLConnection.HTTP_RESET,
    HttpURLConnection.HTTP_PARTIAL,
    HttpURLConnection.HTTP_BAD_REQUEST,
    HttpURLConnection.HTTP_UNAUTHORIZED,
    HttpURLConnection.HTTP_FORBIDDEN,
    HttpURLConnection.HTTP_NOT_FOUND,
    HttpURLConnection.HTTP_NOT_ACCEPTABLE,
    HttpURLConnection.HTTP_INTERNAL_ERROR,
    HttpURLConnection.HTTP_BAD_GATEWAY,
    HttpURLConnection.HTTP_UNAVAILABLE,
    HttpURLConnection.HTTP_GATEWAY_TIMEOUT
  };
  for (int statusCode : statusCodes) {
    assertFalse(defaultSettingsSpiCall.requestWasSuccessful(statusCode));
  }
}
 
Example 3
Source File: GrpcStatus.java    From armeria with Apache License 2.0 6 votes vote down vote up
private static Status.Code httpStatusToGrpcCode(int httpStatusCode) {
    if (httpStatusCode >= 100 && httpStatusCode < 200) {
        // 1xx. These headers should have been ignored.
        return Status.Code.INTERNAL;
    }
    switch (httpStatusCode) {
        case HttpURLConnection.HTTP_BAD_REQUEST:  // 400
        case 431: // Request Header Fields Too Large
            return Status.Code.INTERNAL;
        case HttpURLConnection.HTTP_UNAUTHORIZED:  // 401
            return Status.Code.UNAUTHENTICATED;
        case HttpURLConnection.HTTP_FORBIDDEN:  // 403
            return Status.Code.PERMISSION_DENIED;
        case HttpURLConnection.HTTP_NOT_FOUND:  // 404
            return Status.Code.UNIMPLEMENTED;
        case 429:  // Too Many Requests
        case HttpURLConnection.HTTP_BAD_GATEWAY:  // 502
        case HttpURLConnection.HTTP_UNAVAILABLE:  // 503
        case HttpURLConnection.HTTP_GATEWAY_TIMEOUT:  // 504
            return Status.Code.UNAVAILABLE;
        default:
            return Status.Code.UNKNOWN;
    }
}
 
Example 4
Source File: GrpcUtil.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
private static Status.Code httpStatusToGrpcCode(int httpStatusCode) {
  if (httpStatusCode >= 100 && httpStatusCode < 200) {
    // 1xx. These headers should have been ignored.
    return Status.Code.INTERNAL;
  }
  switch (httpStatusCode) {
    case HttpURLConnection.HTTP_BAD_REQUEST:  // 400
    case 431: // Request Header Fields Too Large
      // TODO(carl-mastrangelo): this should be added to the http-grpc-status-mapping.md doc.
      return Status.Code.INTERNAL;
    case HttpURLConnection.HTTP_UNAUTHORIZED:  // 401
      return Status.Code.UNAUTHENTICATED;
    case HttpURLConnection.HTTP_FORBIDDEN:  // 403
      return Status.Code.PERMISSION_DENIED;
    case HttpURLConnection.HTTP_NOT_FOUND:  // 404
      return Status.Code.UNIMPLEMENTED;
    case 429:  // Too Many Requests
    case HttpURLConnection.HTTP_BAD_GATEWAY:  // 502
    case HttpURLConnection.HTTP_UNAVAILABLE:  // 503
    case HttpURLConnection.HTTP_GATEWAY_TIMEOUT:  // 504
      return Status.Code.UNAVAILABLE;
    default:
      return Status.Code.UNKNOWN;
  }
}
 
Example 5
Source File: ConfigFetchHandler.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Returns true for server errors that are throttle-able.
 *
 * <p>The {@link HttpURLConnection#HTTP_GATEWAY_TIMEOUT} error is included here since it is
 * similar to the other unavailable errors in the previously linked doc.
 */
private boolean isThrottleableServerError(int httpStatusCode) {
  return httpStatusCode == HTTP_TOO_MANY_REQUESTS
      || httpStatusCode == HttpURLConnection.HTTP_BAD_GATEWAY
      || httpStatusCode == HttpURLConnection.HTTP_UNAVAILABLE
      || httpStatusCode == HttpURLConnection.HTTP_GATEWAY_TIMEOUT;
}
 
Example 6
Source File: HTTPUtils.java    From TestRailSDK with MIT License 4 votes vote down vote up
/**
 * Take a fully-baked connection and send it to the server with retry
 * @param connection A composed HTTP request containing the URL and any headers required
 * @return An active, open connection in a post-response state
 * @throws IOException occurs when unable to read response code from HttpUrlConnection
 */
private HttpURLConnection submitRequestFromConnectionWithRetry(HttpURLConnection connection, int retries) throws IOException {
    boolean connected = false;
    int RETRY_DELAY_MS = 500; // initial default value
    int retryDelayInMS;

    connection.setReadTimeout(REQUEST_TIMEOUT);
    connection.setConnectTimeout(REQUEST_TIMEOUT);

    outer: for (int retry = 0; retry < retries && !connected; retry++) {
        if (retry > 0) {
            log.warn("retry " + retry + "/" + retries);
            try {
                log.debug("Sleeping for retry: " + RETRY_DELAY_MS);
                Thread.sleep(RETRY_DELAY_MS);
                RETRY_DELAY_MS = 500; // reset to default value
            } catch (InterruptedException e) {
                // lets ignore this
            }
        }

        // try connect
        connection.connect();
        switch (connection.getResponseCode()) {
            case HttpURLConnection.HTTP_OK:
                log.debug(" **OK**");
                return connection;
            case HttpURLConnection.HTTP_GATEWAY_TIMEOUT:
                log.warn(" **gateway timeout**");
                break;// retry
            case 429: // 429 isn't available in any of the enums
                log.warn(" **429**");
                retryDelayInMS = Integer.parseInt(connection.getHeaderField("Retry-After")) * 1000; // seconds to ms
                RETRY_DELAY_MS = retryDelayInMS;
                break;// retry
            case HttpURLConnection.HTTP_UNAVAILABLE:
                log.warn("**unavailable**");
                break;// retry, server is unstable
            default:
                log.error(" **unknown response code**.");
                break outer; // abort
        }
    }

    return connection;
}
 
Example 7
Source File: StorageException.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
/**
 * Translates the specified HTTP status code into a storage exception.
 * 
 * @param statusCode
 *            The HTTP status code returned by the operation.
 * @param statusDescription
 *            A <code>String</code> that represents the status description.
 * @param inner
 *            An <code>Exception</code> object that represents a reference to the initial exception, if one exists.
 * 
 * @return A <code>StorageException</code> object that represents translated exception.
 **/
protected static StorageException translateFromHttpStatus(final int statusCode, final String statusDescription,
        final Exception inner) {
    String errorCode;
    switch (statusCode) {
    case HttpURLConnection.HTTP_FORBIDDEN:
        errorCode = StorageErrorCode.ACCESS_DENIED.toString();
        break;
    case HttpURLConnection.HTTP_GONE:
    case HttpURLConnection.HTTP_NOT_FOUND:
        errorCode = StorageErrorCode.RESOURCE_NOT_FOUND.toString();
        break;
    case 416:
    case HttpURLConnection.HTTP_BAD_REQUEST:
        // 416: RequestedRangeNotSatisfiable - No corresponding enum in HttpURLConnection
        errorCode = StorageErrorCode.BAD_REQUEST.toString();
        break;

    case HttpURLConnection.HTTP_PRECON_FAILED:
    case HttpURLConnection.HTTP_NOT_MODIFIED:
        errorCode = StorageErrorCode.CONDITION_FAILED.toString();
        break;

    case HttpURLConnection.HTTP_CONFLICT:
        errorCode = StorageErrorCode.RESOURCE_ALREADY_EXISTS.toString();
        break;

    case HttpURLConnection.HTTP_UNAVAILABLE:
        errorCode = StorageErrorCode.SERVER_BUSY.toString();
        break;

    case HttpURLConnection.HTTP_GATEWAY_TIMEOUT:
        errorCode = StorageErrorCode.SERVICE_TIMEOUT.toString();
        break;

    case HttpURLConnection.HTTP_INTERNAL_ERROR:
        errorCode = StorageErrorCode.SERVICE_INTERNAL_ERROR.toString();
        break;

    case HttpURLConnection.HTTP_NOT_IMPLEMENTED:
        errorCode = StorageErrorCode.NOT_IMPLEMENTED.toString();
        break;

    case HttpURLConnection.HTTP_BAD_GATEWAY:
        errorCode = StorageErrorCode.BAD_GATEWAY.toString();
        break;

    case HttpURLConnection.HTTP_VERSION:
        errorCode = StorageErrorCode.HTTP_VERSION_NOT_SUPPORTED.toString();
        break;
    default:
        errorCode = null;
    }

    if (errorCode == null) {
        return null;
    } else {
        return new StorageException(errorCode, statusDescription, statusCode, null, inner);
    }
}
 
Example 8
Source File: Sentry.java    From Sentry-Android with MIT License 4 votes vote down vote up
/**
 * Map from HTTP status code to reason description.
 * Sentry HTTP breadcrumbs expect a text description of the HTTP status-code.
 * This function implements a look-up table with a default value for unknown status-codes.
 * @param statusCode an integer HTTP status code, expected to be in the range [200,505].
 * @return a non-empty string in all cases.
 */
private static String httpReason(int statusCode) {
    switch (statusCode) {
        // 2xx
        case HttpURLConnection.HTTP_OK: return "OK";
        case HttpURLConnection.HTTP_CREATED: return "Created";
        case HttpURLConnection.HTTP_ACCEPTED: return "Accepted";
        case HttpURLConnection.HTTP_NOT_AUTHORITATIVE: return "Non-Authoritative Information";
        case HttpURLConnection.HTTP_NO_CONTENT: return "No Content";
        case HttpURLConnection.HTTP_RESET: return "Reset Content";
        case HttpURLConnection.HTTP_PARTIAL: return "Partial Content";

        // 3xx
        case HttpURLConnection.HTTP_MULT_CHOICE: return "Multiple Choices";
        case HttpURLConnection.HTTP_MOVED_PERM: return "Moved Permanently";
        case HttpURLConnection.HTTP_MOVED_TEMP: return "Temporary Redirect";
        case HttpURLConnection.HTTP_SEE_OTHER: return "See Other";
        case HttpURLConnection.HTTP_NOT_MODIFIED: return "Not Modified";
        case HttpURLConnection.HTTP_USE_PROXY: return "Use Proxy";

        // 4xx
        case HttpURLConnection.HTTP_BAD_REQUEST: return "Bad Request";
        case HttpURLConnection.HTTP_UNAUTHORIZED: return "Unauthorized";
        case HttpURLConnection.HTTP_PAYMENT_REQUIRED: return "Payment Required";
        case HttpURLConnection.HTTP_FORBIDDEN: return "Forbidden";
        case HttpURLConnection.HTTP_NOT_FOUND: return "Not Found";
        case HttpURLConnection.HTTP_BAD_METHOD: return "Method Not Allowed";
        case HttpURLConnection.HTTP_NOT_ACCEPTABLE: return "Not Acceptable";
        case HttpURLConnection.HTTP_PROXY_AUTH: return "Proxy Authentication Required";
        case HttpURLConnection.HTTP_CLIENT_TIMEOUT: return "Request Time-Out";
        case HttpURLConnection.HTTP_CONFLICT: return "Conflict";
        case HttpURLConnection.HTTP_GONE: return "Gone";
        case HttpURLConnection.HTTP_LENGTH_REQUIRED: return "Length Required";
        case HttpURLConnection.HTTP_PRECON_FAILED: return "Precondition Failed";
        case HttpURLConnection.HTTP_ENTITY_TOO_LARGE: return "Request Entity Too Large";
        case HttpURLConnection.HTTP_REQ_TOO_LONG: return "Request-URI Too Large";
        case HttpURLConnection.HTTP_UNSUPPORTED_TYPE: return "Unsupported Media Type";

        // 5xx
        case HttpURLConnection.HTTP_INTERNAL_ERROR: return "Internal Server Error";
        case HttpURLConnection.HTTP_NOT_IMPLEMENTED: return "Not Implemented";
        case HttpURLConnection.HTTP_BAD_GATEWAY: return "Bad Gateway";
        case HttpURLConnection.HTTP_UNAVAILABLE: return "Service Unavailable";
        case HttpURLConnection.HTTP_GATEWAY_TIMEOUT: return "Gateway Timeout";
        case HttpURLConnection.HTTP_VERSION: return "Version Not Supported";

        default: return "unknown";
    }
}