List of usage examples for org.apache.commons.httpclient HttpMethodBase getStatusCode
@Override public int getStatusCode()
From source file:org.apache.kylin.jdbc.KylinClient.java
private IOException asIOException(HttpMethodBase method) throws IOException { return new IOException(method + " failed, error code " + method.getStatusCode() + " and response: " + method.getResponseBodyAsString()); }
From source file:org.apache.solr.servlet.CacheHeaderTest.java
@Test public void testCacheVetoHandler() throws Exception { File f = makeFile(CONTENTS);/*from ww w.j ava 2s. c o m*/ HttpMethodBase m = getUpdateMethod("GET"); m.setQueryString(new NameValuePair[] { new NameValuePair("stream.file", f.getCanonicalPath()) }); getClient().executeMethod(m); assertEquals(200, m.getStatusCode()); checkVetoHeaders(m, true); }
From source file:org.apache.solr.servlet.CacheHeaderTest.java
@Test public void testCacheVetoException() throws Exception { HttpMethodBase m = getSelectMethod("GET"); // We force an exception from Solr. This should emit "no-cache" HTTP headers m.setQueryString(new NameValuePair[] { new NameValuePair("q", "xyz_ignore_exception:solr"), new NameValuePair("qt", "standard") }); getClient().executeMethod(m);//from w w w .j a v a 2 s . c o m assertFalse(m.getStatusCode() == 200); checkVetoHeaders(m, false); }
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);//from w w w. j a va 2s . co m 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 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("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.CacheHeaderTestBase.java
protected void checkResponseBody(String method, HttpMethodBase resp) throws Exception { String responseBody = resp.getResponseBodyAsString(); if ("GET".equals(method)) { switch (resp.getStatusCode()) { case 200: assertTrue("Response body was empty for method " + method, responseBody != null && responseBody.length() > 0); break; case 304: assertTrue("Response body was not empty for method " + method, responseBody == null || responseBody.length() == 0); break; case 412: assertTrue("Response body was not empty for method " + method, responseBody == null || responseBody.length() == 0); break; default:/* ww w. j a va 2s . co m*/ System.err.println(responseBody); assertEquals("Unknown request response", 0, resp.getStatusCode()); } } if ("HEAD".equals(method)) { assertTrue("Response body was not empty for method " + method, responseBody == null || responseBody.length() == 0); } }
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 ww .j ava2s . 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 ww w . j a va 2s .co 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.zeppelin.rest.AbstractTestRestApi.java
protected Matcher<HttpMethodBase> responsesWith(final int expectedStatusCode) { return new TypeSafeMatcher<HttpMethodBase>() { WeakReference<HttpMethodBase> method; @Override/*from w ww . j a va 2 s.co m*/ public boolean matchesSafely(HttpMethodBase httpMethodBase) { method = (method == null) ? new WeakReference<HttpMethodBase>(httpMethodBase) : method; return httpMethodBase.getStatusCode() == expectedStatusCode; } @Override public void describeTo(Description description) { description.appendText("HTTP response ").appendValue(expectedStatusCode).appendText(" from ") .appendText(method.get().getPath()); } @Override protected void describeMismatchSafely(HttpMethodBase item, Description description) { description.appendText("got ").appendValue(item.getStatusCode()).appendText(" ") .appendText(item.getStatusText()); } }; }
From source file:org.archive.crawler.fetcher.OptimizeFetchHTTP.java
protected void innerProcess(final CrawlURI curi) throws InterruptedException { if (!canFetch(curi)) { // Cannot fetch this, due to protocol, retries, or other problems return;//from w w w. ja v a2 s . c o m } HttpClient http = this.getClient(); setLocalIP(http); this.curisHandled++; // Note begin time curi.putLong(A_FETCH_BEGAN_TIME, System.currentTimeMillis()); // Get a reference to the HttpRecorder that is set into this ToeThread. HttpRecorder rec = HttpRecorder.getHttpRecorder(); // Shall we get a digest on the content downloaded? boolean digestContent = ((Boolean) getUncheckedAttribute(curi, ATTR_DIGEST_CONTENT)).booleanValue(); String algorithm = null; if (digestContent) { algorithm = ((String) getUncheckedAttribute(curi, ATTR_DIGEST_ALGORITHM)); rec.getRecordedInput().setDigest(algorithm); } else { // clear rec.getRecordedInput().setDigest((MessageDigest) null); } // Below we do two inner classes that add check of midfetch // filters just as we're about to receive the response body. String curiString = curi.getUURI().toString(); HttpMethodBase method = null; if (curi.isPost()) { method = new HttpRecorderPostMethod(curiString, rec) { protected void readResponseBody(HttpState state, HttpConnection conn) throws IOException, HttpException { addResponseContent(this, curi); if (checkMidfetchAbort(curi, this.httpRecorderMethod, conn)) { doAbort(curi, this, MIDFETCH_ABORT_LOG); } else { super.readResponseBody(state, conn); } } }; } else { method = new HttpRecorderGetMethod(curiString, rec) { protected void readResponseBody(HttpState state, HttpConnection conn) throws IOException, HttpException { addResponseContent(this, curi); if (checkMidfetchAbort(curi, this.httpRecorderMethod, conn)) { doAbort(curi, this, MIDFETCH_ABORT_LOG); } else { super.readResponseBody(state, conn); } } }; } HostConfiguration customConfigOrNull = configureMethod(curi, method); // Set httpRecorder into curi. Subsequent code both here and later // in extractors expects to find the HttpRecorder in the CrawlURI. curi.setHttpRecorder(rec); // Populate credentials. Set config so auth. is not automatic. boolean addedCredentials = populateCredentials(curi, method); method.setDoAuthentication(addedCredentials); // set hardMax on bytes (if set by operator) long hardMax = getMaxLength(curi); // set overall timeout (if set by operator) long timeoutMs = 1000 * getTimeout(curi); // Get max fetch rate (bytes/ms). It comes in in KB/sec long maxRateKBps = getMaxFetchRate(curi); rec.getRecordedInput().setLimits(hardMax, timeoutMs, maxRateKBps); try { http.executeMethod(customConfigOrNull, method); } catch (RecorderTooMuchHeaderException ex) { // when too much header material, abort like other truncations doAbort(curi, method, HEADER_TRUNC); } catch (IOException e) { failedExecuteCleanup(method, curi, e); return; } catch (ArrayIndexOutOfBoundsException e) { // For weird windows-only ArrayIndex exceptions in native // code... see // http://forum.java.sun.com/thread.jsp?forum=11&thread=378356 // treating as if it were an IOException failedExecuteCleanup(method, curi, e); return; } // set softMax on bytes to get (if implied by content-length) long softMax = method.getResponseContentLength(); try { if (!curi.isSeed() && curi.getFetchStatus() == HttpStatus.SC_NOT_MODIFIED) { logger.debug(curi.getUURI().toString() + " is not modify"); curi.skipToProcessorChain(getController().getPostprocessorChain()); } else if (!method.isAborted()) { // Force read-to-end, so that any socket hangs occur here, // not in later modules. rec.getRecordedInput().readFullyOrUntil(softMax); } } catch (RecorderTimeoutException ex) { doAbort(curi, method, TIMER_TRUNC); } catch (RecorderLengthExceededException ex) { doAbort(curi, method, LENGTH_TRUNC); } catch (IOException e) { cleanup(curi, e, "readFully", S_CONNECT_LOST); return; } catch (ArrayIndexOutOfBoundsException e) { // For weird windows-only ArrayIndex exceptions from native code // see http://forum.java.sun.com/thread.jsp?forum=11&thread=378356 // treating as if it were an IOException cleanup(curi, e, "readFully", S_CONNECT_LOST); return; } finally { // ensure recording has stopped rec.closeRecorders(); logger.debug("cloase backup file.&uri= " + curi.getCrawlURIString()); if (!method.isAborted()) { method.releaseConnection(); } // Note completion time curi.putLong(A_FETCH_COMPLETED_TIME, System.currentTimeMillis()); // Set the response charset into the HttpRecord if available. setCharacterEncoding(rec, method); setSizes(curi, rec); } if (digestContent) { curi.setContentDigest(algorithm, rec.getRecordedInput().getDigestValue()); } logger.info((curi.isPost() ? "POST" : "GET") + " " + curi.getUURI().toString() + " " + method.getStatusCode() + " " + rec.getRecordedInput().getSize() + " " + curi.getContentType()); if (curi.isSuccess() && addedCredentials) { // Promote the credentials from the CrawlURI to the CrawlServer // so they are available for all subsequent CrawlURIs on this // server. promoteCredentials(curi); if (logger.isDebugEnabled()) { // Print out the cookie. Might help with the debugging. Header setCookie = method.getResponseHeader("set-cookie"); if (setCookie != null) { logger.debug(setCookie.toString().trim()); } } } else if (method.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) { // 401 is not 'success'. handle401(method, curi); } if (rec.getRecordedInput().isOpen()) { logger.error(curi.toString() + " RIS still open. Should have" + " been closed by method release: " + Thread.currentThread().getName()); try { rec.getRecordedInput().close(); } catch (IOException e) { logger.error("second-chance RIS close failed", e); } } }