Search in sources :

Example 1 with Credentials

use of org.apache.hc.client5.http.auth.Credentials in project ecf by eclipse.

the class HttpClientRetrieveFileTransfer method openStreams.

/* (non-Javadoc)
	 * @see org.eclipse.ecf.provider.filetransfer.retrieve.AbstractRetrieveFileTransfer#openStreams()
	 */
@Override
protected void openStreams() throws IncomingFileTransferException {
    // $NON-NLS-1$
    Trace.entering(Activator.PLUGIN_ID, DebugOptions.METHODS_ENTERING, this.getClass(), "openStreams");
    final String urlString = getRemoteFileURL().toString();
    this.doneFired = false;
    int code = -1;
    try {
        getMethod = new HttpGet(urlString);
        RequestConfig.Builder rcfgBuilder = getRequestConfigBuilder();
        rcfgBuilder.setConnectTimeout(getConnectTimeout(), TimeUnit.MILLISECONDS);
        setupAuthentication(urlString);
        // Define a CredentialsProvider - found that possibility while debugging in org.apache.commons.httpclient.HttpMethodDirector.processProxyAuthChallenge(HttpMethod)
        // Seems to be another way to select the credentials.
        setRequestHeaderValues();
        // $NON-NLS-1$
        Trace.trace(Activator.PLUGIN_ID, "retrieve=" + urlString);
        // 2) The target remote file does *not* end in .gz (see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=280205)
        if (getFileRangeSpecification() == null && !targetHasGzSuffix(super.getRemoteFileName())) {
            // The interceptors to provide gzip are always added and are enabled by default
            // $NON-NLS-1$
            Trace.trace(Activator.PLUGIN_ID, "Accept-Encoding: gzip,deflate added to request header");
        } else {
            // Disable the interceptors to provide gzip
            // $NON-NLS-1$
            Trace.trace(Activator.PLUGIN_ID, "Accept-Encoding NOT added to header");
            rcfgBuilder.setContentCompressionEnabled(false);
        }
        getMethod.setConfig(rcfgBuilder.build());
        fireConnectStartEvent();
        if (checkAndHandleDone()) {
            return;
        }
        // redirect response code handled internally
        if (connectJob == null) {
            performConnect(new NullProgressMonitor());
        } else {
            connectJob.schedule();
            connectJob.join();
            connectJob = null;
        }
        if (checkAndHandleDone()) {
            return;
        }
        code = responseCode;
        responseHeaders = getResponseHeaders();
        // $NON-NLS-1$
        Trace.trace(Activator.PLUGIN_ID, "retrieve resp=" + code);
        // Check for NTLM proxy in response headers
        // This check is to deal with bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=252002
        boolean ntlmProxyFound = NTLMProxyDetector.detectNTLMProxy(httpContext);
        if (ntlmProxyFound)
            ECFHttpClientFactory.getNTLMProxyHandler(httpContext).handleNTLMProxy(getProxy(), code);
        if (NTLMProxyDetector.detectSPNEGOProxy(httpContext))
            ECFHttpClientFactory.getNTLMProxyHandler(httpContext).handleSPNEGOProxy(getProxy(), code);
        if (code == HttpStatus.SC_PARTIAL_CONTENT || code == HttpStatus.SC_OK) {
            getResponseHeaderValues();
            setInputStream(httpResponse.getEntity().getContent());
            fireReceiveStartEvent();
        } else if (code == HttpStatus.SC_NOT_FOUND) {
            EntityUtils.consume(httpResponse.getEntity());
            // $NON-NLS-1$
            throw new IncomingFileTransferException(NLS.bind("File not found: {0}", urlString), code);
        } else if (code == HttpStatus.SC_UNAUTHORIZED) {
            EntityUtils.consume(httpResponse.getEntity());
            throw new IncomingFileTransferException(Messages.HttpClientRetrieveFileTransfer_Unauthorized, code);
        } else if (code == HttpStatus.SC_FORBIDDEN) {
            EntityUtils.consume(httpResponse.getEntity());
            // $NON-NLS-1$
            throw new IncomingFileTransferException("Forbidden", code);
        } else if (code == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) {
            EntityUtils.consume(httpResponse.getEntity());
            throw new IncomingFileTransferException(Messages.HttpClientRetrieveFileTransfer_Proxy_Auth_Required, code);
        } else {
            HttpEntity httpResponseEntity = httpResponse.getEntity();
            if (httpResponseEntity != null) {
                Trace.trace(Activator.PLUGIN_ID, EntityUtils.toString(httpResponseEntity));
            }
            throw new IncomingFileTransferException(NLS.bind(Messages.HttpClientRetrieveFileTransfer_ERROR_GENERAL_RESPONSE_CODE, Integer.valueOf(code)), code);
        }
    } catch (final Exception e) {
        // $NON-NLS-1$
        Trace.throwing(Activator.PLUGIN_ID, DebugOptions.EXCEPTIONS_THROWING, this.getClass(), "openStreams", e);
        if (code == -1) {
            if (!isDone()) {
                setDoneException(e);
            }
            fireTransferReceiveDoneEvent();
        } else {
            IncomingFileTransferException ex = (IncomingFileTransferException) ((e instanceof IncomingFileTransferException) ? e : new IncomingFileTransferException(NLS.bind(Messages.HttpClientRetrieveFileTransfer_EXCEPTION_COULD_NOT_CONNECT, urlString), e, code));
            throw ex;
        }
    }
    // $NON-NLS-1$
    Trace.exiting(Activator.PLUGIN_ID, DebugOptions.METHODS_EXITING, this.getClass(), "openStreams");
}
Also used : RequestConfig(org.apache.hc.client5.http.config.RequestConfig) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) HttpEntity(org.apache.hc.core5.http.HttpEntity) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) IncomingFileTransferException(org.eclipse.ecf.filetransfer.IncomingFileTransferException) IncomingFileTransferException(org.eclipse.ecf.filetransfer.IncomingFileTransferException) IOException(java.io.IOException) UnsupportedCallbackException(org.eclipse.ecf.core.security.UnsupportedCallbackException) InvalidFileRangeSpecificationException(org.eclipse.ecf.filetransfer.InvalidFileRangeSpecificationException)

Example 2 with Credentials

use of org.apache.hc.client5.http.auth.Credentials in project ecf by eclipse.

the class HttpClientRetrieveFileTransfer method setupAuthentication.

protected void setupAuthentication(String urlString) throws UnsupportedCallbackException, IOException {
    Credentials credentials = null;
    if (username == null) {
        credentials = getFileRequestCredentials();
    }
    if (credentials != null && username != null) {
        final AuthScope authScope = new AuthScope(getHostFromURL(urlString), getPortFromURL(urlString));
        // $NON-NLS-1$
        Trace.trace(Activator.PLUGIN_ID, "retrieve credentials=" + credentials);
        credentialsProvider.setCredentials(authScope, credentials);
    }
}
Also used : AuthScope(org.apache.hc.client5.http.auth.AuthScope) Credentials(org.apache.hc.client5.http.auth.Credentials) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials)

Example 3 with Credentials

use of org.apache.hc.client5.http.auth.Credentials in project ecf by eclipse.

the class HttpClientRetrieveFileTransfer method openStreamsForResume.

private boolean openStreamsForResume() {
    // $NON-NLS-1$
    Trace.entering(Activator.PLUGIN_ID, DebugOptions.METHODS_ENTERING, this.getClass(), "openStreamsForResume");
    final String urlString = getRemoteFileURL().toString();
    this.doneFired = false;
    int code = -1;
    try {
        getMethod = new HttpGet(urlString);
        requestConfigBuilder.setContentCompressionEnabled(false);
        setupAuthentication(urlString);
        // Define a CredentialsProvider - found that possibility while debugging in org.apache.commons.httpclient.HttpMethodDirector.processProxyAuthChallenge(HttpMethod)
        // Seems to be another way to select the credentials.
        setResumeRequestHeaderValues();
        // $NON-NLS-1$
        Trace.trace(Activator.PLUGIN_ID, "resume=" + urlString);
        getMethod.setConfig(requestConfigBuilder.build());
        // Gzip encoding is not an option for resume
        fireConnectStartEvent();
        if (checkAndHandleDone()) {
            return false;
        }
        // redirect response code handled internally
        if (connectJob == null) {
            performConnect(new NullProgressMonitor());
        } else {
            connectJob.schedule();
            connectJob.join();
            connectJob = null;
        }
        if (checkAndHandleDone()) {
            return false;
        }
        code = responseCode;
        responseHeaders = getResponseHeaders();
        // $NON-NLS-1$
        Trace.trace(Activator.PLUGIN_ID, "retrieve resp=" + code);
        if (code == HttpStatus.SC_PARTIAL_CONTENT || code == HttpStatus.SC_OK) {
            getResumeResponseHeaderValues();
            setInputStream(httpResponse.getEntity().getContent());
            this.paused = false;
            fireReceiveResumedEvent();
        } else if (code == HttpStatus.SC_NOT_FOUND) {
            EntityUtils.consume(httpResponse.getEntity());
            // $NON-NLS-1$
            throw new IncomingFileTransferException(NLS.bind("File not found: {0}", urlString), code, responseHeaders);
        } else if (code == HttpStatus.SC_UNAUTHORIZED) {
            EntityUtils.consume(httpResponse.getEntity());
            throw new IncomingFileTransferException(Messages.HttpClientRetrieveFileTransfer_Unauthorized, code, responseHeaders);
        } else if (code == HttpStatus.SC_FORBIDDEN) {
            EntityUtils.consume(httpResponse.getEntity());
            // $NON-NLS-1$
            throw new IncomingFileTransferException("Forbidden", code, responseHeaders);
        } else if (code == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) {
            EntityUtils.consume(httpResponse.getEntity());
            throw new IncomingFileTransferException(Messages.HttpClientRetrieveFileTransfer_Proxy_Auth_Required, code, responseHeaders);
        } else {
            EntityUtils.consume(httpResponse.getEntity());
            throw new IncomingFileTransferException(NLS.bind(Messages.HttpClientRetrieveFileTransfer_ERROR_GENERAL_RESPONSE_CODE, Integer.valueOf(code)), code, responseHeaders);
        }
        // $NON-NLS-1$
        Trace.exiting(Activator.PLUGIN_ID, DebugOptions.METHODS_EXITING, this.getClass(), "openStreamsForResume", Boolean.TRUE);
        return true;
    } catch (final Exception e) {
        // $NON-NLS-1$
        Trace.catching(Activator.PLUGIN_ID, DebugOptions.EXCEPTIONS_CATCHING, this.getClass(), "openStreamsForResume", e);
        if (code == -1) {
            if (!isDone()) {
                setDoneException(e);
            }
        } else {
            setDoneException((e instanceof IncomingFileTransferException) ? e : new IncomingFileTransferException(NLS.bind(Messages.HttpClientRetrieveFileTransfer_EXCEPTION_COULD_NOT_CONNECT, urlString), e, code, responseHeaders));
        }
        fireTransferReceiveDoneEvent();
        // $NON-NLS-1$
        Trace.exiting(Activator.PLUGIN_ID, DebugOptions.METHODS_EXITING, this.getClass(), "openStreamsForResume", Boolean.FALSE);
        return false;
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) IncomingFileTransferException(org.eclipse.ecf.filetransfer.IncomingFileTransferException) IncomingFileTransferException(org.eclipse.ecf.filetransfer.IncomingFileTransferException) IOException(java.io.IOException) UnsupportedCallbackException(org.eclipse.ecf.core.security.UnsupportedCallbackException) InvalidFileRangeSpecificationException(org.eclipse.ecf.filetransfer.InvalidFileRangeSpecificationException)

Example 4 with Credentials

use of org.apache.hc.client5.http.auth.Credentials in project ecf by eclipse.

the class HttpClientProxyCredentialProvider method getCredentials.

@Override
public Credentials getCredentials(AuthScope authscope, HttpContext httpContext) {
    // $NON-NLS-1$
    Trace.entering(Activator.PLUGIN_ID, DebugOptions.METHODS_ENTERING, HttpClientProxyCredentialProvider.class, "getCredentials " + authscope);
    // First check to see whether given authscope matches any authscope
    // already cached.
    Credentials result = super.getCredentials(authscope, httpContext);
    // If we have a match, return credentials
    if (result != null) {
        if ("ntlm".equalsIgnoreCase(authscope.getSchemeName())) {
            // $NON-NLS-1$
            // We might have gotten these from a password prompt, making them
            // UsernamePasswordCredentials...
            Credentials fixed = fixNTCredentials(result);
            if (fixed != result) {
                result = fixed;
                setCredentials(authscope, fixed);
            }
        }
        return result;
    }
    // If we don't have a match, first get ECF proxy, if any
    Proxy proxy = getECFProxy();
    if (proxy == null)
        return null;
    // Make sure that authscope and proxy host and port match
    if (!matchAuthScopeAndProxy(authscope, proxy))
        return null;
    // Then match scheme, and get credentials from proxy (if it's scheme we know about)
    Credentials credentials = null;
    if ("ntlm".equalsIgnoreCase(authscope.getSchemeName())) {
        // $NON-NLS-1$
        credentials = getNTLMCredentials(proxy);
    } else if (// $NON-NLS-1$
    "basic".equalsIgnoreCase(authscope.getSchemeName()) || "digest".equalsIgnoreCase(authscope.getSchemeName())) {
        // $NON-NLS-1$
        final String proxyUsername = proxy.getUsername();
        final String proxyPassword = proxy.getPassword();
        // If credentials present for proxy then we're done
        if (proxyUsername != null) {
            credentials = new UsernamePasswordCredentials(proxyUsername, proxyPassword.toCharArray());
        }
    } else if ("negotiate".equalsIgnoreCase(authscope.getSchemeName())) {
        // $NON-NLS-1$
        // $NON-NLS-1$
        Trace.trace(Activator.PLUGIN_ID, "SPNEGO is not supported, if you can contribute support, please do so.");
    } else {
        // $NON-NLS-1$
        Trace.trace(Activator.PLUGIN_ID, "Unrecognized authentication scheme.");
    }
    // Put found credentials in cache for next time
    if (credentials != null)
        setCredentials(authscope, credentials);
    return credentials;
}
Also used : Proxy(org.eclipse.ecf.core.util.Proxy) Credentials(org.apache.hc.client5.http.auth.Credentials) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials) NTCredentials(org.apache.hc.client5.http.auth.NTCredentials) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials)

Example 5 with Credentials

use of org.apache.hc.client5.http.auth.Credentials in project ecf by eclipse.

the class HttpClientProxyCredentialProvider method getNTLMCredentials.

protected Credentials getNTLMCredentials(Credentials credentials) {
    DefaultNTLMProxyHandler.setSeenNTLM();
    Credentials fixed = fixNTCredentials(credentials);
    if (fixed == credentials || allowNTLMAuthentication()) {
        return fixed;
    }
    return null;
}
Also used : Credentials(org.apache.hc.client5.http.auth.Credentials) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials) NTCredentials(org.apache.hc.client5.http.auth.NTCredentials)

Aggregations

UsernamePasswordCredentials (org.apache.hc.client5.http.auth.UsernamePasswordCredentials)13 AuthScope (org.apache.hc.client5.http.auth.AuthScope)8 Credentials (org.apache.hc.client5.http.auth.Credentials)8 BasicCredentialsProvider (org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider)6 IOException (java.io.IOException)5 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)5 RequestConfig (org.apache.hc.client5.http.config.RequestConfig)5 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)5 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)5 NTCredentials (org.apache.hc.client5.http.auth.NTCredentials)4 HttpHost (org.apache.hc.core5.http.HttpHost)4 CredentialsStore (org.apache.hc.client5.http.auth.CredentialsStore)3 HttpEntity (org.apache.hc.core5.http.HttpEntity)3 AltiriaGwException (com.altiria.app.exception.AltiriaGwException)2 ConnectionException (com.altiria.app.exception.ConnectionException)2 GeneralAltiriaException (com.altiria.app.exception.GeneralAltiriaException)2 JsonException (com.altiria.app.exception.JsonException)2 JsonObject (com.google.gson.JsonObject)2 TypeToken (com.google.gson.reflect.TypeToken)2 SocketTimeoutException (java.net.SocketTimeoutException)2