diff --git a/.gradle/6.7/executionHistory/executionHistory.bin b/.gradle/6.7/executionHistory/executionHistory.bin index 3d2cf79f1a0cb21d8dd7b6dc4664ad04079711b4..395a46f759f14a6a52bf46b9b050ee23de1e699c 100644 Binary files a/.gradle/6.7/executionHistory/executionHistory.bin and b/.gradle/6.7/executionHistory/executionHistory.bin differ diff --git a/.gradle/6.7/executionHistory/executionHistory.lock b/.gradle/6.7/executionHistory/executionHistory.lock index 73d688b770403cee5ba89b6f7aa3417b052d8e6d..dd295b01c669c83d1e51bfca4d3764a95e3561a9 100644 Binary files a/.gradle/6.7/executionHistory/executionHistory.lock and b/.gradle/6.7/executionHistory/executionHistory.lock differ diff --git a/.gradle/6.7/fileHashes/fileHashes.bin b/.gradle/6.7/fileHashes/fileHashes.bin index 1ab71d1307cfd0c2a441010d8418d6c0115854e7..4e9958e9f5a454e240bc7330381db4b640420279 100644 Binary files a/.gradle/6.7/fileHashes/fileHashes.bin and b/.gradle/6.7/fileHashes/fileHashes.bin differ diff --git a/.gradle/6.7/fileHashes/fileHashes.lock b/.gradle/6.7/fileHashes/fileHashes.lock index 5f17618648648e8e1721b9522d760be7dedcf838..da9dc0a3f97ec5ef5d5bfcd45f78271f393452b8 100644 Binary files a/.gradle/6.7/fileHashes/fileHashes.lock and b/.gradle/6.7/fileHashes/fileHashes.lock differ diff --git a/.gradle/6.7/javaCompile/javaCompile.lock b/.gradle/6.7/javaCompile/javaCompile.lock index 5a62152ffa40afc92eca99103a38c199b404692c..e4259ff7efc7518993216b39a4985fb900235e69 100644 Binary files a/.gradle/6.7/javaCompile/javaCompile.lock and b/.gradle/6.7/javaCompile/javaCompile.lock differ diff --git a/.gradle/6.7/javaCompile/taskHistory.bin b/.gradle/6.7/javaCompile/taskHistory.bin index db4d3e6fbff59440e1f25d92d3bb9d9180bbe7b9..f6c361d45441137d9562b454c18c6749ebcc1936 100644 Binary files a/.gradle/6.7/javaCompile/taskHistory.bin and b/.gradle/6.7/javaCompile/taskHistory.bin differ diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index 51539976f081e4ee89d5d8611a1e7260c81bccd1..c5972ad46713e782b6f49cf2f542290de16f7985 100644 Binary files a/.gradle/buildOutputCleanup/buildOutputCleanup.lock and b/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ diff --git a/.gradle/buildOutputCleanup/outputFiles.bin b/.gradle/buildOutputCleanup/outputFiles.bin index 35b86299af3d2e71fe63616013d253167ed498c1..2940a03351e8e1af440628714bc818c7e5d6e7d6 100644 Binary files a/.gradle/buildOutputCleanup/outputFiles.bin and b/.gradle/buildOutputCleanup/outputFiles.bin differ diff --git a/build.gradle b/build.gradle index 896690f9c5e4ece3266ad77148cf57ae33b6009f..6ca3c26eaaf6b45f98ca96eac5a38eeeff964cf9 100644 --- a/build.gradle +++ b/build.gradle @@ -1,49 +1,24 @@ plugins { - id "java" - id "war" // for REST + id 'java' + id 'org.springframework.boot' version '2.4.0' + id 'io.spring.dependency-management' version '1.0.15.RELEASE' } -group 'org.example' -version '1.0-SNAPSHOT' +group = 'org.example' +version = '1.0-SNAPSHOT' +sourceCompatibility = '1.8' repositories { mavenCentral() } dependencies { - testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0' - testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine' - - compile 'com.google.code.gson:gson:2.7' - - // REST Dependencies - // https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-core-asl - compile group: 'org.codehaus.jackson', name: 'jackson-core-asl', version: '1.9.2' - // https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-jaxrs - compile group: 'org.codehaus.jackson', name: 'jackson-jaxrs', version: '1.9.2' - // https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl - compile group: 'org.codehaus.jackson', name: 'jackson-mapper-asl', version: '1.9.2' - // https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-xc - compile group: 'org.codehaus.jackson', name: 'jackson-xc', version: '1.9.2' - // https://mvnrepository.com/artifact/com.sun.jersey/jersey-client - compile group: 'com.sun.jersey', name: 'jersey-client', version: '1.19.1' - // https://mvnrepository.com/artifact/com.sun.jersey/jersey-core - compile group: 'com.sun.jersey', name: 'jersey-core', version: '1.19.1' - // https://mvnrepository.com/artifact/com.sun.jersey/jersey-json - compile group: 'com.sun.jersey', name: 'jersey-json', version: '1.19.1' - // https://mvnrepository.com/artifact/com.sun.jersey/jersey-server - compile group: 'com.sun.jersey', name: 'jersey-server', version: '1.19.1' - // https://mvnrepository.com/artifact/com.sun.jersey/jersey-servlet - compile group: 'com.sun.jersey', name: 'jersey-servlet', version: '1.19.1' - // https://mvnrepository.com/artifact/org.codehaus.jettison/jettison - compile group: 'org.codehaus.jettison', name: 'jettison', version: '1.1' - // https://mvnrepository.com/artifact/javax.ws.rs/jsr311-api - compile group: 'javax.ws.rs', name: 'jsr311-api', version: '1.1.1' - // https://mvnrepository.com/artifact/com.sun.jersey/jersey-server - compile group: 'com.sun.jersey', name: 'jersey-server', version: '1.2' + // Spring Web for REST + implementation 'org.springframework.boot:spring-boot-starter-web' + // MQTT + implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5' } - test { useJUnitPlatform() -} \ No newline at end of file +} diff --git a/build/classes/java/main/beans/Dictionary.class b/build/classes/java/main/beans/Dictionary.class deleted file mode 100644 index 190a5986ee80e02b4f66aae7be4e34e3a1ef1772..0000000000000000000000000000000000000000 Binary files a/build/classes/java/main/beans/Dictionary.class and /dev/null differ diff --git a/build/classes/java/main/beans/Word.class b/build/classes/java/main/beans/Word.class deleted file mode 100644 index f23cb1889e7e8fb73ba049b7a7e7f85dac18f16a..0000000000000000000000000000000000000000 Binary files a/build/classes/java/main/beans/Word.class and /dev/null differ diff --git a/build/classes/java/main/client/MyClient.class b/build/classes/java/main/client/MyClient.class index d25d08d99d68224a4aa4f837bacc3914b090e926..ddec1ce27e963c72ea032de7a98340fc1152f163 100644 Binary files a/build/classes/java/main/client/MyClient.class and b/build/classes/java/main/client/MyClient.class differ diff --git a/build/classes/java/main/server/Application.class b/build/classes/java/main/server/Application.class new file mode 100644 index 0000000000000000000000000000000000000000..2981edf38948898f9e2c6945ac3b7b36dc7291a0 Binary files /dev/null and b/build/classes/java/main/server/Application.class differ diff --git a/build/classes/java/main/server/Dictionary.class b/build/classes/java/main/server/Dictionary.class new file mode 100644 index 0000000000000000000000000000000000000000..cd1f20bbda3adb097b1d601318a67e0429061d8f Binary files /dev/null and b/build/classes/java/main/server/Dictionary.class differ diff --git a/build/classes/java/main/server/DictionaryController.class b/build/classes/java/main/server/DictionaryController.class new file mode 100644 index 0000000000000000000000000000000000000000..ad040761e41f471c4c3036642fca0ff300c44dc9 Binary files /dev/null and b/build/classes/java/main/server/DictionaryController.class differ diff --git a/build/classes/java/main/server/Word.class b/build/classes/java/main/server/Word.class new file mode 100644 index 0000000000000000000000000000000000000000..855775a26658d92604247f256598b88f947f18e9 Binary files /dev/null and b/build/classes/java/main/server/Word.class differ diff --git a/build/classes/java/main/services/DictionaryRestService.class b/build/classes/java/main/services/DictionaryRestService.class deleted file mode 100644 index 1292169ebe560328fbb7941f79d61a7093e2b53e..0000000000000000000000000000000000000000 Binary files a/build/classes/java/main/services/DictionaryRestService.class and /dev/null differ diff --git a/build/classes/java/main/services/StartServer.class b/build/classes/java/main/services/StartServer.class deleted file mode 100644 index 1bb409223904dac815326f921e8b5e594f3f2450..0000000000000000000000000000000000000000 Binary files a/build/classes/java/main/services/StartServer.class and /dev/null differ diff --git a/build/tmp/compileJava/source-classes-mapping.txt b/build/tmp/compileJava/source-classes-mapping.txt index 53fb069a2dcde1dfe5b86ff38e68c4b6929c9f76..05efc40220525fdfbc10b889e9480cf8332bf3eb 100644 --- a/build/tmp/compileJava/source-classes-mapping.txt +++ b/build/tmp/compileJava/source-classes-mapping.txt @@ -1,10 +1,10 @@ -beans/Word.java - beans.Word -services/StartServer.java - services.StartServer -beans/Dictionary.java - beans.Dictionary -services/DictionaryRestService.java - services.DictionaryRestService +server/Application.java + server.Application +server/DictionaryController.java + server.DictionaryController +server/Word.java + server.Word +server/Dictionary.java + server.Dictionary client/MyClient.java client.MyClient diff --git a/src/main/java/client/MyClient.java b/src/main/java/client/MyClient.java index f0cbf41364f9c3af5c57cb7aeb653ac25bc94594..e3db193a1d81fee6ec24b532d31e783c72335537 100644 --- a/src/main/java/client/MyClient.java +++ b/src/main/java/client/MyClient.java @@ -1,95 +1,91 @@ package client; -import beans.Word; -import com.google.gson.Gson; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientHandlerException; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; +import org.springframework.http.*; +import org.springframework.web.client.RestTemplate; +import server.Word; public class MyClient { - public static void main(String[] argv){ - Client client = Client.create(); - String serverAddress = "http://localhost:1337"; - ClientResponse clientResponse = null; - //POST + private static final RestTemplate restTemplate = new RestTemplate(); + private static final String serverAddress = "http://localhost:1337"; + + public static void main(String[] args) { + + // POST String postPath = "/dictionary/add"; - Word word = new Word("computer","an electronic machine that can store and arrange large amounts of information"); - clientResponse = postRequest(client,serverAddress+postPath,word); - System.out.println(clientResponse.toString()); + Word word = new Word("computer", "an electronic machine that can store and arrange large amounts of information"); + ResponseEntity postResponse = postRequest(serverAddress + postPath, word); + System.out.println(postResponse); - //GET #1 + // GET #1 String getPath = "/dictionary/get/computer"; - clientResponse = getRequest(client,serverAddress+getPath); - System.out.println(clientResponse.toString()); - String response = clientResponse.getEntity(String.class); - System.out.println(response); + ResponseEntity getResponse = getRequest(serverAddress + getPath); + System.out.println(getResponse); + System.out.println(getResponse.getBody()); - //PUT + // PUT String putPath = "/dictionary/modify"; - word = new Word("computer","little box with many wires and a huge number of bright lights"); - clientResponse = putRequest(client, serverAddress+putPath, word); - System.out.println(clientResponse.toString()); + word = new Word("computer", "little box with many wires and a huge number of bright lights"); + ResponseEntity putResponse = putRequest(serverAddress + putPath, word); + System.out.println(putResponse); - //GET #2 - clientResponse = getRequest(client,serverAddress+getPath); - System.out.println(clientResponse.toString()); - response = clientResponse.getEntity(String.class); - System.out.println(response); + // GET #2 + getResponse = getRequest(serverAddress + getPath); + System.out.println(getResponse); + System.out.println(getResponse.getBody()); - //DELETE #1 + // DELETE String deletePath = "/dictionary/delete/computer"; - clientResponse = deleteRequest(client, serverAddress+deletePath); - System.out.println(clientResponse); - - //GET #3 - clientResponse = getRequest(client,serverAddress+getPath); - System.out.println(clientResponse.toString()); - response = clientResponse.getEntity(String.class); - System.out.println(response); + ResponseEntity deleteResponse = deleteRequest(serverAddress + deletePath); + System.out.println(deleteResponse); + // GET #3 + getResponse = getRequest(serverAddress + getPath); + System.out.println(getResponse); + System.out.println(getResponse.getBody()); } - public static ClientResponse postRequest(Client client, String url, Word w){ - WebResource webResource = client.resource(url); - String input = new Gson().toJson(w); + public static ResponseEntity postRequest(String url, Word word) { try { - return webResource.type("application/json").post(ClientResponse.class, input); - } catch (ClientHandlerException e) { - System.out.println("Server not available"); - return null; + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + HttpEntity request = new HttpEntity<>(word, headers); + return restTemplate.postForEntity(url, request, String.class); + } catch (Exception e) { + System.out.println("Server not available: " + e.getMessage()); + return new ResponseEntity<>(HttpStatus.SERVICE_UNAVAILABLE); } } - public static ClientResponse getRequest(Client client, String url){ - WebResource webResource = client.resource(url); + public static ResponseEntity getRequest(String url) { try { - return webResource.get(ClientResponse.class); - } catch (ClientHandlerException e) { - System.out.println("Server not available"); - return null; + return restTemplate.getForEntity(url, String.class); + } catch (Exception e) { + System.out.println("Server not available: " + e.getMessage()); + return new ResponseEntity<>(HttpStatus.SERVICE_UNAVAILABLE); } } - public static ClientResponse putRequest(Client client, String url, Word word){ - WebResource webResource = client.resource(url); - String input = new Gson().toJson(word); + public static ResponseEntity putRequest(String url, Word word) { try { - return webResource.type("application/json").put(ClientResponse.class, input); - } catch (ClientHandlerException e) { - System.out.println("Server not available"); - return null; + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + HttpEntity request = new HttpEntity<>(word, headers); + restTemplate.put(url, request); + return new ResponseEntity<>("PUT successful", HttpStatus.OK); + } catch (Exception e) { + System.out.println("Server not available: " + e.getMessage()); + return new ResponseEntity<>(HttpStatus.SERVICE_UNAVAILABLE); } } - public static ClientResponse deleteRequest(Client client, String url){ - WebResource webResource = client.resource(url); - try{ - return webResource.delete(ClientResponse.class); - }catch(ClientHandlerException e){ - System.out.println("Server not available"); - return null; + public static ResponseEntity deleteRequest(String url) { + try { + restTemplate.delete(url); + return new ResponseEntity<>("DELETE successful", HttpStatus.OK); + } catch (Exception e) { + System.out.println("Server not available: " + e.getMessage()); + return new ResponseEntity<>(HttpStatus.SERVICE_UNAVAILABLE); } } } diff --git a/src/main/java/server/Application.java b/src/main/java/server/Application.java new file mode 100644 index 0000000000000000000000000000000000000000..ae0ca39416db1f5ada81394f58c25586f27614c1 --- /dev/null +++ b/src/main/java/server/Application.java @@ -0,0 +1,14 @@ +package server; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + System.out.println("Server running on http://localhost:8080"); + } +} + diff --git a/src/main/java/beans/Dictionary.java b/src/main/java/server/Dictionary.java similarity index 60% rename from src/main/java/beans/Dictionary.java rename to src/main/java/server/Dictionary.java index 7553e39214873cd43857e7ee1755c31b41f47e82..07ea5175951fccf419dd597446ee4b89c03eeeef 100644 --- a/src/main/java/beans/Dictionary.java +++ b/src/main/java/server/Dictionary.java @@ -1,34 +1,14 @@ -package beans; - -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; +package server; import java.util.HashMap; -@XmlRootElement -@XmlAccessorType(XmlAccessType.FIELD) -public class Dictionary { - @XmlElement(name = "dictionary") - private HashMap dictionary; +import org.springframework.stereotype.Service; - private static Dictionary instance; - public Dictionary(){ - dictionary = new HashMap(); - } - public Dictionary(HashMap dict){ - dictionary = dict; - } +@Service //Spring automatically makes this class a singleton bean +public class Dictionary { - //See the singleton pattern - public synchronized static Dictionary getInstance(){ - if(instance==null){ - instance = new Dictionary(); - } - return instance; - } + private final HashMap dictionary = new HashMap();; public int addWord(String w, String d){ synchronized (this) { @@ -66,5 +46,4 @@ public class Dictionary { public void deleteWord(String w){ dictionary.remove(w); } - } diff --git a/src/main/java/server/DictionaryController.java b/src/main/java/server/DictionaryController.java new file mode 100644 index 0000000000000000000000000000000000000000..0ca201c23311d59dd34557b4039d1a9aa6501f2d --- /dev/null +++ b/src/main/java/server/DictionaryController.java @@ -0,0 +1,54 @@ +package server; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/dictionary") +public class DictionaryController { + + private final Dictionary dictionary; + + @Autowired//tells Spring to automatically resolve and inject the required bean (object) into the class where it's used + public DictionaryController(Dictionary dictionary) { + this.dictionary = dictionary; + } + + + @PostMapping(value = "/add", consumes = {"application/json", "application/xml"}) + public ResponseEntity addWord(@RequestBody Word word) { + int result = dictionary.addWord(word.getWord().toLowerCase(), word.getDefinition()); + if (result == -1) { + return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).build(); + } else { + return ResponseEntity.ok().build(); + } + } + + @PutMapping(value = "/modify", consumes = {"application/json", "application/xml"}) + public ResponseEntity changeDefinition(@RequestBody Word word) { + int result = dictionary.changeWordDefinition(word.getWord().toLowerCase(), word.getDefinition()); + if (result == -1) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); + } else { + return ResponseEntity.ok().build(); + } + } + + @GetMapping(value = "/get/{word}", produces = "text/plain") + public ResponseEntity getDefinition(@PathVariable("word") String word) { + String definition = dictionary.viewDefinition(word.toLowerCase()); + if (definition == null) { + return ResponseEntity.ok("Word not in dictionary"); + } + return ResponseEntity.ok("Definition of " + word + ": " + definition); + } + + @DeleteMapping("/delete/{word}") + public ResponseEntity deleteWord(@PathVariable("word") String word) { + dictionary.deleteWord(word.toLowerCase()); + return ResponseEntity.ok().build(); + } +} diff --git a/src/main/java/beans/Word.java b/src/main/java/server/Word.java similarity index 84% rename from src/main/java/beans/Word.java rename to src/main/java/server/Word.java index 816f9fa621dda7ae0391e2c3f688b4002dcd1e6e..d64982953c87dda965d44ac4d7fb96a4acf6b726 100644 --- a/src/main/java/beans/Word.java +++ b/src/main/java/server/Word.java @@ -1,8 +1,5 @@ -package beans; +package server; -import javax.xml.bind.annotation.XmlRootElement; - -@XmlRootElement public class Word { private String word; private String definition; diff --git a/src/main/java/services/DictionaryRestService.java b/src/main/java/services/DictionaryRestService.java deleted file mode 100644 index 0170335cd3f8586a07d1b433193ec1cbc22436be..0000000000000000000000000000000000000000 --- a/src/main/java/services/DictionaryRestService.java +++ /dev/null @@ -1,59 +0,0 @@ -package services; - -import beans.Dictionary; -import beans.Word; - -import javax.ws.rs.*; -import javax.ws.rs.core.Response; - - -@Path("dictionary") -public class DictionaryRestService { - - @Path("add") - @POST - @Consumes({"application/json", "application/xml"}) - public Response addWord(Word w){ - Dictionary dict = Dictionary.getInstance(); - int ret = dict.addWord(w.getWord().toLowerCase(), w.getDefinition()); - if(ret == -1){ - return Response.status(Response.Status.NOT_ACCEPTABLE).build(); - }else{ - return Response.ok().build(); - } - } - - @Path("modify") - @PUT - @Consumes({"application/xml", "application/json"}) - public Response changeDefinition(Word w){ - Dictionary dict = Dictionary.getInstance(); - int ret = dict.changeWordDefinition(w.getWord().toLowerCase(), w.getDefinition()); - if(ret == -1){ - return Response.status(Response.Status.NOT_FOUND).build(); - }else{ - return Response.ok().build(); - } - } - - @Path("get/{word}") - @GET - @Produces({"text/plain"}) - public String getDefinition(@PathParam("word") String word){ - Dictionary dict = Dictionary.getInstance(); - String ret = dict.viewDefinition(word.toLowerCase()); - if(ret == null){ - return "Word not in dictionary"; - } - return "Definition of " + word + ": " + ret; - } - - @Path("delete/{word}") - @DELETE - public Response deleteWord(@PathParam("word") String word){ - Dictionary dict = Dictionary.getInstance(); - dict.deleteWord(word.toLowerCase()); - return Response.ok().build(); - } - -} diff --git a/src/main/java/services/StartServer.java b/src/main/java/services/StartServer.java deleted file mode 100644 index 6aab08714a58a01fb50ef43b521ea8c4254e8345..0000000000000000000000000000000000000000 --- a/src/main/java/services/StartServer.java +++ /dev/null @@ -1,27 +0,0 @@ -package services; - -import com.sun.jersey.api.container.httpserver.HttpServerFactory; -import com.sun.net.httpserver.HttpServer; - -import java.io.IOException; - -public class StartServer { - - private static final String HOST = "localhost"; - private static final int PORT = 1337; - - - public static void main(String[] args) throws IOException { - HttpServer server = HttpServerFactory.create("http://"+HOST+":"+PORT+"/"); - server.start(); - - System.out.println("Server running!"); - System.out.println("Server started on: http://"+HOST+":"+PORT); - - System.out.println("Hit return to stop..."); - System.in.read(); - System.out.println("Stopping server"); - server.stop(0); - System.out.println("Server stopped"); - } -}