Examples of URLFetchService


Examples of com.google.appengine.api.urlfetch.URLFetchService

  return new String(content, Constants.HTTP_ENCODEING);
  }

  public static String doPostMultipart(String strUrl, Map<String, String> params, Map<String, String> fileUrlMaps) throws IOException {

  URLFetchService ufs = URLFetchServiceFactory.getURLFetchService();

  java.net.URL url = new java.net.URL(strUrl);

  HTTPRequest request = new HTTPRequest(url, HTTPMethod.POST, FetchOptions.Builder.withDeadline(TIMEOUT_SECONDS).allowTruncate());

  String boundary = makeBoundary();
  request.setHeader(new HTTPHeader("Content-Type", "multipart/form-data; boundary=" + boundary));
  ByteArrayOutputStream baos = new ByteArrayOutputStream();

  // form-data
  for (Map.Entry<String, String> formData : params.entrySet()) {
    write(baos, "--" + boundary + "\r\n");
    writeFormData(baos, formData.getKey(), formData.getValue());
  }

  // multipart
  for (Map.Entry<String, String> fileUrlMap : fileUrlMaps.entrySet()) {
    log.info("fetch binary file: " + fileUrlMap.getValue());
    HTTPRequest donwloadFilerequest = new HTTPRequest(new java.net.URL(fileUrlMap.getValue()), HTTPMethod.GET, FetchOptions.Builder.withDeadline(TIMEOUT_SECONDS));
    HTTPResponse donwloadFileResponse = ufs.fetch(donwloadFilerequest);

    byte[] binaryContent = donwloadFileResponse.getContent();

    String contentType = null;
    for (HTTPHeader header : donwloadFileResponse.getHeaders()) {
    if (header.getName().toLowerCase().equals("content-type")) {
      contentType = header.getValue();
      break;
    }
    }

    // file
    write(baos, "--" + boundary + "\r\n");
    writeMultipart(baos, fileUrlMap.getKey(), StringUtils.getFileName(fileUrlMap.getValue()), contentType, binaryContent);
  }

  write(baos, "--" + boundary + "--\r\n");

  request.setPayload(baos.toByteArray());

  HTTPResponse response = ufs.fetch(request);

  byte[] content = response.getContent();

  return new String(content, Constants.HTTP_ENCODEING);
  }
View Full Code Here

Examples of com.google.appengine.api.urlfetch.URLFetchService

public class URLFetchTest extends URLFetchTestBase {
    private static Charset UTF_8 = Charset.forName("UTF-8");

    @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
    }
View Full Code Here

Examples of com.google.appengine.api.urlfetch.URLFetchService

        sync(5000L); // wait a bit for async to finish
    }

    @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);
        }
    }
View Full Code Here

Examples of com.google.appengine.api.urlfetch.URLFetchService

        }
    }

    @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);
    }
View Full Code Here

Examples of com.google.appengine.api.urlfetch.URLFetchService

        Assert.assertEquals("Hopsasa", content);
    }

    @Test
    public void testHeaders() 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("Headers!".getBytes(UTF_8));

        HTTPResponse response = service.fetch(req);

        boolean found = false;
        List<HTTPHeader> headers = response.getHeadersUncombined();
        for (HTTPHeader h : headers) {
            if (h.getName().equals("ABC")) {
View Full Code Here

Examples of com.google.appengine.api.urlfetch.URLFetchService

    public void fetchNonExistentSite() throws Exception {
        fetchUrl("http://i.do.not.exist/", 503);
    }

    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());
    }
View Full Code Here

Examples of com.google.appengine.api.urlfetch.URLFetchService

        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());
    }
View Full Code Here

Examples of com.google.appengine.api.urlfetch.URLFetchService

        testOptions(url, HTTPMethod.GET, options, handler);
    }

    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);
    }
View Full Code Here

Examples of com.google.appengine.api.urlfetch.URLFetchService

        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));
    }
View Full Code Here

Examples of org.b3log.latke.urlfetch.URLFetchService

    @Override
    public void run() {
        LOGGER.finer("Executing scheduled task....");

        final URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();

        final HTTPRequest request = new HTTPRequest();

        try {
            request.setURL(new URL(url));
            request.setRequestMethod(HTTPRequestMethod.GET);
            urlFetchService.fetchAsync(request);

            LOGGER.log(Level.FINER, "Executed scheduled task[url={0}]", url);
        } catch (final Exception e) {
            LOGGER.log(Level.SEVERE, "Scheduled task execute failed", e);
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.