From 6a8fedcec8315f19eee45cdd9a29d48cd8e68969 Mon Sep 17 00:00:00 2001 From: Anton Wiklund Date: Sat, 23 Feb 2019 23:40:34 +0100 Subject: [PATCH] Included Ict REST API Port in configuration. --- .../com/ictreport/ixi/ReportIxiContext.java | 28 ++++++++++++++----- .../ictreport/ixi/utils/IctRestCaller.java | 18 ++++++------ src/main/java/org/iota/ict/ixi/ReportIxi.java | 3 +- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/ictreport/ixi/ReportIxiContext.java b/src/main/java/com/ictreport/ixi/ReportIxiContext.java index 49ee6e8..06b575e 100644 --- a/src/main/java/com/ictreport/ixi/ReportIxiContext.java +++ b/src/main/java/com/ictreport/ixi/ReportIxiContext.java @@ -23,16 +23,18 @@ public class ReportIxiContext extends ConfigurableIxiContext { private final ReportIxi reportIxi; // Property names - private static final String ICT_REST_PASSWORD = "ictRestPassword"; - private static final String HOST = "host"; - private static final String REPORT_PORT = "reportPort"; - private static final String EXTERNAL_REPORT_PORT = "externalReportPort"; - private static final String NAME = "name"; - private static final String NEIGHBORS = "neighbors"; + private static final String ICT_REST_PORT = "Ict REST API Port"; + private static final String ICT_REST_PASSWORD = "Ict REST API Password"; + private static final String HOST = "Ict Host"; + private static final String REPORT_PORT = "Report.ixi Port"; + private static final String EXTERNAL_REPORT_PORT = "External Report.ixi Port"; + private static final String NAME = "Name"; + private static final String NEIGHBORS = "Neighbors"; private static final String NEIGHBOR_ADDRESS = "_address"; private static final String NEIGHBOR_REPORT_PORT = "reportPort"; // Property defaults + private static final int DEFAULT_ICT_REST_PORT = 2187; private static final String DEFAULT_ICT_REST_PASSWORD = "change_me_now"; private static final JSONObject DEFAULT_CONFIGURATION = new JSONObject(); private static final String DEFAULT_HOST = null; @@ -42,6 +44,7 @@ public class ReportIxiContext extends ConfigurableIxiContext { private static final JSONArray DEFAULT_NEIGHBORS = new JSONArray(); // Context properties + private int ictRestPort = DEFAULT_ICT_REST_PORT; private String ictRestPassword = DEFAULT_ICT_REST_PASSWORD; private String host = DEFAULT_HOST; private int reportPort = DEFAULT_REPORT_PORT; @@ -52,6 +55,7 @@ public class ReportIxiContext extends ConfigurableIxiContext { private String ictVersion = ""; static { + DEFAULT_CONFIGURATION.put(ICT_REST_PORT, DEFAULT_ICT_REST_PORT); DEFAULT_CONFIGURATION.put(ICT_REST_PASSWORD, DEFAULT_ICT_REST_PASSWORD); DEFAULT_CONFIGURATION.put(HOST, DEFAULT_HOST); DEFAULT_CONFIGURATION.put(REPORT_PORT, DEFAULT_REPORT_PORT); @@ -83,6 +87,7 @@ public JSONObject getConfiguration() { final JSONArray jsonNeighbors = new JSONArray(jsonNeighbor); JSONObject configuration = new JSONObject() + .put(ICT_REST_PORT, getIctRestPort()) .put(ICT_REST_PASSWORD, getIctRestPassword()) .put(REPORT_PORT, getReportPort()) .put(NAME, getName()) @@ -109,6 +114,7 @@ protected void validateConfiguration(final JSONObject newConfiguration) { @Override public void applyConfiguration() { + setIctRestPort(configuration.getInt(ICT_REST_PORT)); setIctRestPassword(configuration.getString(ICT_REST_PASSWORD)); setName(configuration.getString(NAME)); setReportPort(configuration.getInt(REPORT_PORT)); @@ -207,6 +213,14 @@ private void validateNeighbors(final JSONObject newConfiguration) { } } + public int getIctRestPort() { + return ictRestPort; + } + + public void setIctRestPort(int ictRestPort) { + this.ictRestPort = ictRestPort; + } + public String getIctRestPassword() { return ictRestPassword; } @@ -285,7 +299,7 @@ private IllegalPropertyException(String field, String cause) { } public void loadIctInfo() { - final JSONObject ictInfo = IctRestCaller.getInfo(getIctRestPassword()); + final JSONObject ictInfo = IctRestCaller.getInfo(getIctRestPort(), getIctRestPassword()); if (ictInfo != null) { String version = ictInfo.getString("version"); this.ictVersion = version; diff --git a/src/main/java/com/ictreport/ixi/utils/IctRestCaller.java b/src/main/java/com/ictreport/ixi/utils/IctRestCaller.java index 2b05870..c9f4f1c 100644 --- a/src/main/java/com/ictreport/ixi/utils/IctRestCaller.java +++ b/src/main/java/com/ictreport/ixi/utils/IctRestCaller.java @@ -25,26 +25,26 @@ public class IctRestCaller { private final static Logger LOGGER = LogManager.getLogger(IctRestCaller.class); - public static JSONObject getInfo(final String ictRestPassword) { - String json = call("getInfo", ictRestPassword); + public static JSONObject getInfo(final int ictRestPort, final String ictRestPassword) { + String json = call("getInfo", ictRestPort, ictRestPassword); if (json != null) { return new JSONObject(json); } return null; } - public static JSONObject getConfig(final String ictRestPassword) { + public static JSONObject getConfig(final int ictRestPort, final String ictRestPassword) { - String json = call("getConfig", ictRestPassword); + String json = call("getConfig", ictRestPort, ictRestPassword); if (json != null) { return new JSONObject(json); } return null; } - public static JSONArray getNeighbors(final String ictRestPassword) { + public static JSONArray getNeighbors(final int ictRestPort, final String ictRestPassword) { - String json = call("getNeighbors", ictRestPassword); + String json = call("getNeighbors", ictRestPort, ictRestPassword); if (json != null) { JSONObject rootObject = new JSONObject(json); return rootObject.getJSONArray("neighbors"); @@ -52,10 +52,10 @@ public static JSONArray getNeighbors(final String ictRestPassword) { return null; } - private static String call(final String route, final String ictRestPassword) { + private static String call(final String route, final int ictRestPort, final String ictRestPassword) { try { final CloseableHttpClient httpclient = HttpClients.createDefault(); - HttpPost httppost = new HttpPost("http://localhost:2187/" + route); + HttpPost httppost = new HttpPost("http://localhost:" + ictRestPort + "/" + route); // Request parameters and other properties. List params = new ArrayList<>(); @@ -83,7 +83,7 @@ private static String call(final String route, final String ictRestPassword) { } } } catch (IOException e) { - e.printStackTrace(); + LOGGER.warn("Failed to call Ict REST API. Check Report.ixi configuration, especially 'Ict REST API port' and 'Ict REST API password'."); } return null; } diff --git a/src/main/java/org/iota/ict/ixi/ReportIxi.java b/src/main/java/org/iota/ict/ixi/ReportIxi.java index 79363c6..1e07d0c 100644 --- a/src/main/java/org/iota/ict/ixi/ReportIxi.java +++ b/src/main/java/org/iota/ict/ixi/ReportIxi.java @@ -135,8 +135,9 @@ private void createDirectoryIfNotExists() { public void syncNeighborsFromIctRest() { final List addressesAndStatsToSync = new LinkedList<>(); + final int ictRestPort = getReportIxiContext().getIctRestPort(); final String ictRestPassword = getReportIxiContext().getIctRestPassword(); - final JSONArray ictNeighbors = IctRestCaller.getNeighbors(ictRestPassword); + final JSONArray ictNeighbors = IctRestCaller.getNeighbors(ictRestPort, ictRestPassword); for (int i=0; ictNeighbors != null && i