Search in sources :

Example 11 with TrustedHttpClientException

use of org.opencastproject.security.api.TrustedHttpClientException in project opencast by opencast.

the class StandAloneTrustedHttpClientImpl method execute.

@Override
public HttpResponse execute(HttpUriRequest httpUriRequest, int connectionTimeout, int socketTimeout) throws TrustedHttpClientException {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectionTimeout);
    // Add the request header to elicit a digest auth response
    httpUriRequest.setHeader(REQUESTED_AUTH_HEADER, DIGEST_AUTH);
    if ("GET".equalsIgnoreCase(httpUriRequest.getMethod()) || "HEAD".equalsIgnoreCase(httpUriRequest.getMethod())) {
        // Set the user/pass
        UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, pass);
        httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);
        // Run the request (the http client handles the multiple back-and-forth requests)
        HttpResponse response = null;
        try {
            response = new HttpResponseWrapper(httpClient.execute(httpUriRequest));
            responseMap.put(response, httpClient);
            return response;
        } catch (IOException e) {
            // close the http connection(s)
            httpClient.getConnectionManager().shutdown();
            throw new TrustedHttpClientException(e);
        }
    }
    // HttpClient doesn't handle the request dynamics for other verbs (especially when sending a streamed multipart
    // request), so we need to handle the details of the digest auth back-and-forth manually
    manuallyHandleDigestAuthentication(httpUriRequest, httpClient);
    HttpResponse response = null;
    try {
        response = new HttpResponseWrapper(httpClient.execute(httpUriRequest));
        if (nonceTimeoutRetries > 0 && hadNonceTimeoutResponse(response)) {
            httpClient.getConnectionManager().shutdown();
            response = retryAuthAndRequestAfterNonceTimeout(httpUriRequest, response);
        }
        responseMap.put(response, httpClient);
        return response;
    } catch (Exception e) {
        // if we have a response, remove it from the map
        if (response != null) {
            responseMap.remove(response);
        }
        // close the http connection(s)
        httpClient.getConnectionManager().shutdown();
        throw new TrustedHttpClientException(e);
    }
}
Also used : HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 12 with TrustedHttpClientException

use of org.opencastproject.security.api.TrustedHttpClientException in project opencast by opencast.

the class StandAloneTrustedHttpClientImpl method getRealmAndNonce.

/**
 * Perform a request, and extract the realm and nonce values
 *
 * @param request
 *         The request to execute in order to obtain the realm and nonce
 * @return A String[] containing the {realm, nonce}
 */
private String[] getRealmAndNonce(HttpRequestBase request) throws TrustedHttpClientException {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpResponse response;
    try {
        response = new HttpResponseWrapper(httpClient.execute(request));
    } catch (IOException e) {
        httpClient.getConnectionManager().shutdown();
        throw new TrustedHttpClientException(e);
    }
    Header[] headers = response.getHeaders("WWW-Authenticate");
    if (headers == null || headers.length == 0) {
        logger.warn("URI {} does not support digest authentication", request.getURI());
        httpClient.getConnectionManager().shutdown();
        return null;
    }
    Header authRequiredResponseHeader = headers[0];
    String nonce = null;
    String realm = null;
    for (HeaderElement element : authRequiredResponseHeader.getElements()) {
        if ("nonce".equals(element.getName())) {
            nonce = element.getValue();
        } else if ("Digest realm".equals(element.getName())) {
            realm = element.getValue();
        }
    }
    httpClient.getConnectionManager().shutdown();
    return new String[] { realm, nonce };
}
Also used : Header(org.apache.http.Header) HeaderElement(org.apache.http.HeaderElement) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Aggregations

TrustedHttpClientException (org.opencastproject.security.api.TrustedHttpClientException)12 IOException (java.io.IOException)8 HttpResponse (org.apache.http.HttpResponse)6 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)4 ClientProtocolException (org.apache.http.client.ClientProtocolException)4 Header (org.apache.http.Header)3 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)3 HttpClient (org.opencastproject.kernel.http.api.HttpClient)3 TrustedHttpClient (org.opencastproject.security.api.TrustedHttpClient)3 UrlSigningException (org.opencastproject.security.urlsigning.exception.UrlSigningException)3 ArrayList (java.util.ArrayList)2 HeaderElement (org.apache.http.HeaderElement)2 HttpGet (org.apache.http.client.methods.HttpGet)2 DigestScheme (org.apache.http.impl.auth.DigestScheme)2 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)2 MediaPackage (org.opencastproject.mediapackage.MediaPackage)2 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)2 HttpResponseWrapper (org.opencastproject.security.util.HttpResponseWrapper)2 WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)2 DataInputStream (java.io.DataInputStream)1