Java Code Examples for com.google.appengine.api.urlfetch.HTTPRequest#setPayload()
The following examples show how to use
com.google.appengine.api.urlfetch.HTTPRequest#setPayload() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: URLFetchUtilsTest.java From appengine-gcs-client with Apache License 2.0 | 6 votes |
@Test public void testDescribeRequestAndResponseF() throws Exception { HTTPRequest request = new HTTPRequest(new URL("http://ping/pong")); request.setPayload("hello".getBytes()); request.addHeader(new HTTPHeader("k1", "v1")); request.addHeader(new HTTPHeader("k2", "v2")); HTTPResponse response = mock(HTTPResponse.class); when(response.getHeadersUncombined()).thenReturn(ImmutableList.of(new HTTPHeader("k3", "v3"))); when(response.getResponseCode()).thenReturn(500); when(response.getContent()).thenReturn("bla".getBytes()); String expected = "Request: GET http://ping/pong\nk1: v1\nk2: v2\n\n" + "5 bytes of content\n\nResponse: 500 with 3 bytes of content\nk3: v3\nbla\n"; String result = URLFetchUtils.describeRequestAndResponse(new HTTPRequestInfo(request), response); assertEquals(expected, result); }
Example 2
Source File: HttpClientGAE.java From flickr-uploader with GNU General Public License v2.0 | 6 votes |
public static String getResponseProxyPOST(URL url) throws MalformedURLException, UnsupportedEncodingException, IOException { URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService(); String base64payload = "base64url=" + Base64UrlSafe.encodeServer(url.toString()); String urlStr = url.toString(); if (urlStr.contains("?")) { base64payload = "base64url=" + Base64UrlSafe.encodeServer(urlStr.substring(0, urlStr.indexOf("?"))); base64payload += "&base64content=" + Base64UrlSafe.encodeServer(urlStr.substring(urlStr.indexOf("?") + 1)); } else { base64payload = "base64url=" + Base64UrlSafe.encodeServer(urlStr); } HTTPRequest httpRequest = new HTTPRequest(new URL(HttpClientGAE.POSTPROXY_PHP), HTTPMethod.POST, FetchOptions.Builder.withDeadline(30d).doNotValidateCertificate()); httpRequest.setPayload(base64payload.getBytes(UTF8)); HTTPResponse response = fetcher.fetch(httpRequest); String processResponse = HttpClientGAE.processResponse(response); logger.info("proxying " + url + "\nprocessResponse:" + processResponse); return processResponse; }
Example 3
Source File: GceApiUtils.java From solutions-google-compute-engine-orchestrator with Apache License 2.0 | 6 votes |
/** * Creates an HTTPRequest with the information passed in. * * @param accessToken the access token necessary to authorize the request. * @param url the url to query. * @param payload the payload for the request. * @return the created HTTP request. * @throws IOException */ public static HTTPResponse makeHttpRequest( String accessToken, final String url, String payload, HTTPMethod method) throws IOException { // Create HTTPRequest and set headers HTTPRequest httpRequest = new HTTPRequest(new URL(url.toString()), method); httpRequest.addHeader(new HTTPHeader("Authorization", "OAuth " + accessToken)); httpRequest.addHeader(new HTTPHeader("Host", "www.googleapis.com")); httpRequest.addHeader(new HTTPHeader("Content-Length", Integer.toString(payload.length()))); httpRequest.addHeader(new HTTPHeader("Content-Type", "application/json")); httpRequest.addHeader(new HTTPHeader("User-Agent", "google-api-java-client/1.0")); httpRequest.setPayload(payload.getBytes()); URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService(); HTTPResponse httpResponse = fetcher.fetch(httpRequest); return httpResponse; }
Example 4
Source File: UrlFetchUtils.java From nomulus with Apache License 2.0 | 5 votes |
/** * Sets payload on request as a {@code multipart/form-data} request. * * <p>This is equivalent to running the command: {@code curl -F [email protected] URL} * * @see <a href="http://www.ietf.org/rfc/rfc2388.txt">RFC2388 - Returning Values from Forms</a> */ public static void setPayloadMultipart( HTTPRequest request, String name, String filename, MediaType contentType, String data, Random random) { String boundary = createMultipartBoundary(random); checkState( !data.contains(boundary), "Multipart data contains autogenerated boundary: %s", boundary); String multipart = String.format("--%s\r\n", boundary) + String.format( "%s: form-data; name=\"%s\"; filename=\"%s\"\r\n", CONTENT_DISPOSITION, name, filename) + String.format("%s: %s\r\n", CONTENT_TYPE, contentType) + "\r\n" + data + "\r\n" + String.format("--%s--\r\n", boundary); byte[] payload = multipart.getBytes(UTF_8); request.addHeader( new HTTPHeader( CONTENT_TYPE, String.format("multipart/form-data;" + " boundary=\"%s\"", boundary))); request.addHeader(new HTTPHeader(CONTENT_LENGTH, Integer.toString(payload.length))); request.setPayload(payload); }
Example 5
Source File: RdeReporter.java From nomulus with Apache License 2.0 | 5 votes |
/** Uploads {@code reportBytes} to ICANN. */ public void send(byte[] reportBytes) throws XmlException { XjcRdeReportReport report = XjcXmlTransformer.unmarshal( XjcRdeReportReport.class, new ByteArrayInputStream(reportBytes)); XjcRdeHeader header = report.getHeader().getValue(); // Send a PUT request to ICANN's HTTPS server. URL url = makeReportUrl(header.getTld(), report.getId()); String username = header.getTld() + "_ry"; String token = base64().encode(String.format("%s:%s", username, password).getBytes(UTF_8)); final HTTPRequest req = new HTTPRequest(url, PUT, validateCertificate().setDeadline(60d)); req.addHeader(new HTTPHeader(CONTENT_TYPE, REPORT_MIME)); req.addHeader(new HTTPHeader(AUTHORIZATION, "Basic " + token)); req.setPayload(reportBytes); logger.atInfo().log("Sending report:\n%s", new String(reportBytes, UTF_8)); HTTPResponse rsp = retrier.callWithRetry( () -> { HTTPResponse rsp1 = urlFetchService.fetch(req); switch (rsp1.getResponseCode()) { case SC_OK: case SC_BAD_REQUEST: break; default: throw new UrlFetchException("PUT failed", req, rsp1); } return rsp1; }, SocketTimeoutException.class); // Ensure the XML response is valid. XjcIirdeaResult result = parseResult(rsp); if (result.getCode().getValue() != 1000) { logger.atWarning().log( "PUT rejected: %d %s\n%s", result.getCode().getValue(), result.getMsg(), result.getDescription()); throw new InternalServerErrorException(result.getMsg()); } }
Example 6
Source File: AppEngineHttpFetcher.java From openid4java with Apache License 2.0 | 5 votes |
private HttpResponse fetch(String url, HttpRequestOptions requestOptions, HTTPMethod method, String content) throws IOException { final FetchOptions options = getFetchOptions(requestOptions); String currentUrl = url; for (int i = 0; i <= requestOptions.getMaxRedirects(); i++) { HTTPRequest httpRequest = new HTTPRequest(new URL(currentUrl), method, options); addHeaders(httpRequest, requestOptions); if (method == HTTPMethod.POST && content != null) { httpRequest.setPayload(content.getBytes()); } HTTPResponse httpResponse; try { httpResponse = fetchService.fetch(httpRequest); } catch (ResponseTooLargeException e) { return new TooLargeResponse(currentUrl); } if (!isRedirect(httpResponse.getResponseCode())) { boolean isResponseTooLarge = (getContentLength(httpResponse) > requestOptions.getMaxBodySize()); return new AppEngineFetchResponse(httpResponse, isResponseTooLarge, currentUrl); } else { currentUrl = getResponseHeader(httpResponse, "Location").getValue(); } } throw new IOException("exceeded maximum number of redirects"); }
Example 7
Source File: OauthRawGcsService.java From appengine-gcs-client with Apache License 2.0 | 5 votes |
@VisibleForTesting HTTPRequest makeRequest(GcsFilename filename, @Nullable Map<String, String> queryStrings, HTTPMethod method, long timeoutMillis, byte[] payload) { HTTPRequest request = new HTTPRequest(makeUrl(filename, queryStrings), method, FetchOptions.Builder.disallowTruncate() .doNotFollowRedirects() .validateCertificate() .setDeadline(timeoutMillis / 1000.0)); for (HTTPHeader header : headers) { request.addHeader(header); } request.addHeader(USER_AGENT); if (payload != null && payload.length > 0) { request.setHeader(new HTTPHeader(CONTENT_LENGTH, String.valueOf(payload.length))); try { request.setHeader(new HTTPHeader(CONTENT_MD5, BaseEncoding.base64().encode(MessageDigest.getInstance("MD5").digest(payload)))); } catch (NoSuchAlgorithmException e) { log.severe( "Unable to get a MessageDigest instance, no Content-MD5 header sent.\n" + e.toString()); } request.setPayload(payload); } else { request.setHeader(ZERO_CONTENT_LENGTH); } return request; }
Example 8
Source File: URLFetchUtils.java From appengine-gcs-client with Apache License 2.0 | 5 votes |
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; }
Example 9
Source File: OauthRawGcsServiceTest.java From appengine-gcs-client with Apache License 2.0 | 5 votes |
@Test public void makeRequestShouldHandleAPresentPayload() throws MalformedURLException { URL url = new URL("https://storage.googleapis.com/sample-bucket/sample-object"); byte[] payload = "hello".getBytes(UTF_8); HTTPRequest expected = new HTTPRequest(url, PUT, FETCH_OPTIONS); expected.addHeader(new HTTPHeader("Content-Length", "5")); expected.addHeader(new HTTPHeader("Content-MD5", "XUFAKrxLKna5cZ2REBfFkg==")); expected.addHeader(new HTTPHeader("User-Agent", OauthRawGcsService.USER_AGENT_PRODUCT)); expected.setPayload(payload); assertHttpRequestEquals(expected, service.makeRequest(GCS_FILENAME, null, PUT, 30000, payload)); }
Example 10
Source File: HttpRequestTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@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()); }
Example 11
Source File: URLFetchTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@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); }
Example 12
Source File: HttpClientGAE.java From flickr-uploader with GNU General Public License v2.0 | 5 votes |
public static String getResponsePUT(String url, Map<String, String> params, String json, Map<String, String> headers) { int retry = 0; while (retry < 3) { long start = System.currentTimeMillis(); try { URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService(); String urlStr = ToolString.toUrl(url.trim(), params); logger.debug("PUT : " + urlStr); HTTPRequest httpRequest = new HTTPRequest(new URL(urlStr), HTTPMethod.PUT, FetchOptions.Builder.withDeadline(deadline)); if (headers != null) { for (String header : headers.keySet()) { httpRequest.addHeader(new HTTPHeader(header, headers.get(header))); } } httpRequest.setPayload(json.getBytes()); HTTPResponse response = fetcher.fetch(httpRequest); return processResponse(response); } catch (Throwable e) { retry++; if (e instanceof RuntimeException) { throw (RuntimeException) e; } else if (retry < 3) { logger.warn("retrying after " + (System.currentTimeMillis() - start) + " and " + retry + " retries\n" + e.getClass() + e.getMessage()); } else { logger.error(e.getClass() + "\n" + ToolString.stack2string(e)); } } } return null; }