Search in sources :

Example 16 with HttpHead

use of org.apache.http.client.methods.HttpHead in project okta-sdk-java by okta.

the class HttpClientRequestFactory method createHttpClientRequest.

/**
 * Creates an HttpClient method object based on the specified request and
 * populates any parameters, headers, etc. from the original request.
 *
 * @param request        The request to convert to an HttpClient method object.
 * @param previousEntity The optional, previous HTTP entity to reuse in the new request.
 * @return The converted HttpClient method object with any parameters,
 *         headers, etc. from the original request set.
 */
HttpRequestBase createHttpClientRequest(Request request, HttpEntity previousEntity) {
    HttpMethod method = request.getMethod();
    URI uri = getFullyQualifiedUri(request);
    InputStream body = request.getBody();
    long contentLength = request.getHeaders().getContentLength();
    HttpRequestBase base;
    switch(method) {
        case DELETE:
            base = new HttpDelete(uri);
            break;
        case GET:
            base = new HttpGet(uri);
            break;
        case HEAD:
            base = new HttpHead(uri);
            break;
        case POST:
            base = new HttpPost(uri);
            ((HttpEntityEnclosingRequestBase) base).setEntity(new RepeatableInputStreamEntity(request));
            break;
        case PUT:
            base = new HttpPut(uri);
            // Enable 100-continue support for PUT operations, since this is  where we're potentially uploading
            // large amounts of data and want to find out as early as possible if an operation will fail. We
            // don't want to do this for all operations since it will cause extra latency in the network
            // interaction.
            base.setConfig(RequestConfig.copy(defaultRequestConfig).setExpectContinueEnabled(true).build());
            if (previousEntity != null) {
                ((HttpEntityEnclosingRequestBase) base).setEntity(previousEntity);
            } else if (body != null) {
                HttpEntity entity = new RepeatableInputStreamEntity(request);
                if (contentLength < 0) {
                    entity = newBufferedHttpEntity(entity);
                }
                ((HttpEntityEnclosingRequestBase) base).setEntity(entity);
            }
            break;
        default:
            throw new IllegalArgumentException("Unrecognized HttpMethod: " + method);
    }
    base.setProtocolVersion(HttpVersion.HTTP_1_1);
    applyHeaders(base, request);
    return base;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpEntity(org.apache.http.HttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) URI(java.net.URI) HttpHead(org.apache.http.client.methods.HttpHead) HttpPut(org.apache.http.client.methods.HttpPut) HttpMethod(com.okta.sdk.http.HttpMethod)

Example 17 with HttpHead

use of org.apache.http.client.methods.HttpHead in project build-info by JFrogDev.

the class ArtifactoryDependenciesClient method execute.

private HttpResponse execute(String artifactUrl, boolean isHead, Map<String, String> headers) throws IOException {
    PreemptiveHttpClient client = httpClient.getHttpClient();
    artifactUrl = ArtifactoryHttpClient.encodeUrl(artifactUrl);
    HttpRequestBase httpRequest = isHead ? new HttpHead(artifactUrl) : new HttpGet(artifactUrl);
    // Explicitly force keep alive
    httpRequest.setHeader("Connection", "Keep-Alive");
    // Add all required headers to the request
    if (headers != null) {
        for (Map.Entry<String, String> entry : headers.entrySet()) {
            httpRequest.setHeader(entry.getKey(), entry.getValue());
        }
    }
    HttpResponse response = client.execute(httpRequest);
    StatusLine statusLine = response.getStatusLine();
    int statusCode = statusLine.getStatusCode();
    if (statusCode == HttpStatus.SC_NOT_FOUND) {
        throw new FileNotFoundException("Unable to find " + artifactUrl);
    }
    if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_PARTIAL_CONTENT) {
        EntityUtils.consume(response.getEntity());
        throw new IOException("Error downloading " + artifactUrl + ". Code: " + statusCode + " Message: " + statusLine.getReasonPhrase());
    }
    return response;
}
Also used : StatusLine(org.apache.http.StatusLine) PreemptiveHttpClient(org.jfrog.build.client.PreemptiveHttpClient) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpGet(org.apache.http.client.methods.HttpGet) FileNotFoundException(java.io.FileNotFoundException) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) Map(java.util.Map) HttpHead(org.apache.http.client.methods.HttpHead)

Example 18 with HttpHead

use of org.apache.http.client.methods.HttpHead in project stocator by CODAIT.

the class SwiftAPIDirect method getTempUrlObjectLength.

/*
   * Sends a HEAD request to get an object's length
   */
public static long getTempUrlObjectLength(Path path, SwiftConnectionManager scm) throws IOException {
    HttpHead head = new HttpHead(path.toString().replace("swift2d", "https"));
    CloseableHttpResponse response = scm.createHttpConnection().execute(head);
    return Long.parseLong(response.getFirstHeader("Content-Length").getValue());
}
Also used : CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpHead(org.apache.http.client.methods.HttpHead)

Example 19 with HttpHead

use of org.apache.http.client.methods.HttpHead in project OpenTripPlanner by opentripplanner.

the class HttpUtils method testUrl.

public static void testUrl(String url) throws IOException {
    HttpHead head = new HttpHead(url);
    HttpClient httpclient = getClient();
    HttpResponse response = httpclient.execute(head);
    StatusLine status = response.getStatusLine();
    if (status.getStatusCode() == 404) {
        throw new FileNotFoundException();
    }
    if (status.getStatusCode() != 200) {
        throw new RuntimeException("Could not get URL: " + status.getStatusCode() + ": " + status.getReasonPhrase());
    }
}
Also used : StatusLine(org.apache.http.StatusLine) HttpClient(org.apache.http.client.HttpClient) FileNotFoundException(java.io.FileNotFoundException) HttpResponse(org.apache.http.HttpResponse) HttpHead(org.apache.http.client.methods.HttpHead)

Example 20 with HttpHead

use of org.apache.http.client.methods.HttpHead in project fess-crawler by codelibs.

the class HcHttpClient method processHttpMethod.

protected ResponseData processHttpMethod(final String url, final HttpUriRequest httpRequest) {
    try {
        processRobotsTxt(url);
    } catch (final CrawlingAccessException e) {
        if (logger.isInfoEnabled()) {
            final StringBuilder buf = new StringBuilder(100);
            buf.append(e.getMessage());
            if (e.getCause() != null) {
                buf.append(e.getCause().getMessage());
            }
            logger.info(buf.toString());
        } else if (logger.isDebugEnabled()) {
            logger.debug("Crawling Access Exception at " + url, e);
        }
    }
    // request header
    for (final Header header : requestHeaderList) {
        httpRequest.addHeader(header);
    }
    ResponseData responseData = new ResponseData();
    HttpEntity httpEntity = null;
    try {
        // get a content
        final HttpResponse response = executeHttpClient(httpRequest);
        httpEntity = response.getEntity();
        final int httpStatusCode = response.getStatusLine().getStatusCode();
        // redirect
        if (isRedirectHttpStatus(httpStatusCode)) {
            final Header locationHeader = response.getFirstHeader("location");
            if (locationHeader == null) {
                logger.warn("Invalid redirect location at " + url);
            } else {
                final String redirectLocation;
                if (locationHeader.getValue().startsWith("/")) {
                    redirectLocation = buildRedirectLocation(url, locationHeader.getValue());
                } else {
                    redirectLocation = locationHeader.getValue();
                }
                responseData = new ResponseData();
                responseData.setRedirectLocation(redirectLocation);
                return responseData;
            }
        }
        String contentType = null;
        final Header contentTypeHeader = response.getFirstHeader("Content-Type");
        if (contentTypeHeader != null) {
            contentType = contentTypeHeader.getValue();
            final int idx = contentType.indexOf(';');
            if (idx > 0) {
                contentType = contentType.substring(0, idx);
                if (APPLICATION_OCTET_STREAM.equals(contentType)) {
                    contentType = null;
                }
            }
        }
        long contentLength = 0;
        String contentEncoding = Constants.UTF_8;
        if (httpEntity == null) {
            responseData.setResponseBody(new byte[0]);
            if (contentType == null) {
                contentType = defaultMimeType;
            }
        } else {
            final InputStream responseBodyStream = httpEntity.getContent();
            final File outputFile = File.createTempFile("crawler-HcHttpClient-", ".out");
            DeferredFileOutputStream dfos = null;
            try {
                try {
                    dfos = new DeferredFileOutputStream((int) maxCachedContentSize, outputFile);
                    CopyUtil.copy(responseBodyStream, dfos);
                    dfos.flush();
                } finally {
                    CloseableUtil.closeQuietly(dfos);
                }
            } catch (final Exception e) {
                if (!outputFile.delete()) {
                    logger.warn("Could not delete " + outputFile.getAbsolutePath());
                }
                throw e;
            }
            if (dfos.isInMemory()) {
                responseData.setResponseBody(dfos.getData());
                contentLength = dfos.getData().length;
                if (!outputFile.delete()) {
                    logger.warn("Could not delete " + outputFile.getAbsolutePath());
                }
                if (contentType == null) {
                    try (InputStream is = new ByteArrayInputStream(dfos.getData())) {
                        contentType = mimeTypeHelper.getContentType(is, url);
                    } catch (final Exception e) {
                        logger.debug("Failed to detect mime-type.", e);
                        contentType = defaultMimeType;
                    }
                }
            } else {
                responseData.setResponseBody(outputFile, true);
                contentLength = outputFile.length();
                if (contentType == null) {
                    try (InputStream is = new FileInputStream(outputFile)) {
                        contentType = mimeTypeHelper.getContentType(is, url);
                    } catch (final Exception e) {
                        logger.debug("Failed to detect mime-type.", e);
                        contentType = defaultMimeType;
                    }
                }
            }
            final Header contentEncodingHeader = httpEntity.getContentEncoding();
            if (contentEncodingHeader != null) {
                contentEncoding = contentEncodingHeader.getValue();
            }
        }
        // check file size
        if (contentLengthHelper != null) {
            final long maxLength = contentLengthHelper.getMaxLength(contentType);
            if (contentLength > maxLength) {
                throw new MaxLengthExceededException("The content length (" + contentLength + " byte) is over " + maxLength + " byte. The url is " + url);
            }
        }
        responseData.setUrl(url);
        responseData.setCharSet(contentEncoding);
        if (httpRequest instanceof HttpHead) {
            responseData.setMethod(Constants.HEAD_METHOD);
        } else {
            responseData.setMethod(Constants.GET_METHOD);
        }
        responseData.setHttpStatusCode(httpStatusCode);
        for (final Header header : response.getAllHeaders()) {
            responseData.addMetaData(header.getName(), header.getValue());
        }
        responseData.setMimeType(contentType);
        final Header contentLengthHeader = response.getFirstHeader("Content-Length");
        if (contentLengthHeader == null) {
            responseData.setContentLength(contentLength);
        } else {
            final String value = contentLengthHeader.getValue();
            try {
                responseData.setContentLength(Long.parseLong(value));
            } catch (final Exception e) {
                responseData.setContentLength(contentLength);
            }
        }
        checkMaxContentLength(responseData);
        final Header lastModifiedHeader = response.getFirstHeader("Last-Modified");
        if (lastModifiedHeader != null) {
            final String value = lastModifiedHeader.getValue();
            if (StringUtil.isNotBlank(value)) {
                final Date d = parseLastModified(value);
                if (d != null) {
                    responseData.setLastModified(d);
                }
            }
        }
        return responseData;
    } catch (final UnknownHostException e) {
        closeResources(httpRequest, responseData);
        throw new CrawlingAccessException("Unknown host(" + e.getMessage() + "): " + url, e);
    } catch (final NoRouteToHostException e) {
        closeResources(httpRequest, responseData);
        throw new CrawlingAccessException("No route to host(" + e.getMessage() + "): " + url, e);
    } catch (final ConnectException e) {
        closeResources(httpRequest, responseData);
        throw new CrawlingAccessException("Connection time out(" + e.getMessage() + "): " + url, e);
    } catch (final SocketException e) {
        closeResources(httpRequest, responseData);
        throw new CrawlingAccessException("Socket exception(" + e.getMessage() + "): " + url, e);
    } catch (final IOException e) {
        closeResources(httpRequest, responseData);
        throw new CrawlingAccessException("I/O exception(" + e.getMessage() + "): " + url, e);
    } catch (final CrawlerSystemException e) {
        closeResources(httpRequest, responseData);
        throw e;
    } catch (final Exception e) {
        closeResources(httpRequest, responseData);
        throw new CrawlerSystemException("Failed to access " + url, e);
    } finally {
        EntityUtils.consumeQuietly(httpEntity);
    }
}
Also used : SocketException(java.net.SocketException) HttpEntity(org.apache.http.HttpEntity) UnknownHostException(java.net.UnknownHostException) CrawlingAccessException(org.codelibs.fess.crawler.exception.CrawlingAccessException) MaxLengthExceededException(org.codelibs.fess.crawler.exception.MaxLengthExceededException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ResponseData(org.codelibs.fess.crawler.entity.ResponseData) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) NoRouteToHostException(java.net.NoRouteToHostException) CrawlingAccessException(org.codelibs.fess.crawler.exception.CrawlingAccessException) MaxLengthExceededException(org.codelibs.fess.crawler.exception.MaxLengthExceededException) CrawlerSystemException(org.codelibs.fess.crawler.exception.CrawlerSystemException) ParseException(java.text.ParseException) NoRouteToHostException(java.net.NoRouteToHostException) SocketException(java.net.SocketException) ConnectException(java.net.ConnectException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) FileInputStream(java.io.FileInputStream) HttpHead(org.apache.http.client.methods.HttpHead) Date(java.util.Date) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) ByteArrayInputStream(java.io.ByteArrayInputStream) CrawlerSystemException(org.codelibs.fess.crawler.exception.CrawlerSystemException) DeferredFileOutputStream(org.apache.commons.io.output.DeferredFileOutputStream) File(java.io.File) ConnectException(java.net.ConnectException)

Aggregations

HttpHead (org.apache.http.client.methods.HttpHead)100 HttpResponse (org.apache.http.HttpResponse)40 HttpGet (org.apache.http.client.methods.HttpGet)28 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)25 Test (org.junit.Test)24 IOException (java.io.IOException)23 URI (java.net.URI)22 HttpPut (org.apache.http.client.methods.HttpPut)22 Header (org.apache.http.Header)21 HttpPost (org.apache.http.client.methods.HttpPost)21 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)19 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)15 HttpDelete (org.apache.http.client.methods.HttpDelete)13 InputStream (java.io.InputStream)12 HttpEntity (org.apache.http.HttpEntity)10 File (java.io.File)9 StringEntity (org.apache.http.entity.StringEntity)9 HttpOptions (org.apache.http.client.methods.HttpOptions)8 URISyntaxException (java.net.URISyntaxException)6 URL (java.net.URL)6