Search in sources :

Example 46 with JsonObject

use of com.google.gson.JsonObject 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 47 with JsonObject

use of com.google.gson.JsonObject in project ion by koush.

the class HeadersTests method testBustedJson.

public void testBustedJson() 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);
        Response<JsonObject> response = Ion.with(getContext()).load("http://localhost:5555/").asJsonObject().withResponse().get();
        assertNull(response.getResult());
        assertNotNull(response.getException());
    } finally {
        httpServer.stop();
        Ion.getDefault(getContext()).getServer().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)

Example 48 with JsonObject

use of com.google.gson.JsonObject in project ion by koush.

the class HttpTests method testGson.

public void testGson() throws Exception {
    JsonObject dummy1 = new JsonObject();
    dummy1.addProperty("foo", "bar");
    JsonObject dummy2 = new JsonObject();
    dummy2.addProperty("pong", "ping");
    JsonArray array = new JsonArray();
    array.add(dummy1);
    array.add(dummy2);
    final Semaphore semaphore = new Semaphore(0);
    Ion.with(getContext()).load("https://koush.clockworkmod.com/test/echo").setHandler(null).setJsonArrayBody(array).as(new TypeToken<List<Dummy>>() {
    }).setCallback(new FutureCallback<List<Dummy>>() {

        @Override
        public void onCompleted(Exception e, List<Dummy> result) {
            assertEquals("bar", result.get(0).foo);
            semaphore.release();
        }
    });
    assertTrue(semaphore.tryAcquire(50000, TimeUnit.MILLISECONDS));
}
Also used : JsonArray(com.google.gson.JsonArray) TypeToken(com.google.gson.reflect.TypeToken) JsonObject(com.google.gson.JsonObject) List(java.util.List) Semaphore(java.util.concurrent.Semaphore)

Example 49 with JsonObject

use of com.google.gson.JsonObject in project ion by koush.

the class HttpTests method testUrlEncodedFormBodyWithNull.

public void testUrlEncodedFormBodyWithNull() throws Exception {
    JsonObject ret = Ion.with(getContext()).load("https://koush.clockworkmod.com/test/echo").setTimeout(3000000).setBodyParameter("blit", null).setBodyParameter("foo", "bar").asJsonObject().get();
    assertTrue(!ret.has("blit"));
    assertEquals("bar", ret.get("foo").getAsString());
}
Also used : JsonObject(com.google.gson.JsonObject)

Example 50 with JsonObject

use of com.google.gson.JsonObject in project bigbluebutton by bigbluebutton.

the class WhiteboardMessageReceiver method handleMessage.

@Override
public void handleMessage(String pattern, String channel, String message) {
    if (channel.equalsIgnoreCase(MessagingConstants.TO_WHITEBOARD_CHANNEL)) {
        JsonParser parser = new JsonParser();
        JsonObject obj = (JsonObject) parser.parse(message);
        if (obj.has("header") && obj.has("payload")) {
            JsonObject header = (JsonObject) obj.get("header");
            if (header.has("name")) {
                String messageName = header.get("name").getAsString();
                if (UndoWhiteboardRequest.UNDO_WHITEBOARD_REQUEST.equals(messageName)) {
                    UndoWhiteboardRequest msg = UndoWhiteboardRequest.fromJson(message);
                    bbbInGW.undoWhiteboard(msg.meetingId, msg.requesterId, msg.whiteboardId);
                } else if (ClearWhiteboardRequestMessage.CLEAR_WHITEBOARD_REQUEST.equals(messageName)) {
                    ClearWhiteboardRequestMessage msg = ClearWhiteboardRequestMessage.fromJson(message);
                    bbbInGW.clearWhiteboard(msg.meetingId, msg.requesterId, msg.whiteboardId);
                } else if (RequestWhiteboardAnnotationHistoryRequestMessage.REQUEST_WHITEBOARD_ANNOTATION_HISTORY_REQUEST.equals(messageName)) {
                    RequestWhiteboardAnnotationHistoryRequestMessage msg = RequestWhiteboardAnnotationHistoryRequestMessage.fromJson(message);
                    bbbInGW.requestWhiteboardAnnotationHistory(msg.meetingId, msg.requesterId, msg.whiteboardId, msg.replyTo);
                } else if (IsWhiteboardEnabledRequestMessage.IS_WHITEBOARD_ENABLED_REQUEST.equals(messageName)) {
                    IsWhiteboardEnabledRequestMessage msg = IsWhiteboardEnabledRequestMessage.fromJson(message);
                    bbbInGW.isWhiteboardEnabled(msg.meetingId, msg.requesterId, msg.replyTo);
                } else if (EnableWhiteboardRequestMessage.ENABLE_WHITEBOARD_REQUEST.equals(messageName)) {
                    EnableWhiteboardRequestMessage msg = EnableWhiteboardRequestMessage.fromJson(message);
                    bbbInGW.enableWhiteboard(msg.meetingId, msg.requesterId, msg.enable);
                } else if (SendWhiteboardAnnotationRequestMessage.SEND_WHITEBOARD_ANNOTATION_REQUEST.equals(messageName)) {
                    SendWhiteboardAnnotationRequestMessage msg = SendWhiteboardAnnotationRequestMessage.fromJson(message);
                    bbbInGW.sendWhiteboardAnnotation(msg.meetingId, msg.requesterId, msg.annotation);
                }
            }
        }
    }
}
Also used : EnableWhiteboardRequestMessage(org.bigbluebutton.common.messages.EnableWhiteboardRequestMessage) RequestWhiteboardAnnotationHistoryRequestMessage(org.bigbluebutton.common.messages.RequestWhiteboardAnnotationHistoryRequestMessage) SendWhiteboardAnnotationRequestMessage(org.bigbluebutton.common.messages.SendWhiteboardAnnotationRequestMessage) JsonObject(com.google.gson.JsonObject) UndoWhiteboardRequest(org.bigbluebutton.common.messages.UndoWhiteboardRequest) ClearWhiteboardRequestMessage(org.bigbluebutton.common.messages.ClearWhiteboardRequestMessage) IsWhiteboardEnabledRequestMessage(org.bigbluebutton.common.messages.IsWhiteboardEnabledRequestMessage) JsonParser(com.google.gson.JsonParser)

Aggregations

JsonObject (com.google.gson.JsonObject)1417 JsonElement (com.google.gson.JsonElement)389 JsonArray (com.google.gson.JsonArray)293 JsonParser (com.google.gson.JsonParser)285 JsonPrimitive (com.google.gson.JsonPrimitive)137 Gson (com.google.gson.Gson)91 Test (org.junit.Test)81 HashMap (java.util.HashMap)79 Map (java.util.Map)78 ArrayList (java.util.ArrayList)77 IOException (java.io.IOException)66 Test (org.testng.annotations.Test)61 InputStreamReader (java.io.InputStreamReader)38 JsonParseException (com.google.gson.JsonParseException)27 File (java.io.File)25 List (java.util.List)21 HttpResponse (org.apache.http.HttpResponse)21 JsonReader (com.google.gson.stream.JsonReader)19 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)19 InputStream (java.io.InputStream)19