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;
}
Aggregations