Skip to content
This repository has been archived by the owner on Oct 1, 2020. It is now read-only.

Commit

Permalink
Included Ict REST API Port in configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
ixuz committed Feb 23, 2019
1 parent 9b2a948 commit 6a8fedc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
28 changes: 21 additions & 7 deletions src/main/java/com/ictreport/ixi/ReportIxiContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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())
Expand All @@ -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));
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/com/ictreport/ixi/utils/IctRestCaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,37 @@ 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");
}
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<NameValuePair> params = new ArrayList<>();
Expand Down Expand Up @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/iota/ict/ixi/ReportIxi.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ private void createDirectoryIfNotExists() {

public void syncNeighborsFromIctRest() {
final List<AddressAndStats> 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<ictNeighbors.length(); i++) {
final JSONObject ictNeighbor = (JSONObject)ictNeighbors.get(i);
Expand Down

0 comments on commit 6a8fedc

Please sign in to comment.