Example usage for org.json.simple JSONObject isEmpty

List of usage examples for org.json.simple JSONObject isEmpty

Introduction

In this page you can find the example usage for org.json.simple JSONObject isEmpty.

Prototype

boolean isEmpty();

Source Link

Document

Returns true if this map contains no key-value mappings.

Usage

From source file:smops.dao.BusinessManager.java

public static void prepareTableReport(List<Business> bizs) {
    JSONArray all_json = new JSONArray();
    for (Business biz : bizs) {
        //            if (getValidFormsCount(biz) == 0) {
        //                continue;
        //            }
        //            System.out.println("");
        //            System.out.println("[" + biz.getName() + "] " + biz.getUrl());
        JSONObject biz_json = new JSONObject();
        biz_json.put("name", biz.getName());
        //            biz_json.put("url", biz.getUrl());
        JSONArray forms_json = new JSONArray();
        for (Object f : biz.getForms()) {
            Form form = (Form) f;//from w w w  . j  a  v a2s .  com
            //                if (!form.getPurpose().equals("unknown")) {
            //                    continue;
            //                }
            JSONObject f_json = new JSONObject();
            f_json.put("page_url", " " + form.getPageUrl() + " ");
            f_json.put("purpose", form.getPurpose());
            System.out.print(biz.getName() + "\t");
            System.out.print(form.getPageUrl() + "\t");
            System.out.print(form.getPurpose() + "\t");
            //                System.out.println("\t" + form.getPageUrl());
            //                System.out.print("\t[" + form.getPurpose() + "]: ");
            String fields_str = "";
            List<String> fields_infoType = new ArrayList<>();
            for (Object inp : form.getFields()) {
                Field field = (Field) inp;
                fields_infoType.add(field.getInfoType());
            }
            for (String inftype : Constants.infoType_keywords.keySet()) {
                if (fields_infoType.contains(inftype)) {
                    System.out.print("1\t");
                } else {
                    System.out.print(" \t");
                }

            }
            for (Object inp : form.getFields()) {
                Field field = (Field) inp;
                if (field.getInfoType().equals("unknown")) {
                    continue;
                }
                fields_str += field.getInfoType() + " ";
            }
            f_json.put("fields", fields_str);
            forms_json.add(f_json);
            //                System.out.println(fields_str);
            System.out.println();
        }
        if (!forms_json.isEmpty()) {
            biz_json.put("forms", forms_json);
        }
        if (!biz_json.isEmpty()) {
            all_json.add(biz_json);
        }
    }
    StringWriter out = new StringWriter();
    try {
        all_json.writeJSONString(out);
    } catch (IOException ex) {
        Logger.getLogger(BusinessManager.class.getName()).log(Level.SEVERE, null, ex);
    }
    String jsonText = out.toString();
    //        System.out.print(jsonText.replaceAll("/", ""));
}

From source file:uk.ac.susx.tag.method51.core.params.codec.generic.ParamsCodec.java

@Override
public JSONObject encode(Params value) throws EncodeException {
    JSONObject result = new JSONObject();

    for (String fieldName : value.getFieldNames()) {
        Params.Param p = value.getParamByName(fieldName);
        Maybe mv = p.getGiven();//from ww w . j av a2s  . com
        if (mv instanceof Just) {
            Object v = ((Just) mv).val;
            ICodec codec = p.getType();
            try {
                result.put(fieldName, codec.encode(v));
            } catch (EncodeException e) {
                LOG.error("", e);
                throw new EncodeException("Bad value for field: '" + fieldName + "'", e);
            } catch (Exception e) {
                LOG.error("", e);
                throw new EncodeException("Unknown error encoding field '" + fieldName + "' with value: " + v,
                        e);
            }
        }
    }

    JSONObject encodedGlobals = new JSONObject();

    Map<String, Params> globals = value.getGlobals();

    for (String key : globals.keySet()) {
        Params global = globals.get(key);
        ParamsCodec codec = new ParamsCodec(global.getClass());
        JSONObject encoded = codec.encode(global);
        encodedGlobals.put(key, encoded);
    }

    if (!encodedGlobals.isEmpty()) {
        result.put(ParamsRegistry.GLOBALS_KEY, encodedGlobals);
    }

    return result;
}

From source file:webrtc.server.WebRTCServer.java

public static void main(String[] args) {

    wsServer = WebSocketServer.createServer();

    db = new Database("127.0.0.1:3306", "webcamtest-db", "admin", "admin");

    db.getTable("rooms").findAll(row -> {
        try {/*ww w  .  j  a  va 2 s .  com*/
            wsServer.newRoom((String) row.get(RoomTable.RID), WebSocketRoom.TYPES.PERMANENT);
        } catch (SQLException ex) {
            LOG.log(Level.SEVERE, null, ex);
        }
    });

    HTTPServer httpServer = (HTTPServer) HTTPServer.createServer();
    httpServer.setRootPath("/Users/gaelph/NetBeansProjects/WebRTC Server/www/");

    httpServer.on("GET", "/api/user/:uid", (request, response) -> {

        Table users = db.getTable("users");
        JSONObject user = new JSONObject();
        response.status = HTTPStatusCodes.NOT_FOUND;

        users.find(UserTable.UID, request.params.get("uid"), row -> {
            try {
                user.put("sid", (String) row.get(UserTable.SID));
                user.put("firstname", (String) row.get(UserTable.FIRST_NAME));
                user.put("lastname", (String) row.get(UserTable.LAST_NAME));

                response.status = HTTPStatusCodes.OK;
                response.setBody(user.toJSONString());

            } catch (SQLException ex) {
                LOG.log(Level.SEVERE, null, ex);
            }
        });
        try {
            response.send();
        } catch (Exception ex) {
            LOG.log(Level.SEVERE, null, ex);
        }
    });

    httpServer.on("POST", "/api/user/:uid", (request, response) -> {
        UserTable users = UserTable.newInstance();

        users.find(UserTable.UID, request.formContent.get("UID"), row -> {
            try {
                row.set(UserTable.EMAIL, request.formContent.get("email"));
                row.set(UserTable.PASSWORD, request.formContent.get("password"));
                row.set(UserTable.FIRST_NAME, request.formContent.get("firstName"));
                row.set(UserTable.LAST_NAME, request.formContent.get("lastName"));

                row.commit();

                JSONObject jsonUser = new JSONObject();
                jsonUser.put("UID", request.formContent.get("UID"));
                jsonUser.put("firstname", request.formContent.get("firstName"));
                jsonUser.put("lastname", request.formContent.get("lastName"));
                response.setBody(jsonUser.toJSONString());

                response.status = HTTPStatusCodes.OK;
                response.send();
            } catch (SQLException ex) {
                LOG.log(Level.SEVERE, null, ex);
                response.status = HTTPStatusCodes.INTERNAL_SERVER_ERROR;
                try {
                    response.send();
                } catch (Exception ex1) {
                    LOG.log(Level.SEVERE, null, ex1);
                }
            } catch (Exception ex) {
                LOG.log(Level.SEVERE, null, ex);
            }
        });
    });

    httpServer.on("DELETE", "/api/user/:uid", (request, response) -> {
        UserTable users = UserTable.newInstance();

        users.delete(UserTable.UID, request.params.get("UID"));

        try {
            response.send();
        } catch (Exception ex) {
            LOG.log(Level.SEVERE, null, ex);
        }
    });

    httpServer.on("GET", "/api/rooms", (request, response) -> {
        JSONArray jRooms = new JSONArray();

        wsServer.getRooms().stream().forEach((room) -> {
            jRooms.add(room.toJSONString());
        });

        response.status = HTTPStatusCodes.OK;
        response.setBody(jRooms.toJSONString());

        try {
            response.send();
        } catch (Exception ex) {
            LOG.log(Level.SEVERE, null, ex);
        }
    });

    httpServer.on("GET", "/api/room/:rid/users", (request, response) -> {

        wsServer.getRooms().stream().filter((room) -> {
            return room.rid.equals(request.params.get("rid"));
        }).findFirst().ifPresent((room) -> {
            JSONArray users = new JSONArray();

            room.participants.forEach((participant) -> {
                JSONObject jP = new JSONObject();

                db.getTable("users").find(UserTable.SID, participant.sid, row -> {
                    try {
                        jP.put("firstname", row.get("first_name"));
                        jP.put("sid", row.get("SID"));
                    } catch (SQLException ex) {
                        LOG.log(Level.SEVERE, null, ex);
                    }
                });

                users.add(jP.toJSONString());
            });

            response.status = HTTPStatusCodes.OK;
            response.setContentType(HTTPMIMETypes.get(HTTPMIMETypes.TYPES.JSON) + "; charset=utf-8");
            response.setBody(users.toJSONString());
        });

        try {
            response.send();
        } catch (Exception ex) {
            LOG.log(Level.SEVERE, null, ex);
        }
    });

    httpServer.on("POST", "/api/login", (request, response) -> {
        UserTable userTable = UserTable.newInstance();

        Object[] params;

        String contentType = request.header.get("content-type").split(";")[0].trim();

        switch (contentType) {
        case "application/json":
            params = Arrays.asList(request.jsonContent.get("email"), request.jsonContent.get("password"))
                    .toArray();
            break;

        default:
            response.status = 415; {
            try {
                response.send();
            } catch (Exception ex) {
                LOG.log(Level.SEVERE, null, ex);
            }
        }
            return;
        }

        response.status = HTTPStatusCodes.UNAUTHORIZED;
        response.setBody("No user with such email or password");

        JSONObject message = new JSONObject();
        JSONObject user = new JSONObject();

        userTable.select(null, "`email`=? and `password`=?", params, null, false, row -> {
            try {
                user.put("UID", row.get(UserTable.UID));
                user.put("SID", row.get(UserTable.SID)); //Is this relevant?
                user.put("firstname", row.get(UserTable.FIRST_NAME));
                user.put("lastname", row.get(UserTable.LAST_NAME));
            } catch (SQLException ex) {
                LOG.log(Level.SEVERE, null, ex);
            }
        });

        if (!user.isEmpty()) {
            message.put("user", user);
            response.status = HTTPStatusCodes.OK;
            response.setBody(message.toJSONString());

            HTTPCookie cookie = new HTTPCookie("sid", (String) user.get("SID"), "127.0.0.1", "/");
            response.cookies.add(cookie);
            cookie = new HTTPCookie("sid", (String) user.get("SID"), "localhost", "/");
            response.cookies.add(cookie);
        }

        try {
            response.send();
        } catch (Exception ex) {
            LOG.log(Level.SEVERE, null, ex);
        }

    });

    httpServer.on("POST", "api/user/create", (request, response) -> {
        UserTable users = UserTable.newInstance();

        if (users.find(UserTable.EMAIL, request.formContent.get("email")).isPresent()) {
            response.status = HTTPStatusCodes.BAD_REQUEST;
            response.setBody(
                    "User with email already exists (" + (String) request.formContent.get("email") + ")");
            try {
                response.send();
            } catch (Exception ex) {
                LOG.log(Level.SEVERE, null, ex);
            }
            return;
        }

        String UID = Utils.makeRandomString(24);
        String apiToken = Utils.makeRandomString(32);

        try {
            users.newRow(row -> {
                try {
                    row.set(UserTable.UID, UID);
                    row.set(UserTable.API_TOKEN, apiToken);
                    row.set(UserTable.EMAIL, request.formContent.get("email"));
                    row.set(UserTable.PASSWORD, request.formContent.get("password"));
                    row.set(UserTable.FIRST_NAME, request.formContent.get("firstName"));
                    row.set(UserTable.LAST_NAME, request.formContent.get("lastName"));
                    row.set(UserTable.STATUS, 0);

                    row.commit();
                } catch (SQLException ex) {
                    LOG.log(Level.SEVERE, null, ex);

                    response.status = HTTPStatusCodes.INTERNAL_SERVER_ERROR;
                    try {
                        response.send();
                    } catch (Exception ex1) {
                        LOG.log(Level.SEVERE, null, ex1);
                    }
                }
            });
        } catch (SQLException ex) {
            LOG.log(Level.SEVERE, null, ex);

            response.status = HTTPStatusCodes.INTERNAL_SERVER_ERROR;
            try {
                response.send();
            } catch (Exception ex1) {
                LOG.log(Level.SEVERE, null, ex1);
            }

            return;
        }

        users.find(UserTable.UID, UID, row -> {
            try {
                JSONObject user = new JSONObject();

                user.put("UID", row.get(UserTable.UID));
                user.put("api_token", apiToken);
                user.put("SID", row.get(UserTable.SID)); //Is this relevant?
                user.put("firstname", row.get(UserTable.FIRST_NAME));
                user.put("lastname", row.get(UserTable.LAST_NAME));

                response.status = HTTPStatusCodes.CREATED;
                response.setBody(user.toJSONString());

                response.send();

            } catch (SQLException ex) {
                LOG.log(Level.SEVERE, null, ex);
                response.status = HTTPStatusCodes.INTERNAL_SERVER_ERROR;
                try {
                    response.send();
                } catch (Exception ex1) {
                    LOG.log(Level.SEVERE, null, ex1);
                }
            } catch (Exception ex) {
                LOG.log(Level.SEVERE, null, ex);
            }
        });
    });

    httpServer.listen(HTTP_PORT);

    wsServer.on("connect", (WebSocketConnection connection) -> {
        if (connection.request.cookies.stream().anyMatch(cookie -> cookie.name.equals("token"))) {
            String UID = connection.request.cookies.stream().filter(cookie -> cookie.name.equals("token"))
                    .findFirst().get().value;

            if (!db.getTable("users").find(UserTable.UID, UID).isPresent()) {
                try {
                    connection.close();
                } catch (IOException ex) {
                    LOG.log(Level.SEVERE, null, ex);
                }
                return;
            }

            db.getTable("users").find(UserTable.UID, UID, row -> {
                try {
                    row.set(UserTable.SID, connection.sid);
                    row.commit();
                    LOG.log(Level.INFO, "User {0} connected",
                            row.get(UserTable.UID) + ", " + row.get(UserTable.FIRST_NAME));
                } catch (SQLException ex) {
                    LOG.log(Level.SEVERE, null, ex);
                }

                connection.uid = UID;

            });

        }

        PeerConnection pConnection = new PeerConnection();

        pConnection.on("icecandidate", object -> connection.emit("icecandidate", (String) object));

        connection.on("joined", rid -> {
            connection.getJoinedRooms().forEach(room -> {
                JSONObject msg = new JSONObject();
                msg.put("sid", connection.sid);

                db.getTable("users").find(UserTable.SID, connection.sid, row -> {
                    try {
                        msg.put("firstname", row.get(UserTable.FIRST_NAME));

                        LOG.log(Level.INFO, "User {0}", row.get(UserTable.SID) + " joined room " + rid);
                    } catch (SQLException ex) {
                        LOG.log(Level.SEVERE, null, ex);
                    }
                });

                room.emit("user_joined", msg.toJSONString());
            });
        });

        connection.on("left", rid -> {
            connection.getJoinedRooms().forEach(room -> {
                JSONObject msg = new JSONObject();
                msg.put("sid", connection.sid);

                LOG.log(Level.INFO, "User {0}", connection.sid + " left room " + rid);

                room.emit("user_left", msg.toJSONString());
            });
        });

        connection.on("disconnect", data -> {
            wsServer.getRooms().stream().filter(room -> room.participants.contains(connection))
                    .forEach(room -> room.leave(connection));

            db.getTable("users").find(UserTable.SID, connection.sid, row -> {
                try {
                    LOG.log(Level.INFO, "User {0} disconnected",
                            row.get(UserTable.UID) + ", " + row.get(UserTable.FIRST_NAME));
                } catch (SQLException ex) {
                    LOG.log(Level.SEVERE, null, ex);
                }
            });

        });

        connection.on("message", data -> {

        });

        connection.on("offer", data -> {
            try {
                JSONParser parser = new JSONParser();
                String string = (String) parser.parse(data);
                JSONObject offer = (JSONObject) parser.parse(string);

                JSONObject body = (JSONObject) ((JSONObject) offer).get("body");

                pConnection.setRemoteDescription(body);

                body = pConnection.createAnswer();
                body.put("sid", connection.sid);

                connection.emit("answer", body.toJSONString());

                pConnection.setLocalDescription(body);
            } catch (ParseException ex) {
                LOG.log(Level.SEVERE, null, ex);
            } catch (Exception ex) {
                Logger.getLogger(WebRTCServer.class.getName()).log(Level.SEVERE, null, ex);
            }
        });

        connection.on("answer", data -> {
            try {
                JSONParser parser = new JSONParser();
                String string = (String) parser.parse(data);
                JSONObject offer = (JSONObject) parser.parse(string);

                JSONObject body = (JSONObject) ((JSONObject) offer).get("body");

                pConnection.setRemoteDescription(body);
            } catch (ParseException ex) {
                LOG.log(Level.SEVERE, null, ex);
            }
        });

        connection.on("icecandidate", (String data) -> {
            try {
                JSONParser parser = new JSONParser();
                JSONObject message = (JSONObject) parser.parse((String) parser.parse(data));
                JSONObject candidateDict = (JSONObject) message.get("body");
                pConnection.addCandidate(candidateDict);
            } catch (ParseException ex) {
                LOG.log(Level.SEVERE, null, ex);
            }
        });

        connection.on("hangup", data -> {
            try {
                JSONObject offer = (JSONObject) new JSONParser().parse(data);
            } catch (ParseException ex) {
                LOG.log(Level.SEVERE, null, ex);
            }
        });
    });

    wsServer.listen(WS_PORT);

}