Search in sources :

Example 16 with HttpServerRequestCallback

use of com.koushikdutta.async.http.server.HttpServerRequestCallback in project ion by koush.

the class HttpTests method testPut.

public void testPut() throws Exception {
    AsyncHttpServer httpServer = new AsyncHttpServer();
    try {
        httpServer.addAction("PUT", "/", new HttpServerRequestCallback() {

            @Override
            public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
                response.send(request.getMethod());
            }
        });
        httpServer.listen(Ion.getDefault(getContext()).getServer(), 5555);
        Future<String> ret = Ion.with(getContext()).load("PUT", "http://localhost:5555/").asString();
        assertEquals(ret.get(), "PUT");
    } finally {
        httpServer.stop();
    }
}
Also used : HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse)

Example 17 with HttpServerRequestCallback

use of com.koushikdutta.async.http.server.HttpServerRequestCallback in project ion by koush.

the class StreamTests method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    AsyncHttpServer server = new AsyncHttpServer();
    port = server.listen(0).getLocalPort();
    server.get("/", new HttpServerRequestCallback() {

        @Override
        public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
            response.code(200);
            ByteBuffer b = ByteBufferList.obtain(random.length);
            b.put(random);
            b.flip();
            ByteBufferList list = new ByteBufferList(b);
            Util.writeAll(response, list, new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    response.end();
                }
            });
        }
    });
}
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) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) ByteBuffer(java.nio.ByteBuffer)

Example 18 with HttpServerRequestCallback

use of com.koushikdutta.async.http.server.HttpServerRequestCallback 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)

Example 19 with HttpServerRequestCallback

use of com.koushikdutta.async.http.server.HttpServerRequestCallback in project AndroidAsync by koush.

the class RedirectTests method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    AsyncHttpServer server = new AsyncHttpServer();
    server.listen(6003);
    server.get("/foo", new HttpServerRequestCallback() {

        @Override
        public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
            response.redirect("/bar");
        }
    });
    server.get("/bar", new HttpServerRequestCallback() {

        @Override
        public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
            response.send("BORAT!");
        }
    });
    server.get("/foo/poo", new HttpServerRequestCallback() {

        @Override
        public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
            response.redirect("../poo");
        }
    });
    server.get("/poo", new HttpServerRequestCallback() {

        @Override
        public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
            response.send("SWEET!");
        }
    });
    server.get("/foo/bar", new HttpServerRequestCallback() {

        @Override
        public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
            response.redirect("baz");
        }
    });
    server.get("/foo/baz", new HttpServerRequestCallback() {

        @Override
        public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
            response.send("SUCCESS!");
        }
    });
}
Also used : HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse)

Example 20 with HttpServerRequestCallback

use of com.koushikdutta.async.http.server.HttpServerRequestCallback in project AndroidAsync by koush.

the class CacheTests method testMaxAgePrivate.

public void testMaxAgePrivate() throws Exception {
    AsyncHttpClient client = new AsyncHttpClient(AsyncServer.getDefault());
    ResponseCacheMiddleware cache = ResponseCacheMiddleware.addCache(client, new File(getContext().getFilesDir(), "AndroidAsyncTest"), 1024 * 1024 * 10);
    AsyncHttpServer httpServer = new AsyncHttpServer();
    try {
        httpServer.get("/uname/(.*)", new HttpServerRequestCallback() {

            @Override
            public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
                response.getHeaders().set("Date", HttpDate.format(new Date()));
                response.getHeaders().set("Cache-Control", "private, max-age=10000");
                response.send(request.getMatcher().group(1));
            }
        });
        AsyncServerSocket socket = httpServer.listen(AsyncServer.getDefault(), 0);
        int port = socket.getLocalPort();
        // clear the old cache
        cache.clear();
        client.executeString(new AsyncHttpGet("http://localhost:" + port + "/uname/43434"), null).get();
        client.executeString(new AsyncHttpGet("http://localhost:" + port + "/uname/43434"), null).get();
        assertEquals(cache.getCacheHitCount(), 1);
        assertEquals(cache.getNetworkCount(), 1);
    } finally {
        AsyncServer.getDefault().stop();
        client.getMiddleware().remove(cache);
    }
}
Also used : AsyncServerSocket(com.koushikdutta.async.AsyncServerSocket) AsyncHttpGet(com.koushikdutta.async.http.AsyncHttpGet) HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) ResponseCacheMiddleware(com.koushikdutta.async.http.cache.ResponseCacheMiddleware) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) File(java.io.File) Date(java.util.Date) HttpDate(com.koushikdutta.async.http.HttpDate) AsyncHttpClient(com.koushikdutta.async.http.AsyncHttpClient)

Aggregations

AsyncHttpServerRequest (com.koushikdutta.async.http.server.AsyncHttpServerRequest)21 AsyncHttpServerResponse (com.koushikdutta.async.http.server.AsyncHttpServerResponse)21 HttpServerRequestCallback (com.koushikdutta.async.http.server.HttpServerRequestCallback)21 AsyncHttpServer (com.koushikdutta.async.http.server.AsyncHttpServer)20 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)6 MultipartFormDataBody (com.koushikdutta.async.http.body.MultipartFormDataBody)4 JsonObject (com.google.gson.JsonObject)3 AsyncHttpGet (com.koushikdutta.async.http.AsyncHttpGet)3 Part (com.koushikdutta.async.http.body.Part)3 AsyncServer (com.koushikdutta.async.AsyncServer)2 ByteBufferList (com.koushikdutta.async.ByteBufferList)2 UrlEncodedFormBody (com.koushikdutta.async.http.body.UrlEncodedFormBody)2 HeadersResponse (com.koushikdutta.ion.HeadersResponse)2 File (java.io.File)2 KeyStore (java.security.KeyStore)2 Semaphore (java.util.concurrent.Semaphore)2 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)2 SSLContext (javax.net.ssl.SSLContext)2 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)2 JsonParseException (com.google.gson.JsonParseException)1