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;
}
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;
}
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());
}
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());
}
}
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);
}
}
Aggregations