Package com.google.appengine.api.urlfetch

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


    }

    @Test
    public void testFindFavIconUrlWhenNotFoundByFinder() throws Exception {
        String html = "html";
        HTTPResponse mockResponse = createMock(HTTPResponse.class);
        expect(mockResponse.getFinalUrl()).andReturn(null);
        expect(mockResponse.getContent()).andStubReturn(html.getBytes());

        String urlAsString = "http://my.domain.com/foo/index.html";
        URL url = new URL(urlAsString);
        expect(mockUrlFetchService.fetch(url)).andReturn(mockResponse);

        expect(mockFavIconFinder.findFavIcon(EasyMock.<InputSource>notNull(), eq(url))).andReturn(null);

        HTTPResponse mockIconResponse = createMock(HTTPResponse.class);
        expect(mockIconResponse.getResponseCode()).andStubReturn(200);
        expect(mockUrlFetchService.fetch(new URL("http://my.domain.com/favicon.ico"))).andReturn(mockIconResponse);

        replay(mockUrlFetchService, mockResponse, mockFavIconFinder, mockIconResponse);

        String result = impl.findFavIconUrl(urlAsString);
View Full Code Here


    }

    @Test
    public void testFindFavIconUrlWhenNoDefaultFavIcon() throws Exception {
        String html = "html";
        HTTPResponse mockResponse = createMock(HTTPResponse.class);
        expect(mockResponse.getFinalUrl()).andStubReturn(null);
        expect(mockResponse.getContent()).andStubReturn(html.getBytes());

        String urlAsString = "http://my.domain.com/foo/index.html";
        URL url = new URL(urlAsString);
        expect(mockUrlFetchService.fetch(url)).andReturn(mockResponse);

        expect(mockFavIconFinder.findFavIcon(EasyMock.<InputSource>notNull(), eq(url))).andReturn(null);

        HTTPResponse mockIconResponse = createMock(HTTPResponse.class);
        expect(mockIconResponse.getResponseCode()).andStubReturn(404);
        expect(mockUrlFetchService.fetch(new URL("http://my.domain.com/favicon.ico"))).andReturn(mockIconResponse);

        replay(mockUrlFetchService, mockResponse, mockFavIconFinder, mockIconResponse);

        String result = impl.findFavIconUrl(urlAsString);
View Full Code Here

        if (future.isCancelled()) {
            th = new TwitterException("HttpResponse already disconnected.");
            throw new TwitterRuntimeException(th);
        }
        try {
            HTTPResponse r = future.get();
            statusCode = r.getResponseCode();
            headers = new HashMap<String, String>();
            for (HTTPHeader h : r.getHeaders()) {
                headers.put(h.getName(), h.getValue());
            }
            byte[] content = r.getContent();
            is = new ByteArrayInputStream(content);
            if ("gzip".equals(headers.get("Content-Encoding"))) {
                // the response is gzipped
                try {
                    is = new GZIPInputStream(is);
View Full Code Here

   private URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();

   public InputStream fetch(URL url) throws IOException
   {
      HTTPRequest request = new HTTPRequest(url, HTTPMethod.POST);
      HTTPResponse response = urlFetchService.fetch(request);
      return new ByteArrayInputStream(response.getContent());
   }
View Full Code Here

    if (text.startsWith("[test]")) return;

    final HTTPRequest request = new HTTPRequest(KWeiboUpdateApiUrl, HTTPMethod.POST);
    request.addHeader(new HTTPHeader("Authorization", "Basic " + iBase64.encode(iWeiboCredential.getBytes("US-ASCII"))));
    request.setPayload(("source=" + mConfig.getProperty("weibo.app.key") + "&status=" + URLEncoder.encode(text, "UTF-8")).getBytes("US-ASCII"));
    final HTTPResponse result = iURLFetch.fetch(request);
    if (result.getResponseCode() != 200) {
      final String msg = "Failed to post to Weibo: " + result.getResponseCode() + "\n" + new String(result.getContent(), "UTF-8");
      log.warning(msg);
      Notifier.send(msg);
    } else
      Notifier.send("To Weibo: " + text);
  }
View Full Code Here

    // credentials accompany the request".  I didn't read the document in enough
    // detail to check whether this is supposed to apply to photos as well, but
    // it seems to work.
    URL targetUrl = new URL("https://www.google.com/m8/feeds/photos/media/default/"
        + UriEscapers.uriQueryStringEscaper(false).escape(photoId));
    HTTPResponse response = fetch.fetch(
        new HTTPRequest(targetUrl, HTTPMethod.GET,
            FetchOptions.Builder.withDefaults().disallowTruncate()));
    if (response.getResponseCode() == 404) {
      // Rather than a broken image, use the unknown avatar.
      log.info("Missing image, using default");
      resp.sendRedirect(SharedConstants.UNKNOWN_AVATAR_URL);
    } else {
      ProxyHandler.copyResponse(response, resp);
View Full Code Here

            URL url = new URL(GEOIP_URL + ip);
            HTTPRequest request = new HTTPRequest(url, HTTPMethod.GET);
            request.addHeader(new HTTPHeader("User-Agent", USER_AGENT));

            HTTPResponse response = urlfetch.fetch(request);
            if (response.getResponseCode() != 200) {
                return null;
            }

            String country = new String(response.getContent(), "UTF-8");
            return country;
        } catch (Throwable t) {
            logger.log(Level.WARNING, "Unable to fetch GeoIp for " + ip, t);
            return null;
        }
View Full Code Here

      b.append("\n" + new String(resp.getContent(), Charsets.UTF_8));
      return "" + b;
    }

    private String fetch(HTTPRequest req) throws IOException {
      HTTPResponse response = fetchService.fetch(req);
      int responseCode = response.getResponseCode();

      if (responseCode >= 300 && responseCode < 400) {
        throw new RuntimeException("Unexpected redirect for url " + req.getURL()
            + ": " + describeResponse(response));
      }

      byte[] rawResponseBody = response.getContent();
      String responseBody;
      if (rawResponseBody == null) {
        responseBody = "";
      } else {
        responseBody = new String(rawResponseBody, Charsets.UTF_8);
View Full Code Here

      boolean tokenJustRefreshed) throws IOException {
    log.info("Sending request (token just refreshed: " + tokenJustRefreshed + "): "
        + describeRequest(req));
    helper.authorize(req);
    //log.info("req after authorizing: " + describeRequest(req));
    HTTPResponse resp = fetch.fetch(req);
    log.info("response: " + describeResponse(resp, false));
    if (refreshNeeded.refreshNeeded(resp)) {
      if (tokenJustRefreshed) {
        throw new NeedNewOAuthTokenException("Token just refreshed, still no good: "
            + describeResponse(resp, true));
View Full Code Here

              log.info("Fetching attachment " + url);
              try {
                // We could fetch several attachments asynchronously in parallel to save instance
                // hours, but let's hope that the connection between App Engine and Google Wave
                // is fast enough to make that irrelevant.
                HTTPResponse response = urlfetch.fetch(
                    new HTTPRequest(new URL(url), HTTPMethod.GET));
                if (response.getResponseCode() != 200) {
                  throw new RetryableFailure("Unexpected response code: "
                      + response.getResponseCode());
                }
                byte[] bytes = response.getContent();
                if (expectedBytes != null) {
                  Assert.check(expectedBytes == bytes.length, "Expected %s bytes, got %s: %s",
                      expectedBytes, bytes.length, prettyBytes(bytes));
                }
                final AppEngineFile file = dump(mimeType, filename, ByteBuffer.wrap(bytes));
View Full Code Here

TOP

Related Classes of com.google.appengine.api.urlfetch.HTTPResponse

Copyright © 2018 www.massapicom. 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.