Search in sources :

Example 1 with HTTPResponse

use of com.google.appengine.api.urlfetch.HTTPResponse in project twitter4j by yusuke.

the class AppEngineHttpResponseImpl method ensureResponseEvaluated.

private void ensureResponseEvaluated() {
    if (th != null) {
        throw new TwitterRuntimeException(th);
    }
    if (responseGot) {
        return;
    }
    responseGot = true;
    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().toLowerCase(Locale.ENGLISH), 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);
            } catch (IOException e) {
                th = e;
                throw new TwitterRuntimeException(th);
            }
        }
        responseAsString = inputStreamToString(is);
        if (statusCode < OK || (statusCode != FOUND && MULTIPLE_CHOICES <= statusCode)) {
            if (statusCode == ENHANCE_YOUR_CLAIM || statusCode == BAD_REQUEST || statusCode < INTERNAL_SERVER_ERROR) {
                th = new TwitterException(responseAsString, null, statusCode);
                throw new TwitterRuntimeException(th);
            }
        }
    } catch (ExecutionException e) {
        th = e.getCause();
    } catch (InterruptedException e) {
        th = e.getCause();
    }
    if (th != null) {
        throw new TwitterRuntimeException(th);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) HTTPResponse(com.google.appengine.api.urlfetch.HTTPResponse) ExecutionException(java.util.concurrent.ExecutionException) HTTPHeader(com.google.appengine.api.urlfetch.HTTPHeader)

Example 2 with HTTPResponse

use of com.google.appengine.api.urlfetch.HTTPResponse in project dropbox-sdk-java by dropbox.

the class GoogleAppEngineRequestor method doGet.

@Override
public Response doGet(String url, Iterable<Header> headers) throws IOException {
    HTTPRequest request = newRequest(url, HTTPMethod.GET, headers);
    HTTPResponse response = service.fetch(request);
    return toRequestorResponse(response);
}
Also used : HTTPRequest(com.google.appengine.api.urlfetch.HTTPRequest) HTTPResponse(com.google.appengine.api.urlfetch.HTTPResponse)

Example 3 with HTTPResponse

use of com.google.appengine.api.urlfetch.HTTPResponse in project dropbox-sdk-java by dropbox.

the class GoogleAppEngineRequestor method toRequestorResponse.

private static Response toRequestorResponse(HTTPResponse response) {
    Map<String, List<String>> headers = new HashMap<String, List<String>>();
    for (HTTPHeader header : response.getHeadersUncombined()) {
        List<String> existing = headers.get(header.getName());
        if (existing == null) {
            existing = new ArrayList<String>();
            headers.put(header.getName(), existing);
        }
        existing.add(header.getValue());
    }
    return new Response(response.getResponseCode(), new ByteArrayInputStream(response.getContent()), headers);
}
Also used : HTTPResponse(com.google.appengine.api.urlfetch.HTTPResponse) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) List(java.util.List) HTTPHeader(com.google.appengine.api.urlfetch.HTTPHeader)

Aggregations

HTTPResponse (com.google.appengine.api.urlfetch.HTTPResponse)3 HTTPHeader (com.google.appengine.api.urlfetch.HTTPHeader)2 HTTPRequest (com.google.appengine.api.urlfetch.HTTPRequest)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 GZIPInputStream (java.util.zip.GZIPInputStream)1