Java Code Examples for com.google.appengine.api.urlfetch.URLFetchServiceFactory
The following examples show how to use
com.google.appengine.api.urlfetch.URLFetchServiceFactory.
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: google-http-java-client Author: googleapis File: UrlFetchRequest.java License: Apache License 2.0 | 6 votes |
@Override public LowLevelHttpResponse execute() throws IOException { // write content if (getStreamingContent() != null) { String contentType = getContentType(); if (contentType != null) { addHeader("Content-Type", contentType); } String contentEncoding = getContentEncoding(); if (contentEncoding != null) { addHeader("Content-Encoding", contentEncoding); } ByteArrayOutputStream out = new ByteArrayOutputStream(); getStreamingContent().writeTo(out); byte[] payload = out.toByteArray(); if (payload.length != 0) { request.setPayload(payload); } } // connect URLFetchService service = URLFetchServiceFactory.getURLFetchService(); HTTPResponse response = service.fetch(request); return new UrlFetchResponse(response); }
Example #2
Source Project: appengine-java-vm-runtime Author: GoogleCloudPlatform File: AppstatsMonitoredServlet.java License: Apache License 2.0 | 6 votes |
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { try { URLFetchServiceFactory.getURLFetchService() .fetchAsync(new URL("http://www.example.com/asyncWithGet")) .get(); } catch (Exception anythingMightGoWrongHere) { // fall through } URLFetchServiceFactory.getURLFetchService() .fetchAsync(new URL("http://www.example.com/asyncWithoutGet")); MemcacheService memcache = MemcacheServiceFactory.getMemcacheService(); String randomKey = "" + Math.random(); String randomValue = "" + Math.random(); memcache.put(randomKey, randomValue); resp.setContentType("text/plain"); resp.getOutputStream().println(randomKey); }
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: 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 #5
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 #6
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 #7
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 #8
Source Project: java-docs-samples Author: GoogleCloudPlatform File: AnalyticsServlet.java License: Apache License 2.0 | 5 votes |
@Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { String trackingId = System.getenv("GA_TRACKING_ID"); URIBuilder builder = new URIBuilder(); builder .setScheme("http") .setHost("www.google-analytics.com") .setPath("/collect") .addParameter("v", "1") // API Version. .addParameter("tid", trackingId) // Tracking ID / Property ID. // Anonymous Client Identifier. Ideally, this should be a UUID that // is associated with particular user, device, or browser instance. .addParameter("cid", "555") .addParameter("t", "event") // Event hit type. .addParameter("ec", "example") // Event category. .addParameter("ea", "test action"); // Event action. URI uri = null; try { uri = builder.build(); } catch (URISyntaxException e) { throw new ServletException("Problem building URI", e); } URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService(); URL url = uri.toURL(); fetcher.fetch(url); resp.getWriter().println("Event tracked."); }
Example #9
Source Project: appengine-tck Author: GoogleCloudPlatform File: URLFetchServiceTest.java License: Apache License 2.0 | 5 votes |
@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()); }
Example #10
Source Project: appengine-tck Author: GoogleCloudPlatform File: URLFetchTest.java License: Apache License 2.0 | 5 votes |
@Test public void testBasicOps() throws Exception { URLFetchService service = URLFetchServiceFactory.getURLFetchService(); URL adminConsole = findAvailableUrl(URLS); HTTPResponse response = service.fetch(adminConsole); printResponse(response); URL jbossOrg = new URL("http://www.jboss.org"); if (available(jbossOrg)) { response = service.fetch(jbossOrg); printResponse(response); } }
Example #11
Source Project: appengine-tck Author: GoogleCloudPlatform File: URLFetchTest.java License: 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 Project: appengine-tck Author: GoogleCloudPlatform File: FetchOptionsBuilderTest.java License: Apache License 2.0 | 5 votes |
@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)); }
Example #13
Source Project: flickr-uploader Author: rafali File: HttpClientGAE.java License: 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; }
Example #14
Source Project: flickr-uploader Author: rafali File: HttpClientGAE.java License: GNU General Public License v2.0 | 5 votes |
public static String getResponseGET(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); HTTPRequest httpRequest = new HTTPRequest(new URL(urlStr), HTTPMethod.GET, FetchOptions.Builder.withDeadline(deadline)); if (headers != null) { for (String name : headers.keySet()) { httpRequest.addHeader(new HTTPHeader(name, headers.get(name))); } } return processResponse(fetcher.fetch(httpRequest)); } 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: openid4java Author: jbufu File: AppEngineGuiceModule.java License: Apache License 2.0 | 4 votes |
@Provides @Singleton public URLFetchService providerUrlFetchService() { return URLFetchServiceFactory.getURLFetchService(); }
Example #16
Source Project: appengine-tck Author: GoogleCloudPlatform File: URLFetchServiceTest.java License: Apache License 2.0 | 4 votes |
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()); }
Example #17
Source Project: appengine-tck Author: GoogleCloudPlatform File: URLFetchTestBase.java License: Apache License 2.0 | 4 votes |
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); }
Example #18
Source Project: dropbox-sdk-java Author: dropbox File: GoogleAppEngineRequestor.java License: MIT License | 4 votes |
public GoogleAppEngineRequestor(FetchOptions options) { this(options, URLFetchServiceFactory.getURLFetchService()); }