Search in sources :

Example 71 with HttpHead

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);
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) AmazonClientException(com.amazonaws.AmazonClientException) NotFoundException(org.opencastproject.util.NotFoundException) Upload(com.amazonaws.services.s3.transfer.Upload) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) HttpHead(org.apache.http.client.methods.HttpHead) URISyntaxException(java.net.URISyntaxException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) AmazonServiceException(com.amazonaws.AmazonServiceException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) AmazonClientException(com.amazonaws.AmazonClientException) DistributionException(org.opencastproject.distribution.api.DistributionException) NotFoundException(org.opencastproject.util.NotFoundException) ConfigurationException(org.opencastproject.util.ConfigurationException) IOException(java.io.IOException) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) DistributionException(org.opencastproject.distribution.api.DistributionException) File(java.io.File)

Example 72 with HttpHead

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;
}
Also used : CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpHead(org.apache.http.client.methods.HttpHead)

Example 73 with HttpHead

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;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) HttpHead(org.apache.http.client.methods.HttpHead)

Example 74 with HttpHead

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));
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpTrace(org.apache.http.client.methods.HttpTrace) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpGet(org.apache.http.client.methods.HttpGet) HttpOptions(org.apache.http.client.methods.HttpOptions) URI(java.net.URI) HttpHead(org.apache.http.client.methods.HttpHead) HttpPut(org.apache.http.client.methods.HttpPut) HttpPatch(org.apache.http.client.methods.HttpPatch) Test(org.testng.annotations.Test)

Example 75 with HttpHead

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));
        }
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) URI(java.net.URI) HttpHead(org.apache.http.client.methods.HttpHead) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Aggregations

HttpHead (org.apache.http.client.methods.HttpHead)104 HttpResponse (org.apache.http.HttpResponse)42 HttpGet (org.apache.http.client.methods.HttpGet)30 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)27 IOException (java.io.IOException)26 HttpPut (org.apache.http.client.methods.HttpPut)24 Test (org.junit.Test)24 HttpPost (org.apache.http.client.methods.HttpPost)23 URI (java.net.URI)22 Header (org.apache.http.Header)21 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)19 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)16 HttpDelete (org.apache.http.client.methods.HttpDelete)14 InputStream (java.io.InputStream)13 HttpEntity (org.apache.http.HttpEntity)11 File (java.io.File)9 HttpOptions (org.apache.http.client.methods.HttpOptions)9 StringEntity (org.apache.http.entity.StringEntity)9 HttpPatch (org.apache.http.client.methods.HttpPatch)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6