Search in sources :

Example 1 with SwiftAuthenticationFailedException

use of org.apache.hadoop.fs.swift.exceptions.SwiftAuthenticationFailedException in project hadoop by apache.

the class SwiftRestClient method buildException.

/**
   * Build an exception from a failed operation. This can include generating
   * specific exceptions (e.g. FileNotFound), as well as the default
   * {@link SwiftInvalidResponseException}.
   *
   * @param uri URI for operation
   * @param method operation that failed
   * @param statusCode status code
   * @param <M> method type
   * @return an exception to throw
   */
private <M extends HttpMethod> IOException buildException(URI uri, M method, int statusCode) {
    IOException fault;
    //log the failure @debug level
    String errorMessage = String.format("Method %s on %s failed, status code: %d," + " status line: %s", method.getName(), uri, statusCode, method.getStatusLine());
    if (LOG.isDebugEnabled()) {
        LOG.debug(errorMessage);
    }
    //send the command
    switch(statusCode) {
        case SC_NOT_FOUND:
            fault = new FileNotFoundException("Operation " + method.getName() + " on " + uri);
            break;
        case SC_BAD_REQUEST:
            //bad HTTP request
            fault = new SwiftBadRequestException("Bad request against " + uri, method.getName(), uri, method);
            break;
        case SC_REQUESTED_RANGE_NOT_SATISFIABLE:
            //out of range
            StringBuilder errorText = new StringBuilder(method.getStatusText());
            //get the requested length
            Header requestContentLen = method.getRequestHeader(HEADER_CONTENT_LENGTH);
            if (requestContentLen != null) {
                errorText.append(" requested ").append(requestContentLen.getValue());
            }
            //and the result
            Header availableContentRange = method.getResponseHeader(HEADER_CONTENT_RANGE);
            if (availableContentRange != null) {
                errorText.append(" available ").append(availableContentRange.getValue());
            }
            fault = new EOFException(errorText.toString());
            break;
        case SC_UNAUTHORIZED:
            //auth failure; should only happen on the second attempt
            fault = new SwiftAuthenticationFailedException("Operation not authorized- current access token =" + getToken(), method.getName(), uri, method);
            break;
        case SwiftProtocolConstants.SC_TOO_MANY_REQUESTS_429:
        case SwiftProtocolConstants.SC_THROTTLED_498:
            //response code that may mean the client is being throttled
            fault = new SwiftThrottledRequestException("Client is being throttled: too many requests", method.getName(), uri, method);
            break;
        default:
            //return a generic invalid HTTP response
            fault = new SwiftInvalidResponseException(errorMessage, method.getName(), uri, method);
    }
    return fault;
}
Also used : Header(org.apache.commons.httpclient.Header) FileNotFoundException(java.io.FileNotFoundException) EOFException(java.io.EOFException) IOException(java.io.IOException) SwiftAuthenticationFailedException(org.apache.hadoop.fs.swift.exceptions.SwiftAuthenticationFailedException) SwiftInvalidResponseException(org.apache.hadoop.fs.swift.exceptions.SwiftInvalidResponseException) SwiftThrottledRequestException(org.apache.hadoop.fs.swift.exceptions.SwiftThrottledRequestException) SwiftBadRequestException(org.apache.hadoop.fs.swift.exceptions.SwiftBadRequestException)

Example 2 with SwiftAuthenticationFailedException

use of org.apache.hadoop.fs.swift.exceptions.SwiftAuthenticationFailedException in project hadoop by apache.

the class SwiftRestClient method exec.

/**
   * Execute a method in a new HttpClient instance.
   * If the auth failed, authenticate then retry the method.
   *
   * @param method method to exec
   * @param <M> Method type
   * @return the status code
   * @throws IOException on any failure
   */
private <M extends HttpMethod> int exec(M method) throws IOException {
    final HttpClient client = new HttpClient();
    if (proxyHost != null) {
        client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(proxyHost, proxyPort));
    }
    int statusCode = execWithDebugOutput(method, client);
    if ((statusCode == HttpStatus.SC_UNAUTHORIZED || statusCode == HttpStatus.SC_BAD_REQUEST) && method instanceof AuthPostMethod && !useKeystoneAuthentication) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Operation failed with status " + method.getStatusCode() + " attempting keystone auth");
        }
        //if rackspace key authentication failed - try custom Keystone authentication
        useKeystoneAuthentication = true;
        final AuthPostMethod authentication = (AuthPostMethod) method;
        //replace rackspace auth with keystone one
        authentication.setRequestEntity(getAuthenticationRequst(keystoneAuthRequest));
        statusCode = execWithDebugOutput(method, client);
    }
    if (statusCode == HttpStatus.SC_UNAUTHORIZED) {
        if (method instanceof AuthPostMethod) {
            //unauth response from the AUTH URI itself.
            throw new SwiftAuthenticationFailedException(authRequest.toString(), "auth", authUri, method);
        }
        //any other URL: try again
        if (LOG.isDebugEnabled()) {
            LOG.debug("Reauthenticating");
        }
        //re-auth, this may recurse into the same dir
        authenticate();
        if (LOG.isDebugEnabled()) {
            LOG.debug("Retrying original request");
        }
        statusCode = execWithDebugOutput(method, client);
    }
    return statusCode;
}
Also used : HttpHost(org.apache.commons.httpclient.HttpHost) HttpClient(org.apache.commons.httpclient.HttpClient) SwiftAuthenticationFailedException(org.apache.hadoop.fs.swift.exceptions.SwiftAuthenticationFailedException) Endpoint(org.apache.hadoop.fs.swift.auth.entities.Endpoint)

Aggregations

SwiftAuthenticationFailedException (org.apache.hadoop.fs.swift.exceptions.SwiftAuthenticationFailedException)2 EOFException (java.io.EOFException)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Header (org.apache.commons.httpclient.Header)1 HttpClient (org.apache.commons.httpclient.HttpClient)1 HttpHost (org.apache.commons.httpclient.HttpHost)1 Endpoint (org.apache.hadoop.fs.swift.auth.entities.Endpoint)1 SwiftBadRequestException (org.apache.hadoop.fs.swift.exceptions.SwiftBadRequestException)1 SwiftInvalidResponseException (org.apache.hadoop.fs.swift.exceptions.SwiftInvalidResponseException)1 SwiftThrottledRequestException (org.apache.hadoop.fs.swift.exceptions.SwiftThrottledRequestException)1