updated webutils to convert objects to json strings, avoid repeating the process for every use case
This commit is contained in:
parent
3dc9586817
commit
c5f62a106f
5 changed files with 34 additions and 24 deletions
|
@ -5,7 +5,6 @@ import com.core.client.ICoreClient;
|
||||||
import com.core.data.*;
|
import com.core.data.*;
|
||||||
import com.core.graph.NetworkGraph;
|
import com.core.graph.NetworkGraph;
|
||||||
import com.core.ui.Toast;
|
import com.core.ui.Toast;
|
||||||
import com.core.utils.JsonUtils;
|
|
||||||
import com.core.utils.WebUtils;
|
import com.core.utils.WebUtils;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
@ -168,7 +167,7 @@ public class CoreRestClient implements ICoreClient {
|
||||||
String url = getUrl(String.format("sessions/%s/state", sessionId));
|
String url = getUrl(String.format("sessions/%s/state", sessionId));
|
||||||
Map<String, Integer> data = new HashMap<>();
|
Map<String, Integer> data = new HashMap<>();
|
||||||
data.put("state", state.getValue());
|
data.put("state", state.getValue());
|
||||||
boolean result = WebUtils.putJson(url, JsonUtils.toString(data));
|
boolean result = WebUtils.putJson(url, data);
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
sessionState = state;
|
sessionState = state;
|
||||||
|
@ -190,7 +189,7 @@ public class CoreRestClient implements ICoreClient {
|
||||||
@Override
|
@Override
|
||||||
public boolean setService(CoreNode node, String serviceName, CoreService service) throws IOException {
|
public boolean setService(CoreNode node, String serviceName, CoreService service) throws IOException {
|
||||||
String url = getUrl(String.format("sessions/%s/nodes/%s/services/%s", sessionId, node.getId(), serviceName));
|
String url = getUrl(String.format("sessions/%s/nodes/%s/services/%s", sessionId, node.getId(), serviceName));
|
||||||
return WebUtils.putJson(url, JsonUtils.toString(service));
|
return WebUtils.putJson(url, service);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -206,7 +205,7 @@ public class CoreRestClient implements ICoreClient {
|
||||||
public boolean setServiceFile(CoreNode node, String serviceName, ServiceFile serviceFile) throws IOException {
|
public boolean setServiceFile(CoreNode node, String serviceName, ServiceFile serviceFile) throws IOException {
|
||||||
String url = getUrl(String.format("sessions/%s/nodes/%s/services/%s/file", sessionId, node.getId(),
|
String url = getUrl(String.format("sessions/%s/nodes/%s/services/%s/file", sessionId, node.getId(),
|
||||||
serviceName));
|
serviceName));
|
||||||
return WebUtils.putJson(url, JsonUtils.toString(serviceFile));
|
return WebUtils.putJson(url, serviceFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -238,7 +237,7 @@ public class CoreRestClient implements ICoreClient {
|
||||||
SetEmaneConfig setEmaneConfig = new SetEmaneConfig();
|
SetEmaneConfig setEmaneConfig = new SetEmaneConfig();
|
||||||
setEmaneConfig.setNode(node.getId());
|
setEmaneConfig.setNode(node.getId());
|
||||||
setEmaneConfig.setValues(options);
|
setEmaneConfig.setValues(options);
|
||||||
return WebUtils.putJson(url, JsonUtils.toString(setEmaneConfig));
|
return WebUtils.putJson(url, setEmaneConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -248,7 +247,7 @@ public class CoreRestClient implements ICoreClient {
|
||||||
setEmaneModelConfig.setNode(node.getId());
|
setEmaneModelConfig.setNode(node.getId());
|
||||||
setEmaneModelConfig.setName(model);
|
setEmaneModelConfig.setName(model);
|
||||||
setEmaneModelConfig.setValues(options);
|
setEmaneModelConfig.setValues(options);
|
||||||
return WebUtils.putJson(url, JsonUtils.toString(setEmaneModelConfig));
|
return WebUtils.putJson(url, setEmaneModelConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateController() {
|
private void updateController() {
|
||||||
|
@ -284,14 +283,13 @@ public class CoreRestClient implements ICoreClient {
|
||||||
@Override
|
@Override
|
||||||
public boolean setSessionConfig(SetConfig config) throws IOException {
|
public boolean setSessionConfig(SetConfig config) throws IOException {
|
||||||
String url = getUrl(String.format("sessions/%s/options", sessionId));
|
String url = getUrl(String.format("sessions/%s/options", sessionId));
|
||||||
return WebUtils.putJson(url, JsonUtils.toString(config));
|
return WebUtils.putJson(url, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean createNode(CoreNode node) throws IOException {
|
public boolean createNode(CoreNode node) throws IOException {
|
||||||
String url = getUrl(String.format("sessions/%s/nodes", sessionId));
|
String url = getUrl(String.format("sessions/%s/nodes", sessionId));
|
||||||
String data = JsonUtils.toString(node);
|
return WebUtils.postJson(url, node);
|
||||||
return WebUtils.postJson(url, data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -303,15 +301,13 @@ public class CoreRestClient implements ICoreClient {
|
||||||
@Override
|
@Override
|
||||||
public boolean createLink(CoreLink link) throws IOException {
|
public boolean createLink(CoreLink link) throws IOException {
|
||||||
String url = getUrl(String.format("sessions/%s/links", sessionId));
|
String url = getUrl(String.format("sessions/%s/links", sessionId));
|
||||||
String data = JsonUtils.toString(link);
|
return WebUtils.postJson(url, link);
|
||||||
return WebUtils.postJson(url, data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean createHook(Hook hook) throws IOException {
|
public boolean createHook(Hook hook) throws IOException {
|
||||||
String url = getUrl(String.format("sessions/%s/hooks", sessionId));
|
String url = getUrl(String.format("sessions/%s/hooks", sessionId));
|
||||||
String data = JsonUtils.toString(hook);
|
return WebUtils.postJson(url, hook);
|
||||||
return WebUtils.postJson(url, data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -329,8 +325,7 @@ public class CoreRestClient implements ICoreClient {
|
||||||
@Override
|
@Override
|
||||||
public boolean setWlanConfig(CoreNode node, WlanConfig config) throws IOException {
|
public boolean setWlanConfig(CoreNode node, WlanConfig config) throws IOException {
|
||||||
String url = getUrl(String.format("sessions/%s/nodes/%s/wlan", sessionId, node.getId()));
|
String url = getUrl(String.format("sessions/%s/nodes/%s/wlan", sessionId, node.getId()));
|
||||||
String jsonData = JsonUtils.toString(config);
|
return WebUtils.putJson(url, config);
|
||||||
return WebUtils.putJson(url, jsonData);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -348,8 +343,7 @@ public class CoreRestClient implements ICoreClient {
|
||||||
|
|
||||||
String url = getUrl(String.format("sessions/%s/nodes/%s/mobility", sessionId, node.getId()));
|
String url = getUrl(String.format("sessions/%s/nodes/%s/mobility", sessionId, node.getId()));
|
||||||
config.setFile(config.getScriptFile().getName());
|
config.setFile(config.getScriptFile().getName());
|
||||||
String data = JsonUtils.toString(config);
|
return WebUtils.postJson(url, config);
|
||||||
return WebUtils.postJson(url, data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -3,6 +3,7 @@ package com.core.ui;
|
||||||
import com.core.Controller;
|
import com.core.Controller;
|
||||||
import com.core.data.CoreNode;
|
import com.core.data.CoreNode;
|
||||||
import com.core.data.MobilityConfig;
|
import com.core.data.MobilityConfig;
|
||||||
|
import com.core.utils.IconUtils;
|
||||||
import com.jfoenix.controls.JFXButton;
|
import com.jfoenix.controls.JFXButton;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
import javafx.fxml.FXMLLoader;
|
import javafx.fxml.FXMLLoader;
|
||||||
|
@ -15,6 +16,8 @@ import java.io.IOException;
|
||||||
|
|
||||||
public class MobilityPlayer extends HBox {
|
public class MobilityPlayer extends HBox {
|
||||||
private static final Logger logger = LogManager.getLogger();
|
private static final Logger logger = LogManager.getLogger();
|
||||||
|
private static final int ICON_SIZE = 20;
|
||||||
|
private static final String ICON_FILL = "white";
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private Label label;
|
private Label label;
|
||||||
|
@ -44,8 +47,11 @@ public class MobilityPlayer extends HBox {
|
||||||
throw new RuntimeException(ex);
|
throw new RuntimeException(ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
playButton.setGraphic(IconUtils.get("play_arrow", ICON_SIZE, ICON_FILL));
|
||||||
playButton.setOnAction(event -> action("start"));
|
playButton.setOnAction(event -> action("start"));
|
||||||
|
pauseButton.setGraphic(IconUtils.get("pause", ICON_SIZE, ICON_FILL));
|
||||||
pauseButton.setOnAction(event -> action("pause"));
|
pauseButton.setOnAction(event -> action("pause"));
|
||||||
|
stopButton.setGraphic(IconUtils.get("stop", ICON_SIZE, ICON_FILL));
|
||||||
stopButton.setOnAction(event -> action("stop"));
|
stopButton.setOnAction(event -> action("stop"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ package com.core.utils;
|
||||||
import com.jfoenix.svg.SVGGlyph;
|
import com.jfoenix.svg.SVGGlyph;
|
||||||
import com.jfoenix.svg.SVGGlyphLoader;
|
import com.jfoenix.svg.SVGGlyphLoader;
|
||||||
import edu.uci.ics.jung.visualization.LayeredIcon;
|
import edu.uci.ics.jung.visualization.LayeredIcon;
|
||||||
|
import javafx.scene.paint.Paint;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
|
@ -37,4 +38,13 @@ public final class IconUtils {
|
||||||
}
|
}
|
||||||
return svg;
|
return svg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static SVGGlyph get(String name, int size, String paint) {
|
||||||
|
SVGGlyph svg = get(name);
|
||||||
|
if (svg != null) {
|
||||||
|
svg.setSize(size);
|
||||||
|
svg.setFill(Paint.valueOf(paint));
|
||||||
|
}
|
||||||
|
return svg;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -103,9 +103,9 @@ public final class WebUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean postJson(String url, String json) throws IOException {
|
public static boolean postJson(String url, Object json) throws IOException {
|
||||||
logger.debug("post json: {} - {}", url, json);
|
logger.debug("post json: {} - {}", url, json);
|
||||||
RequestBody body = RequestBody.create(JSON, json);
|
RequestBody body = RequestBody.create(JSON, JsonUtils.toString(json));
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
.url(url)
|
.url(url)
|
||||||
.post(body)
|
.post(body)
|
||||||
|
@ -127,9 +127,9 @@ public final class WebUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean putJson(String url, String json) throws IOException {
|
public static boolean putJson(String url, Object json) throws IOException {
|
||||||
logger.debug("put json: {} - {}", url, json);
|
logger.debug("put json: {} - {}", url, json);
|
||||||
RequestBody body = RequestBody.create(JSON, json);
|
RequestBody body = RequestBody.create(JSON, JsonUtils.toString(json));
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
.url(url)
|
.url(url)
|
||||||
.put(body)
|
.put(body)
|
||||||
|
|
|
@ -8,9 +8,9 @@
|
||||||
<fx:root alignment="CENTER" maxHeight="-Infinity" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" spacing="10.0" type="HBox" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
|
<fx:root alignment="CENTER" maxHeight="-Infinity" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" spacing="10.0" type="HBox" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
|
||||||
<children>
|
<children>
|
||||||
<Label fx:id="label" maxHeight="1.7976931348623157E308" text="Label" />
|
<Label fx:id="label" maxHeight="1.7976931348623157E308" text="Label" />
|
||||||
<JFXButton fx:id="playButton" styleClass="core-button" text="Play" />
|
<JFXButton fx:id="playButton" contentDisplay="GRAPHIC_ONLY" styleClass="core-button" />
|
||||||
<JFXButton fx:id="pauseButton" styleClass="core-button" text="Pause" />
|
<JFXButton fx:id="pauseButton" contentDisplay="GRAPHIC_ONLY" styleClass="core-button" />
|
||||||
<JFXButton fx:id="stopButton" styleClass="core-button" text="Stop" />
|
<JFXButton fx:id="stopButton" contentDisplay="GRAPHIC_ONLY" styleClass="core-button" />
|
||||||
</children>
|
</children>
|
||||||
<padding>
|
<padding>
|
||||||
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
|
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
|
||||||
|
|
Loading…
Add table
Reference in a new issue