Search in sources :

Example 1 with ResponseContentEncoding

use of org.apache.http.client.protocol.ResponseContentEncoding in project ecf by eclipse.

the class HttpClientRetrieveFileTransfer method openStreams.

/* (non-Javadoc)
	 * @see org.eclipse.ecf.provider.filetransfer.retrieve.AbstractRetrieveFileTransfer#openStreams()
	 */
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 {
        httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, getSocketReadTimeout());
        int connectTimeout = getConnectTimeout();
        httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout);
        setupAuthentication(urlString);
        getMethod = new HttpGet(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())) {
            // $NON-NLS-1$
            Trace.trace(Activator.PLUGIN_ID, "Accept-Encoding: gzip,deflate added to request header");
            // Add the interceptors to provide the gzip
            httpClient.addRequestInterceptor(new RequestAcceptEncoding());
            httpClient.addResponseInterceptor(new ResponseContentEncoding());
        } else {
            // $NON-NLS-1$
            Trace.trace(Activator.PLUGIN_ID, "Accept-Encoding NOT added to header");
        }
        fireConnectStartEvent();
        if (checkAndHandleDone()) {
            return;
        }
        connectingSockets.clear();
        // 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 && !hasForceNTLMProxyOption())
            // $NON-NLS-1$
            throw new IncomingFileTransferException("HttpClient Provider is not configured to support NTLM proxy authentication.", HttpClientOptions.NTLM_PROXY_RESPONSE_CODE);
        if (NTLMProxyDetector.detectSPNEGOProxy(httpContext))
            // $NON-NLS-1$
            throw new BrowseFileTransferException("HttpClient Provider does not support the use of SPNEGO proxy authentication.");
        if (code == HttpURLConnection.HTTP_PARTIAL || code == HttpURLConnection.HTTP_OK) {
            getResponseHeaderValues();
            setInputStream(httpResponse.getEntity().getContent());
            fireReceiveStartEvent();
        } else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
            EntityUtils.consume(httpResponse.getEntity());
            // $NON-NLS-1$
            throw new IncomingFileTransferException(NLS.bind("File not found: {0}", urlString), code);
        } else if (code == HttpURLConnection.HTTP_UNAUTHORIZED) {
            EntityUtils.consume(httpResponse.getEntity());
            throw new IncomingFileTransferException(Messages.HttpClientRetrieveFileTransfer_Unauthorized, code);
        } else if (code == HttpURLConnection.HTTP_FORBIDDEN) {
            EntityUtils.consume(httpResponse.getEntity());
            // $NON-NLS-1$
            throw new IncomingFileTransferException("Forbidden", code);
        } else if (code == HttpURLConnection.HTTP_PROXY_AUTH) {
            EntityUtils.consume(httpResponse.getEntity());
            throw new IncomingFileTransferException(Messages.HttpClientRetrieveFileTransfer_Proxy_Auth_Required, code);
        } else {
            Trace.trace(Activator.PLUGIN_ID, EntityUtils.toString(httpResponse.getEntity()));
            // EntityUtils.consume(httpResponse.getEntity());
            throw new IncomingFileTransferException(NLS.bind(Messages.HttpClientRetrieveFileTransfer_ERROR_GENERAL_RESPONSE_CODE, new Integer(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 : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) BrowseFileTransferException(org.eclipse.ecf.filetransfer.BrowseFileTransferException) HttpGet(org.apache.http.client.methods.HttpGet) ResponseContentEncoding(org.apache.http.client.protocol.ResponseContentEncoding) IncomingFileTransferException(org.eclipse.ecf.filetransfer.IncomingFileTransferException) RequestAcceptEncoding(org.apache.http.client.protocol.RequestAcceptEncoding) ECFRuntimeException(org.eclipse.ecf.core.util.ECFRuntimeException) BrowseFileTransferException(org.eclipse.ecf.filetransfer.BrowseFileTransferException) IncomingFileTransferException(org.eclipse.ecf.filetransfer.IncomingFileTransferException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) UnsupportedCallbackException(org.eclipse.ecf.core.security.UnsupportedCallbackException) InvalidFileRangeSpecificationException(org.eclipse.ecf.filetransfer.InvalidFileRangeSpecificationException)

Example 2 with ResponseContentEncoding

use of org.apache.http.client.protocol.ResponseContentEncoding in project opennms by OpenNMS.

the class HttpUrlConnection method connect.

/* (non-Javadoc)
     * @see java.net.URLConnection#connect()
     */
@Override
public void connect() throws IOException {
    if (m_clientWrapper != null) {
        return;
    }
    m_clientWrapper = HttpClientWrapper.create();
    if (m_request != null) {
        int timeout = m_request.getParameterAsInt("timeout");
        if (timeout > 0) {
            m_clientWrapper.setConnectionTimeout(timeout).setSocketTimeout(timeout);
        }
        int retries = m_request.getParameterAsInt("retries");
        if (retries == 0) {
            retries = m_request.getParameterAsInt("retry");
        }
        if (retries > 0) {
            m_clientWrapper.setRetries(retries);
        }
        String disableSslVerification = m_request.getParameter("disable-ssl-verification");
        if (Boolean.parseBoolean(disableSslVerification)) {
            try {
                m_clientWrapper.useRelaxedSSL("https");
            } catch (final GeneralSecurityException e) {
                LOG.warn("Failed to set up relaxed SSL.", e);
            }
        }
    }
    m_clientWrapper.addRequestInterceptor(new RequestAcceptEncoding()).addResponseInterceptor(new ResponseContentEncoding());
    // Add User Authentication
    String[] userInfo = m_url.getUserInfo() == null ? null : m_url.getUserInfo().split(":");
    if (userInfo != null && userInfo.length == 2) {
        // If the URL contains a username/password, it might need to be decoded
        String uname = URLDecoder.decode(userInfo[0], StandardCharsets.UTF_8.name());
        String pwd = URLDecoder.decode(userInfo[1], StandardCharsets.UTF_8.name());
        m_clientWrapper.addBasicCredentials(uname, pwd);
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) ResponseContentEncoding(org.apache.http.client.protocol.ResponseContentEncoding) RequestAcceptEncoding(org.apache.http.client.protocol.RequestAcceptEncoding)

Aggregations

RequestAcceptEncoding (org.apache.http.client.protocol.RequestAcceptEncoding)2 ResponseContentEncoding (org.apache.http.client.protocol.ResponseContentEncoding)2 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 HttpGet (org.apache.http.client.methods.HttpGet)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 UnsupportedCallbackException (org.eclipse.ecf.core.security.UnsupportedCallbackException)1 ECFRuntimeException (org.eclipse.ecf.core.util.ECFRuntimeException)1 BrowseFileTransferException (org.eclipse.ecf.filetransfer.BrowseFileTransferException)1 IncomingFileTransferException (org.eclipse.ecf.filetransfer.IncomingFileTransferException)1 InvalidFileRangeSpecificationException (org.eclipse.ecf.filetransfer.InvalidFileRangeSpecificationException)1