diff --git a/src/CamelComponents/CamelREST.java b/src/CamelComponents/CamelREST.java index c0e46ae..c245229 100644 --- a/src/CamelComponents/CamelREST.java +++ b/src/CamelComponents/CamelREST.java @@ -1,10 +1,12 @@ package CamelComponents; +import Models.Vehicle; import core.StationsInteractor; +import core.TimeTablesInteractor; +import core.VehiclesInteractor; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.model.rest.RestBindingMode; import org.springframework.stereotype.Component; -import org.xml.sax.SAXException; import parsers.ParserUtils; import javax.xml.bind.JAXBException; @@ -13,9 +15,13 @@ @Component public class CamelREST extends RouteBuilder { private final StationsInteractor stations_interactor; + private final VehiclesInteractor vehicles_interactor; + private final TimeTablesInteractor timetables_interactor; private final ParserUtils putils; - public CamelREST() throws JAXBException, ParserConfigurationException, SAXException { + public CamelREST() throws JAXBException, ParserConfigurationException { + this.vehicles_interactor = new VehiclesInteractor("data/vehicles.xml"); + this.timetables_interactor = new TimeTablesInteractor("data/timetables.xml"); this.stations_interactor = new StationsInteractor("data/statii-ratt.xml"); this.putils = new ParserUtils("data/statii-ratt.xml"); } @@ -38,103 +44,40 @@ public void configure() throws Exception { .produces("application/xml") .route() .bean(stations_interactor, "getStation(${header.id})") + .endRest() + + .get("/vehicles") + .produces("application/xml") + .route() + .bean(vehicles_interactor, "getAllVehicles") + .endRest() + + .get("/vehicle/{id}") + .produces("application/xml") + .route() + .bean(vehicles_interactor, "getVehicle(${header.id})") + .endRest() + + .put("/vehicle/{id}") + .type(Vehicle.class) + .consumes("application/xml") + .produces("application/xml") + .route() + .bean(vehicles_interactor, "replaceVehicle(${header.id}, ${body})") + .endRest() + + .post("/vehicle") + .type(Vehicle.class) + .consumes("application/xml") + .produces("application/xml") + .route() + .bean(vehicles_interactor, "createVehicle(${body})") + .endRest() + + .delete("/vehicle/{id}") + .produces("application/xml") + .route() + .bean(vehicles_interactor, "deleteVehicle(${header.id})") .endRest(); -// -// .get("/publications") -// .produces("application/xml") -// .route() -// .bean(xml_interactor, "getAllPublications") -// .endRest() -// -// .get("/affiliations") -// .produces("application/xml") -// .route() -// .bean(xml_interactor, "getAllAffiliations") -// .endRest() -// -// .get("/author/{id}") -// .produces("application/xml") -// .route() -// .bean(xml_interactor, "getAuthor(${header.id})") -// .endRest() -// -// .put("/author/{id}") -// .type(Author.class) -// .consumes("application/xml") -// .produces("application/xml") -// .route() -// .to("log:mylogger?showAll=true") -// .bean(xml_interactor, "replaceAuthor(${header.id}, ${body})") -// .endRest() -// -// .delete("/author/{id}") -// .produces("application/xml") -// .route() -// .bean(xml_interactor, "deleteAuthor(${header.id})") -// .endRest() -// -// .post("/author/{name}") -// .produces("application/xml") -// .route() -// .bean(xml_interactor, "createAuthor(${header.name})") -// .endRest() -// -// // affiliations -// -// .get("/affiliation/{rid}") -// .produces("application/xml") -// .route() -// .bean(xml_interactor, "getAffiliation(${header.rid})") -// .endRest() -// -// .put("/affiliation/{rid}") -// .type(Affiliation.class) -// .produces("application/xml") -// .consumes("application/xml") -// .route() -// .to("log:mylogger?showAll=true") -// .bean(xml_interactor, "replaceAffiliation(${header.rid}, ${body})") -// .endRest() -// -// .delete("/affiliation/{rid}") -// .produces("application/xml") -// .route() -// .bean(xml_interactor, "deleteAffiliation(${header.rid})") -// .endRest() -// -// .post("/affiliation/{rid}/{institution_name}") -// .produces("application/xml") -// .route() -// .bean(xml_interactor, "createAffiliation(${header.rid}, ${header.institution_name})") -// .endRest() -// -// // Publications -// -// .get("/publication?doi={doi}") -// .produces("application/xml") -// .route() -// .bean(xml_interactor, "getPublication(${header.doi})") -// .endRest() -// -// .put("/publication?doi={doi}") -// .type(Article.class) -// .produces("application/xml") -// .consumes("application/xml") -// .route() -// .bean(xml_interactor, "replacePublication(${body})") -// .endRest() -// -// .delete("/publication?doi={doi}") -// .produces("application/xml") -// .route() -// .bean(xml_interactor, "deletePublication(${header.doi})") -// .endRest() -// -// .post("/publication?doi={doi}") -// .type(Article.class) -// .produces("application/xml") -// .route() -// .bean(xml_interactor, "createPublication(${body})") -// .endRest(); } }; diff --git a/src/Models/Vehicle.java b/src/Models/Vehicle.java index d79d0e8..2b3cb51 100644 --- a/src/Models/Vehicle.java +++ b/src/Models/Vehicle.java @@ -23,8 +23,8 @@ public Vehicle(int id, String name, String type){ public String toString(){ return "Vehicle{" + - ", id=" + this.vehicleID + + "id=" + this.vehicleID + ", name=" + this.vehicleName + - ", type=" + this.vehicleType; + ", type=" + this.vehicleType + "}"; } } diff --git a/src/STPTMain.java b/src/STPTMain.java index a97269f..a69f474 100644 --- a/src/STPTMain.java +++ b/src/STPTMain.java @@ -1,7 +1,5 @@ -import parsers.ParserUtils; -import core.*; - -import org.w3c.dom.Document; +import CamelComponents.CamelREST; +import org.apache.camel.main.Main; import org.w3c.dom.NodeList; public class STPTMain { @@ -14,11 +12,11 @@ public static void IterateNodeList(NodeList query_res){ } public static void main(String args[]) throws Exception { - ParserUtils putils = new ParserUtils("data/statii-ratt.xml"); - Document doc = putils.parseJAXB(); - StationsInteractor s = new StationsInteractor("data/statii-ratt.xml"); - IterateNodeList(s.getAllStations()); - System.out.println(s.getStation(4680)); +// ParserUtils putils = new ParserUtils("data/statii-ratt.xml"); +// Document doc = putils.parseJAXB(); +// StationsInteractor s = new StationsInteractor("data/statii-ratt.xml"); +// IterateNodeList(s.getAllStations()); +// System.out.println(s.getStation(4680)); // // VehiclesInteractor v = new VehiclesInteractor("data/vehicles.xml"); // IterateNodeList(v.getAllVehicles()); @@ -28,8 +26,8 @@ public static void main(String args[]) throws Exception { // IterateNodeList(t.getAllTimeTables()); // IterateNodeList(t.getTimeTable(1106)); -// Main main = new Main(); -// main.configure().addRoutesBuilder(new CamelREST()); -// main.run(args); + Main main = new Main(); + main.configure().addRoutesBuilder(new CamelREST()); + main.run(args); } } diff --git a/src/core/VehiclesInteractor.java b/src/core/VehiclesInteractor.java index 812247b..88ceec8 100644 --- a/src/core/VehiclesInteractor.java +++ b/src/core/VehiclesInteractor.java @@ -1,6 +1,9 @@ package core; +import Models.Vehicle; import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; import org.w3c.dom.NodeList; import parsers.ParserUtils; import parsers.XPathUtils; @@ -23,12 +26,55 @@ public VehiclesInteractor(String path_to_doc) throws this.xputils = new XPathUtils(this.document); } + public Node createVehicle(Integer new_id, String vehicleName, String vehicleType) throws XPathExpressionException { + // get last vehicle ID, create a new node and add it to it's parent + Element last_vehicle = (Element) (xputils.QueryXPath("//vehicle[not(@id <= preceding-sibling::vehicle/@id) and not(@id <=following-sibling::vehicle/@id)]").item(0)); + + if(new_id == null) { + new_id = Integer.parseInt(last_vehicle.getAttribute("id")) + 1; + } + + Element new_vehicle = this.document.createElement("vehicle"); + new_vehicle.setAttribute("id", new_id.toString()); + Element vehicle_name = this.document.createElement("vehicle-name"); + vehicle_name.setTextContent(vehicleName); + Element vehicle_type = this.document.createElement("vehicle-type"); + vehicle_type.setTextContent(vehicleType); + + new_vehicle.appendChild(vehicle_name); + new_vehicle.appendChild(vehicle_type); + last_vehicle.getParentNode().appendChild(new_vehicle); + + return new_vehicle; + } + + public Node createVehicle(Vehicle v) throws XPathExpressionException { + return this.createVehicle(v.vehicleID, v.vehicleName, v.vehicleType); + } + + public NodeList getAllVehicles() throws XPathExpressionException { return xputils.QueryXPath("/root/vehicles"); } - public NodeList getVehicle(Integer vehicle_id) throws XPathExpressionException { - return xputils.QueryXPath(String.format("//vehicle[@id=%s]", vehicle_id)); + public Node getVehicle(Integer vehicle_id) throws XPathExpressionException { + return xputils.QueryXPath(String.format("//vehicle[@id=%s]", vehicle_id)).item(0); + } + + public Document deleteVehicle(Integer id) throws XPathExpressionException { + Element vehicle_to_delete = (Element) xputils.QueryXPath(String.format("//vehicle[@id=%s]", id)).item(0); + vehicle_to_delete.getParentNode().removeChild(vehicle_to_delete); + + return this.document; + } + + public Document replaceVehicle(Integer id, Vehicle vehicle) throws XPathExpressionException { + System.out.println("Replacing: " + vehicle.toString()); + Node node_to_replace = this.getVehicle(id); + Node new_au = this.createVehicle(vehicle.vehicleID, vehicle.vehicleName, vehicle.vehicleType); + node_to_replace.getParentNode().replaceChild(new_au, node_to_replace); + + return this.document; } public Document getDocument(){ diff --git a/xml-project.postman_collection.json b/xml-project.postman_collection.json new file mode 100644 index 0000000..97025cc --- /dev/null +++ b/xml-project.postman_collection.json @@ -0,0 +1,173 @@ +{ + "info": { + "_postman_id": "acca805a-ca4b-47f1-ab5e-ebba7ce28156", + "name": "xml-project", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" + }, + "item": [ + { + "name": "getAllStations", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "http://localhost:9091/api/stations/", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "9091", + "path": [ + "api", + "stations", + "" + ] + } + }, + "response": [] + }, + { + "name": "getStationByID", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "http://localhost:9091/api/station/2810", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "9091", + "path": [ + "api", + "station", + "2810" + ] + } + }, + "response": [] + }, + { + "name": "getVehicles", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "http://localhost:9091/api/vehicles/", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "9091", + "path": [ + "api", + "vehicles", + "" + ] + } + }, + "response": [] + }, + { + "name": "http://localhost:9091/api/vehicle/1006", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "http://localhost:9091/api/vehicle/1006", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "9091", + "path": [ + "api", + "vehicle", + "1006" + ] + } + }, + "response": [] + }, + { + "name": "deleteVehicle", + "request": { + "method": "DELETE", + "header": [], + "url": { + "raw": "http://localhost:9091/api/vehicle/1126", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "9091", + "path": [ + "api", + "vehicle", + "1126" + ] + } + }, + "response": [] + }, + { + "name": "replaceVehicle", + "request": { + "method": "PUT", + "header": [], + "body": { + "mode": "raw", + "raw": " \n 42\n Bus\n ", + "options": { + "raw": { + "language": "xml" + } + } + }, + "url": { + "raw": "http://localhost:9091/api/vehicle/1106", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "9091", + "path": [ + "api", + "vehicle", + "1106" + ] + } + }, + "response": [] + }, + { + "name": "createVehicle", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": " \n Express 42\n Spaceship\n ", + "options": { + "raw": { + "language": "xml" + } + } + }, + "url": { + "raw": "http://localhost:9091/api/vehicle", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "9091", + "path": [ + "api", + "vehicle" + ] + } + }, + "response": [] + } + ] +} \ No newline at end of file diff --git a/xml-rest.postman_collection.json b/xml-rest.postman_collection.json deleted file mode 100644 index 9d7d86e..0000000 --- a/xml-rest.postman_collection.json +++ /dev/null @@ -1,378 +0,0 @@ -{ - "info": { - "_postman_id": "eb6af686-bad8-443c-a6fc-b53d80bead45", - "name": "xml-rest", - "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" - }, - "item": [ - { - "name": "http://localhost:9091/api/authors", - "request": { - "method": "GET", - "header": [], - "url": { - "raw": "http://localhost:9091/api/authors", - "protocol": "http", - "host": [ - "localhost" - ], - "port": "9091", - "path": [ - "api", - "authors" - ] - } - }, - "response": [] - }, - { - "name": "http://localhost:9091/api/publications", - "request": { - "method": "GET", - "header": [], - "url": { - "raw": "http://localhost:9091/api/publications", - "protocol": "http", - "host": [ - "localhost" - ], - "port": "9091", - "path": [ - "api", - "publications" - ] - } - }, - "response": [] - }, - { - "name": "http://localhost:9091/api/affiliations", - "request": { - "method": "GET", - "header": [], - "url": { - "raw": "http://localhost:9091/api/affiliations", - "protocol": "http", - "host": [ - "localhost" - ], - "port": "9091", - "path": [ - "api", - "affiliations" - ] - } - }, - "response": [] - }, - { - "name": "http://localhost:9091/api/author/2", - "request": { - "method": "GET", - "header": [], - "url": { - "raw": "http://localhost:9091/api/author/2", - "protocol": "http", - "host": [ - "localhost" - ], - "port": "9091", - "path": [ - "api", - "author", - "2" - ] - } - }, - "response": [] - }, - { - "name": "http://localhost:9091/api/author/8", - "request": { - "method": "PUT", - "header": [], - "body": { - "mode": "raw", - "raw": "\nAlexandru Munteanu", - "options": { - "raw": { - "language": "xml" - } - } - }, - "url": { - "raw": "http://localhost:9091/api/author/8", - "protocol": "http", - "host": [ - "localhost" - ], - "port": "9091", - "path": [ - "api", - "author", - "8" - ] - } - }, - "response": [] - }, - { - "name": "http://localhost:9091/api/author/Alexandru Munteanu", - "request": { - "method": "POST", - "header": [], - "url": { - "raw": "http://localhost:9091/api/author/Alexandru Munteanu", - "protocol": "http", - "host": [ - "localhost" - ], - "port": "9091", - "path": [ - "api", - "author", - "Alexandru Munteanu" - ] - } - }, - "response": [] - }, - { - "name": "http://localhost:9091/api/author/1", - "request": { - "method": "DELETE", - "header": [], - "url": { - "raw": "http://localhost:9091/api/author/3", - "protocol": "http", - "host": [ - "localhost" - ], - "port": "9091", - "path": [ - "api", - "author", - "3" - ] - } - }, - "response": [] - }, - { - "name": "http://localhost:9091/api/affiliaiton/4", - "request": { - "method": "GET", - "header": [], - "url": { - "raw": "http://localhost:9091/api/affiliation/4", - "protocol": "http", - "host": [ - "localhost" - ], - "port": "9091", - "path": [ - "api", - "affiliation", - "4" - ] - } - }, - "response": [] - }, - { - "name": "http://localhost:9091/api/affiliation/4", - "request": { - "method": "PUT", - "header": [], - "body": { - "mode": "raw", - "raw": "\nWest University of Timişoara", - "options": { - "raw": { - "language": "xml" - } - } - }, - "url": { - "raw": "http://localhost:9091/api/affiliation/1", - "protocol": "http", - "host": [ - "localhost" - ], - "port": "9091", - "path": [ - "api", - "affiliation", - "1" - ] - } - }, - "response": [] - }, - { - "name": "http://localhost:9091/api/affiliation/3/Some University", - "request": { - "method": "POST", - "header": [], - "url": { - "raw": "http://localhost:9091/api/affiliation/3/Some University", - "protocol": "http", - "host": [ - "localhost" - ], - "port": "9091", - "path": [ - "api", - "affiliation", - "3", - "Some University" - ] - } - }, - "response": [] - }, - { - "name": "http://localhost:9091/api/affiliation/3", - "request": { - "method": "DELETE", - "header": [], - "url": { - "raw": "http://localhost:9091/api/affiliation/3", - "protocol": "http", - "host": [ - "localhost" - ], - "port": "9091", - "path": [ - "api", - "affiliation", - "3" - ] - } - }, - "response": [] - }, - { - "name": "10.1186/2192-113X-2-12", - "request": { - "method": "GET", - "header": [], - "url": { - "raw": "http://localhost:9091/api/publication?doi=10.4018/IJGHPC.2016010101", - "protocol": "http", - "host": [ - "localhost" - ], - "port": "9091", - "path": [ - "api", - "publication" - ], - "query": [ - { - "key": "doi", - "value": "10.4018/IJGHPC.2016010101" - } - ] - } - }, - "response": [] - }, - { - "name": "http://localhost:9091/api/publication?doi=10.1093/jigpal/jzaa034", - "request": { - "method": "PUT", - "header": [], - "body": { - "mode": "raw", - "raw": "
\n \n \n \n \n A replaced publication\n 2021\n A journal\n
", - "options": { - "raw": { - "language": "xml" - } - } - }, - "url": { - "raw": "http://localhost:9091/api/publication?doi=10.1093/jigpal/jzaa034", - "protocol": "http", - "host": [ - "localhost" - ], - "port": "9091", - "path": [ - "api", - "publication" - ], - "query": [ - { - "key": "doi", - "value": "10.1093/jigpal/jzaa034" - } - ] - } - }, - "response": [] - }, - { - "name": "http://localhost:9091/api/publication/10.1186/2192-113X-2-12", - "request": { - "method": "POST", - "header": [], - "body": { - "mode": "raw", - "raw": "
\n \n \n \n \n \n Towards real-time DNA biometrics using GPU-accelerated processing\n 2017\n Logic Journal of the IGPL\n
", - "options": { - "raw": { - "language": "xml" - } - } - }, - "url": { - "raw": "http://localhost:9091/api/publication?doi=10.1093/jigpal/jzaa034", - "protocol": "http", - "host": [ - "localhost" - ], - "port": "9091", - "path": [ - "api", - "publication" - ], - "query": [ - { - "key": "doi", - "value": "10.1093/jigpal/jzaa034" - } - ] - } - }, - "response": [] - }, - { - "name": "http://localhost:9091/api/publication?doi=10.4018/IJGHPC.2016010101", - "request": { - "method": "DELETE", - "header": [], - "url": { - "raw": "http://localhost:9091/api/publication?doi=10.4018/IJGHPC.2016010101", - "protocol": "http", - "host": [ - "localhost" - ], - "port": "9091", - "path": [ - "api", - "publication" - ], - "query": [ - { - "key": "doi", - "value": "10.4018/IJGHPC.2016010101" - } - ] - } - }, - "response": [] - } - ] -} \ No newline at end of file