diff options
author | Magdalena Grodzińska <mag.grodzinska@gmail.com> | 2019-11-20 16:19:25 +0100 |
---|---|---|
committer | Magdalena Grodzińska <mag.grodzinska@gmail.com> | 2019-11-20 16:19:25 +0100 |
commit | 906996114a16ef5d7576de7f48ea22fd1f680b97 (patch) | |
tree | 2539936d44e174f00974973262d433f35e0ec741 /src/main | |
parent | f7bc805b5e63269107ba87e8d2d3007737cb379e (diff) |
Add contact submission backend
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java | 44 | ||||
-rw-r--r-- | src/main/resources/pl/edu/mimuw/cloudatlas/client/templates/contactsForm.html | 4 |
2 files changed, 44 insertions, 4 deletions
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java b/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java index bdc71df..31d1c06 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java @@ -1,12 +1,21 @@ package pl.edu.mimuw.cloudatlas.client; +import com.google.gson.Gson; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import org.springframework.stereotype.Controller; import pl.edu.mimuw.cloudatlas.api.Api; +import pl.edu.mimuw.cloudatlas.model.PathName; +import pl.edu.mimuw.cloudatlas.model.ValueContact; +import java.net.InetAddress; +import java.net.UnknownHostException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; /* should enable reading attribute values stored by the agent @@ -72,12 +81,43 @@ public class ClientController { @GetMapping("/contacts") public String contactPage(Model model) { + model.addAttribute("contactsObject" , new ContactsString()); return "contactsForm"; } @PostMapping("/contacts") - public String contactPage(@ModelAttribute String contactsObject, Model model) { - model.addAttribute("homeMessage", "Contact list submitted successfully"); + public String contactPage(@ModelAttribute ContactsString contactsObject, Model model) { + boolean success = true; + Gson gson = new Gson(); + Map<String, ArrayList> contactStrings = gson.fromJson(contactsObject.getString(), Map.class); + Set<ValueContact> contactObjects = new HashSet<ValueContact>(); + ArrayList<Double> cAddr; + byte[] inetArray = new byte[4]; + + try { + for (Map.Entry<String, ArrayList> cursor : contactStrings.entrySet()) { + cAddr = cursor.getValue(); // gson always reads numerical values as doubles + for (int i = 0; i < 4; i++) { + inetArray[i] = (byte) cAddr.get(i).doubleValue(); + } + contactObjects.add(new ValueContact( + new PathName(cursor.getKey()), + InetAddress.getByAddress(inetArray)) + ); + } + this.api.setFallbackContacts(contactObjects); + } catch (Exception e) { + success = false; + System.err.println("Client exception:"); + e.printStackTrace(); + } + + if (success) { + model.addAttribute("homeMessage", "Contact list submitted successfully"); + } else { + model.addAttribute("homeMessage", "Contact list submission failed"); + } + return "home"; } diff --git a/src/main/resources/pl/edu/mimuw/cloudatlas/client/templates/contactsForm.html b/src/main/resources/pl/edu/mimuw/cloudatlas/client/templates/contactsForm.html index 027f831..40af9b4 100644 --- a/src/main/resources/pl/edu/mimuw/cloudatlas/client/templates/contactsForm.html +++ b/src/main/resources/pl/edu/mimuw/cloudatlas/client/templates/contactsForm.html @@ -13,8 +13,8 @@ <div id="queryForm"> <form action="#" th:action="@{/contacts}" th:object="${contactsObject}" method="post"> <div class="form-group"> - <label for="Textarea1">Enter contacts as a Json list</label> - <textarea class="form-control" id="Textarea1" rows="3"></textarea> + <label for="Textarea1">Enter contacts as a Json map with string key and integer list as value</label> + <textarea class="form-control" id="Textarea1" rows="3" th:field="*{string}"></textarea> </div> <button type="submit" class="btn btn-primary">Submit</button> </form> |