List of usage examples for org.apache.commons.httpclient HttpMethodBase getResponseHeader
@Override
public Header getResponseHeader(String headerName)
From source file:org.apache.sling.commons.testing.integration.HttpTestBase.java
/** retrieve the contents of given URL and assert its content type * @param expectedContentType use CONTENT_TYPE_DONTCARE if must not be checked * @param httpMethod supports just GET and POST methods * @throws IOException/*from w w w. j a v a2s.co m*/ * @throws HttpException */ public String getContent(String url, String expectedContentType, List<NameValuePair> params, int expectedStatusCode, String httpMethod) throws IOException { HttpMethodBase method = null; if (HTTP_METHOD_GET.equals(httpMethod)) { method = new GetMethod(url); } else if (HTTP_METHOD_POST.equals(httpMethod)) { method = new PostMethod(url); } else { fail("Http Method not supported in this test suite, method: " + httpMethod); } if (params != null) { final NameValuePair[] nvp = new NameValuePair[0]; method.setQueryString(params.toArray(nvp)); } final int status = httpClient.executeMethod(method); final String content = getResponseBodyAsStream(method, 0); assertEquals("Expected status " + expectedStatusCode + " for " + url + " (content=" + content + ")", expectedStatusCode, status); final Header h = method.getResponseHeader("Content-Type"); if (expectedContentType == null) { if (h != null) { fail("Expected null Content-Type, got " + h.getValue()); } } else if (CONTENT_TYPE_DONTCARE.equals(expectedContentType)) { // no check } else if (h == null) { fail("Expected Content-Type that starts with '" + expectedContentType + " but got no Content-Type header at " + url); } else { assertTrue("Expected Content-Type that starts with '" + expectedContentType + "' for " + url + ", got '" + h.getValue() + "'", h.getValue().startsWith(expectedContentType)); } return content.toString(); }
From source file:org.apache.solr.servlet.CacheHeaderTest.java
protected void checkVetoHeaders(HttpMethodBase m, boolean checkExpires) throws Exception { Header head = m.getResponseHeader("Cache-Control"); assertNotNull("We got no Cache-Control header", head); assertTrue("We got no no-cache in the Cache-Control header", head.getValue().contains("no-cache")); assertTrue("We got no no-store in the Cache-Control header", head.getValue().contains("no-store")); head = m.getResponseHeader("Pragma"); assertNotNull("We got no Pragma header", head); assertEquals("no-cache", head.getValue()); if (checkExpires) { head = m.getResponseHeader("Expires"); assertNotNull("We got no Expires header:" + m.getResponseHeaders(), head); Date d = DateUtil.parseDate(head.getValue()); assertTrue("We got no Expires header far in the past", System.currentTimeMillis() - d.getTime() > 100000); }// ww w .j av a2 s. co m }
From source file:org.apache.solr.servlet.CacheHeaderTest.java
protected void doLastModified(String method) throws Exception { // We do a first request to get the last modified // This must result in a 200 OK response HttpMethodBase get = getSelectMethod(method); getClient().executeMethod(get);// w w w .j a v a 2 s .c om checkResponseBody(method, get); assertEquals("Got no response code 200 in initial request", 200, get.getStatusCode()); Header head = get.getResponseHeader("Last-Modified"); assertNotNull("We got no Last-Modified header", head); Date lastModified = DateUtil.parseDate(head.getValue()); // If-Modified-Since tests get = getSelectMethod(method); get.addRequestHeader("If-Modified-Since", DateUtil.formatDate(new Date())); getClient().executeMethod(get); checkResponseBody(method, get); assertEquals("Expected 304 NotModified response with current date", 304, get.getStatusCode()); get = getSelectMethod(method); get.addRequestHeader("If-Modified-Since", DateUtil.formatDate(new Date(lastModified.getTime() - 10000))); getClient().executeMethod(get); checkResponseBody(method, get); assertEquals("Expected 200 OK response with If-Modified-Since in the past", 200, get.getStatusCode()); // If-Unmodified-Since tests get = getSelectMethod(method); get.addRequestHeader("If-Unmodified-Since", DateUtil.formatDate(new Date(lastModified.getTime() - 10000))); getClient().executeMethod(get); checkResponseBody(method, get); assertEquals("Expected 412 Precondition failed with If-Unmodified-Since in the past", 412, get.getStatusCode()); get = getSelectMethod(method); get.addRequestHeader("If-Unmodified-Since", DateUtil.formatDate(new Date())); getClient().executeMethod(get); checkResponseBody(method, get); assertEquals("Expected 200 OK response with If-Unmodified-Since and current date", 200, get.getStatusCode()); }
From source file:org.apache.solr.servlet.CacheHeaderTest.java
protected void doETag(String method) throws Exception { HttpMethodBase get = getSelectMethod(method); getClient().executeMethod(get);//from ww w . j av a2 s .com checkResponseBody(method, get); assertEquals("Got no response code 200 in initial request", 200, get.getStatusCode()); Header head = get.getResponseHeader("ETag"); assertNotNull("We got no ETag in the response", head); assertTrue("Not a valid ETag", head.getValue().startsWith("\"") && head.getValue().endsWith("\"")); String etag = head.getValue(); // If-None-Match tests // we set a non matching ETag get = getSelectMethod(method); get.addRequestHeader("If-None-Match", "\"xyz123456\""); getClient().executeMethod(get); checkResponseBody(method, get); assertEquals("If-None-Match: Got no response code 200 in response to non matching ETag", 200, get.getStatusCode()); // now we set matching ETags get = getSelectMethod(method); get.addRequestHeader("If-None-Match", "\"xyz1223\""); get.addRequestHeader("If-None-Match", "\"1231323423\", \"1211211\", " + etag); getClient().executeMethod(get); checkResponseBody(method, get); assertEquals("If-None-Match: Got no response 304 to matching ETag", 304, get.getStatusCode()); // we now set the special star ETag get = getSelectMethod(method); get.addRequestHeader("If-None-Match", "*"); getClient().executeMethod(get); checkResponseBody(method, get); assertEquals("If-None-Match: Got no response 304 for star ETag", 304, get.getStatusCode()); // If-Match tests // we set a non matching ETag get = getSelectMethod(method); get.addRequestHeader("If-Match", "\"xyz123456\""); getClient().executeMethod(get); checkResponseBody(method, get); assertEquals("If-Match: Got no response code 412 in response to non matching ETag", 412, get.getStatusCode()); // now we set matching ETags get = getSelectMethod(method); get.addRequestHeader("If-Match", "\"xyz1223\""); get.addRequestHeader("If-Match", "\"1231323423\", \"1211211\", " + etag); getClient().executeMethod(get); checkResponseBody(method, get); assertEquals("If-Match: Got no response 200 to matching ETag", 200, get.getStatusCode()); // now we set the special star ETag get = getSelectMethod(method); get.addRequestHeader("If-Match", "*"); getClient().executeMethod(get); checkResponseBody(method, get); assertEquals("If-Match: Got no response 200 to star ETag", 200, get.getStatusCode()); }
From source file:org.apache.solr.servlet.CacheHeaderTest.java
protected void doCacheControl(String method) throws Exception { if ("POST".equals(method)) { HttpMethodBase m = getSelectMethod(method); getClient().executeMethod(m);/*from ww w . j ava 2 s . c o m*/ checkResponseBody(method, m); Header head = m.getResponseHeader("Cache-Control"); assertNull("We got a cache-control header in response to POST", head); head = m.getResponseHeader("Expires"); assertNull("We got an Expires header in response to POST", head); } else { HttpMethodBase m = getSelectMethod(method); getClient().executeMethod(m); checkResponseBody(method, m); Header head = m.getResponseHeader("Cache-Control"); assertNotNull("We got no cache-control header", head); head = m.getResponseHeader("Expires"); assertNotNull("We got no Expires header in response", head); } }
From source file:org.apache.solr.servlet.NoCacheHeaderTest.java
protected void doLastModified(String method) throws Exception { // We do a first request to get the last modified // This must result in a 200 OK response HttpMethodBase get = getSelectMethod(method); getClient().executeMethod(get);/*from w w w . j a v a2 s . c om*/ checkResponseBody(method, get); assertEquals("Got no response code 200 in initial request", 200, get.getStatusCode()); Header head = get.getResponseHeader("Last-Modified"); assertNull("We got a Last-Modified header", head); // If-Modified-Since tests get = getSelectMethod(method); get.addRequestHeader("If-Modified-Since", DateUtil.formatDate(new Date())); getClient().executeMethod(get); checkResponseBody(method, get); assertEquals("Expected 200 with If-Modified-Since header. We should never get a 304 here", 200, get.getStatusCode()); get = getSelectMethod(method); get.addRequestHeader("If-Modified-Since", DateUtil.formatDate(new Date(System.currentTimeMillis() - 10000))); getClient().executeMethod(get); checkResponseBody(method, get); assertEquals("Expected 200 with If-Modified-Since header. We should never get a 304 here", 200, get.getStatusCode()); // If-Unmodified-Since tests get = getSelectMethod(method); get.addRequestHeader("If-Unmodified-Since", DateUtil.formatDate(new Date(System.currentTimeMillis() - 10000))); getClient().executeMethod(get); checkResponseBody(method, get); assertEquals("Expected 200 with If-Unmodified-Since header. We should never get a 304 here", 200, get.getStatusCode()); get = getSelectMethod(method); get.addRequestHeader("If-Unmodified-Since", DateUtil.formatDate(new Date())); getClient().executeMethod(get); checkResponseBody(method, get); assertEquals("Expected 200 with If-Unmodified-Since header. We should never get a 304 here", 200, get.getStatusCode()); }
From source file:org.apache.solr.servlet.NoCacheHeaderTest.java
protected void doETag(String method) throws Exception { HttpMethodBase get = getSelectMethod(method); getClient().executeMethod(get);//from www. jav a2s. c o m checkResponseBody(method, get); assertEquals("Got no response code 200 in initial request", 200, get.getStatusCode()); Header head = get.getResponseHeader("ETag"); assertNull("We got an ETag in the response", head); // If-None-Match tests // we set a non matching ETag get = getSelectMethod(method); get.addRequestHeader("If-None-Match", "\"xyz123456\""); getClient().executeMethod(get); checkResponseBody(method, get); assertEquals("If-None-Match: Got no response code 200 in response to non matching ETag", 200, get.getStatusCode()); // we now set the special star ETag get = getSelectMethod(method); get.addRequestHeader("If-None-Match", "*"); getClient().executeMethod(get); checkResponseBody(method, get); assertEquals("If-None-Match: Got no response 200 for star ETag", 200, get.getStatusCode()); // If-Match tests // we set a non matching ETag get = getSelectMethod(method); get.addRequestHeader("If-Match", "\"xyz123456\""); getClient().executeMethod(get); checkResponseBody(method, get); assertEquals("If-Match: Got no response code 200 in response to non matching ETag", 200, get.getStatusCode()); // now we set the special star ETag get = getSelectMethod(method); get.addRequestHeader("If-Match", "*"); getClient().executeMethod(get); checkResponseBody(method, get); assertEquals("If-Match: Got no response 200 to star ETag", 200, get.getStatusCode()); }
From source file:org.apache.solr.servlet.NoCacheHeaderTest.java
protected void doCacheControl(String method) throws Exception { HttpMethodBase m = getSelectMethod(method); getClient().executeMethod(m);//from w w w . j a v a 2s . c om checkResponseBody(method, m); Header head = m.getResponseHeader("Cache-Control"); assertNull("We got a cache-control header in response", head); head = m.getResponseHeader("Expires"); assertNull("We got an Expires header in response", head); }
From source file:org.apache.synapse.mediators.json.SynapseJsonSender.java
private void processResponse(HttpMethodBase httpMethod, MessageContext msgContext) throws IOException { obtainHTTPHeaderInformation(httpMethod, msgContext); InputStream in = httpMethod.getResponseBodyAsStream(); Header contentEncoding = httpMethod.getResponseHeader(HTTPConstants.HEADER_CONTENT_ENCODING); if (contentEncoding != null) { if (contentEncoding.getValue().equalsIgnoreCase(HTTPConstants.COMPRESSION_GZIP)) { in = new GZIPInputStream(in); } else {// w w w .j ava 2 s. c o m throw new AxisFault( "HTTP :" + "unsupported content-encoding of '" + contentEncoding.getValue() + "' found"); } } if (in == null) { throw new AxisFault(Messages.getMessage("canNotBeNull", "InputStream")); } if (msgContext.getOperationContext() != null) { msgContext.getOperationContext().setProperty(MessageContext.TRANSPORT_IN, in); } }
From source file:org.apache.synapse.mediators.json.SynapseJsonSender.java
private void obtainHTTPHeaderInformation(HttpMethodBase method, MessageContext msgContext) { Header header = method.getResponseHeader(HTTPConstants.HEADER_CONTENT_TYPE); if (header != null) { HeaderElement[] headers = header.getElements(); for (int i = 0; i < headers.length; i++) { NameValuePair charsetEnc = headers[i].getParameterByName(HTTPConstants.CHAR_SET_ENCODING); OperationContext opContext = msgContext.getOperationContext(); if (charsetEnc != null) { if (opContext != null) { opContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charsetEnc.getValue()); }/*from w w w .j av a2 s . co m*/ } } } }