use of com.koushikdutta.async.DataEmitter in project AndroidAsync by koush.
the class AsyncHttpClient method executeSocket.
private void executeSocket(final AsyncHttpRequest request, final int redirectCount, final FutureAsyncHttpResponse cancel, final HttpConnectCallback callback, final AsyncHttpClientMiddleware.OnResponseCompleteDataOnRequestSentData data) {
// 4) wait for request to be sent fully
// and
// 6) wait for headers
final AsyncHttpResponseImpl ret = new AsyncHttpResponseImpl(request) {
@Override
protected void onRequestCompleted(Exception ex) {
if (ex != null) {
reportConnectedCompleted(cancel, ex, null, request, callback);
return;
}
request.logv("request completed");
if (cancel.isCancelled())
return;
// 5) after request is sent, set a header timeout
if (cancel.timeoutRunnable != null && mHeaders == null) {
mServer.removeAllCallbacks(cancel.scheduled);
cancel.scheduled = mServer.postDelayed(cancel.timeoutRunnable, getTimeoutRemaining(request));
}
for (AsyncHttpClientMiddleware middleware : mMiddleware) {
middleware.onRequestSent(data);
}
}
@Override
public void setDataEmitter(DataEmitter emitter) {
data.bodyEmitter = emitter;
for (AsyncHttpClientMiddleware middleware : mMiddleware) {
middleware.onBodyDecoder(data);
}
super.setDataEmitter(data.bodyEmitter);
Headers headers = mHeaders;
int responseCode = code();
if ((responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == 307) && request.getFollowRedirect()) {
String location = headers.get("Location");
Uri redirect;
try {
redirect = Uri.parse(location);
if (redirect.getScheme() == null) {
redirect = Uri.parse(new URL(new URL(request.getUri().toString()), location).toString());
}
} catch (Exception e) {
reportConnectedCompleted(cancel, e, this, request, callback);
return;
}
final String method = request.getMethod().equals(AsyncHttpHead.METHOD) ? AsyncHttpHead.METHOD : AsyncHttpGet.METHOD;
AsyncHttpRequest newReq = new AsyncHttpRequest(redirect, method);
newReq.executionTime = request.executionTime;
newReq.logLevel = request.logLevel;
newReq.LOGTAG = request.LOGTAG;
newReq.proxyHost = request.proxyHost;
newReq.proxyPort = request.proxyPort;
setupAndroidProxy(newReq);
copyHeader(request, newReq, "User-Agent");
copyHeader(request, newReq, "Range");
request.logi("Redirecting");
newReq.logi("Redirected");
execute(newReq, redirectCount + 1, cancel, callback);
setDataCallback(new NullDataCallback());
return;
}
request.logv("Final (post cache response) headers:\n" + toString());
// at this point the headers are done being modified
reportConnectedCompleted(cancel, null, this, request, callback);
}
protected void onHeadersReceived() {
super.onHeadersReceived();
if (cancel.isCancelled())
return;
// 7) on headers, cancel timeout
if (cancel.timeoutRunnable != null)
mServer.removeAllCallbacks(cancel.scheduled);
// allow the middleware to massage the headers before the body is decoded
request.logv("Received headers:\n" + toString());
for (AsyncHttpClientMiddleware middleware : mMiddleware) {
middleware.onHeadersReceived(data);
}
// drop through, and setDataEmitter will be called for the body decoder.
// headers will be further massaged in there.
}
@Override
protected void report(Exception ex) {
if (ex != null)
request.loge("exception during response", ex);
if (cancel.isCancelled())
return;
if (ex instanceof AsyncSSLException) {
request.loge("SSL Exception", ex);
AsyncSSLException ase = (AsyncSSLException) ex;
request.onHandshakeException(ase);
if (ase.getIgnore())
return;
}
final AsyncSocket socket = socket();
if (socket == null)
return;
super.report(ex);
if (!socket.isOpen() || ex != null) {
if (headers() == null && ex != null)
reportConnectedCompleted(cancel, ex, null, request, callback);
}
data.exception = ex;
for (AsyncHttpClientMiddleware middleware : mMiddleware) {
middleware.onResponseComplete(data);
}
}
@Override
public AsyncSocket detachSocket() {
request.logd("Detaching socket");
AsyncSocket socket = socket();
if (socket == null)
return null;
socket.setWriteableCallback(null);
socket.setClosedCallback(null);
socket.setEndCallback(null);
socket.setDataCallback(null);
setSocket(null);
return socket;
}
};
data.sendHeadersCallback = new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
if (ex != null)
ret.report(ex);
else
ret.onHeadersSent();
}
};
data.receiveHeadersCallback = new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
if (ex != null)
ret.report(ex);
else
ret.onHeadersReceived();
}
};
data.response = ret;
ret.setSocket(data.socket);
for (AsyncHttpClientMiddleware middleware : mMiddleware) {
if (middleware.exchangeHeaders(data))
break;
}
}
use of com.koushikdutta.async.DataEmitter in project AndroidAsync by koush.
the class NetworkEventReporterWrapper method interpretResponseEmitter.
public DataEmitter interpretResponseEmitter(final String requestId, @Nullable DataEmitter body, final boolean b64Encode) {
final NetworkPeerManager peerManager = getPeerManagerIfEnabled();
if (peerManager == null)
return null;
final WritableByteChannel channel;
try {
if (b64Encode) {
final Base64OutputStream b64out = new Base64OutputStream(peerManager.getResponseBodyFileManager().openResponseBodyFile(requestId, false), Base64.DEFAULT);
channel = Channels.newChannel(b64out);
} else {
channel = ((FileOutputStream) peerManager.getResponseBodyFileManager().openResponseBodyFile(requestId, false)).getChannel();
}
} catch (IOException e) {
return null;
}
FilteredDataEmitter ret = new FilteredDataEmitter() {
ByteBufferList pending = new ByteBufferList();
@Override
protected void report(Exception e) {
super.report(e);
StreamUtility.closeQuietly(channel);
if (e == null)
responseReadFinished(requestId);
else
responseReadFailed(requestId, e.toString());
}
@Override
public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
int amount = bb.remaining();
ByteBuffer[] original = bb.getAllArray();
ByteBuffer[] copy = new ByteBuffer[original.length];
for (int i = 0; i < original.length; i++) {
copy[i] = original[i].duplicate();
}
try {
for (ByteBuffer c : copy) {
channel.write(c);
}
} catch (IOException ignored) {
StreamUtility.closeQuietly(channel);
}
pending.addAll(original);
dataReceived(requestId, amount, amount);
super.onDataAvailable(emitter, pending);
}
};
ret.setDataEmitter(body);
return ret;
}
use of com.koushikdutta.async.DataEmitter in project AndroidAsync by koush.
the class StethoMiddleware method onBodyDecoder.
@Override
public void onBodyDecoder(OnBodyDataOnRequestSentData data) {
super.onBodyDecoder(data);
AsyncInspectorRequest inspect = data.state.get("inspect");
if (inspect == null)
return;
String ct = data.response.headers().get("Content-Type");
boolean isImage = ct != null && ct.startsWith("image/");
DataEmitter emitter = eventReporter.interpretResponseEmitter(inspect.id(), data.bodyEmitter, isImage);
if (emitter != null)
data.bodyEmitter = emitter;
}
use of com.koushikdutta.async.DataEmitter in project FileTransfer by CPPAlien.
the class WebService method startServer.
private void startServer() {
server.get("/images/.*", this::sendResources);
server.get("/scripts/.*", this::sendResources);
server.get("/css/.*", this::sendResources);
// index page
server.get("/", (AsyncHttpServerRequest request, AsyncHttpServerResponse response) -> {
try {
response.send(getIndexContent());
} catch (IOException e) {
e.printStackTrace();
response.code(500).end();
}
});
// query upload list
server.get("/files", (AsyncHttpServerRequest request, AsyncHttpServerResponse response) -> {
JSONArray array = new JSONArray();
File dir = Constants.DIR;
if (dir.exists() && dir.isDirectory()) {
String[] fileNames = dir.list();
if (fileNames != null) {
for (String fileName : fileNames) {
File file = new File(dir, fileName);
if (file.exists() && file.isFile()) {
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", fileName);
long fileLen = file.length();
DecimalFormat df = new DecimalFormat("0.00");
if (fileLen > 1024 * 1024) {
jsonObject.put("size", df.format(fileLen * 1f / 1024 / 1024) + "MB");
} else if (fileLen > 1024) {
jsonObject.put("size", df.format(fileLen * 1f / 1024) + "KB");
} else {
jsonObject.put("size", fileLen + "B");
}
array.put(jsonObject);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}
response.send(array.toString());
});
// delete
server.post("/files/.*", (AsyncHttpServerRequest request, AsyncHttpServerResponse response) -> {
final UrlEncodedFormBody body = (UrlEncodedFormBody) request.getBody();
if ("delete".equalsIgnoreCase(body.get().getString("_method"))) {
String path = request.getPath().replace("/files/", "");
try {
path = URLDecoder.decode(path, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
File file = new File(Constants.DIR, path);
if (file.exists() && file.isFile() && file.delete()) {
RxBus.get().post(Constants.RxBusEventType.LOAD_BOOK_LIST, 0);
}
}
response.end();
});
// download
server.get("/files/.*", (AsyncHttpServerRequest request, AsyncHttpServerResponse response) -> {
String path = request.getPath().replace("/files/", "");
try {
path = URLDecoder.decode(path, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
File file = new File(Constants.DIR, path);
if (file.exists() && file.isFile()) {
try {
response.getHeaders().add("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), "utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.sendFile(file);
return;
}
response.code(404).send("Not found!");
});
// upload
server.post("/files", (AsyncHttpServerRequest request, AsyncHttpServerResponse response) -> {
final MultipartFormDataBody body = (MultipartFormDataBody) request.getBody();
body.setMultipartCallback((Part part) -> {
if (part.isFile()) {
body.setDataCallback((DataEmitter emitter, ByteBufferList bb) -> {
fileUploadHolder.write(bb.getAllByteArray());
bb.recycle();
});
} else {
if (body.getDataCallback() == null) {
body.setDataCallback((DataEmitter emitter, ByteBufferList bb) -> {
try {
String fileName = URLDecoder.decode(new String(bb.getAllByteArray()), "UTF-8");
fileUploadHolder.setFileName(fileName);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
bb.recycle();
});
}
}
});
request.setEndCallback((Exception e) -> {
fileUploadHolder.reset();
response.end();
RxBus.get().post(Constants.RxBusEventType.LOAD_BOOK_LIST, 0);
});
});
server.get("/progress/.*", (final AsyncHttpServerRequest request, final AsyncHttpServerResponse response) -> {
JSONObject res = new JSONObject();
String path = request.getPath().replace("/progress/", "");
if (path.equals(fileUploadHolder.fileName)) {
try {
res.put("fileName", fileUploadHolder.fileName);
res.put("size", fileUploadHolder.totalSize);
res.put("progress", fileUploadHolder.fileOutPutStream == null ? 1 : 0.1);
} catch (JSONException e) {
e.printStackTrace();
}
}
response.send(res);
});
server.listen(mAsyncServer, Constants.HTTP_PORT);
}
use of com.koushikdutta.async.DataEmitter in project AndroidAsync by koush.
the class CacheTests method testFilteredDataEmitter.
public void testFilteredDataEmitter() throws Exception {
final Semaphore semaphore = new Semaphore(0);
FilteredDataEmitter f = new FilteredDataEmitter() {
@Override
public boolean isPaused() {
return false;
}
};
f.setDataCallback(new DataCallback() {
@Override
public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
assertEquals(bb.readString(), "hello");
bb.recycle();
semaphore.release();
}
});
f.onDataAvailable(f, new ByteBufferList().add(ByteBuffer.wrap("hello".getBytes())));
assertTrue("timeout", semaphore.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS));
f.setDataCallback(new DataCallback() {
@Override
public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
fail();
}
});
f.close();
f.onDataAvailable(f, new ByteBufferList().add(ByteBuffer.wrap("hello".getBytes())));
}
Aggregations