@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); }
@Override public void addHeader(String name, String value) { request.addHeader(new HTTPHeader(name, value)); }
@Override public void addHeader(String name, String value) { request.addHeader(new HTTPHeader(name, value)); }
URL url = new URL(URBAN_AIRSHIP_API_URL); HTTPRequest req = new HTTPRequest(url, HTTPMethod.POST); String authString = ApplicationProperties.getUrbanAirshipAppId() + ":" + ApplicationProperties.getUrbanAirshipMasterSecret(); String authStringBase64 = new String(Base64.encodeBase64(authString.getBytes())); authStringBase64 = authStringBase64.trim(); req.addHeader(new HTTPHeader("Content-Type", "application/json")); req.addHeader(new HTTPHeader("Authorization", "Basic " + authStringBase64)); ObjectMapper mapper = new ObjectMapper(); ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); mapper.writeValue(byteStream, new PushNotificationObj(userEmail, message)); req.setPayload(byteStream.toByteArray());
String json ="{}"; URL url = new URL("https://android.googleapis.com/gcm/send"); HTTPRequest request = new HTTPRequest(url, HTTPMethod.POST); request.addHeader(new HTTPHeader("Content-Type","application/json")); request.addHeader(new HTTPHeader("Authorization", "key=<>")); request.setPayload(json.getBytes("UTF-8")); HTTPResponse response = URLFetchServiceFactory.getURLFetchService().fetch(request);
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()));
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 }
private void addHeaders(HttpRequestImpl request, HTTPRequest fetchRequest) { for (Map.Entry<String, String> header : request.getHeaders().entrySet()) { fetchRequest.addHeader(new HTTPHeader(header.getKey(), StringUtil.toString(header.getValue()))); } }
protected void writeRequestHeaders(GHttpEndpoint endpoint, Exchange exchange, HTTPRequest request) { HeaderFilterStrategy strategy = endpoint.getHeaderFilterStrategy(); for (String headerName : exchange.getIn().getHeaders().keySet()) { String headerValue = exchange.getIn().getHeader(headerName, String.class); if (strategy != null && !strategy.applyFilterToCamelHeaders(headerName, headerValue, exchange)) { request.addHeader(new HTTPHeader(headerName, headerValue)); } } }
private void setContentTypeIfNotPresent(HttpRequestImpl request, HTTPRequest fetchRequest, ContentType contentType) { if (!request.getHeaders().containsKey(Header.ContentType)) { fetchRequest.setHeader(new HTTPHeader(Header.ContentType, contentType.value())); } }
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; }
private HTTPRequest createAuthorizeRequest(final HTTPRequest req) throws RetryHelperException { String token = RetryHelper.runWithRetries(new Callable<String>() { @Override public String call() { return getToken(); } }, RETRY_PARAMS, EXCEPTION_HANDLER); HTTPRequest request = URLFetchUtils.copyRequest(req); request.setHeader(new HTTPHeader("Authorization", "Bearer " + token)); return request; }
private void addCookies(HttpRequestImpl request, HTTPRequest fetchRequest) { for (HttpCookie cookie : request.getCookies()) { fetchRequest.addHeader(new HTTPHeader("Cookie", cookie.toString())); } }
private void addAuthorization(HttpRequestImpl request, HTTPRequest fetchRequest) { String username = request.getUsername(); String password = request.getPassword(); String scheme = request.getScheme(); if (username != null && password != null) { if (Authorization.Basic.equalsIgnoreCase(scheme)) { fetchRequest.addHeader(new HTTPHeader(Header.Authorization, Authorization.createBasicHeader(username, password))); } else { throw new HttpRequestException("%s only currently supports %s authorization", HttpServiceImpl.class.getSimpleName(), Authorization.Basic); } } }
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; }
@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"); }
@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 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()); }