Search in sources :

Example 1 with HTTPHeader

use of com.google.appengine.api.urlfetch.HTTPHeader 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 HTTPHeader

use of com.google.appengine.api.urlfetch.HTTPHeader in project OpenRefine by OpenRefine.

the class AppEngineClientConnection method receiveResponseHeader.

public HttpResponse receiveResponseHeader() {
    URLFetchService ufs = URLFetchServiceFactory.getURLFetchService();
    try {
        _appengine_hresponse = ufs.fetch(_appengine_hrequest);
    } catch (java.io.IOException e) {
        throw new RuntimeException(e);
    }
    org.apache.http.HttpResponse apache_response = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 0), _appengine_hresponse.getResponseCode(), null);
    for (HTTPHeader h : _appengine_hresponse.getHeaders()) {
        apache_response.addHeader(h.getName(), h.getValue());
    }
    return apache_response;
}
Also used : BasicHttpResponse(org.apache.http.message.BasicHttpResponse) URLFetchService(com.google.appengine.api.urlfetch.URLFetchService) HttpResponse(org.apache.http.HttpResponse) ProtocolVersion(org.apache.http.ProtocolVersion) HTTPHeader(com.google.appengine.api.urlfetch.HTTPHeader)

Example 3 with HTTPHeader

use of com.google.appengine.api.urlfetch.HTTPHeader in project OpenRefine by OpenRefine.

the class AppEngineClientConnection method sendRequestHeader.

public void sendRequestHeader(org.apache.http.HttpRequest apache_request) {
    URL request_url;
    HttpHost host = _route.getTargetHost();
    String protocol = host.getSchemeName();
    String addr = host.getHostName();
    int port = host.getPort();
    String path = apache_request.getRequestLine().getUri();
    try {
        request_url = new URL(protocol, addr, port, path);
    } catch (java.net.MalformedURLException e) {
        throw new RuntimeException(e);
    }
    HTTPMethod method = HTTPMethod.valueOf(apache_request.getRequestLine().getMethod());
    _appengine_hrequest = new HTTPRequest(request_url, method, allowTruncate().doNotFollowRedirects());
    Header[] apache_headers = apache_request.getAllHeaders();
    for (int i = 0; i < apache_headers.length; i++) {
        Header h = apache_headers[i];
        _appengine_hrequest.setHeader(new HTTPHeader(h.getName(), h.getValue()));
    }
}
Also used : HTTPRequest(com.google.appengine.api.urlfetch.HTTPRequest) HTTPHeader(com.google.appengine.api.urlfetch.HTTPHeader) Header(org.apache.http.Header) HTTPMethod(com.google.appengine.api.urlfetch.HTTPMethod) HttpHost(org.apache.http.HttpHost) URL(java.net.URL) HTTPHeader(com.google.appengine.api.urlfetch.HTTPHeader)

Example 4 with HTTPHeader

use of com.google.appengine.api.urlfetch.HTTPHeader in project pratilipi by Pratilipi.

the class HttpUtil method _toBlobEntry.

private static BlobEntry _toBlobEntry(String targetUrl, HTTPResponse response) throws UnexpectedServerException {
    String mimeType = null;
    int status = response.getResponseCode();
    byte[] data = response.getContent();
    for (HTTPHeader header : response.getHeadersUncombined()) {
        if (header.getName().equals("Content-Type")) {
            mimeType = header.getValue();
            break;
        }
    }
    logger.log(Level.INFO, "Http GET Request: " + targetUrl);
    logger.log(Level.INFO, "Status: " + status);
    logger.log(Level.INFO, "Type: " + mimeType);
    logger.log(Level.INFO, "Length: " + data.length);
    if (status != 200) {
        try {
            logger.log(Level.SEVERE, "Response: " + new String(data, "UTF-8"));
        } catch (IOException e) {
            logger.log(Level.SEVERE, "Failed to parse error response.", e);
        }
        throw new UnexpectedServerException();
    }
    return DataAccessorFactory.getBlobAccessor().newBlob(null, data, mimeType);
}
Also used : UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) IOException(java.io.IOException) HTTPHeader(com.google.appengine.api.urlfetch.HTTPHeader)

Example 5 with HTTPHeader

use of com.google.appengine.api.urlfetch.HTTPHeader 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

HTTPHeader (com.google.appengine.api.urlfetch.HTTPHeader)5 HTTPResponse (com.google.appengine.api.urlfetch.HTTPResponse)2 HTTPMethod (com.google.appengine.api.urlfetch.HTTPMethod)1 HTTPRequest (com.google.appengine.api.urlfetch.HTTPRequest)1 URLFetchService (com.google.appengine.api.urlfetch.URLFetchService)1 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 URL (java.net.URL)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 Header (org.apache.http.Header)1 HttpHost (org.apache.http.HttpHost)1 HttpResponse (org.apache.http.HttpResponse)1 ProtocolVersion (org.apache.http.ProtocolVersion)1 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)1