use of org.apache.http.client.methods.HttpHead in project opencast by opencast.
the class AwsS3DistributionServiceImpl method distributeElement.
/**
* Distribute a media package element to AWS S3.
*
* @param mediaPackage
* The media package that contains the element to distribute.
* @param element
* The element that should be distributed contained within the media package.
* @param checkAvailability
* Checks if the distributed element is available
* @return A reference to the MediaPackageElement that has been distributed.
* @throws DistributionException
*/
public MediaPackageElement distributeElement(String channelId, final MediaPackage mediaPackage, MediaPackageElement element, boolean checkAvailability) throws DistributionException {
notNull(channelId, "channelId");
notNull(mediaPackage, "mediapackage");
notNull(element, "element");
try {
File source;
try {
source = workspace.get(element.getURI());
} catch (NotFoundException e) {
throw new DistributionException("Unable to find " + element.getURI() + " in the workspace", e);
} catch (IOException e) {
throw new DistributionException("Error loading " + element.getURI() + " from the workspace", e);
}
// Use TransferManager to take advantage of multipart upload.
// TransferManager processes all transfers asynchronously, so this call will return immediately.
String objectName = buildObjectName(channelId, mediaPackage.getIdentifier().toString(), element);
logger.info("Uploading {} to bucket {}...", objectName, bucketName);
Upload upload = s3TransferManager.upload(bucketName, objectName, source);
long start = System.currentTimeMillis();
try {
// Block and wait for the upload to finish
upload.waitForCompletion();
logger.info("Upload of {} to bucket {} completed in {} seconds", objectName, bucketName, (System.currentTimeMillis() - start) / 1000);
} catch (AmazonClientException e) {
throw new DistributionException("AWS error: " + e.getMessage(), e);
}
// Create a representation of the distributed file in the media package
MediaPackageElement distributedElement = (MediaPackageElement) element.clone();
try {
distributedElement.setURI(getDistributionUri(objectName));
} catch (URISyntaxException e) {
throw new DistributionException("Distributed element produces an invalid URI", e);
}
logger.info("Distributed element {}, object {}", element.getIdentifier(), objectName);
if (checkAvailability) {
URI uri = distributedElement.getURI();
int tries = 0;
CloseableHttpResponse response = null;
boolean success = false;
while (tries < MAX_TRIES) {
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
logger.trace("Trying to access {}", uri);
response = httpClient.execute(new HttpHead(uri));
if (response.getStatusLine().getStatusCode() == HttpServletResponse.SC_OK) {
logger.trace("Successfully got {}", uri);
success = true;
// Exit the loop, response is closed
break;
} else {
logger.debug("Http status code when checking distributed element {} is {}", objectName, response.getStatusLine().getStatusCode());
}
} catch (Exception e) {
logger.info("Checking availability of {} threw exception {}. Trying again.", objectName, e.getMessage());
// Just try again
} finally {
if (null != response) {
response.close();
}
}
tries++;
logger.trace("Sleeping for {} seconds...", SLEEP_INTERVAL / 1000);
Thread.sleep(SLEEP_INTERVAL);
}
if (!success) {
logger.warn("Could not check availability of distributed file {}", uri);
// throw new DistributionException("Unable to load distributed file " + uri.toString());
}
}
return distributedElement;
} catch (Exception e) {
logger.warn("Error distributing element " + element.getIdentifier() + " of media package " + mediaPackage, e);
if (e instanceof DistributionException) {
throw (DistributionException) e;
} else {
throw new DistributionException(e);
}
}
}
use of org.apache.http.client.methods.HttpHead in project alien4cloud by alien4cloud.
the class RestClient method isApplicationUrlAvailable.
public Boolean isApplicationUrlAvailable() throws IOException {
log.debug("Send head request to [" + applicationUrl + "]");
HttpHead httpHead = new HttpHead(applicationUrl);
CloseableHttpResponse response = httpClient.execute(httpHead);
log.debug("response status of head request to [" + applicationUrl + "] is: " + response.getStatusLine().getStatusCode());
return response.getStatusLine().getStatusCode() == HttpStatus.SC_OK;
}
use of org.apache.http.client.methods.HttpHead in project eol-globi-data by jhpoelen.
the class ResourceUtil method resourceExists.
public static boolean resourceExists(URI descriptor) {
boolean exists = false;
try {
if (isHttpURI(descriptor)) {
HttpResponse resp = HttpUtil.getHttpClient().execute(new HttpHead(descriptor));
exists = resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK;
} else {
InputStream input = asInputStream(descriptor.toString());
IOUtils.closeQuietly(input);
exists = input != null;
}
} catch (IOException e) {
//
}
return exists;
}
use of org.apache.http.client.methods.HttpHead in project vespa by vespa-engine.
the class JDiscHttpServletTest method requireThatServerRespondsToAllMethods.
@Test
public void requireThatServerRespondsToAllMethods() throws Exception {
final TestDriver driver = TestDrivers.newInstance(newEchoHandler());
final URI uri = driver.client().newUri("/status.html");
driver.client().execute(new HttpGet(uri)).expectStatusCode(is(OK));
driver.client().execute(new HttpPost(uri)).expectStatusCode(is(OK));
driver.client().execute(new HttpHead(uri)).expectStatusCode(is(OK));
driver.client().execute(new HttpPut(uri)).expectStatusCode(is(OK));
driver.client().execute(new HttpDelete(uri)).expectStatusCode(is(OK));
driver.client().execute(new HttpOptions(uri)).expectStatusCode(is(OK));
driver.client().execute(new HttpTrace(uri)).expectStatusCode(is(OK));
driver.client().execute(new HttpPatch(uri)).expectStatusCode(is(OK));
assertThat(driver.close(), is(true));
}
use of org.apache.http.client.methods.HttpHead in project wildfly by wildfly.
the class AbstractWebFailoverTestCase method testNonPrimaryOwner.
@Test
public void testNonPrimaryOwner(@ArquillianResource(SimpleServlet.class) @OperateOnDeployment(DEPLOYMENT_1) URL baseURL1, @ArquillianResource(SimpleServlet.class) @OperateOnDeployment(DEPLOYMENT_2) URL baseURL2, @ArquillianResource(SimpleServlet.class) @OperateOnDeployment(DEPLOYMENT_3) URL baseURL3) throws Exception {
URI uri1 = SimpleServlet.createURI(baseURL1);
URI uri2 = SimpleServlet.createURI(baseURL2);
URI uri3 = SimpleServlet.createURI(baseURL3);
try (CloseableHttpClient client = TestHttpClientUtils.promiscuousCookieHttpClient()) {
// Create session, establishing primary owner
try (CloseableHttpResponse response = client.execute(new HttpHead(uri1))) {
Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertFalse(response.containsHeader(SimpleServlet.VALUE_HEADER));
}
// Test session attribute creation/mutation on non-owners
for (URI uri : Arrays.asList(uri2, uri3)) {
// Validate correct mutation using different session access patterns
for (HttpUriRequest request : Arrays.asList(new HttpGet(uri), new HttpPost(uri))) {
this.nonTxWait.run();
int value = 1;
try (CloseableHttpResponse response = client.execute(request)) {
Assert.assertEquals(request.getMethod(), HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertEquals(request.getMethod(), value++, Integer.parseInt(response.getFirstHeader(SimpleServlet.VALUE_HEADER).getValue()));
}
try (CloseableHttpResponse response = client.execute(request)) {
Assert.assertEquals(request.getMethod(), HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertEquals(request.getMethod(), value++, Integer.parseInt(response.getFirstHeader(SimpleServlet.VALUE_HEADER).getValue()));
}
// Remove attribute so we can try again on another non-owner
try (CloseableHttpResponse response = client.execute(new HttpPut(uri))) {
Assert.assertEquals(request.getMethod(), HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
}
}
}
this.nonTxWait.run();
// Destroy session
try (CloseableHttpResponse response = client.execute(new HttpDelete(uri1))) {
Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertFalse(response.containsHeader(SimpleServlet.VALUE_HEADER));
}
}
}
Aggregations