From 75208adcf60b4b3a48b96be70ff9352683a37075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Thu, 21 Nov 2019 10:00:50 +0100 Subject: Refactor contact value parsing --- .../mimuw/cloudatlas/client/ClientController.java | 36 +++++++++++++--------- 1 file changed, 22 insertions(+), 14 deletions(-) (limited to 'src/main/java/pl/edu/mimuw/cloudatlas') 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 27c7ad4..922c357 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java @@ -85,26 +85,34 @@ public class ClientController { return "contactsForm"; } - @PostMapping("/contacts") - public String contactPage(@ModelAttribute ContactsString contactsObject, Model model) { - boolean success = true; + private Set parseContactsString(ContactsString contactsInput) throws Exception { Gson gson = new Gson(); - Map contactStrings = gson.fromJson(contactsObject.getString(), Map.class); + Map contactStrings = gson.fromJson(contactsInput.getString(), Map.class); Set contactObjects = new HashSet(); ArrayList cAddr; byte[] inetArray = new byte[4]; - try { - for (Map.Entry 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)) - ); + for (Map.Entry 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)) + ); + } + + return contactObjects; + } + + @PostMapping("/contacts") + public String contactPage(@ModelAttribute ContactsString contactsObject, Model model) { + boolean success = true; + Set contactObjects; + + try { + contactObjects = parseContactsString(contactsObject); this.api.setFallbackContacts(contactObjects); } catch (Exception e) { success = false; -- cgit v1.2.3