updated webutils to convert objects to json strings, avoid repeating the process for every use case

This commit is contained in:
Blake J. Harnden 2018-09-13 13:30:45 -07:00
parent 3dc9586817
commit c5f62a106f
5 changed files with 34 additions and 24 deletions

View file

@ -5,7 +5,6 @@ import com.core.client.ICoreClient;
import com.core.data.*;
import com.core.graph.NetworkGraph;
import com.core.ui.Toast;
import com.core.utils.JsonUtils;
import com.core.utils.WebUtils;
import lombok.Data;
import org.apache.logging.log4j.LogManager;
@ -168,7 +167,7 @@ public class CoreRestClient implements ICoreClient {
String url = getUrl(String.format("sessions/%s/state", sessionId));
Map<String, Integer> data = new HashMap<>();
data.put("state", state.getValue());
boolean result = WebUtils.putJson(url, JsonUtils.toString(data));
boolean result = WebUtils.putJson(url, data);
if (result) {
sessionState = state;
@ -190,7 +189,7 @@ public class CoreRestClient implements ICoreClient {
@Override
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));
return WebUtils.putJson(url, JsonUtils.toString(service));
return WebUtils.putJson(url, service);
}
@Override
@ -206,7 +205,7 @@ public class CoreRestClient implements ICoreClient {
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(),
serviceName));
return WebUtils.putJson(url, JsonUtils.toString(serviceFile));
return WebUtils.putJson(url, serviceFile);
}
@Override
@ -238,7 +237,7 @@ public class CoreRestClient implements ICoreClient {
SetEmaneConfig setEmaneConfig = new SetEmaneConfig();
setEmaneConfig.setNode(node.getId());
setEmaneConfig.setValues(options);
return WebUtils.putJson(url, JsonUtils.toString(setEmaneConfig));
return WebUtils.putJson(url, setEmaneConfig);
}
@Override
@ -248,7 +247,7 @@ public class CoreRestClient implements ICoreClient {
setEmaneModelConfig.setNode(node.getId());
setEmaneModelConfig.setName(model);
setEmaneModelConfig.setValues(options);
return WebUtils.putJson(url, JsonUtils.toString(setEmaneModelConfig));
return WebUtils.putJson(url, setEmaneModelConfig);
}
private void updateController() {
@ -284,14 +283,13 @@ public class CoreRestClient implements ICoreClient {
@Override
public boolean setSessionConfig(SetConfig config) throws IOException {
String url = getUrl(String.format("sessions/%s/options", sessionId));
return WebUtils.putJson(url, JsonUtils.toString(config));
return WebUtils.putJson(url, config);
}
@Override
public boolean createNode(CoreNode node) throws IOException {
String url = getUrl(String.format("sessions/%s/nodes", sessionId));
String data = JsonUtils.toString(node);
return WebUtils.postJson(url, data);
return WebUtils.postJson(url, node);
}
@Override
@ -303,15 +301,13 @@ public class CoreRestClient implements ICoreClient {
@Override
public boolean createLink(CoreLink link) throws IOException {
String url = getUrl(String.format("sessions/%s/links", sessionId));
String data = JsonUtils.toString(link);
return WebUtils.postJson(url, data);
return WebUtils.postJson(url, link);
}
@Override
public boolean createHook(Hook hook) throws IOException {
String url = getUrl(String.format("sessions/%s/hooks", sessionId));
String data = JsonUtils.toString(hook);
return WebUtils.postJson(url, data);
return WebUtils.postJson(url, hook);
}
@Override
@ -329,8 +325,7 @@ public class CoreRestClient implements ICoreClient {
@Override
public boolean setWlanConfig(CoreNode node, WlanConfig config) throws IOException {
String url = getUrl(String.format("sessions/%s/nodes/%s/wlan", sessionId, node.getId()));
String jsonData = JsonUtils.toString(config);
return WebUtils.putJson(url, jsonData);
return WebUtils.putJson(url, config);
}
@Override
@ -348,8 +343,7 @@ public class CoreRestClient implements ICoreClient {
String url = getUrl(String.format("sessions/%s/nodes/%s/mobility", sessionId, node.getId()));
config.setFile(config.getScriptFile().getName());
String data = JsonUtils.toString(config);
return WebUtils.postJson(url, data);
return WebUtils.postJson(url, config);
}
@Override

View file

@ -3,6 +3,7 @@ package com.core.ui;
import com.core.Controller;
import com.core.data.CoreNode;
import com.core.data.MobilityConfig;
import com.core.utils.IconUtils;
import com.jfoenix.controls.JFXButton;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
@ -15,6 +16,8 @@ import java.io.IOException;
public class MobilityPlayer extends HBox {
private static final Logger logger = LogManager.getLogger();
private static final int ICON_SIZE = 20;
private static final String ICON_FILL = "white";
@FXML
private Label label;
@ -44,8 +47,11 @@ public class MobilityPlayer extends HBox {
throw new RuntimeException(ex);
}
playButton.setGraphic(IconUtils.get("play_arrow", ICON_SIZE, ICON_FILL));
playButton.setOnAction(event -> action("start"));
pauseButton.setGraphic(IconUtils.get("pause", ICON_SIZE, ICON_FILL));
pauseButton.setOnAction(event -> action("pause"));
stopButton.setGraphic(IconUtils.get("stop", ICON_SIZE, ICON_FILL));
stopButton.setOnAction(event -> action("stop"));
}

View file

@ -3,6 +3,7 @@ package com.core.utils;
import com.jfoenix.svg.SVGGlyph;
import com.jfoenix.svg.SVGGlyphLoader;
import edu.uci.ics.jung.visualization.LayeredIcon;
import javafx.scene.paint.Paint;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -37,4 +38,13 @@ public final class IconUtils {
}
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;
}
}

View file

@ -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);
RequestBody body = RequestBody.create(JSON, json);
RequestBody body = RequestBody.create(JSON, JsonUtils.toString(json));
Request request = new Request.Builder()
.url(url)
.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);
RequestBody body = RequestBody.create(JSON, json);
RequestBody body = RequestBody.create(JSON, JsonUtils.toString(json));
Request request = new Request.Builder()
.url(url)
.put(body)

View file

@ -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">
<children>
<Label fx:id="label" maxHeight="1.7976931348623157E308" text="Label" />
<JFXButton fx:id="playButton" styleClass="core-button" text="Play" />
<JFXButton fx:id="pauseButton" styleClass="core-button" text="Pause" />
<JFXButton fx:id="stopButton" styleClass="core-button" text="Stop" />
<JFXButton fx:id="playButton" contentDisplay="GRAPHIC_ONLY" styleClass="core-button" />
<JFXButton fx:id="pauseButton" contentDisplay="GRAPHIC_ONLY" styleClass="core-button" />
<JFXButton fx:id="stopButton" contentDisplay="GRAPHIC_ONLY" styleClass="core-button" />
</children>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />