Search in sources :

Example 1 with EasySSLProtocolSocketFactory

use of org.xdi.util.EasySSLProtocolSocketFactory in project oxCore by GluuFederation.

the class HTTPFileDownloader method getResource.

public static String getResource(String path, String contentType, String user, String password) {
    boolean isUseAuthentication = (user != null) && (password != null);
    if (!path.contains("://")) {
        path = "http://" + path;
    }
    String result = null;
    GetMethod method = new GetMethod(path);
    try {
        method.setRequestHeader("Accept", contentType);
        if (getEasyhttps() == null) {
            setEasyhttps(new Protocol("https", new EasySSLProtocolSocketFactory(), 443));
        }
        Protocol.registerProtocol("https", getEasyhttps());
        final HttpClient httpClient;
        if (isUseAuthentication) {
            httpClient = createHttpClientWithBasicAuth(user, password);
        } else {
            httpClient = new HttpClient();
        }
        httpClient.executeMethod(method);
        if (method.getStatusCode() == HttpStatus.SC_OK) {
            result = method.getResponseBodyAsString();
        }
    } catch (IOException ex) {
        result = null;
        log.error(String.format("Failed to get resource %s", path), ex);
    } catch (Exception ex) {
        result = null;
        log.error(String.format("Failed to get resource %s", path), ex);
    } finally {
        method.releaseConnection();
    }
    return result;
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) IOException(java.io.IOException) Protocol(org.apache.commons.httpclient.protocol.Protocol) IOException(java.io.IOException) EasySSLProtocolSocketFactory(org.xdi.util.EasySSLProtocolSocketFactory)

Aggregations

IOException (java.io.IOException)1 HttpClient (org.apache.commons.httpclient.HttpClient)1 GetMethod (org.apache.commons.httpclient.methods.GetMethod)1 Protocol (org.apache.commons.httpclient.protocol.Protocol)1 EasySSLProtocolSocketFactory (org.xdi.util.EasySSLProtocolSocketFactory)1