Search in sources :

Example 31 with HttpHead

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

the class ConditionalsTest method testPutCheckLastModified.

// private DateFormat HTTPDATEFORMAT = new SimpleDateFormat("EEE, dd MMM
// yyyy HH:mm:ss ZZZ", Locale.ENGLISH);
public void testPutCheckLastModified() throws IOException, ParseException {
    String testUri = this.uri.toString() + (this.uri.toString().endsWith("/") ? "" : "/") + "testPutCheckLastModified";
    try {
        // create test resource
        {
            HttpPut put = new HttpPut(testUri);
            put.setEntity(new StringEntity("foobar"));
            HttpResponse response = this.client.execute(put, this.context);
            int status = response.getStatusLine().getStatusCode();
            assertEquals(201, status);
        }
        long created = System.currentTimeMillis();
        // get last modified date
        Header etag = null;
        Header lm = null;
        {
            HttpHead head = new HttpHead(testUri);
            HttpResponse response = this.client.execute(head, this.context);
            int status = response.getStatusLine().getStatusCode();
            assertEquals(200, status);
            lm = response.getFirstHeader("last-modified");
            assertNotNull(lm);
            etag = response.getFirstHeader("etag");
        }
        // Date created = HTTPDATEFORMAT.parse(lm.getValue());
        // conditional GET
        {
            HttpGet get = new HttpGet(testUri);
            get.setHeader("If-Modified-Since", lm.getValue());
            HttpResponse response = this.client.execute(get, this.context);
            int status = response.getStatusLine().getStatusCode();
            assertEquals(304, status);
            if (etag != null) {
                Header newetag = response.getFirstHeader("etag");
                assertNotNull(newetag);
                assertEquals(etag.getValue(), newetag.getValue());
            }
        }
        // conditional HEAD
        {
            HttpHead head = new HttpHead(testUri);
            head.setHeader("If-Modified-Since", lm.getValue());
            HttpResponse response = this.client.execute(head, this.context);
            int status = response.getStatusLine().getStatusCode();
            assertEquals(304, status);
            if (etag != null) {
                Header newetag = response.getFirstHeader("etag");
                assertNotNull(newetag);
                assertEquals(etag.getValue(), newetag.getValue());
            }
        }
        // conditional HEAD with broken date (MUST ignore header field)
        {
            HttpHead head = new HttpHead(testUri);
            head.setHeader("If-Modified-Since", "broken");
            HttpResponse response = this.client.execute(head, this.context);
            int status = response.getStatusLine().getStatusCode();
            assertEquals(200, status);
        }
        // conditional GET with broken date (MUST ignore header field)
        {
            HttpGet req = new HttpGet(testUri);
            req.addHeader("If-Modified-Since", lm.getValue());
            req.addHeader("If-Modified-Since", "foo");
            HttpResponse response = this.client.execute(req, this.context);
            int status = response.getStatusLine().getStatusCode();
            assertEquals(200, status);
            EntityUtils.consume(response.getEntity());
        }
        // let one sec elapse
        while (System.currentTimeMillis() < created + 1000) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException ignored) {
            }
        }
        // verify last modified did not change
        {
            HttpHead head = new HttpHead(testUri);
            HttpResponse response = this.client.execute(head, this.context);
            int status = response.getStatusLine().getStatusCode();
            assertEquals(200, status);
            Header newlm = response.getFirstHeader("last-modified");
            assertNotNull(newlm);
            assertEquals(lm.getValue(), newlm.getValue());
        }
        // conditional PUT
        {
            HttpPut put = new HttpPut(testUri);
            put.setHeader("If-Unmodified-Since", lm.getValue());
            put.setEntity(new StringEntity("qux"));
            HttpResponse response = this.client.execute(put, this.context);
            int status = response.getStatusLine().getStatusCode();
            assertEquals(204, status);
        }
        // conditional PUT once more should fail
        {
            HttpPut put = new HttpPut(testUri);
            put.setHeader("If-Unmodified-Since", lm.getValue());
            put.setEntity(new StringEntity("lazydog"));
            HttpResponse response = this.client.execute(put, this.context);
            int status = response.getStatusLine().getStatusCode();
            assertEquals(412, status);
        }
        // conditional PUT with broken If-Unmodified-Since should pass
        {
            HttpPut put = new HttpPut(testUri);
            put.addHeader("If-Unmodified-Since", lm.getValue());
            put.addHeader("If-Unmodified-Since", "foo");
            put.setEntity(new StringEntity("qux"));
            HttpResponse response = this.client.execute(put, this.context);
            int status = response.getStatusLine().getStatusCode();
            assertEquals(204, status);
        }
    } finally {
        delete(testUri);
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) Header(org.apache.http.Header) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) HttpPut(org.apache.http.client.methods.HttpPut) HttpHead(org.apache.http.client.methods.HttpHead)

Example 32 with HttpHead

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

the class ValueLoader method loadHeaders.

public Map<String, String> loadHeaders(String uri, String[] headerNames) throws IOException, RepositoryException {
    HttpHead request = new HttpHead(uri);
    try {
        HttpResponse response = client.execute(request, context);
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode == DavServletResponse.SC_OK) {
            Map<String, String> headers = new HashMap<String, String>();
            for (String name : headerNames) {
                Header hdr = response.getFirstHeader(name);
                if (hdr != null) {
                    headers.put(name, hdr.getValue());
                }
            }
            return headers;
        } else {
            throw ExceptionConverter.generate(new DavException(statusCode, ("Unable to load headers at " + uri + " - Status line = " + response.getStatusLine().toString())));
        }
    } finally {
        request.releaseConnection();
    }
}
Also used : Header(org.apache.http.Header) HashMap(java.util.HashMap) DavException(org.apache.jackrabbit.webdav.DavException) HttpResponse(org.apache.http.HttpResponse) HttpHead(org.apache.http.client.methods.HttpHead)

Example 33 with HttpHead

use of org.apache.http.client.methods.HttpHead in project TaEmCasa by Dionen.

the class HttpClientStackTest method createHeadRequest.

@Test
public void createHeadRequest() throws Exception {
    TestRequest.Head request = new TestRequest.Head();
    assertEquals(request.getMethod(), Method.HEAD);
    HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null);
    assertTrue(httpRequest instanceof HttpHead);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpHead(org.apache.http.client.methods.HttpHead) HttpHead(org.apache.http.client.methods.HttpHead) TestRequest(com.android.volley.mock.TestRequest) Test(org.junit.Test)

Example 34 with HttpHead

use of org.apache.http.client.methods.HttpHead in project undertow by undertow-io.

the class SimpleBlockingServerTestCase method testHeadRequests.

@Test
public void testHeadRequests() throws IOException {
    message = "My HTTP Request!";
    TestHttpClient client = new TestHttpClient();
    HttpHead head = new HttpHead(DefaultServer.getDefaultServerURL() + "/path");
    try {
        for (int i = 0; i < 3; ++i) {
            // WFLY-1540 run a few requests to make sure persistent re
            HttpResponse result = client.execute(head);
            Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
            Assert.assertEquals("", HttpClientUtils.readResponse(result));
            Assert.assertEquals(message.length() + "", result.getFirstHeader(Headers.CONTENT_LENGTH_STRING).getValue());
        }
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpResponse(org.apache.http.HttpResponse) HttpHead(org.apache.http.client.methods.HttpHead) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 35 with HttpHead

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

the class HttpRequestHandler method getHttpStatus.

public int getHttpStatus(String url) throws IOException {
    String encodedUrl = getEncodedUrl(url);
    CloseableHttpClient httpClient = getHttpClient(encodedUrl);
    HttpHead head = new HttpHead(encodedUrl);
    try {
        LOGGER.debug("executing head request to retrieve page status on " + head.getURI());
        HttpResponse response = httpClient.execute(head);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("received " + response.getStatusLine().getStatusCode() + " from head request");
            for (Header h : head.getAllHeaders()) {
                LOGGER.debug("header : " + h.getName() + " " + h.getValue());
            }
        }
        return response.getStatusLine().getStatusCode();
    } catch (UnknownHostException uhe) {
        LOGGER.warn("UnknownHostException on " + encodedUrl);
        return HttpStatus.SC_NOT_FOUND;
    } catch (IllegalArgumentException iae) {
        LOGGER.warn("IllegalArgumentException on " + encodedUrl);
        return HttpStatus.SC_NOT_FOUND;
    } catch (IOException ioe) {
        LOGGER.warn("IOException on " + encodedUrl);
        return HttpStatus.SC_NOT_FOUND;
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        head.releaseConnection();
        httpClient.close();
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Header(org.apache.http.Header) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) HttpHead(org.apache.http.client.methods.HttpHead)

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