Search in sources :

Example 91 with HttpClientContext

use of org.apache.http.client.protocol.HttpClientContext in project briefcase by opendatakit.

the class AggregateUtils method httpRetrieveXmlDocument.

/**
 * Common method for returning a parsed xml document given a url and the http
 * context and client objects involved in the web connection. The document is
 * the body of the response entity and should be xml.
 *
 * @return
 */
private static final DocumentFetchResult httpRetrieveXmlDocument(HttpUriRequest request, int[] validStatusList, ServerConnectionInfo serverInfo, boolean alwaysResetCredentials, DocumentDescription description, ResponseAction action) throws XmlDocumentFetchException {
    HttpClient httpClient = WebUtils.createHttpClient();
    // get shared HttpContext so that authentication and cookies are retained.
    HttpClientContext localContext = WebUtils.getHttpContext();
    URI uri = request.getURI();
    log.info("Attempting URI {}", uri);
    WebUtils.setCredentials(localContext, serverInfo, uri, alwaysResetCredentials);
    if (description.isCancelled()) {
        throw new XmlDocumentFetchException("Transfer of " + description.getDocumentDescriptionType() + " aborted.");
    }
    HttpResponse response = null;
    try {
        response = httpClient.execute(request, localContext);
        int statusCode = response.getStatusLine().getStatusCode();
        HttpEntity entity = response.getEntity();
        String lcContentType = (entity == null) ? null : entity.getContentType().getValue().toLowerCase();
        XmlDocumentFetchException ex = null;
        boolean statusCodeValid = false;
        for (int i : validStatusList) {
            if (i == statusCode) {
                statusCodeValid = true;
                break;
            }
        }
        if (!statusCodeValid) {
            String webError = response.getStatusLine().getReasonPhrase() + " (" + statusCode + ")";
            if (statusCode == 401) {
                // We reset the Http context to force next request to authenticate itself
                WebUtils.resetHttpContext();
                ex = new XmlDocumentFetchException("Authentication failure");
            } else if (statusCode == 400) {
                ex = new XmlDocumentFetchException(description.getFetchDocFailed() + webError + " while accessing: " + uri.toString() + "\nPlease verify that the " + description.getDocumentDescriptionType() + " that is being uploaded is well-formed.");
            } else {
                ex = new XmlDocumentFetchException(description.getFetchDocFailed() + webError + " while accessing: " + uri.toString() + "\nPlease verify that the URL, your user credentials and your permissions are all correct.");
            }
        } else if (entity == null) {
            log.warn("No entity body returned");
            ex = new XmlDocumentFetchException(description.getFetchDocFailed() + " Server unexpectedly returned no content");
        } else if (!(lcContentType.contains(HTTP_CONTENT_TYPE_TEXT_XML) || lcContentType.contains(HTTP_CONTENT_TYPE_APPLICATION_XML))) {
            log.warn("Wrong ContentType: " + entity.getContentType().getValue() + "returned");
            ex = new XmlDocumentFetchException(description.getFetchDocFailed() + "A non-XML document was returned. A network login screen may be interfering with the transmission to the server.");
        }
        if (ex != null) {
            flushEntityBytes(entity);
            // and throw the exception...
            throw ex;
        }
        // parse the xml document...
        Document doc = null;
        try {
            InputStream is = null;
            InputStreamReader isr = null;
            try {
                is = entity.getContent();
                isr = new InputStreamReader(is, "UTF-8");
                doc = new Document();
                KXmlParser parser = new KXmlParser();
                parser.setInput(isr);
                parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
                doc.parse(parser);
                isr.close();
            } finally {
                if (isr != null) {
                    try {
                        isr.close();
                    } catch (Exception e) {
                    // no-op
                    }
                }
                if (is != null) {
                    try {
                        is.close();
                    } catch (Exception e) {
                    // no-op
                    }
                }
            }
        } catch (Exception e) {
            log.warn("Parsing failed with " + e.getMessage(), e);
            throw new XmlDocumentFetchException(description.getFetchDocFailed());
        }
        // examine header fields...
        // is it an OpenRosa server?
        boolean isOR = false;
        Header[] fields = response.getHeaders(WebUtils.OPEN_ROSA_VERSION_HEADER);
        if (fields != null && fields.length >= 1) {
            isOR = true;
            boolean versionMatch = false;
            boolean first = true;
            StringBuilder b = new StringBuilder();
            for (Header h : fields) {
                if (WebUtils.OPEN_ROSA_VERSION.equals(h.getValue())) {
                    versionMatch = true;
                    break;
                }
                if (!first) {
                    b.append("; ");
                }
                first = false;
                b.append(h.getValue());
            }
            if (!versionMatch) {
                log.warn(WebUtils.OPEN_ROSA_VERSION_HEADER + " unrecognized version(s): " + b);
            }
        }
        // what about location?
        Header[] locations = response.getHeaders("Location");
        if (locations != null && locations.length == 1) {
            try {
                URL url = new URL(locations[0].getValue());
                URI uNew = url.toURI();
                log.info("Redirection to URI {}", uNew);
                if (uri.getHost().equalsIgnoreCase(uNew.getHost())) {
                    // trust the server to tell us a new location
                    // ... and possibly to use https instead.
                    String fullUrl = url.toExternalForm();
                    int idx = fullUrl.lastIndexOf("/");
                    serverInfo.setUrl(fullUrl.substring(0, idx));
                } else {
                    // Don't follow a redirection attempt to a different host.
                    // We can't tell if this is a spoof or not.
                    String msg = description.getFetchDocFailed() + "Unexpected redirection attempt";
                    log.warn(msg);
                    throw new XmlDocumentFetchException(msg);
                }
            } catch (MalformedURLException | URISyntaxException e) {
                String msg = description.getFetchDocFailed() + "Unexpected exception: " + e.getMessage();
                log.warn(msg, e);
                throw new XmlDocumentFetchException(msg);
            }
        }
        DocumentFetchResult result = new DocumentFetchResult(doc, isOR);
        if (action != null) {
            action.doAction(result);
        }
        return result;
    } catch (UnknownHostException e) {
        String msg = description.getFetchDocFailed() + "Unknown host";
        log.warn(msg, e);
        throw new XmlDocumentFetchException(msg);
    } catch (IOException | MetadataUpdateException e) {
        String msg = description.getFetchDocFailed() + "Unexpected exception: " + e;
        log.warn(msg, e);
        throw new XmlDocumentFetchException(msg);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) HttpEntity(org.apache.http.HttpEntity) URISyntaxException(java.net.URISyntaxException) Document(org.kxml2.kdom.Document) URI(java.net.URI) URL(java.net.URL) XmlDocumentFetchException(org.opendatakit.briefcase.model.XmlDocumentFetchException) KXmlParser(org.kxml2.io.KXmlParser) InputStreamReader(java.io.InputStreamReader) UnknownHostException(java.net.UnknownHostException) InputStream(java.io.InputStream) HttpResponse(org.apache.http.HttpResponse) MetadataUpdateException(org.opendatakit.briefcase.model.MetadataUpdateException) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) URISyntaxException(java.net.URISyntaxException) TransmissionException(org.opendatakit.briefcase.model.TransmissionException) MalformedURLException(java.net.MalformedURLException) XmlDocumentFetchException(org.opendatakit.briefcase.model.XmlDocumentFetchException) IOException(java.io.IOException) MetadataUpdateException(org.opendatakit.briefcase.model.MetadataUpdateException) UnknownHostException(java.net.UnknownHostException) Header(org.apache.http.Header) HttpClient(org.apache.http.client.HttpClient)

Example 92 with HttpClientContext

use of org.apache.http.client.protocol.HttpClientContext in project briefcase by opendatakit.

the class AggregateUtils method testServerConnectionWithHeadRequest.

/**
 * Send a HEAD request to the server to confirm the validity of the URL and
 * credentials.
 *
 * @param serverInfo
 * @param actionAddr
 * @return the confirmed URI of this action.
 * @throws TransmissionException
 */
public static final URI testServerConnectionWithHeadRequest(ServerConnectionInfo serverInfo, String actionAddr) throws TransmissionException {
    String urlString = serverInfo.getUrl();
    if (urlString.endsWith("/")) {
        urlString = urlString + actionAddr;
    } else {
        urlString = urlString + "/" + actionAddr;
    }
    log.info("Parsing URL {}", urlString);
    URI u;
    try {
        URL url = new URL(urlString);
        u = url.toURI();
    } catch (MalformedURLException e) {
        String msg = "Invalid url for " + actionAddr + ". Failed with error: " + e.getMessage();
        if (!urlString.toLowerCase().startsWith("http://") && !urlString.toLowerCase().startsWith("https://")) {
            msg += ". Did you forget to prefix the address with 'http://' or 'https://' ?";
        }
        log.warn(msg, e);
        throw new TransmissionException(msg);
    } catch (URISyntaxException e) {
        String msg = "Invalid uri for " + actionAddr + ". Failed with error: " + e.getMessage();
        log.warn(msg, e);
        throw new TransmissionException(msg);
    }
    HttpClient httpClient = WebUtils.createHttpClient();
    // get shared HttpContext so that authentication and cookies are retained.
    HttpClientContext localContext = WebUtils.getHttpContext();
    WebUtils.setCredentials(localContext, serverInfo, u);
    {
        // we need to issue a head request
        HttpHead httpHead = WebUtils.createOpenRosaHttpHead(u);
        // prepare response
        HttpResponse response = null;
        try {
            response = httpClient.execute(httpHead, localContext);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 401) {
                // We reset the Http context to force next request to authenticate itself
                WebUtils.resetHttpContext();
                throw new TransmissionException("Authentication failure");
            } else if (statusCode == 204) {
                Header[] openRosaVersions = response.getHeaders(WebUtils.OPEN_ROSA_VERSION_HEADER);
                if (openRosaVersions == null || openRosaVersions.length == 0) {
                    String msg = "Header missing: " + WebUtils.OPEN_ROSA_VERSION_HEADER;
                    log.warn(msg);
                    throw new TransmissionException(msg);
                }
                Header[] locations = response.getHeaders("Location");
                if (locations != null && locations.length == 1) {
                    try {
                        URL url = new URL(locations[0].getValue());
                        URI uNew = url.toURI();
                        log.info("Redirection to URI {}", uNew);
                        if (u.getHost().equalsIgnoreCase(uNew.getHost())) {
                            // trust the server to tell us a new location
                            // ... and possibly to use https instead.
                            u = uNew;
                            // out.
                            return u;
                        } else {
                            // Don't follow a redirection attempt to a different host.
                            // We can't tell if this is a spoof or not.
                            String msg = "Unexpected redirection attempt";
                            log.warn(msg);
                            throw new TransmissionException(msg);
                        }
                    } catch (Exception e) {
                        String msg = "Unexpected exception: " + e.getLocalizedMessage();
                        log.warn(msg, e);
                        throw new TransmissionException(msg);
                    }
                } else {
                    String msg = "The url is not ODK Aggregate - status code on Head request: " + statusCode;
                    log.warn(msg);
                    throw new TransmissionException(msg);
                }
            } else {
                // may be a server that does not handle HEAD requests
                if (response.getEntity() != null) {
                    try {
                        // don't really care about the stream...
                        InputStream is = response.getEntity().getContent();
                        // read to end of stream...
                        final long count = 1024L;
                        while (is.skip(count) == count) ;
                        is.close();
                    } catch (Exception e) {
                        log.error("failed to process http stream", e);
                    }
                }
                String msg = "The username or password may be incorrect or the url is not ODK Aggregate - status code on Head request: " + statusCode;
                log.warn(msg);
                throw new TransmissionException(msg);
            }
        } catch (Exception e) {
            String msg = "Unexpected exception: " + e.getLocalizedMessage();
            log.warn(msg, e);
            throw new TransmissionException(msg);
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) InputStream(java.io.InputStream) HttpResponse(org.apache.http.HttpResponse) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL) HttpHead(org.apache.http.client.methods.HttpHead) ClientProtocolException(org.apache.http.client.ClientProtocolException) URISyntaxException(java.net.URISyntaxException) TransmissionException(org.opendatakit.briefcase.model.TransmissionException) MalformedURLException(java.net.MalformedURLException) XmlDocumentFetchException(org.opendatakit.briefcase.model.XmlDocumentFetchException) IOException(java.io.IOException) MetadataUpdateException(org.opendatakit.briefcase.model.MetadataUpdateException) UnknownHostException(java.net.UnknownHostException) Header(org.apache.http.Header) TransmissionException(org.opendatakit.briefcase.model.TransmissionException) HttpClient(org.apache.http.client.HttpClient)

Example 93 with HttpClientContext

use of org.apache.http.client.protocol.HttpClientContext in project aliyun-oss-java-sdk by aliyun.

the class DefaultServiceClient method createHttpContext.

protected HttpClientContext createHttpContext() {
    HttpClientContext httpContext = HttpClientContext.create();
    httpContext.setRequestConfig(this.requestConfig);
    if (this.credentialsProvider != null) {
        httpContext.setCredentialsProvider(this.credentialsProvider);
        httpContext.setAuthCache(this.authCache);
    }
    return httpContext;
}
Also used : HttpClientContext(org.apache.http.client.protocol.HttpClientContext)

Example 94 with HttpClientContext

use of org.apache.http.client.protocol.HttpClientContext in project aliyun-oss-java-sdk by aliyun.

the class TimeoutServiceClient method sendRequestCore.

@Override
public ResponseMessage sendRequestCore(ServiceClient.Request request, ExecutionContext context) throws IOException {
    HttpRequestBase httpRequest = httpRequestFactory.createHttpRequest(request, context);
    HttpClientContext httpContext = HttpClientContext.create();
    httpContext.setRequestConfig(this.requestConfig);
    CloseableHttpResponse httpResponse = null;
    HttpRequestTask httpRequestTask = new HttpRequestTask(httpRequest, httpContext);
    Future<CloseableHttpResponse> future = executor.submit(httpRequestTask);
    try {
        httpResponse = future.get(this.config.getRequestTimeout(), TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        logException("[ExecutorService]The current thread was interrupted while waiting: ", e);
        httpRequest.abort();
        throw new ClientException(e.getMessage(), e);
    } catch (ExecutionException e) {
        RuntimeException ex;
        httpRequest.abort();
        if (e.getCause() instanceof IOException) {
            ex = ExceptionFactory.createNetworkException((IOException) e.getCause());
        } else {
            ex = new OSSException(e.getMessage(), e);
        }
        logException("[ExecutorService]The computation threw an exception: ", ex);
        throw ex;
    } catch (TimeoutException e) {
        logException("[ExecutorService]The wait " + this.config.getRequestTimeout() + " timed out: ", e);
        httpRequest.abort();
        throw new ClientException(e.getMessage(), OSSErrorCode.REQUEST_TIMEOUT, "Unknown", e);
    }
    return buildResponse(request, httpResponse);
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) OSSException(com.aliyun.oss.OSSException) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) ClientException(com.aliyun.oss.ClientException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 95 with HttpClientContext

use of org.apache.http.client.protocol.HttpClientContext in project RxJavaInAction by fengzhizi715.

the class RetryHandler method retryRequest.

@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
    if (executionCount >= 3) {
        // 如果已经重试了3次,就放弃
        return false;
    }
    if (exception instanceof NoHttpResponseException) {
        // 如果服务器丢掉了连接,那么就重试
        return true;
    }
    if (exception instanceof SSLHandshakeException) {
        // 不要重试SSL握手异常
        return false;
    }
    if (exception instanceof InterruptedIOException) {
        // 超时
        return true;
    }
    if (exception instanceof UnknownHostException) {
        // 目标服务器不可达
        return false;
    }
    if (exception instanceof ConnectTimeoutException) {
        // 连接被拒绝
        return false;
    }
    if (exception instanceof SSLException) {
        // ssl握手异常
        return false;
    }
    HttpClientContext clientContext = HttpClientContext.adapt(context);
    HttpRequest request = clientContext.getRequest();
    // 如果请求是幂等的,就再次尝试
    if (!(request instanceof HttpEntityEnclosingRequest)) {
        return true;
    }
    return false;
}
Also used : NoHttpResponseException(org.apache.http.NoHttpResponseException) HttpRequest(org.apache.http.HttpRequest) InterruptedIOException(java.io.InterruptedIOException) UnknownHostException(java.net.UnknownHostException) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) SSLException(javax.net.ssl.SSLException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Aggregations

HttpClientContext (org.apache.http.client.protocol.HttpClientContext)160 HttpGet (org.apache.http.client.methods.HttpGet)56 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)54 IOException (java.io.IOException)48 HttpHost (org.apache.http.HttpHost)47 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)45 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)45 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)39 CredentialsProvider (org.apache.http.client.CredentialsProvider)39 URI (java.net.URI)32 HttpResponse (org.apache.http.HttpResponse)32 BasicScheme (org.apache.http.impl.auth.BasicScheme)32 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)32 AuthScope (org.apache.http.auth.AuthScope)31 AuthCache (org.apache.http.client.AuthCache)29 Test (org.junit.Test)29 HttpEntity (org.apache.http.HttpEntity)22 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)21 HttpClient (org.apache.http.client.HttpClient)18 RequestConfig (org.apache.http.client.config.RequestConfig)17