@Override public <T, R extends ApiResponse<T>> PendingResult<T> handle( String hostName, String url, String userAgent, Class<R> clazz, FieldNamingPolicy fieldNamingPolicy, long errorTimeout, Integer maxRetries, ExceptionsAllowedToRetry exceptionsAllowedToRetry) { FetchOptions fetchOptions = FetchOptions.Builder.withDeadline(10); HTTPRequest req; try { req = new HTTPRequest(new URL(hostName + url), HTTPMethod.POST, fetchOptions); } catch (MalformedURLException e) { LOG.error("Request: {}{}", hostName, url, e); throw (new RuntimeException(e)); } return new GaePendingResult<>( req, client, clazz, fieldNamingPolicy, errorTimeout, maxRetries, exceptionsAllowedToRetry); }
@Override public <T, R extends ApiResponse<T>> PendingResult<T> handlePost( String hostName, String url, String payload, String userAgent, Class<R> clazz, FieldNamingPolicy fieldNamingPolicy, long errorTimeout, Integer maxRetries, ExceptionsAllowedToRetry exceptionsAllowedToRetry) { FetchOptions fetchOptions = FetchOptions.Builder.withDeadline(10); HTTPRequest req = null; try { req = new HTTPRequest(new URL(hostName + url), HTTPMethod.POST, fetchOptions); req.setHeader(new HTTPHeader("Content-Type", "application/json; charset=utf-8")); req.setPayload(payload.getBytes(UTF_8)); } catch (MalformedURLException e) { LOG.error("Request: {}{}", hostName, url, e); throw (new RuntimeException(e)); } return new GaePendingResult<>( req, client, clazz, fieldNamingPolicy, errorTimeout, maxRetries, exceptionsAllowedToRetry); }
String lResponse = new HTTPRequest().execute( pURL ).get();
UrlFetchRequest(HTTPMethod method, String url) throws IOException { request = new HTTPRequest(new URL(url), method, OPTIONS); }
UrlFetchRequest(FetchOptions fetchOptions, HTTPMethod method, String url) throws IOException { request = new HTTPRequest(new URL(url), method, fetchOptions); }
URL fetchurl = new URL(url); String nameAndPassword = credentials.get("name")+":"+credentials.get("password"); String authorizationString = "Basic " + Base64.encode(nameAndPassword.getBytes()); HTTPRequest request = new HTTPRequest(fetchurl); request.addHeader(new HTTPHeader("Authorization", authorizationString)); HTTPResponse response = URLFetchServiceFactory.getURLFetchService().fetch(request); System.out.println(new String(response.getContent()));
@Override public <T, R extends ApiResponse<T>> PendingResult<T> handle( String hostName, String url, String userAgent, Class<R> clazz, FieldNamingPolicy fieldNamingPolicy, long errorTimeout, Integer maxRetries, ExceptionsAllowedToRetry exceptionsAllowedToRetry) { FetchOptions fetchOptions = FetchOptions.Builder.withDeadline(10); HTTPRequest req; try { req = new HTTPRequest(new URL(hostName + url), HTTPMethod.POST, fetchOptions); } catch (MalformedURLException e) { LOG.error("Request: {}{}", hostName, url, e); throw (new RuntimeException(e)); } return new GaePendingResult<>( req, client, clazz, fieldNamingPolicy, errorTimeout, maxRetries, exceptionsAllowedToRetry); }
private HTTPRequest newRequest(String url, HTTPMethod method, Iterable<Header> headers) throws IOException { HTTPRequest request = new HTTPRequest(new URL(url), method, options); for (Header header : headers) { request.addHeader(new HTTPHeader(header.getKey(), header.getValue())); } return request; }
@Override public <T, R extends ApiResponse<T>> PendingResult<T> handlePost( String hostName, String url, String payload, String userAgent, Class<R> clazz, FieldNamingPolicy fieldNamingPolicy, long errorTimeout, Integer maxRetries, ExceptionsAllowedToRetry exceptionsAllowedToRetry) { FetchOptions fetchOptions = FetchOptions.Builder.withDeadline(10); HTTPRequest req = null; try { req = new HTTPRequest(new URL(hostName + url), HTTPMethod.POST, fetchOptions); req.setHeader(new HTTPHeader("Content-Type", "application/json; charset=utf-8")); req.setPayload(payload.getBytes(UTF_8)); } catch (MalformedURLException e) { LOG.error("Request: {}{}", hostName, url, e); throw (new RuntimeException(e)); } return new GaePendingResult<>( req, client, clazz, fieldNamingPolicy, errorTimeout, maxRetries, exceptionsAllowedToRetry); }
/** * Reads data from <code>exchange</code> and writes it to a newly created * {@link HTTPRequest} instance. The <code>request</code> parameter is * ignored. * * @return a newly created {@link HTTPRequest} instance containing data from * <code>exchange</code>. */ public HTTPRequest writeRequest(GHttpEndpoint endpoint, Exchange exchange, HTTPRequest request) throws Exception { HTTPRequest answer = new HTTPRequest( getRequestUrl(endpoint, exchange), getRequestMethod(endpoint, exchange)); writeRequestHeaders(endpoint, exchange, answer); writeRequestBody(endpoint, exchange, answer); return answer; }
@Test public void testFollowRedirectsExternal() throws Exception { final URL redirectUrl = new URL("http://google.com/"); final String expectedDestinationURLPrefix = "http://www.google."; FetchOptions options = FetchOptions.Builder.followRedirects(); HTTPRequest request = new HTTPRequest(redirectUrl, HTTPMethod.GET, options); URLFetchService service = URLFetchServiceFactory.getURLFetchService(); HTTPResponse response = service.fetch(request); String destinationUrl = response.getFinalUrl().toString(); assertTrue("Did not get redirected.", destinationUrl.startsWith(expectedDestinationURLPrefix)); }
protected void testOptions(URL url, HTTPMethod method, FetchOptions options, ResponseHandler handler) throws Exception { HTTPRequest request = new HTTPRequest(url, method, options); URLFetchService service = URLFetchServiceFactory.getURLFetchService(); HTTPResponse response = service.fetch(request); handler.handle(response); }
static HTTPRequest copyRequest(HTTPRequest in) { HTTPRequest out = new HTTPRequest(in.getURL(), in.getMethod(), in.getFetchOptions()); for (HTTPHeader h : in.getHeaders()) { out.addHeader(h); } out.setPayload(in.getPayload()); return out; } }
static HTTPRequest createRequest(Request request) throws IOException { HTTPMethod httpMethod = getHttpMethod(request.getMethod()); URL url = new URL(request.getUrl()); HTTPRequest fetchRequest = new HTTPRequest(url, httpMethod); for (Header header : request.getHeaders()) { fetchRequest.addHeader(new HTTPHeader(header.getName(), header.getValue())); } TypedOutput body = request.getBody(); if (body != null) { String mimeType = body.mimeType(); if (mimeType != null) { fetchRequest.addHeader(new HTTPHeader("Content-Type", mimeType)); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); body.writeTo(baos); fetchRequest.setPayload(baos.toByteArray()); } return fetchRequest; }
protected HttpResponseImpl postOrPut(HttpRequestImpl request, HTTPMethod postOrPut) { FetchOptions fetchOptions = createFetchOptions(request); URL requestUrl = buildPostRequestUrl(request); HTTPRequest fetchRequest = new HTTPRequest(requestUrl, postOrPut, fetchOptions); setContentTypeIfNotPresent(request, fetchRequest, ContentType.ApplicationFormUrlEncoded); addAuthorization(request, fetchRequest); addHeaders(request, fetchRequest); addCookies(request, fetchRequest); addBody(request, fetchRequest, true); return fetch(fetchRequest, postOrPut); }
protected HttpResponseImpl headGetDelete(HttpRequestImpl request, HTTPMethod headGetDelete) { FetchOptions fetchOptions = createFetchOptions(request); URL requestUrl = buildGetRequestUrl(request); HTTPRequest fetchRequest = new HTTPRequest(requestUrl, headGetDelete, fetchOptions); setContentTypeIfNotPresent(request, fetchRequest, ContentType.TextPlain); addAuthorization(request, fetchRequest); addHeaders(request, fetchRequest); addCookies(request, fetchRequest); addBody(request, fetchRequest, false); return fetch(fetchRequest, headGetDelete); }
@Test public void testAsyncOps() throws Exception { URLFetchService service = URLFetchServiceFactory.getURLFetchService(); URL adminConsole = findAvailableUrl(URLS); Future<HTTPResponse> response = service.fetchAsync(adminConsole); printResponse(response.get(5, TimeUnit.SECONDS)); response = service.fetchAsync(new HTTPRequest(adminConsole)); printResponse(response.get(5, TimeUnit.SECONDS)); URL jbossOrg = new URL("http://www.jboss.org"); if (available(jbossOrg)) { response = service.fetchAsync(jbossOrg); printResponse(response.get(30, TimeUnit.SECONDS)); } sync(5000L); // wait a bit for async to finish }
@Test public void testPayload() throws Exception { URLFetchService service = URLFetchServiceFactory.getURLFetchService(); URL url = getFetchUrl(); HTTPRequest req = new HTTPRequest(url, HTTPMethod.POST); req.setHeader(new HTTPHeader("Content-Type", "application/octet-stream")); req.setPayload("Tralala".getBytes(UTF_8)); HTTPResponse response = service.fetch(req); String content = new String(response.getContent()); Assert.assertEquals("Hopsasa", content); }
@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()); }
@Test public void testGetters() throws Exception { HTTPRequest request = new HTTPRequest(getFetchUrl(), HTTPMethod.PATCH, FetchOptions.Builder.withDefaults()); request.addHeader(new HTTPHeader("foo", "bar")); request.setPayload("qwerty".getBytes()); Assert.assertEquals(getFetchUrl(), request.getURL()); Assert.assertEquals(HTTPMethod.PATCH, request.getMethod()); Assert.assertNotNull(request.getFetchOptions()); Assert.assertNotNull(request.getHeaders()); Assert.assertEquals(1, request.getHeaders().size()); assertEquals(new HTTPHeader("foo", "bar"), request.getHeaders().get(0)); Assert.assertArrayEquals("qwerty".getBytes(), request.getPayload()); }