Search in sources :

Example 1 with AsyncHttpServerRequest

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

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

the class IssueWithWebSocketFuturesTests method testWebSocketFutureWithHandshakeFailureCallback.

//testing that websocket callback gets called with the correct parameters.
public void testWebSocketFutureWithHandshakeFailureCallback() throws Exception {
    //creating a faulty server!
    AsyncHttpServer httpServer = new AsyncHttpServer();
    httpServer.websocket(".*", new AsyncHttpServer.WebSocketRequestCallback() {

        @Override
        public void onConnected(WebSocket webSocket, AsyncHttpServerRequest request) {
        }
    });
    httpServer.listen(6666);
    final Exception[] callbackException = { null };
    final WebSocket[] callbackWs = { null };
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    //for some reason, it fails with a WebSocketHandshakeException.
    //But in general, if the handshake fails, the callback must be called with an exception.
    Future<WebSocket> wsFuture = AsyncHttpClient.getDefaultInstance().websocket("ws://127.0.0.1:6666", "ws", new AsyncHttpClient.WebSocketConnectCallback() {

        @Override
        public void onCompleted(Exception ex, WebSocket webSocket) {
            callbackException[0] = ex;
            callbackWs[0] = webSocket;
            countDownLatch.countDown();
        }
    });
    //wait for the future to complete
    countDownLatch.await();
    //exactly one mut be null
    assertTrue(callbackWs[0] == null ^ callbackException[0] == null);
    //callback parameters must be the same as the future's result
    assertEquals(wsFuture.tryGet(), callbackWs[0]);
    assertEquals(wsFuture.tryGetException(), callbackException[0]);
}
Also used : AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) CountDownLatch(java.util.concurrent.CountDownLatch) WebSocket(com.koushikdutta.async.http.WebSocket) AsyncHttpClient(com.koushikdutta.async.http.AsyncHttpClient)

Example 3 with AsyncHttpServerRequest

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

the class SSLTests method testKeys.

public void testKeys() throws Exception {
    KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    ks.load(getContext().getResources().openRawResource(R.raw.keystore), "storepass".toCharArray());
    kmf.init(ks, "storepass".toCharArray());
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    KeyStore ts = KeyStore.getInstance(KeyStore.getDefaultType());
    ts.load(getContext().getResources().openRawResource(R.raw.keystore), "storepass".toCharArray());
    tmf.init(ts);
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
    AsyncHttpServer httpServer = new AsyncHttpServer();
    httpServer.listenSecure(8888, sslContext);
    httpServer.get("/", new HttpServerRequestCallback() {

        @Override
        public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
            response.send("hello");
        }
    });
    Thread.sleep(1000);
    AsyncHttpClient.getDefaultInstance().getSSLSocketMiddleware().setSSLContext(sslContext);
    AsyncHttpClient.getDefaultInstance().getSSLSocketMiddleware().setTrustManagers(tmf.getTrustManagers());
    AsyncHttpClient.getDefaultInstance().executeString(new AsyncHttpGet("https://localhost:8888/"), null).get();
}
Also used : AsyncHttpGet(com.koushikdutta.async.http.AsyncHttpGet) HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 4 with AsyncHttpServerRequest

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

the class GsonTests method testJunkPayload.

public void testJunkPayload() throws Exception {
    AsyncHttpServer httpServer = new AsyncHttpServer();
    try {
        httpServer.get("/", new HttpServerRequestCallback() {

            @Override
            public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
                response.send("not json!");
            }
        });
        httpServer.listen(5555);
        Future<JsonObject> ret = Ion.with(getContext()).load("PUT", "http://localhost:5555/").asJsonObject();
        ret.get();
        fail();
    } catch (ExecutionException e) {
        assertTrue(e.getCause() instanceof JsonParseException);
    } finally {
        httpServer.stop();
        AsyncServer.getDefault().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) JsonObject(com.google.gson.JsonObject) ExecutionException(java.util.concurrent.ExecutionException) JsonParseException(com.google.gson.JsonParseException)

Example 5 with AsyncHttpServerRequest

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

the class HeadersTests method testHeadersCallback.

public void testHeadersCallback() throws Exception {
    AsyncHttpServer httpServer = new AsyncHttpServer();
    try {
        httpServer.get("/", new HttpServerRequestCallback() {

            @Override
            public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
                response.send("hello");
            }
        });
        httpServer.listen(Ion.getDefault(getContext()).getServer(), 5555);
        final Semaphore semaphore = new Semaphore(0);
        Ion.with(getContext()).load("http://localhost:5555/").asString().withResponse().setCallback(new FutureCallback<Response<String>>() {

            @Override
            public void onCompleted(Exception e, Response<String> result) {
                assertEquals(result.getHeaders().code(), 200);
                semaphore.release();
            }
        });
        assertTrue(semaphore.tryAcquire(10000, TimeUnit.MILLISECONDS));
    } finally {
        httpServer.stop();
        Ion.getDefault(getContext()).getServer().stop();
    }
}
Also used : HeadersResponse(com.koushikdutta.ion.HeadersResponse) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) Response(com.koushikdutta.ion.Response) 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) Semaphore(java.util.concurrent.Semaphore)

Aggregations

AsyncHttpServerRequest (com.koushikdutta.async.http.server.AsyncHttpServerRequest)25 AsyncHttpServerResponse (com.koushikdutta.async.http.server.AsyncHttpServerResponse)23 AsyncHttpServer (com.koushikdutta.async.http.server.AsyncHttpServer)22 HttpServerRequestCallback (com.koushikdutta.async.http.server.HttpServerRequestCallback)21 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)7 AsyncServer (com.koushikdutta.async.AsyncServer)4 AsyncHttpGet (com.koushikdutta.async.http.AsyncHttpGet)4 MultipartFormDataBody (com.koushikdutta.async.http.body.MultipartFormDataBody)4 JsonObject (com.google.gson.JsonObject)3 AsyncServerSocket (com.koushikdutta.async.AsyncServerSocket)3 Part (com.koushikdutta.async.http.body.Part)3 ByteBufferList (com.koushikdutta.async.ByteBufferList)2 AsyncHttpClient (com.koushikdutta.async.http.AsyncHttpClient)2 WebSocket (com.koushikdutta.async.http.WebSocket)2 UrlEncodedFormBody (com.koushikdutta.async.http.body.UrlEncodedFormBody)2 AsyncProxyServer (com.koushikdutta.async.http.server.AsyncProxyServer)2 HeadersResponse (com.koushikdutta.ion.HeadersResponse)2 File (java.io.File)2 KeyStore (java.security.KeyStore)2 Semaphore (java.util.concurrent.Semaphore)2