Search in sources :

Example 1 with TransformFuture

use of com.koushikdutta.async.future.TransformFuture in project AndroidAsync by koush.

the class SpdyMiddleware method exchangeHeaders.

@Override
public boolean exchangeHeaders(final OnExchangeHeaderData data) {
    if (!(data.socket instanceof AsyncSpdyConnection.SpdySocket))
        return super.exchangeHeaders(data);
    AsyncHttpRequestBody requestBody = data.request.getBody();
    if (requestBody != null) {
        data.response.sink(data.socket);
    }
    // headers were already sent as part of the socket being opened.
    data.sendHeadersCallback.onCompleted(null);
    final AsyncSpdyConnection.SpdySocket spdySocket = (AsyncSpdyConnection.SpdySocket) data.socket;
    spdySocket.headers().then(new TransformFuture<Headers, List<Header>>() {

        @Override
        protected void transform(List<Header> result) throws Exception {
            Headers headers = new Headers();
            for (Header header : result) {
                String key = header.name.utf8();
                String value = header.value.utf8();
                headers.add(key, value);
            }
            String status = headers.remove(Header.RESPONSE_STATUS.utf8());
            String[] statusParts = status.split(" ", 2);
            data.response.code(Integer.parseInt(statusParts[0]));
            if (statusParts.length == 2)
                data.response.message(statusParts[1]);
            data.response.protocol(headers.remove(Header.VERSION.utf8()));
            data.response.headers(headers);
            setComplete(headers);
        }
    }).setCallback(new FutureCallback<Headers>() {

        @Override
        public void onCompleted(Exception e, Headers result) {
            data.receiveHeadersCallback.onCompleted(e);
            DataEmitter emitter = HttpUtil.getBodyDecoder(spdySocket, spdySocket.getConnection().protocol, result, false);
            data.response.emitter(emitter);
        }
    });
    return true;
}
Also used : Headers(com.koushikdutta.async.http.Headers) IOException(java.io.IOException) DataEmitter(com.koushikdutta.async.DataEmitter) ArrayList(java.util.ArrayList) ByteBufferList(com.koushikdutta.async.ByteBufferList) List(java.util.List) AsyncHttpRequestBody(com.koushikdutta.async.http.body.AsyncHttpRequestBody) TransformFuture(com.koushikdutta.async.future.TransformFuture)

Aggregations

ByteBufferList (com.koushikdutta.async.ByteBufferList)1 DataEmitter (com.koushikdutta.async.DataEmitter)1 TransformFuture (com.koushikdutta.async.future.TransformFuture)1 Headers (com.koushikdutta.async.http.Headers)1 AsyncHttpRequestBody (com.koushikdutta.async.http.body.AsyncHttpRequestBody)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1