Example usage for com.fasterxml.jackson.databind JsonNode toString

List of usage examples for com.fasterxml.jackson.databind JsonNode toString

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind JsonNode toString.

Prototype

public abstract String toString();

Source Link

Usage

From source file:controllers.AnyplaceMapping.java

/**
 * Retrieve all the pois of a building/floor combination.
 *
 * @return//from   w  w w.ja  v  a 2s.  com
 */
public static Result poisByFloor() {
    OAuth2Request anyReq = new OAuth2Request(request(), response());
    if (!anyReq.assertJsonBody()) {
        return AnyResponseHelper.bad_request(AnyResponseHelper.CANNOT_PARSE_BODY_AS_JSON);
    }
    JsonNode json = anyReq.getJsonBody();
    LPLogger.info("AnyplaceMapping::poisByFloor(): " + json.toString());

    List<String> requiredMissing = JsonUtils.requirePropertiesInJson(json, "buid", "floor_number");
    if (!requiredMissing.isEmpty()) {
        return AnyResponseHelper.requiredFieldsMissing(requiredMissing);
    }

    String buid = json.findPath("buid").textValue();
    String floor_number = json.findPath("floor_number").textValue();
    try {
        List<JsonNode> pois = ProxyDataSource.getIDatasource().poisByBuildingFloorAsJson(buid, floor_number);
        ObjectNode res = JsonUtils.createObjectNode();
        res.put("pois", JsonUtils.getJsonFromList(pois));
        try {
            //                if (request().getHeader("Accept-Encoding") != null && request().getHeader("Accept-Encoding").contains("gzip")) {
            return gzippedJSONOk(res.toString());
            //                }
            //                return AnyResponseHelper.ok(res.toString());
        } catch (IOException ioe) {
            return AnyResponseHelper.ok(res,
                    "Successfully retrieved all pois from floor " + floor_number + "!");
        }
    } catch (DatasourceException e) {
        return AnyResponseHelper.internal_server_error("Server Internal Error [" + e.getMessage() + "]");
    }
}

From source file:controllers.AnyplaceMapping.java

/**
 * Retrieve all the pois of a building/floor combination.
 *
 * @return/*from   ww  w  . j av a  2s.c o  m*/
 */
public static Result connectionsByFloor() {
    OAuth2Request anyReq = new OAuth2Request(request(), response());
    if (!anyReq.assertJsonBody()) {
        return AnyResponseHelper.bad_request(AnyResponseHelper.CANNOT_PARSE_BODY_AS_JSON);
    }
    JsonNode json = anyReq.getJsonBody();
    LPLogger.info("AnyplaceMapping::poisByFloor(): " + json.toString());

    List<String> requiredMissing = JsonUtils.requirePropertiesInJson(json, "buid", "floor_number");
    if (!requiredMissing.isEmpty()) {
        return AnyResponseHelper.requiredFieldsMissing(requiredMissing);
    }

    String buid = json.findPath("buid").textValue();
    String floor_number = json.findPath("floor_number").textValue();
    try {
        List<JsonNode> pois = ProxyDataSource.getIDatasource().connectionsByBuildingFloorAsJson(buid,
                floor_number);
        ObjectNode res = JsonUtils.createObjectNode();
        res.put("connections", JsonUtils.getJsonFromList(pois));
        try {
            //                if (request().getHeader("Accept-Encoding") != null && request().getHeader("Accept-Encoding").contains("gzip")) {
            return gzippedJSONOk(res.toString());
            //                }
            //                return AnyResponseHelper.ok(res.toString());
        } catch (IOException ioe) {
            return AnyResponseHelper.ok(res,
                    "Successfully retrieved all pois from floor " + floor_number + "!");
        }
    } catch (DatasourceException e) {
        return AnyResponseHelper.internal_server_error("Server Internal Error [" + e.getMessage() + "]");
    }
}

From source file:controllers.AnyplaceMapping.java

/**
 * Returns the floor plan tiles in a .zip file for the requested building floor.
 * Used by the Android client.//from  ww w.  j  ava2 s  . co  m
 *
 * @param buid         Building id
 * @param floor_number floor number inside the above building
 * @return the file or an error
 */
public static Result serveFloorPlanTilesZip(String buid, String floor_number) {
    OAuth2Request anyReq = new OAuth2Request(request(), response());
    if (!anyReq.assertJsonBody()) {
        return AnyResponseHelper.bad_request(AnyResponseHelper.CANNOT_PARSE_BODY_AS_JSON);
    }
    JsonNode json = anyReq.getJsonBody();
    LPLogger.info("AnyplaceMapping::serveFloorPlanTilesZip(): " + json.toString());

    if (!Floor.checkFloorNumberFormat(floor_number)) {
        return AnyResponseHelper.bad_request("Floor number cannot contain whitespace!");
    }

    String filePath = AnyPlaceTilerHelper.getFloorTilesZipFor(buid, floor_number);
    LPLogger.info("requested: " + filePath);

    try {
        File file = new java.io.File(filePath);
        if (!file.exists() || !file.canRead()) {
            return AnyResponseHelper.bad_request(
                    "Requested floor plan does not exist or cannot be read! (" + floor_number + ")");
        }
        InputStream a = new FileInputStream(file);
        return ok(a);
    } catch (FileNotFoundException e) {
        // cannot get in here i think cause of the check if canRead()
        return AnyResponseHelper.internal_server_error("Could not read floor plan.");
    }
}

From source file:controllers.AnyplaceMapping.java

/**
 * Retrieve all the buildings that are inside the bounding box of the specified coordinates.
 *
 * @return/*from ww w  .ja  va 2  s  . c o  m*/
 */
public static Result buildingCoordinates() {
    OAuth2Request anyReq = new OAuth2Request(request(), response());
    if (!anyReq.assertJsonBody()) {
        return AnyResponseHelper.bad_request(AnyResponseHelper.CANNOT_PARSE_BODY_AS_JSON);
    }
    JsonNode json = anyReq.getJsonBody();
    LPLogger.info("AnyplaceMapping::buildingCoordinates(): " + json.toString());

    List<String> requiredMissing = JsonUtils.requirePropertiesInJson(json, "coordinates_lat",
            "coordinates_lon");
    if (!requiredMissing.isEmpty()) {
        return AnyResponseHelper.requiredFieldsMissing(requiredMissing);
    }

    try {
        List<JsonNode> buildings = ProxyDataSource.getIDatasource().getAllBuildingsNearMe(
                Double.parseDouble(json.path("coordinates_lat").textValue()),
                Double.parseDouble(json.path("coordinates_lon").textValue()));
        ObjectNode res = JsonUtils.createObjectNode();
        res.put("buildings", JsonUtils.getJsonFromList(buildings));
        try {
            //                if (request().getHeader("Accept-Encoding") != null && request().getHeader("Accept-Encoding").contains("gzip")) {
            return gzippedJSONOk(res.toString());
            //                }
            //                return AnyResponseHelper.ok(res.toString());
        } catch (IOException ioe) {
            return AnyResponseHelper.ok(res, "Successfully retrieved all buildings near your position!");
        }
    } catch (DatasourceException e) {
        return AnyResponseHelper.internal_server_error("Server Internal Error [" + e.getMessage() + "]");
    }
}

From source file:controllers.AnyplaceMapping.java

public static Result getRadioHeatmapByBuildingFloor() {
    OAuth2Request anyReq = new OAuth2Request(request(), response());
    if (!anyReq.assertJsonBody()) {
        return AnyResponseHelper.bad_request(AnyResponseHelper.CANNOT_PARSE_BODY_AS_JSON);
    }//from   w ww . j ava2s.  c  o  m

    JsonNode json = anyReq.getJsonBody();
    LPLogger.info("AnyplaceMapping::getRadioHeatmap(): " + json.toString());

    List<String> requiredMissing = JsonUtils.requirePropertiesInJson(json, "buid", "floor");

    if (!requiredMissing.isEmpty()) {
        return AnyResponseHelper.requiredFieldsMissing(requiredMissing);
    }

    String buid = json.path("buid").textValue();
    String floor = json.path("floor").textValue();

    try {
        List<JsonNode> radioPoints = ProxyDataSource.getIDatasource().getRadioHeatmapByBuildingFloor(buid,
                floor);
        if (radioPoints == null) {
            return AnyResponseHelper.bad_request("Building does not exist or could not be retrieved!");
        }
        ObjectNode res = JsonUtils.createObjectNode();
        res.put("radioPoints", JsonUtils.getJsonFromList(radioPoints));
        try {
            //                if (request().getHeader("Accept-Encoding") != null && request().getHeader("Accept-Encoding").contains("gzip")) {
            return gzippedJSONOk(res.toString());
            //                }
            //                return AnyResponseHelper.ok(res.toString());
        } catch (IOException ioe) {
            return AnyResponseHelper.ok(res, "Successfully retrieved all radio points!");
        }
    } catch (DatasourceException e) {
        return AnyResponseHelper.internal_server_error("Server Internal Error [" + e.getMessage() + "]");
    }
}

From source file:controllers.AnyplaceMapping.java

/**
 * Returns the floor plan tiles .zip file LINK for the requested building floor.
 * Used by the Developers API.// w  w  w. j  av a2  s . c  om
 *
 * @param buid         Building id
 * @param floor_number floor number inside the above building
 * @return the file or an error
 */
public static Result serveFloorPlanTilesZipLink(String buid, String floor_number) {
    OAuth2Request anyReq = new OAuth2Request(request(), response());
    if (!anyReq.assertJsonBody()) {
        return AnyResponseHelper.bad_request(AnyResponseHelper.CANNOT_PARSE_BODY_AS_JSON);
    }
    JsonNode json = anyReq.getJsonBody();
    LPLogger.info("AnyplaceMapping::serveFloorPlanTilesZipLink(): " + json.toString());

    if (!Floor.checkFloorNumberFormat(floor_number)) {
        return AnyResponseHelper.bad_request("Floor number cannot contain whitespace!");
    }
    String filePath = AnyPlaceTilerHelper.getFloorTilesZipFor(buid, floor_number);

    LPLogger.info("requested: " + filePath);

    File file = new java.io.File(filePath);

    if (!file.exists() || !file.canRead()) {
        return AnyResponseHelper
                .bad_request("Requested floor plan does not exist or cannot be read! (" + floor_number + ")");
    }

    ObjectNode res = JsonUtils.createObjectNode();
    res.put("tiles_archive", AnyPlaceTilerHelper.getFloorTilesZipLinkFor(buid, floor_number));

    return AnyResponseHelper.ok(res, "Successfully fetched link for the tiles archive!");
}

From source file:controllers.AnyplaceMapping.java

public static Result buildingGetOne() {
    OAuth2Request anyReq = new OAuth2Request(request(), response());
    if (!anyReq.assertJsonBody()) {
        return AnyResponseHelper.bad_request(AnyResponseHelper.CANNOT_PARSE_BODY_AS_JSON);
    }/*from   w  w w  .  ja  v  a2  s. c  o m*/
    JsonNode json = anyReq.getJsonBody();
    LPLogger.info("AnyplaceMapping::buildingGet(): " + json.toString());

    List<String> requiredMissing = JsonUtils.requirePropertiesInJson(json, "buid");
    if (!requiredMissing.isEmpty()) {
        return AnyResponseHelper.requiredFieldsMissing(requiredMissing);
    }

    String buid = json.findPath("buid").textValue();

    try {
        JsonNode building = ProxyDataSource.getIDatasource().getFromKeyAsJson(buid);

        if (building != null && building.get("buid") != null && building.get("coordinates_lat") != null
                && building.get("coordinates_lon") != null && building.get("owner_id") != null
                && building.get("name") != null && building.get("description") != null
                && building.get("puid") == null && building.get("floor_number") == null) {

            ((ObjectNode) building).remove("owner_id");
            ((ObjectNode) building).remove("co_owners");

            ObjectNode res = JsonUtils.createObjectNode();
            res.put("building", building);
            try {
                //                    if (request().getHeader("Accept-Encoding") != null && request().getHeader("Accept-Encoding").contains("gzip")) {
                return gzippedJSONOk(res.toString());
                //                    }
                //                    return AnyResponseHelper.ok(res.toString());
            } catch (IOException ioe) {
                return AnyResponseHelper.ok(res, "Successfully retrieved all buildings!");
            }
        }

        return AnyResponseHelper.not_found("Building not found.");

    } catch (DatasourceException e) {
        return AnyResponseHelper.internal_server_error("Server Internal Error [" + e.getMessage() + "]");
    }
}

From source file:controllers.AnyplaceMapping.java

public static Result buildingAllByOwner() {
    OAuth2Request anyReq = new OAuth2Request(request(), response());
    if (!anyReq.assertJsonBody()) {
        return AnyResponseHelper.bad_request(AnyResponseHelper.CANNOT_PARSE_BODY_AS_JSON);
    }//from w  w w. java 2  s.  c  o m
    JsonNode json = anyReq.getJsonBody();
    LPLogger.info("AnyplaceMapping::buildingAll(): " + json.toString());

    List<String> requiredMissing = JsonUtils.requirePropertiesInJson(json, "access_token");
    if (!requiredMissing.isEmpty()) {
        return AnyResponseHelper.requiredFieldsMissing(requiredMissing);
    }

    // get access token from url and check it against google's service
    if (json.findValue("access_token") == null) {
        return AnyResponseHelper.forbidden("Unauthorized");
    }
    String owner_id = verifyOwnerId(json.findValue("access_token").textValue());
    if (owner_id == null) {
        return AnyResponseHelper.forbidden("Unauthorized");
    }
    owner_id = appendToOwnerId(owner_id);
    ((ObjectNode) json).put("owner_id", owner_id);

    if (owner_id == null || owner_id.isEmpty()) {
        return AnyResponseHelper.requiredFieldsMissing(requiredMissing);
    }

    try {
        List<JsonNode> buildings = ProxyDataSource.getIDatasource().getAllBuildingsByOwner(owner_id);
        ObjectNode res = JsonUtils.createObjectNode();
        res.put("buildings", JsonUtils.getJsonFromList(buildings));

        try {
            //                if (request().getHeader("Accept-Encoding") != null && request().getHeader("Accept-Encoding").contains("gzip")) {
            return gzippedJSONOk(res.toString());
            //                }
            //                return AnyResponseHelper.ok(res.toString());
        } catch (IOException ioe) {
            return AnyResponseHelper.ok(res, "Successfully retrieved all buildings!");
        }
    } catch (DatasourceException e) {
        return AnyResponseHelper.internal_server_error("Server Internal Error [" + e.getMessage() + "]");
    }
}

From source file:controllers.AnyplaceMapping.java

/**
 * Returns the floorplan in base64 form. Used by the Anyplace websites
 *
 * @param buid//from  w ww.j  a  v  a 2 s  .  c  om
 * @param floor_number
 * @return
 */
public static Result serveFloorPlanBase64(String buid, String floor_number) {
    OAuth2Request anyReq = new OAuth2Request(request(), response());
    if (!anyReq.assertJsonBody()) {
        return AnyResponseHelper.bad_request(AnyResponseHelper.CANNOT_PARSE_BODY_AS_JSON);
    }
    JsonNode json = anyReq.getJsonBody();
    LPLogger.info("AnyplaceMapping::serveFloorPlanBase64(): " + json.toString());

    String filePath = AnyPlaceTilerHelper.getFloorPlanFor(buid, floor_number);
    LPLogger.info("requested: " + filePath);

    File file = new java.io.File(filePath);
    try {
        if (!file.exists() || !file.canRead()) {
            return AnyResponseHelper.bad_request(
                    "Requested floor plan does not exist or cannot be read! (" + floor_number + ")");
        }
        try {
            String s = encodeFileToBase64Binary(filePath);
            try {
                //                    if (request().getHeader("Accept-Encoding") != null && request().getHeader("Accept-Encoding").contains("gzip")) {
                return gzippedOk(s);
                //                    }
                //                    return AnyResponseHelper.ok(s);
            } catch (IOException ioe) {
                return ok(s);
            }
        } catch (IOException e) {
            return AnyResponseHelper.bad_request(
                    "Requested floor plan cannot be encoded in base64 properly! (" + floor_number + ")");
        }
    } catch (Exception e) {
        // cannot get in here i think cause of the check if canRead()
        return AnyResponseHelper.internal_server_error("Unknown server error during floor plan delivery!");
    }
}

From source file:controllers.AnyplaceMapping.java

/**
 * Adds a new building to the database//from   w  w w  . j  a  v  a2 s  . co m
 *
 * @return the newly created Building ID is included in the response if success
 */
public static Result buildingAdd() {

    OAuth2Request anyReq = new OAuth2Request(request(), response());
    if (!anyReq.assertJsonBody()) {
        return AnyResponseHelper.bad_request(AnyResponseHelper.CANNOT_PARSE_BODY_AS_JSON);
    }

    JsonNode json = anyReq.getJsonBody();
    LPLogger.info("AnyplaceMapping::buildingAdd(): " + json.toString());

    List<String> requiredMissing = JsonUtils.requirePropertiesInJson(json, "is_published", "name",
            "description", "url", "address", "coordinates_lat", "coordinates_lon", "access_token");

    if (!requiredMissing.isEmpty()) {
        return AnyResponseHelper.requiredFieldsMissing(requiredMissing);
    }

    // get access token from url and check it against google's service
    if (json.findValue("access_token") == null) {
        return AnyResponseHelper.forbidden("Unauthorized");
    }
    String owner_id = verifyOwnerId(json.findValue("access_token").textValue());
    if (owner_id == null) {
        return AnyResponseHelper.forbidden("Unauthorized");
    }
    owner_id = appendToOwnerId(owner_id);
    ((ObjectNode) json).put("owner_id", owner_id);

    try {
        Building building;
        try {
            building = new Building(json);
        } catch (NumberFormatException e) {
            return AnyResponseHelper.bad_request("Building coordinates are invalid!");
        }
        //System.out.println(building.toValidCouchJson());
        if (!ProxyDataSource.getIDatasource().addJsonDocument(building.getId(), 0, building.toCouchGeoJSON())) {
            return AnyResponseHelper.bad_request("Building already exists or could not be added!");
        }
        ObjectNode res = JsonUtils.createObjectNode();
        res.put("buid", building.getId());
        return AnyResponseHelper.ok(res, "Successfully added building!");
    } catch (DatasourceException e) {
        return AnyResponseHelper.internal_server_error("Server Internal Error [" + e.getMessage() + "]");
    }
}