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