Search in sources :

Example 1 with MultipartCallback

use of com.koushikdutta.async.http.body.MultipartFormDataBody.MultipartCallback in project AndroidAsync by koush.

the class MultipartTests method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    httpServer = new AsyncHttpServer();
    httpServer.setErrorCallback(new CompletedCallback() {

        @Override
        public void onCompleted(Exception ex) {
            fail();
        }
    });
    httpServer.listen(AsyncServer.getDefault(), 5000);
    httpServer.post("/", new HttpServerRequestCallback() {

        int gotten = 0;

        @Override
        public void onRequest(final AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
            final MultipartFormDataBody body = (MultipartFormDataBody) request.getBody();
            body.setMultipartCallback(new MultipartCallback() {

                @Override
                public void onPart(Part part) {
                    if (part.isFile()) {
                        body.setDataCallback(new DataCallback() {

                            @Override
                            public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
                                gotten += bb.remaining();
                                bb.recycle();
                            }
                        });
                    }
                }
            });
            request.setEndCallback(new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    response.send(body.getField("baz") + gotten + body.getField("foo"));
                }
            });
        }
    });
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) ByteBufferList(com.koushikdutta.async.ByteBufferList) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) MultipartCallback(com.koushikdutta.async.http.body.MultipartFormDataBody.MultipartCallback) DataCallback(com.koushikdutta.async.callback.DataCallback) Part(com.koushikdutta.async.http.body.Part) DataEmitter(com.koushikdutta.async.DataEmitter) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) MultipartFormDataBody(com.koushikdutta.async.http.body.MultipartFormDataBody)

Aggregations

ByteBufferList (com.koushikdutta.async.ByteBufferList)1 DataEmitter (com.koushikdutta.async.DataEmitter)1 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)1 DataCallback (com.koushikdutta.async.callback.DataCallback)1 MultipartFormDataBody (com.koushikdutta.async.http.body.MultipartFormDataBody)1 MultipartCallback (com.koushikdutta.async.http.body.MultipartFormDataBody.MultipartCallback)1 Part (com.koushikdutta.async.http.body.Part)1 AsyncHttpServer (com.koushikdutta.async.http.server.AsyncHttpServer)1 AsyncHttpServerRequest (com.koushikdutta.async.http.server.AsyncHttpServerRequest)1 AsyncHttpServerResponse (com.koushikdutta.async.http.server.AsyncHttpServerResponse)1 HttpServerRequestCallback (com.koushikdutta.async.http.server.HttpServerRequestCallback)1