Java Code Examples for com.google.appengine.api.urlfetch.HTTPRequest
The following examples show how to use
com.google.appengine.api.urlfetch.HTTPRequest.
These examples are extracted from open source projects.
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 Project: nomulus Author: google File: NordnUploadActionTest.java License: Apache License 2.0 | 6 votes |
@Before public void before() throws Exception { inject.setStaticField(Ofy.class, "clock", clock); when(fetchService.fetch(any(HTTPRequest.class))).thenReturn(httpResponse); when(httpResponse.getContent()).thenReturn("Success".getBytes(US_ASCII)); when(httpResponse.getResponseCode()).thenReturn(SC_ACCEPTED); when(httpResponse.getHeadersUncombined()) .thenReturn(ImmutableList.of(new HTTPHeader(LOCATION, "http://trololol"))); persistResource(loadRegistrar("TheRegistrar").asBuilder().setIanaIdentifier(99999L).build()); createTld("tld"); persistResource(Registry.get("tld").asBuilder().setLordnUsername("lolcat").build()); action.clock = clock; action.fetchService = fetchService; action.lordnRequestInitializer = lordnRequestInitializer; action.phase = "claims"; action.taskQueueUtils = new TaskQueueUtils(new Retrier(new FakeSleeper(clock), 3)); action.tld = "tld"; action.tmchMarksdbUrl = "http://127.0.0.1"; action.random = new SecureRandom(); action.retrier = new Retrier(new FakeSleeper(clock), 3); }
Example #2
Source Project: openid4java Author: jbufu File: AppEngineHttpFetcher.java License: Apache License 2.0 | 6 votes |
private static void addHeaders(HTTPRequest httpRequest, HttpRequestOptions requestOptions) { String contentType = requestOptions.getContentType(); if (contentType != null) { httpRequest.addHeader(new HTTPHeader("Content-Type", contentType)); } Map<String, String> headers = getRequestHeaders(requestOptions); if (headers != null) { for (Map.Entry<String, String> header : headers.entrySet()) { httpRequest.addHeader(new HTTPHeader(header.getKey(), header.getValue())); } } }
Example #3
Source Project: appengine-java-vm-runtime Author: GoogleCloudPlatform File: CurlServlet.java License: Apache License 2.0 | 6 votes |
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { String url = req.getParameter("url"); String deadlineSecs = req.getParameter("deadline"); URLFetchService service = URLFetchServiceFactory.getURLFetchService(); HTTPRequest fetchReq = new HTTPRequest(new URL(url)); if (deadlineSecs != null) { fetchReq.getFetchOptions().setDeadline(Double.valueOf(deadlineSecs)); } HTTPResponse fetchRes = service.fetch(fetchReq); for (HTTPHeader header : fetchRes.getHeaders()) { res.addHeader(header.getName(), header.getValue()); } if (fetchRes.getResponseCode() == 200) { res.getOutputStream().write(fetchRes.getContent()); } else { res.sendError(fetchRes.getResponseCode(), "Error while fetching"); } }
Example #4
Source Project: google-maps-services-java Author: googlemaps File: GaePendingResult.java License: Apache License 2.0 | 6 votes |
/** * @param request HTTP request to execute. * @param client The client used to execute the request. * @param responseClass Model class to unmarshal JSON body content. * @param fieldNamingPolicy FieldNamingPolicy for unmarshaling JSON. * @param errorTimeOut Number of milliseconds to re-send erroring requests. * @param maxRetries Number of times allowed to re-send erroring requests. */ public GaePendingResult( HTTPRequest request, URLFetchService client, Class<R> responseClass, FieldNamingPolicy fieldNamingPolicy, long errorTimeOut, Integer maxRetries, ExceptionsAllowedToRetry exceptionsAllowedToRetry, RequestMetrics metrics) { this.request = request; this.client = client; this.responseClass = responseClass; this.fieldNamingPolicy = fieldNamingPolicy; this.errorTimeOut = errorTimeOut; this.maxRetries = maxRetries; this.exceptionsAllowedToRetry = exceptionsAllowedToRetry; this.metrics = metrics; metrics.startNetwork(); this.call = client.fetchAsync(request); }
Example #5
Source Project: appengine-gcs-client Author: GoogleCloudPlatform File: OauthRawGcsService.java License: Apache License 2.0 | 6 votes |
@Override public RawGcsCreationToken beginObjectCreation( GcsFilename filename, GcsFileOptions options, long timeoutMillis) throws IOException { HTTPRequest req = makeRequest(filename, null, POST, timeoutMillis); req.setHeader(RESUMABLE_HEADER); addOptionsHeaders(req, options); HTTPResponse resp; try { resp = urlfetch.fetch(req); } catch (IOException e) { throw createIOException(new HTTPRequestInfo(req), e); } if (resp.getResponseCode() == 201) { String location = URLFetchUtils.getSingleHeader(resp, LOCATION); String queryString = new URL(location).getQuery(); Preconditions.checkState( queryString != null, LOCATION + " header," + location + ", witout a query string"); Map<String, String> params = Splitter.on('&').withKeyValueSeparator('=').split(queryString); Preconditions.checkState(params.containsKey(UPLOAD_ID), LOCATION + " header," + location + ", has a query string without " + UPLOAD_ID); return new GcsRestCreationToken(filename, params.get(UPLOAD_ID), 0); } else { throw HttpErrorHandler.error(new HTTPRequestInfo(req), resp); } }
Example #6
Source Project: appengine-gcs-client Author: GoogleCloudPlatform File: OauthRawGcsService.java License: Apache License 2.0 | 6 votes |
private void addOptionsHeaders(HTTPRequest req, GcsFileOptions options) { if (options == null) { return; } if (options.getMimeType() != null) { req.setHeader(new HTTPHeader(CONTENT_TYPE, options.getMimeType())); } if (options.getAcl() != null) { req.setHeader(new HTTPHeader(ACL, options.getAcl())); } if (options.getCacheControl() != null) { req.setHeader(new HTTPHeader(CACHE_CONTROL, options.getCacheControl())); } if (options.getContentDisposition() != null) { req.setHeader(new HTTPHeader(CONTENT_DISPOSITION, options.getContentDisposition())); } if (options.getContentEncoding() != null) { req.setHeader(new HTTPHeader(CONTENT_ENCODING, options.getContentEncoding())); } for (Entry<String, String> entry : options.getUserMetadata().entrySet()) { req.setHeader(new HTTPHeader(X_GOOG_META + entry.getKey(), entry.getValue())); } }
Example #7
Source Project: appengine-gcs-client Author: GoogleCloudPlatform File: OauthRawGcsService.java License: Apache License 2.0 | 6 votes |
/** * Same as {@link #put} but is runs asynchronously and returns a future. In the event of an error * the exception out of the future will be an ExecutionException with the cause set to the same * exception that would have been thrown by put. */ private Future<RawGcsCreationToken> putAsync(final GcsRestCreationToken token, ByteBuffer chunk, final boolean isFinalChunk, long timeoutMillis) { final int length = chunk.remaining(); HTTPRequest request = createPutRequest(token, chunk, isFinalChunk, timeoutMillis, length); final HTTPRequestInfo info = new HTTPRequestInfo(request); return new FutureWrapper<HTTPResponse, RawGcsCreationToken>(urlfetch.fetchAsync(request)) { @Override protected Throwable convertException(Throwable e) { return OauthRawGcsService.convertException(info, e); } @Override protected GcsRestCreationToken wrap(HTTPResponse resp) throws Exception { return handlePutResponse(token, isFinalChunk, length, info, resp); } }; }
Example #8
Source Project: appengine-gcs-client Author: GoogleCloudPlatform File: OauthRawGcsService.java License: Apache License 2.0 | 6 votes |
/** True if deleted, false if not found. */ @Override public boolean deleteObject(GcsFilename filename, long timeoutMillis) throws IOException { HTTPRequest req = makeRequest(filename, null, DELETE, timeoutMillis); HTTPResponse resp; try { resp = urlfetch.fetch(req); } catch (IOException e) { throw createIOException(new HTTPRequestInfo(req), e); } switch (resp.getResponseCode()) { case 204: return true; case 404: return false; default: throw HttpErrorHandler.error(new HTTPRequestInfo(req), resp); } }
Example #9
Source Project: appengine-gcs-client Author: GoogleCloudPlatform File: OauthRawGcsService.java License: Apache License 2.0 | 6 votes |
@Override public GcsFileMetadata getObjectMetadata(GcsFilename filename, long timeoutMillis) throws IOException { HTTPRequest req = makeRequest(filename, null, HEAD, timeoutMillis); HTTPResponse resp; try { resp = urlfetch.fetch(req); } catch (IOException e) { throw createIOException(new HTTPRequestInfo(req), e); } int responseCode = resp.getResponseCode(); if (responseCode == 404) { return null; } if (responseCode != 200) { throw HttpErrorHandler.error(new HTTPRequestInfo(req), resp); } return getMetadataFromResponse( filename, resp, getLengthFromHeader(resp, X_GOOG_CONTENT_LENGTH)); }
Example #10
Source Project: appengine-gcs-client Author: GoogleCloudPlatform File: OauthRawGcsService.java License: Apache License 2.0 | 6 votes |
@Override public void composeObject(Iterable<String> source, GcsFilename dest, long timeoutMillis) throws IOException { StringBuilder xmlContent = new StringBuilder(Iterables.size(source) * 50); Escaper escaper = XmlEscapers.xmlContentEscaper(); xmlContent.append("<ComposeRequest>"); for (String srcFileName : source) { xmlContent.append("<Component><Name>") .append(escaper.escape(srcFileName)) .append("</Name></Component>"); } xmlContent.append("</ComposeRequest>"); HTTPRequest req = makeRequest( dest, COMPOSE_QUERY_STRINGS, PUT, timeoutMillis, xmlContent.toString().getBytes(UTF_8)); 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); } }
Example #11
Source Project: appengine-gcs-client Author: GoogleCloudPlatform File: OauthRawGcsService.java License: Apache License 2.0 | 6 votes |
@Override public void copyObject(GcsFilename source, GcsFilename dest, GcsFileOptions fileOptions, long timeoutMillis) throws IOException { HTTPRequest req = makeRequest(dest, null, PUT, timeoutMillis); req.setHeader(new HTTPHeader(X_GOOG_COPY_SOURCE, makePath(source))); if (fileOptions != null) { req.setHeader(REPLACE_METADATA_HEADER); addOptionsHeaders(req, fileOptions); } 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); } }
Example #12
Source Project: appengine-gcs-client Author: GoogleCloudPlatform File: URLFetchUtilsTest.java License: 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 #13
Source Project: appengine-tck Author: GoogleCloudPlatform File: URLFetchTest.java License: Apache License 2.0 | 6 votes |
@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 }
Example #14
Source Project: flickr-uploader Author: rafali File: HttpClientGAE.java License: GNU General Public License v2.0 | 6 votes |
public static String getResponseDELETE(String url, Map<String, String> params, 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("DELETE : " + urlStr); HTTPRequest httpRequest = new HTTPRequest(new URL(urlStr), HTTPMethod.DELETE, FetchOptions.Builder.withDeadline(deadline)); 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; }
Example #15
Source Project: flickr-uploader Author: rafali File: HttpClientGAE.java License: 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 #16
Source Project: solutions-google-compute-engine-orchestrator Author: GoogleCloudPlatform File: GceApiUtils.java License: 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 #17
Source Project: nomulus Author: google File: UrlFetchUtils.java License: 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 #18
Source Project: nomulus Author: google File: UrlFetchUtils.java License: Apache License 2.0 | 5 votes |
/** Sets the HTTP Basic Authentication header on an {@link HTTPRequest}. */ public static void setAuthorizationHeader(HTTPRequest req, Optional<String> login) { if (login.isPresent()) { String token = base64().encode(login.get().getBytes(UTF_8)); req.addHeader(new HTTPHeader(AUTHORIZATION, "Basic " + token)); } }
Example #19
Source Project: nomulus Author: google File: NordnUploadAction.java License: Apache License 2.0 | 5 votes |
/** * Upload LORDN file to MarksDB. * * <p>Idempotency: If the exact same LORDN report is uploaded twice, the MarksDB server will * return the same confirmation number. * * @see <a href="http://tools.ietf.org/html/draft-lozano-tmch-func-spec-08#section-6.3"> * TMCH functional specifications - LORDN File</a> */ private void uploadCsvToLordn(String urlPath, String csvData) throws IOException { String url = tmchMarksdbUrl + urlPath; logger.atInfo().log( "LORDN upload task %s: Sending to URL: %s ; data: %s", actionLogId, url, csvData); HTTPRequest req = new HTTPRequest(new URL(url), POST, validateCertificate().setDeadline(60d)); lordnRequestInitializer.initialize(req, tld); setPayloadMultipart(req, "file", "claims.csv", CSV_UTF_8, csvData, random); HTTPResponse rsp; try { rsp = fetchService.fetch(req); } catch (IOException e) { throw new IOException( String.format("Error connecting to MarksDB at URL %s", url), e); } if (logger.atInfo().isEnabled()) { String response = (rsp.getContent() == null) ? "(null)" : new String(rsp.getContent(), US_ASCII); logger.atInfo().log( "LORDN upload task %s response: HTTP response code %d, response data: %s", actionLogId, rsp.getResponseCode(), response); } if (rsp.getResponseCode() != SC_ACCEPTED) { throw new UrlFetchException( String.format( "LORDN upload task %s error: Failed to upload LORDN claims to MarksDB", actionLogId), req, rsp); } Optional<String> location = getHeaderFirst(rsp, LOCATION); if (!location.isPresent()) { throw new UrlFetchException( String.format( "LORDN upload task %s error: MarksDB failed to provide a Location header", actionLogId), req, rsp); } getQueue(NordnVerifyAction.QUEUE).add(makeVerifyTask(new URL(location.get()))); }
Example #20
Source Project: nomulus Author: google File: Marksdb.java License: Apache License 2.0 | 5 votes |
byte[] fetch(URL url, Optional<String> loginAndPassword) throws IOException { HTTPRequest req = new HTTPRequest(url, GET, validateCertificate().setDeadline(60d)); setAuthorizationHeader(req, loginAndPassword); HTTPResponse rsp; try { rsp = fetchService.fetch(req); } catch (IOException e) { throw new IOException( String.format("Error connecting to MarksDB at URL %s", url), e); } if (rsp.getResponseCode() != SC_OK) { throw new UrlFetchException("Failed to fetch from MarksDB", req, rsp); } return rsp.getContent(); }
Example #21
Source Project: nomulus Author: google File: RdeReporter.java License: 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 #22
Source Project: nomulus Author: google File: NordnVerifyActionTest.java License: Apache License 2.0 | 5 votes |
@Before public void before() throws Exception { when(httpResponse.getResponseCode()).thenReturn(SC_OK); when(httpResponse.getContent()).thenReturn(LOG_ACCEPTED.getBytes(UTF_8)); when(fetchService.fetch(any(HTTPRequest.class))).thenReturn(httpResponse); createTld("gtld"); persistResource(Registry.get("gtld").asBuilder().setLordnUsername("lolcat").build()); action.tld = "gtld"; action.fetchService = fetchService; action.lordnRequestInitializer = lordnRequestInitializer; action.response = response; action.url = new URL("http://127.0.0.1/blobio"); }
Example #23
Source Project: nomulus Author: google File: TmchActionTestCase.java License: Apache License 2.0 | 5 votes |
@Before public void commonBefore() throws Exception { marksdb.fetchService = fetchService; marksdb.tmchMarksdbUrl = MARKSDB_URL; marksdb.marksdbPublicKey = TmchData.loadPublicKey(TmchTestData.loadBytes("pubkey")); when(fetchService.fetch(any(HTTPRequest.class))).thenReturn(httpResponse); when(httpResponse.getResponseCode()).thenReturn(SC_OK); }
Example #24
Source Project: nomulus Author: google File: RdeReportActionTest.java License: Apache License 2.0 | 5 votes |
@Test public void testRunWithLock_fetchFailed_throwsRuntimeException() throws Exception { class ExpectedThrownException extends RuntimeException {} when(urlFetchService.fetch(any(HTTPRequest.class))).thenThrow(new ExpectedThrownException()); assertThrows( ExpectedThrownException.class, () -> createAction().runWithLock(loadRdeReportCursor())); }
Example #25
Source Project: nomulus Author: google File: UrlFetchUtilsTest.java License: Apache License 2.0 | 5 votes |
@Test public void testSetPayloadMultipart() { HTTPRequest request = mock(HTTPRequest.class); setPayloadMultipart( request, "lol", "cat", CSV_UTF_8, "The nice people at the store say hello. ヘ(◕。◕ヘ)", random); ArgumentCaptor<HTTPHeader> headerCaptor = ArgumentCaptor.forClass(HTTPHeader.class); verify(request, times(2)).addHeader(headerCaptor.capture()); List<HTTPHeader> addedHeaders = headerCaptor.getAllValues(); assertThat(addedHeaders.get(0).getName()).isEqualTo(CONTENT_TYPE); assertThat(addedHeaders.get(0).getValue()) .isEqualTo( "multipart/form-data; " + "boundary=\"------------------------------AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\""); assertThat(addedHeaders.get(1).getName()).isEqualTo(CONTENT_LENGTH); assertThat(addedHeaders.get(1).getValue()).isEqualTo("294"); String payload = "--------------------------------AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\r\n" + "Content-Disposition: form-data; name=\"lol\"; filename=\"cat\"\r\n" + "Content-Type: text/csv; charset=utf-8\r\n" + "\r\n" + "The nice people at the store say hello. ヘ(◕。◕ヘ)\r\n" + "--------------------------------AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA--\r\n"; verify(request).setPayload(payload.getBytes(UTF_8)); verifyNoMoreInteractions(request); }
Example #26
Source Project: nomulus Author: google File: UrlFetchUtilsTest.java License: Apache License 2.0 | 5 votes |
@Test public void testSetPayloadMultipart_boundaryInPayload() { HTTPRequest request = mock(HTTPRequest.class); String payload = "I screamed------------------------------AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHHH"; IllegalStateException thrown = assertThrows( IllegalStateException.class, () -> setPayloadMultipart(request, "lol", "cat", CSV_UTF_8, payload, random)); assertThat(thrown) .hasMessageThat() .contains( "Multipart data contains autogenerated boundary: " + "------------------------------AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); }
Example #27
Source Project: nomulus Author: google File: FakeURLFetchService.java License: Apache License 2.0 | 5 votes |
@Override public HTTPResponse fetch(HTTPRequest request) { URL requestURL = request.getURL(); if (backingMap.containsKey(requestURL)) { return backingMap.get(requestURL); } else { return new HTTPResponse(HttpURLConnection.HTTP_NOT_FOUND, null, null, ImmutableList.of()); } }
Example #28
Source Project: nomulus Author: google File: ForwardingURLFetchService.java License: Apache License 2.0 | 5 votes |
@Override public Future<HTTPResponse> fetchAsync(HTTPRequest request) { try { return Futures.immediateFuture(fetch(request)); } catch (Exception e) { return Futures.immediateFailedFuture(e); } }
Example #29
Source Project: openid4java Author: jbufu File: AppEngineHttpFetcher.java License: 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 #30
Source Project: appengine-gcs-client Author: GoogleCloudPlatform File: AbstractOAuthURLFetchService.java License: Apache License 2.0 | 5 votes |
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; }