private boolean shouldRetry(HTTPResponse response) { return RETRY_ERROR_CODES.contains(response.getResponseCode()) && cumulativeSleepTime < errorTimeOut && (maxRetries == null || retryCounter < maxRetries); }
&& contentType.startsWith("image") && responseClass == ImageResult.Response.class && response.getResponseCode() == 200) { ImageResult result = new ImageResult(contentType, bytes); return (T) result; } catch (JsonSyntaxException e) { if (response.getResponseCode() > 399) { String.format( "Server Error: %d %s", response.getResponseCode(), new String(response.getContent(), Charset.defaultCharset())));
@Override public int getStatusCode() { return fetchResponse.getResponseCode(); }
@Override public int getStatusCode() { return fetchResponse.getResponseCode(); }
private boolean shouldRetry(HTTPResponse response) { return RETRY_ERROR_CODES.contains(response.getResponseCode()) && cumulativeSleepTime < errorTimeOut && (maxRetries == null || retryCounter < maxRetries); }
@Override public int getStatus() { return response().getResponseCode(); }
static void appendResponse(HTTPResponse resp, StringBuilder b) { byte[] content = resp.getContent(); b.append(resp.getResponseCode()).append(" with ").append(content == null ? 0 : content.length); b.append(" bytes of content"); for (HTTPHeader h : resp.getHeadersUncombined()) { b.append('\n').append(h.getName()).append(": ").append(h.getValue()); } b.append('\n').append(content == null ? "" : new String(content, UTF_8)).append('\n'); }
public static IOException error(HTTPRequestInfo req, HTTPResponse resp) throws IOException { return error(resp.getResponseCode(), URLFetchUtils.describeRequestAndResponse(req, resp)); } }
private static Response toRequestorResponse(HTTPResponse response) { Map<String, List<String>> headers = new HashMap<String, List<String>>(); for (HTTPHeader header : response.getHeadersUncombined()) { List<String> existing = headers.get(header.getName()); if (existing == null) { existing = new ArrayList<String>(); headers.put(header.getName(), existing); } existing.add(header.getValue()); } return new Response(response.getResponseCode(), new ByteArrayInputStream(response.getContent()), headers); }
public void handle(HTTPResponse response) throws Exception { Assert.assertEquals(302, response.getResponseCode()); } });
public void handle(HTTPResponse response) throws Exception { Assert.assertEquals(302, response.getResponseCode()); } });
String pivotalUrl = "http://www.pivotaltracker.com/services/v3/projects/"+projectID+"/stories" String body = "<story><story_type>feature</story_type><name>Story name</name><requested_by>James Kirk</requested_by></story>" URLFetchService fetchService = URLFetchServiceFactory.getURLFetchService(); HTTPRequest request = new HTTPRequest(pivotalUrl, HTTPMethod.POST, FetchOptions.Builder.withDeadline(30)); request.addHeader(new HTTPHeader("X-TrackerToken","putTokenHere")); request.addHeader(new HTTPHeader("Content-type","application/xml")); HTTPResponse response = fetchService.fetch(request); if (response.getResponseCode() != 200) { // something went wrong }
@Test void testConvertWithHeaders() throws IOException { HTTPResponse gaeResponse = createMock(HTTPResponse.class); expect(gaeResponse.getResponseCode()).andReturn(200); List<HTTPHeader> headers = Lists.newArrayList(); headers.add(new HTTPHeader(HttpHeaders.CONTENT_TYPE, "text/xml")); expect(gaeResponse.getHeaders()).andReturn(headers); expect(gaeResponse.getContent()).andReturn(null).atLeastOnce(); replay(gaeResponse); HttpResponse response = req.apply(gaeResponse); assertEquals(response.getStatusCode(), 200); assertEquals(response.getPayload(), null); assertEquals(response.getHeaders().size(), 0); }
@Test void testConvertWithHeaders() throws IOException { HTTPResponse gaeResponse = createMock(HTTPResponse.class); expect(gaeResponse.getResponseCode()).andReturn(200); List<HTTPHeader> headers = Lists.newArrayList(); headers.add(new HTTPHeader(HttpHeaders.CONTENT_TYPE, "text/xml")); expect(gaeResponse.getHeaders()).andReturn(headers); expect(gaeResponse.getContent()).andReturn(null).atLeastOnce(); replay(gaeResponse); HttpResponse response = req.apply(gaeResponse); assertEquals(response.getStatusCode(), 200); assertEquals(response.getPayload(), null); assertEquals(response.getHeaders().size(), 0); }
@Test void testConvertWithContent() throws IOException { HTTPResponse gaeResponse = createMock(HTTPResponse.class); expect(gaeResponse.getResponseCode()).andReturn(200); List<HTTPHeader> headers = Lists.newArrayList(); headers.add(new HTTPHeader(HttpHeaders.CONTENT_TYPE, "text/xml")); expect(gaeResponse.getHeaders()).andReturn(headers); expect(gaeResponse.getContent()).andReturn("hello".getBytes()).atLeastOnce(); replay(gaeResponse); HttpResponse response = req.apply(gaeResponse); assertEquals(response.getStatusCode(), 200); assertEquals(Strings2.toString(response.getPayload()), "hello"); assertEquals(response.getHeaders().size(), 0); assertEquals(response.getPayload().getContentMetadata().getContentType(), "text/xml"); }
protected String fetchUrl(String url, int expectedResponse) throws IOException { URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService(); HTTPResponse httpResponse = urlFetchService.fetch(new URL(url)); assertEquals(url, expectedResponse, httpResponse.getResponseCode()); return new String(httpResponse.getContent()); }
@Override public void putObject(GcsFilename filename, GcsFileOptions options, ByteBuffer content, long timeoutMillis) throws IOException { HTTPRequest req = makeRequest(filename, null, PUT, timeoutMillis, content); addOptionsHeaders(req, options); HTTPResponse resp; try { resp = urlfetch.fetch(req); } catch (IOException e) { throw createIOException(new HTTPRequestInfo(req), e); } if (resp.getResponseCode() != 200) { throw HttpErrorHandler.error(new HTTPRequestInfo(req), resp); } }
public HttpResponse receiveResponseHeader() throws HttpException, IOException { if (this.response == null) { flush(); } HttpResponse response = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), this.response.getResponseCode(), null); // System.err.println("RECV: " + response.getStatusLine()); for (HTTPHeader h : this.response.getHeaders()) { // System.err.println("RECV: " + h.getName() + ": " + h.getValue()); response.addHeader(h.getName(), h.getValue()); } return response; }
public HttpResponse receiveResponseHeader() throws HttpException, IOException { if (this.response == null) { flush(); } HttpResponse response = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), this.response.getResponseCode(), null); // System.err.println("RECV: " + response.getStatusLine()); for (HTTPHeader h : this.response.getHeaders()) { // System.err.println("RECV: " + h.getName() + ": " + h.getValue()); response.addHeader(h.getName(), h.getValue()); } return response; }
@Test(expected = IOException.class) public void fetchNonExistentLocalAppThrowsException() throws Exception { URL selfURL = new URL("BOGUS-" + appUrlBase + "/"); FetchOptions fetchOptions = FetchOptions.Builder.withDefaults() .doNotFollowRedirects() .setDeadline(10.0); HTTPRequest httpRequest = new HTTPRequest(selfURL, HTTPMethod.GET, fetchOptions); URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService(); HTTPResponse httpResponse = urlFetchService.fetch(httpRequest); fail("expected exception, got " + httpResponse.getResponseCode()); }