From dca514f31102e9a6c09577a08939d756cedfff4a Mon Sep 17 00:00:00 2001 From: alexrodriguez <alejandro.rodriguez@tecnalia.com> Date: Tue, 15 Nov 2022 09:04:29 +0100 Subject: [PATCH] Added remove points service, change path to "anonymize" --- .../anonymize/rest/AnonymizerAPI.java | 28 ++++++- .../anonymize/service/AnonymizerService.java | 74 +++++++++++++++++++ .../anonymize/service/DataService.java | 21 +++++- src/main/java/model/Location.java | 34 +++++++++ src/main/resources/application.properties | 2 +- 5 files changed, 154 insertions(+), 5 deletions(-) create mode 100644 src/main/java/model/Location.java diff --git a/src/main/java/com/tecnalia/urbanite/anonymize/rest/AnonymizerAPI.java b/src/main/java/com/tecnalia/urbanite/anonymize/rest/AnonymizerAPI.java index 51290de..7b5c7f3 100644 --- a/src/main/java/com/tecnalia/urbanite/anonymize/rest/AnonymizerAPI.java +++ b/src/main/java/com/tecnalia/urbanite/anonymize/rest/AnonymizerAPI.java @@ -15,9 +15,11 @@ */ package com.tecnalia.urbanite.anonymize.rest; +import org.codehaus.jettison.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @@ -29,6 +31,7 @@ import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.tags.Tag; +import model.Location; @RestController @Tag(name = "Anonymize", description = "Operations to Anonymize Data") @@ -51,5 +54,28 @@ public class AnonymizerAPI { return anonymizerService.anonymizeProperty(city,model,property,value); } - + + @RequestMapping(method = RequestMethod.POST, value = "/removePointsOutOfArea/{city}/{model}/{property}/{value}") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "Successful operation."), + @ApiResponse(responseCode = "400", description = "Bad request. ")}) + @Operation(summary = "Given an input polygon, delete the points outside that polygon ", description = "Given an input polygon,{location} delete the points outside that polygon. The points are those defined by the city, property and value model.\n" + + "And they must have a \"location\" field with an array of \"coordinates\". ") + public ResponseEntity<String> removePointsOutOfArea( + @Parameter(description = "Points array to be validated ", example = "{\"location\": {\n" + + " \"type\": \"LineString\",\n" + + " \"coordinates\": [\n" + + " [15.5612754821777, 38.2057461528513],\n" + + " [15.4648876190186, 38.1668198052653],\n" + + " [15.4643297195435, 38.1587890805601]\n" + + " ]\n" + + " }}") @RequestBody Location location, + @Parameter(description = "City where is the model to be anonymized", example = "bilbao") @PathVariable String city, + @Parameter(description = "Model that is going to be anonymized.", example = "vehicle") @PathVariable String model, + @Parameter(description = "Property of the model that is going to be anonymized.", example = "name") @PathVariable String property, + @Parameter(description = "Only the properties with this value are going to be modified.", example = "Jonh") @PathVariable String value) throws Exception { + + return anonymizerService.removePointsOutOfArea(location,city,model,property,value); + + } } diff --git a/src/main/java/com/tecnalia/urbanite/anonymize/service/AnonymizerService.java b/src/main/java/com/tecnalia/urbanite/anonymize/service/AnonymizerService.java index b70a552..3f05c8b 100644 --- a/src/main/java/com/tecnalia/urbanite/anonymize/service/AnonymizerService.java +++ b/src/main/java/com/tecnalia/urbanite/anonymize/service/AnonymizerService.java @@ -1,8 +1,10 @@ package com.tecnalia.urbanite.anonymize.service; +import java.awt.geom.Path2D; import java.io.IOException; import java.net.URISyntaxException; import java.security.NoSuchAlgorithmException; +import java.util.List; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; @@ -14,6 +16,8 @@ import org.springframework.stereotype.Service; import com.tecnalia.urbanite.anonymize.utils.Crypto; +import model.Location; + @Service public class AnonymizerService { @@ -66,4 +70,74 @@ public class AnonymizerService { } } + + public ResponseEntity<String> removePointsOutOfArea(Location location, String city,String model, String property,String value) { + try { + JSONArray objects = dataService.getObjectsByName(property,value, model, city); + List<List<Double>> coordinateslist = location.getCoordinates(); + if (!coordinateslist.isEmpty()) { + + Path2D path = new Path2D.Double(); + for (int i=0; i < coordinateslist.size();i++) { + List<Double> coordinates = coordinateslist.get(i); + if (i==0) { + path.moveTo(coordinates.get(0), coordinates.get(1)); + } + else { + path.lineTo(coordinates.get(0), coordinates.get(1)); + } + } + path.closePath(); + + + for (int i=0; i < objects.length();i++) { + JSONObject object = objects.getJSONObject(i); + JSONObject josnlocation =object.getJSONObject("location"); + JSONArray coordinate = josnlocation.getJSONArray("coordinates"); + if (!path.contains(coordinate.getDouble(0), coordinate.getDouble(1))) { + + System.out.println("Contains!"); + /*JSONObject result =dataService.deleteObject(model, city,object.getString("id")); + if (result.has("Error")) { + return new ResponseEntity<>("Error while anonymizig:" + property + ". Please try again !!\n " + result.getString("Error"), HttpStatus.INTERNAL_SERVER_ERROR); + } + else { + System.out.println(result.toString()); + }*/ + } + else{ + System.out.println("Not Contains!"); + } + + + } + + return new ResponseEntity<>("Points removed successfully !!", HttpStatus.OK); + + + } + + + else { + return new ResponseEntity<>("Coordinates not defined.", HttpStatus.BAD_REQUEST); + } + + + } catch (JSONException e) { + e.printStackTrace(); + return new ResponseEntity<>("Error while removePointsOutOfArea:" + property + ". Please try again !! " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); + } catch (URISyntaxException e) { + + e.printStackTrace(); + return new ResponseEntity<>("Error while removePointsOutOfArea:" + property + ". Please try again !! " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); + } catch (IOException e) { + e.printStackTrace(); + return new ResponseEntity<>("Error while removePointsOutOfArea:" + property + ". Please try again !! " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); + } catch (InterruptedException e) { + e.printStackTrace(); + return new ResponseEntity<>("Error while removePointsOutOfArea:" + property + ". Please try again !! " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); + } + + + } } diff --git a/src/main/java/com/tecnalia/urbanite/anonymize/service/DataService.java b/src/main/java/com/tecnalia/urbanite/anonymize/service/DataService.java index e8dbfcd..91ace8a 100644 --- a/src/main/java/com/tecnalia/urbanite/anonymize/service/DataService.java +++ b/src/main/java/com/tecnalia/urbanite/anonymize/service/DataService.java @@ -52,12 +52,27 @@ public class DataService { HttpResponse<String> response = HttpClient .newBuilder() .build() - .send(request, BodyHandlers.ofString()); - System.out.println(response.body()); + .send(request, BodyHandlers.ofString()); return new JSONObject(response.body()); } - + public JSONObject deleteObject(String model, String city, String id) throws JSONException, URISyntaxException, IOException, InterruptedException { + String URL = Configuration.getExporterUrl(); + + //http://localhost:8080/data/updateTData/vehicle/bilbao/vehicle%3AWasteManagement%3A1 + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI(URL+"/data/deleteTData/"+model+"/"+city+"/"+id)) + .PUT(HttpRequest.BodyPublishers.noBody()) + //.setHeader("Content-Type", "application/json") + .build(); + + HttpResponse<String> response = HttpClient + .newBuilder() + .build() + .send(request, BodyHandlers.ofString()); + System.out.println(response.body()); + return new JSONObject(response.body()); + } private String encode(String value) throws UnsupportedEncodingException { return URLEncoder.encode(value, StandardCharsets.UTF_8.toString()); diff --git a/src/main/java/model/Location.java b/src/main/java/model/Location.java new file mode 100644 index 0000000..05d7a7f --- /dev/null +++ b/src/main/java/model/Location.java @@ -0,0 +1,34 @@ +package model; + +import java.util.List; + +public class Location { + + private List<List<Double>> coordinates; + private String type; + public Location() { + super(); + + } + + public List<List<Double>> getCoordinates() { + return coordinates; + } + + + public void setCoordinates(List<List<Double>> coordinates) { + this.coordinates = coordinates; + } + + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 3b0eda2..1226fe0 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,7 +1,7 @@ application-description=@project.description@ application-version=@project.version@ logging.level.org.springframework.boot.autoconfigure=ERROR -server.servlet.context-path=/data +server.servlet.context-path=/anonymize server.port=80 logging.level.com.tecnalia.urbanite.anonymize=DEBUG -- GitLab