From 6260dc4e7993d1cca3526f43fb295d1671341302 Mon Sep 17 00:00:00 2001 From: Nikolaus Krismer <niko@krismer.de> Date: Thu, 20 Feb 2014 19:07:12 +0100 Subject: [PATCH] removed reference to json library.... everything related to json is now implemented with jackson --- build.gradle | 1 - .../it/unibz/inf/isochrone/network/Node.java | 5 ++ .../inf/isoga/datastructure/QueryPoint.java | 55 +++++++++++-------- .../java/it/unibz/inf/isoga/db/DbUtility.java | 11 +--- .../inf/isoga/service/ServiceIsochrone.java | 24 ++------ .../isoga/service/dto/RequestIsochrone.java | 8 ++- .../it/unibz/inf/isoga/web/JsonWebsocket.java | 27 ++++++--- 7 files changed, 66 insertions(+), 65 deletions(-) diff --git a/build.gradle b/build.gradle index 651d551f..8342a02f 100644 --- a/build.gradle +++ b/build.gradle @@ -80,7 +80,6 @@ dependencies { compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.3.0' compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.3.0' compile group: 'com.google.code.findbugs', name: 'annotations', version: '2.0.3' - compile group: 'org.json', name: 'json', version:'20131018' compile(group: 'org.postgis', name: 'postgis-jdbc', version:'1.3+') { exclude(module: 'postgis-stubs') } diff --git a/src/main/java/it/unibz/inf/isochrone/network/Node.java b/src/main/java/it/unibz/inf/isochrone/network/Node.java index 400c6fe5..c7ea6ce6 100644 --- a/src/main/java/it/unibz/inf/isochrone/network/Node.java +++ b/src/main/java/it/unibz/inf/isochrone/network/Node.java @@ -12,6 +12,7 @@ import java.util.List; * used to track how many adjacent links have already been explored. */ public class Node implements Comparable<Node> { + // TODO: Check how we deal with empty ids (shouldn't they be null? by now they are Integer.MIN_VALUE) private final int id; private double distance; private short nrAdjacentLinks; @@ -24,6 +25,10 @@ public class Node implements Comparable<Node> { // Constructors + protected Node() { + id = Integer.MIN_VALUE; + } + public Node(final int nodeId) { id = nodeId; distance = Double.POSITIVE_INFINITY; diff --git a/src/main/java/it/unibz/inf/isoga/datastructure/QueryPoint.java b/src/main/java/it/unibz/inf/isoga/datastructure/QueryPoint.java index 3d6c2181..5c5e8ad4 100644 --- a/src/main/java/it/unibz/inf/isoga/datastructure/QueryPoint.java +++ b/src/main/java/it/unibz/inf/isoga/datastructure/QueryPoint.java @@ -2,38 +2,27 @@ package it.unibz.inf.isoga.datastructure; import it.unibz.inf.isochrone.network.Node; +import com.fasterxml.jackson.annotation.JsonProperty; + /** * The <code>QueryPoint</code> class represents a query point. Query points are particular pedestrian nodes with * temporal distance equal to zero, since they are used as the starting point of the isochrone computation. - * + * * @author Markus Innerebner * @author Willi Cometti * @version 3.0 */ public class QueryPoint extends Node { - private final int x, y; + private int x; + private int y; /** - * Class Constructor that creates a query point. Query points have a temporal distance equal to zero. - * - * @param nodeId the node ID - * @param x the x coordinate - * @param y the y coordinate + * Create a query point. + * Query points have a temporal distance equal to zero. */ - public QueryPoint(final int nodeId, final int x, final int y) { - super(nodeId); - + public QueryPoint() { + super(); setDistance(0); - this.x = x; - this.y = y; - } - - public int getX() { - return x; - } - - public int getY() { - return y; } @Override @@ -46,19 +35,37 @@ public class QueryPoint extends Node { return getID() == other.getID() && x == other.x && y == other.y; } + public int getX() { + return x; + } + + public int getY() { + return y; + } + @Override public int hashCode() { return getID() + x + y; } @Override - public String toString() { - return "[" + getID() + ", (" + x + "," + y + ")]"; + public boolean isClosed() { + return true; + } + + @JsonProperty + public void setX(final int x) { + this.x = x; + } + + @JsonProperty + public void setY(final int y) { + this.y = y; } @Override - public boolean isClosed() { - return true; + public String toString() { + return "[" + getID() + ", (" + x + "," + y + ")]"; } } diff --git a/src/main/java/it/unibz/inf/isoga/db/DbUtility.java b/src/main/java/it/unibz/inf/isoga/db/DbUtility.java index 543d519c..6d60bca9 100644 --- a/src/main/java/it/unibz/inf/isoga/db/DbUtility.java +++ b/src/main/java/it/unibz/inf/isoga/db/DbUtility.java @@ -3,10 +3,10 @@ package it.unibz.inf.isoga.db; import it.unibz.inf.isochrone.util.EnumContainer.QueryType; import it.unibz.inf.isoga.network.RouteEntity; import it.unibz.inf.isoga.service.dto.ResponsePoiFeature; +import it.unibz.inf.isoga.service.dto.ResponsePoiFeature.PoiFeature; import it.unibz.inf.isoga.service.dto.ResponseWps; import it.unibz.inf.isoga.service.dto.WpsFeature; import it.unibz.inf.isoga.service.dto.WpsStatistic; -import it.unibz.inf.isoga.service.dto.ResponsePoiFeature.PoiFeature; import it.unibz.inf.isoga.util.ConfigWeb; import java.sql.CallableStatement; @@ -24,7 +24,6 @@ import java.util.SortedMap; import java.util.TreeMap; import org.apache.commons.dbutils.DbUtils; -import org.json.JSONException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -151,7 +150,7 @@ public final class DbUtility { + " PRIMARY KEY (\"ID\"))"); } - public static ResponsePoiFeature getPoiFeatures(final String sqlExpr, final ConfigWeb config) throws JSONException { + public static ResponsePoiFeature getPoiFeatures(final String sqlExpr, final ConfigWeb config) { final ResponsePoiFeature result = new ResponsePoiFeature(); if (sqlExpr.equals("noResult")) { result.setType("invalid"); @@ -177,8 +176,6 @@ public final class DbUtility { } catch (final SQLException e1) { e1.printStackTrace(); } - } catch (final JSONException e) { - e.printStackTrace(); } finally { DbUtils.closeQuietly(rSet); DbUtils.closeQuietly(stmt); @@ -187,7 +184,7 @@ public final class DbUtility { return result; } - public static ResponseWps spatialIntersection2JSON(final String sqlExpr, final SqlQuery query, final ConfigWeb config) throws JSONException { + public static ResponseWps spatialIntersection2JSON(final String sqlExpr, final SqlQuery query, final ConfigWeb config) { final boolean isSum = query.getQueryType().equals(QueryType.SUM); final ResponseWps result = new ResponseWps(query.getQueryType().toString().toLowerCase()); if (sqlExpr.equals("noResult")) { @@ -299,8 +296,6 @@ public final class DbUtility { } catch (final SQLException e1) { e1.printStackTrace(); } - } catch (final JSONException e) { - e.printStackTrace(); } finally { DbUtils.closeQuietly(rSet); DbUtils.closeQuietly(stmt); diff --git a/src/main/java/it/unibz/inf/isoga/service/ServiceIsochrone.java b/src/main/java/it/unibz/inf/isoga/service/ServiceIsochrone.java index 864e44bb..43882e4d 100644 --- a/src/main/java/it/unibz/inf/isoga/service/ServiceIsochrone.java +++ b/src/main/java/it/unibz/inf/isoga/service/ServiceIsochrone.java @@ -10,8 +10,8 @@ import it.unibz.inf.isochrone.util.EnumContainer.Dataset; import it.unibz.inf.isochrone.util.Query; import it.unibz.inf.isoga.coverage.CoverageBuilder; import it.unibz.inf.isoga.datastructure.QueryPoint; -import it.unibz.inf.isoga.db.DbUtility; import it.unibz.inf.isoga.db.DatabaseWeb; +import it.unibz.inf.isoga.db.DbUtility; import it.unibz.inf.isoga.network.MemoryBBoxOutput; import it.unibz.inf.isoga.service.dto.RequestIsochrone; import it.unibz.inf.isoga.service.dto.ResponseIsochrone; @@ -20,14 +20,12 @@ import it.unibz.inf.isoga.util.ConfigDataset; import it.unibz.inf.isoga.util.ConfigWeb; import java.sql.Connection; +import java.util.Collection; import java.util.HashSet; import java.util.Set; import javax.websocket.Session; -import org.json.JSONArray; -import org.json.JSONException; - /** * @author Nikolaus Krismer */ @@ -112,24 +110,10 @@ public class ServiceIsochrone extends AbstractService<RequestIsochrone, Response return new MineX(query); } - private Set<Location> locationsFromQueryPoints(final String qPoints, final ConfigWeb config) { - final Set<QueryPoint> queryPoints = new HashSet<QueryPoint>(); - - try { - final JSONArray arr = new JSONArray(qPoints); - for (int i = 0; i < arr.length(); i++) { - final JSONArray ordinates = (JSONArray) arr.get(i); - final int x = ordinates.getInt(0); - final int y = ordinates.getInt(1); - - queryPoints.add(new QueryPoint(Integer.MIN_VALUE, x, y)); - } - } catch (final JSONException e) { - e.printStackTrace(); - } - + private Set<Location> locationsFromQueryPoints(final Collection<QueryPoint> queryPoints, final ConfigWeb config) { final Set<Location> locations = new HashSet<Location>(queryPoints.size()); final DatabaseWeb db = new DatabaseWeb(config); + for (final QueryPoint qPoint : queryPoints) { locations.addAll(db.projectOnLinks(qPoint)); } diff --git a/src/main/java/it/unibz/inf/isoga/service/dto/RequestIsochrone.java b/src/main/java/it/unibz/inf/isoga/service/dto/RequestIsochrone.java index 7c4dfe0a..80534d1e 100644 --- a/src/main/java/it/unibz/inf/isoga/service/dto/RequestIsochrone.java +++ b/src/main/java/it/unibz/inf/isoga/service/dto/RequestIsochrone.java @@ -3,11 +3,13 @@ package it.unibz.inf.isoga.service.dto; import it.unibz.inf.isochrone.util.EnumContainer.Dataset; import it.unibz.inf.isochrone.util.EnumContainer.Direction; import it.unibz.inf.isochrone.util.EnumContainer.Mode; +import it.unibz.inf.isoga.datastructure.QueryPoint; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; +import java.util.Collection; import com.fasterxml.jackson.annotation.JsonProperty; @@ -22,7 +24,7 @@ public class RequestIsochrone implements IRequest { private String datasetString; private Boolean expirationMode; private String modeString; - private String queryPoints; + private Collection<QueryPoint> queryPoints; private Double speed; private Long dmax; @@ -85,7 +87,7 @@ public class RequestIsochrone implements IRequest { return Mode.valueOf(modeString.toUpperCase()); } - public String getQueryPoints() { + public Collection<QueryPoint> getQueryPoints() { return queryPoints; } @@ -157,7 +159,7 @@ public class RequestIsochrone implements IRequest { } @JsonProperty - public void setQueryPoints(final String queryPoints) { + public void setQueryPoints(final Collection<QueryPoint> queryPoints) { this.queryPoints = queryPoints; } diff --git a/src/main/java/it/unibz/inf/isoga/web/JsonWebsocket.java b/src/main/java/it/unibz/inf/isoga/web/JsonWebsocket.java index c0fa8a73..275245ec 100644 --- a/src/main/java/it/unibz/inf/isoga/web/JsonWebsocket.java +++ b/src/main/java/it/unibz/inf/isoga/web/JsonWebsocket.java @@ -27,11 +27,11 @@ import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.server.ServerEndpoint; -import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; @ServerEndpoint(value = "/websocket") @@ -61,11 +61,21 @@ public class JsonWebsocket { LOGGER.debug("Websocket: Message from client \"" + clientId + "\": " + message); final boolean emptyRequest = (message == null || message.trim().length() <= 0); - final JSONObject requestJson = (emptyRequest) ? new JSONObject() : new JSONObject(message); + if (emptyRequest) { + throw new IllegalArgumentException("Invalid request given... empty object"); + } + + final ObjectMapper mapper = new ObjectMapper(); + JsonNode requestJsonNode = null; + try { + requestJsonNode = mapper.readValue(message.getBytes(), JsonNode.class); + } catch (final IOException e) { + throw new IllegalArgumentException("Invalid request given... can not be parsed to json object"); + } - final Class<S> aService = this.<S, T, U>getService(requestJson); + final Class<S> aService = this.<S, T, U>getService(requestJsonNode); if (aService == null) { - throw new IllegalArgumentException("Invalid request message given... service could not be found"); + throw new IllegalArgumentException("Invalid request message given... referenced service could not be found"); } S service = null; @@ -75,10 +85,9 @@ public class JsonWebsocket { e.printStackTrace(); } if (service == null) { - throw new IllegalStateException("Service \"" + aService.getClass().getSimpleName() + "\" could not be created!"); + throw new IllegalStateException("Referenced service \"" + aService.getClass().getSimpleName() + "\" could not be created!"); } - final ObjectMapper mapper = new ObjectMapper(); T request = null; try { request = mapper.readValue(message, service.getRequestClass()); @@ -125,12 +134,12 @@ public class JsonWebsocket { // Private methods - private <S extends IService<T, U>, T extends IRequest, U extends IResponse> Class<S> getService(final JSONObject jsonObject) { - if (!jsonObject.has("action")) { + private <S extends IService<T, U>, T extends IRequest, U extends IResponse> Class<S> getService(final JsonNode jsonNode) { + if (!jsonNode.has("action") || !jsonNode.isTextual()) { return null; } - final String action = jsonObject.getString("action"); + final String action = jsonNode.get("action").asText(); if (action == null || action.trim().length() <= 0) { return null; } -- GitLab