List of usage examples for org.apache.commons.httpclient HttpMethod getResponseBodyAsString
public abstract String getResponseBodyAsString() throws IOException;
From source file:net.sf.ehcache.constructs.web.filter.SimpleCachingHeadersPageCachingFilterTest.java
/** * When the servlet container generates a 404 page not found, we want to pass * it through without caching and without adding anything to it. * <p/>/*from w ww . ja v a 2 s .c o m*/ * Manual Test: wget -d --server-response --header='Accept-Encoding: gzip' http://localhost:9090/non_ok/SendRedirect.jsp */ @Test public void testRedirect() throws Exception { String url = buildUrl("/non_ok/SendRedirect.jsp"); HttpClient httpClient = new HttpClient(); HttpMethod httpMethod = new GetMethod(url); httpMethod.addRequestHeader("Accept-Encoding", "gzip"); int responseCode = httpClient.executeMethod(httpMethod); //httpclient follows redirects, so gets the home page. assertEquals(HttpURLConnection.HTTP_OK, responseCode); String responseBody = httpMethod.getResponseBodyAsString(); assertNotNull(responseBody); assertNull(httpMethod.getResponseHeader("Content-Encoding")); }
From source file:net.sf.ehcache.constructs.web.filter.SimpleCachingHeadersPageCachingFilterTest.java
/** * When the servlet container generates a 404 page not found, we want to pass * it through without caching and without adding anything to it. * <p/>/*ww w .java 2 s . c o m*/ * Manual Test: wget -d --server-response --header='Accept-Encoding: gzip' http://localhost:9090/non_ok/PageNotFound.jsp */ @Test public void testNotFound() throws Exception { String url = buildUrl("/non_ok/PageNotFound.jsp"); HttpClient httpClient = new HttpClient(); HttpMethod httpMethod = new GetMethod(url); httpMethod.addRequestHeader("If-modified-Since", "Fri, 13 May 3006 23:54:18 GMT"); httpMethod.addRequestHeader("Accept-Encoding", "gzip"); int responseCode = httpClient.executeMethod(httpMethod); assertEquals(HttpURLConnection.HTTP_NOT_FOUND, responseCode); String responseBody = httpMethod.getResponseBodyAsString(); assertNotNull(responseBody); assertNull(httpMethod.getResponseHeader("Content-Encoding")); }
From source file:net.sf.ehcache.constructs.web.filter.SimpleCachingHeadersPageCachingFilterTest.java
/** * Servlets and JSPs can send content even when the response is set to no content. * In this case there should not be a body but there is. Orion seems to kill the body * after is has left the Servlet filter chain. To avoid wget going into an inifinite * retry loop, and presumably some other web clients, the content length should be 0 * and the body 0.//from w w w . j a v a2 s . co m * <p/> * Manual Test: wget -d --server-response --timestamping --header='If-modified-Since: Fri, 13 May 3006 23:54:18 GMT' --header='Accept-Encoding: gzip' http://localhost:9090/empty_caching_filter/SC_NO_CONTENT.jsp */ @Test public void testNoContentJSPGzipFilter() throws Exception { String url = buildUrl("/empty_caching_filter/SC_NO_CONTENT.jsp"); HttpClient httpClient = new HttpClient(); HttpMethod httpMethod = new GetMethod(url); httpMethod.addRequestHeader("If-modified-Since", "Fri, 13 May 3006 23:54:18 GMT"); httpMethod.addRequestHeader("Accept-Encoding", "gzip"); int responseCode = httpClient.executeMethod(httpMethod); assertEquals(HttpURLConnection.HTTP_NO_CONTENT, responseCode); String responseBody = httpMethod.getResponseBodyAsString(); assertEquals(null, responseBody); assertNull(httpMethod.getResponseHeader("Content-Encoding")); assertNotNull(httpMethod.getResponseHeader("Last-Modified").getValue()); checkNullOrZeroContentLength(httpMethod); }
From source file:net.sf.ehcache.constructs.web.filter.SimpleCachingHeadersPageCachingFilterTest.java
/** * Servlets and JSPs can send content even when the response is set to no content. * In this case there should not be a body but there is. Orion seems to kill the body * after is has left the Servlet filter chain. To avoid wget going into an inifinite * retry loop, and presumably some other web clients, the content length should be 0 * and the body 0.//from www. j a v a 2 s.c o m * <p/> * wget -d --server-response --timestamping --header='If-modified-Since: Fri, 13 May 3006 23:54:18 GMT' --header='Accept-Encoding: gzip' http://localhost:9090/empty_caching_filter/SC_NOT_MODIFIED.jsp */ @Test public void testNotModifiedJSPGzipFilter() throws Exception { String url = buildUrl("/empty_caching_filter/SC_NOT_MODIFIED.jsp"); HttpClient httpClient = new HttpClient(); HttpMethod httpMethod = new GetMethod(url); httpMethod.addRequestHeader("If-modified-Since", "Fri, 13 May 3006 23:54:18 GMT"); httpMethod.addRequestHeader("Accept-Encoding", "gzip"); int responseCode = httpClient.executeMethod(httpMethod); assertEquals(HttpURLConnection.HTTP_NOT_MODIFIED, responseCode); String responseBody = httpMethod.getResponseBodyAsString(); assertEquals(null, responseBody); assertNull(httpMethod.getResponseHeader("Content-Encoding")); assertNotNull(httpMethod.getResponseHeader("Last-Modified").getValue()); checkNullOrZeroContentLength(httpMethod); }
From source file:it.intecs.pisa.openCatalogue.solr.SolrHandler.java
public SaxonDocument search(HashMap<String, String> request) throws UnsupportedEncodingException, IOException, SaxonApiException, Exception { HttpClient client = new HttpClient(); HttpMethod method; String urlStr = prepareUrl(request); Log.debug("The following search is goint to be executed:" + urlStr); // Create a method instance. method = new GetMethod(urlStr); // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); // Execute the method. int statusCode = client.executeMethod(method); SaxonDocument solrResponse = new SaxonDocument(method.getResponseBodyAsString()); //Log.debug(solrResponse.getXMLDocumentString()); if (statusCode != HttpStatus.SC_OK) { Log.error("Method failed: " + method.getStatusLine()); String errorMessage = (String) solrResponse.evaluatePath("//lst[@name='error']/str[@name='msg']/text()", XPathConstants.STRING); throw new Exception(errorMessage); }/*from ww w. jav a 2s .com*/ return solrResponse; }
From source file:net.sf.ehcache.constructs.web.filter.SimpleCachingHeadersPageCachingFilterTest.java
/** * A 0 length body should give a 0 length nongzipped body and content length * Manual Test: wget -d --server-response --timestamping --header='If-modified-Since: Fri, 13 May 3006 23:54:18 GMT' --header='Accept-Encoding: gzip' http://localhost:9090/empty_caching_filter/empty.html */// w ww .ja v a 2s .c om @Test public void testIfModifiedZeroLengthHTML() throws Exception { String url = buildUrl("/empty_caching_filter/empty.html"); HttpClient httpClient = new HttpClient(); HttpMethod httpMethod = new GetMethod(url); httpMethod.addRequestHeader("If-modified-Since", "Fri, 13 May 3006 23:54:18 GMT"); httpMethod.addRequestHeader("Accept-Encoding", "gzip"); int responseCode = httpClient.executeMethod(httpMethod); assertTrue( HttpURLConnection.HTTP_OK == responseCode || HttpURLConnection.HTTP_NOT_MODIFIED == responseCode); String responseBody = httpMethod.getResponseBodyAsString(); assertTrue("".equals(responseBody) || null == responseBody); checkNullOrZeroContentLength(httpMethod); }
From source file:com.bugclipse.fogbugz.api.client.FogBugzClient.java
private Response unmarshalResponse(HttpMethod method) throws FogBugzClientException { try {//from w w w .j ava 2s . c o m Unmarshaller un = new Unmarshaller(Response.class); un.setIgnoreExtraElements(true); un.setIgnoreExtraAttributes(true); un.setValidation(false); un.setWhitespacePreserve(true); String respBody = method.getResponseBodyAsString(); System.out.println("response - " + respBody); Response r = (Response) un.unmarshal(new StringReader(respBody)); if (r.getError() != null) throw new FogBugzAPIException(r.getError()); return r; } catch (IOException e) { throw new FogBugzClientException(e); } catch (MarshalException e) { throw new FogBugzClientException(e); } catch (ValidationException e) { throw new FogBugzClientException(e); } finally { method.releaseConnection(); } }
From source file:com.zimbra.qa.unittest.TestAccessKeyGrant.java
private void executeHttpMethod(HttpClient client, HttpMethod method) throws Exception { try {//from w w w .j a v a2s. co m int respCode = HttpClientUtil.executeMethod(client, method); if (respCode != HttpStatus.SC_OK) { System.out.println("failed, respCode=" + respCode); } else { boolean chunked = false; boolean textContent = false; System.out.println("Headers:"); System.out.println("--------"); for (Header header : method.getRequestHeaders()) { System.out.print(" " + header.toString()); } System.out.println(); System.out.println("Body:"); System.out.println("-----"); String respBody = method.getResponseBodyAsString(); System.out.println(respBody); } } finally { // Release the connection. method.releaseConnection(); } }
From source file:edu.uci.ics.pregelix.example.util.TestExecutor.java
private int executeHttpMethod(HttpMethod method) throws Exception { HttpClient client = new HttpClient(); int statusCode; try {/*ww w. jav a 2s . co m*/ statusCode = client.executeMethod(method); } catch (Exception e) { GlobalConfig.ASTERIX_LOGGER.log(Level.SEVERE, e.getMessage(), e); e.printStackTrace(); throw e; } if (statusCode != HttpStatus.SC_OK) { // QQQ For now, we are indeed assuming we get back JSON errors. // In future this may be changed depending on the requested // output format sent to the servlet. String errorBody = method.getResponseBodyAsString(); JSONObject result = new JSONObject(errorBody); String[] errors = { result.getJSONArray("error-code").getString(0), result.getString("summary"), result.getString("stacktrace") }; GlobalConfig.ASTERIX_LOGGER.log(Level.SEVERE, errors[2]); throw new Exception("HTTP operation failed: " + errors[0] + "\nSTATUS LINE: " + method.getStatusLine() + "\nSUMMARY: " + errors[1] + "\nSTACKTRACE: " + errors[2]); } return statusCode; }
From source file:it.intecs.pisa.openCatalogue.solr.SolrHandler.java
public SaxonDocument delete(String id) throws UnsupportedEncodingException, IOException, SaxonApiException, Exception { HttpClient client = new HttpClient(); HttpMethod method; String urlStr = this.solrHost + "/update?stream.body=" + URLEncoder.encode("<delete><query>id:" + id + "</query></delete>") + "&commit=true"; Log.debug("The " + id + " item is going to be deleted"); // Create a method instance. method = new GetMethod(urlStr); // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); // Execute the method. int statusCode = client.executeMethod(method); SaxonDocument solrResponse = new SaxonDocument(method.getResponseBodyAsString()); //Log.debug(solrResponse.getXMLDocumentString()); if (statusCode != HttpStatus.SC_OK) { Log.error("Method failed: " + method.getStatusLine()); String errorMessage = (String) solrResponse.evaluatePath("//lst[@name='error']/str[@name='msg']/text()", XPathConstants.STRING); throw new Exception(errorMessage); }/* w w w. j av a 2 s . c o m*/ return solrResponse; }