Search in sources :

Example 1 with MultipartFormDataBody

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

the class HttpServerTests 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.get("/hello", new HttpServerRequestCallback() {

        @Override
        public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
            assertNotNull(request.getHeaders().get("Host"));
            response.send("hello");
        }
    });
    httpServer.post("/echo", new HttpServerRequestCallback() {

        @Override
        public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
            try {
                assertNotNull(request.getHeaders().get("Host"));
                JSONObject json = new JSONObject();
                if (request.getBody() instanceof UrlEncodedFormBody) {
                    UrlEncodedFormBody body = (UrlEncodedFormBody) request.getBody();
                    for (NameValuePair pair : body.get()) {
                        json.put(pair.getName(), pair.getValue());
                    }
                } else if (request.getBody() instanceof JSONObjectBody) {
                    json = ((JSONObjectBody) request.getBody()).get();
                } else if (request.getBody() instanceof StringBody) {
                    json.put("foo", ((StringBody) request.getBody()).get());
                } else if (request.getBody() instanceof MultipartFormDataBody) {
                    MultipartFormDataBody body = (MultipartFormDataBody) request.getBody();
                    for (NameValuePair pair : body.get()) {
                        json.put(pair.getName(), pair.getValue());
                    }
                }
                response.send(json);
            } catch (Exception e) {
            }
        }
    });
}
Also used : NameValuePair(com.koushikdutta.async.http.NameValuePair) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) JSONObjectBody(com.koushikdutta.async.http.body.JSONObjectBody) JSONObject(org.json.JSONObject) StringBody(com.koushikdutta.async.http.body.StringBody) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) UrlEncodedFormBody(com.koushikdutta.async.http.body.UrlEncodedFormBody) MultipartFormDataBody(com.koushikdutta.async.http.body.MultipartFormDataBody)

Example 2 with MultipartFormDataBody

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

the class HttpTests method testMultipartFileContentType.

public void testMultipartFileContentType() throws Exception {
    File f = getContext().getFileStreamPath("empty");
    f.getParentFile().mkdirs();
    f.createNewFile();
    AsyncHttpServer httpServer = new AsyncHttpServer();
    httpServer.post("/", new HttpServerRequestCallback() {

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

                @Override
                public void onPart(Part part) {
                    response.send(part.getContentType());
                }
            });
        }
    });
    try {
        httpServer.listen(AsyncServer.getDefault(), 6666);
        String mime = Ion.with(getContext()).load("http://localhost:6666/").setMultipartFile("foo", "test/mime", f).asString().get(1000, TimeUnit.MILLISECONDS);
        assertEquals(mime, "test/mime");
    } finally {
        httpServer.stop();
    }
}
Also used : HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) Part(com.koushikdutta.async.http.body.Part) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) File(java.io.File) MultipartFormDataBody(com.koushikdutta.async.http.body.MultipartFormDataBody)

Example 3 with MultipartFormDataBody

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

the class ProgressTests method testUpload.

public void testUpload() throws Exception {
    AsyncHttpServer httpServer = new AsyncHttpServer();
    try {
        httpServer.listen(Ion.getDefault(getContext()).getServer(), 5000);
        httpServer.post("/", new HttpServerRequestCallback() {

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

                    @Override
                    public void onPart(Part part) {
                    }
                });
                multipartFormDataBody.setEndCallback(new CompletedCallback() {

                    @Override
                    public void onCompleted(Exception ex) {
                        response.send("Got parts!");
                    }
                });
            }
        });
        final Semaphore semaphore = new Semaphore(0);
        Ion.with(getContext()).load("http://localhost:5000/").uploadProgress(new ProgressCallback() {

            @Override
            public void onProgress(long downloaded, long total) {
                semaphore.release();
            }
        }).setMultipartParameter("foo", "bar").asString().get();
        assertTrue(semaphore.tryAcquire());
    } finally {
        Ion.getDefault(getContext()).getServer().stop();
    }
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) Part(com.koushikdutta.async.http.body.Part) ProgressCallback(com.koushikdutta.ion.ProgressCallback) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) Semaphore(java.util.concurrent.Semaphore) MultipartFormDataBody(com.koushikdutta.async.http.body.MultipartFormDataBody)

Example 4 with MultipartFormDataBody

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

the class IonRequestBuilder method setMultipartParameter.

@Override
public IonRequestBuilder setMultipartParameter(String name, String value) {
    if (multipartBody == null) {
        multipartBody = new MultipartFormDataBody();
        setBody(multipartBody);
    }
    if (value != null)
        multipartBody.addStringPart(name, value);
    return this;
}
Also used : MultipartFormDataBody(com.koushikdutta.async.http.body.MultipartFormDataBody)

Example 5 with MultipartFormDataBody

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

the class MultipartTests method testUpload.

public void testUpload() throws Exception {
    File dummy = getContext().getFileStreamPath("dummy.txt");
    final String FIELD_VAL = "bar";
    dummy.getParentFile().mkdirs();
    FileOutputStream fout = new FileOutputStream(dummy);
    byte[] zeroes = new byte[100000];
    for (int i = 0; i < 10; i++) {
        fout.write(zeroes);
    }
    fout.close();
    //        StreamUtility.writeFile(dummy, DUMMY_VAL);
    AsyncHttpPost post = new AsyncHttpPost("http://localhost:5000");
    MultipartFormDataBody body = new MultipartFormDataBody();
    body.addStringPart("foo", FIELD_VAL);
    body.addFilePart("my-file", dummy);
    body.addStringPart("baz", FIELD_VAL);
    post.setBody(body);
    Future<String> ret = AsyncHttpClient.getDefaultInstance().executeString(post, new StringCallback() {

        @Override
        public void onCompleted(Exception e, AsyncHttpResponse source, String result) {
        }
    });
    String data = ret.get(10000, TimeUnit.MILLISECONDS);
    assertEquals(data, FIELD_VAL + (zeroes.length * 10) + FIELD_VAL);
}
Also used : AsyncHttpResponse(com.koushikdutta.async.http.AsyncHttpResponse) FileOutputStream(java.io.FileOutputStream) StringCallback(com.koushikdutta.async.http.AsyncHttpClient.StringCallback) File(java.io.File) AsyncHttpPost(com.koushikdutta.async.http.AsyncHttpPost) MultipartFormDataBody(com.koushikdutta.async.http.body.MultipartFormDataBody)

Aggregations

MultipartFormDataBody (com.koushikdutta.async.http.body.MultipartFormDataBody)10 Part (com.koushikdutta.async.http.body.Part)5 AsyncHttpServer (com.koushikdutta.async.http.server.AsyncHttpServer)4 AsyncHttpServerRequest (com.koushikdutta.async.http.server.AsyncHttpServerRequest)4 AsyncHttpServerResponse (com.koushikdutta.async.http.server.AsyncHttpServerResponse)4 HttpServerRequestCallback (com.koushikdutta.async.http.server.HttpServerRequestCallback)4 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)3 FilePart (com.koushikdutta.async.http.body.FilePart)3 File (java.io.File)2 ByteBufferList (com.koushikdutta.async.ByteBufferList)1 DataEmitter (com.koushikdutta.async.DataEmitter)1 DataCallback (com.koushikdutta.async.callback.DataCallback)1 StringCallback (com.koushikdutta.async.http.AsyncHttpClient.StringCallback)1 AsyncHttpPost (com.koushikdutta.async.http.AsyncHttpPost)1 AsyncHttpResponse (com.koushikdutta.async.http.AsyncHttpResponse)1 NameValuePair (com.koushikdutta.async.http.NameValuePair)1 JSONObjectBody (com.koushikdutta.async.http.body.JSONObjectBody)1 MultipartCallback (com.koushikdutta.async.http.body.MultipartFormDataBody.MultipartCallback)1 StringBody (com.koushikdutta.async.http.body.StringBody)1 UrlEncodedFormBody (com.koushikdutta.async.http.body.UrlEncodedFormBody)1