use of com.exalttech.trex.remote.models.common.RPCError in project trex-stateless-gui by cisco-system-traffic-generator.
the class ConnectionManager method sendRequest.
public String sendRequest(String cmd, String parameters) {
try {
String param = parameters;
ObjectMapper mapper = new ObjectMapper();
if (parameters != null) {
String apiHParam = "\"api_h\": \"" + apiH + "\"";
if ("".equals(parameters)) {
param = "{\"api_h\": \"" + apiH + "\"}";
} else if ("api_sync".equals(cmd)) {
param = "{" + parameters + "}";
} else {
param = "{" + apiHParam + " , " + parameters + "}";
}
} else {
param = "{}";
}
String request = "{ \"id\" : \"aggogxls\", \"jsonrpc\" : \"2.0\", \"method\" : \"" + cmd + "\", \"params\" :" + param + " }";
LOG.trace("Sending request \n" + Util.toPrettyFormat(request));
logProperty.setValue("Sending request " + Util.toPrettyFormat(request));
byte[] reply = getServerRPCResponse(request);
if (reply != null) {
String serversResponse = new String(reply, "UTF-8");
LOG.trace("Received Server response \n" + Util.toPrettyFormat(serversResponse));
logProperty.setValue("Received Server response " + Util.toPrettyFormat(serversResponse));
if (serversResponse.contains("\"error\"")) {
try {
String rpcResponse = Util.removeFirstBrackets(serversResponse);
RPCError rpcError = mapper.readValue(rpcResponse, RPCError.class);
LOG.error(rpcError.getError().getSpecificOrMessage());
LogsController.getInstance().appendText(LogType.ERROR, rpcError.getError().getSpecificOrMessage());
} catch (IOException ex) {
LOG.warn("Error parsing response", ex);
}
}
return serversResponse;
}
} catch (UnsupportedEncodingException | SizeLimitExceededException ex) {
LOG.error("Error while sending request", ex);
}
return null;
}
use of com.exalttech.trex.remote.models.common.RPCError in project trex-stateless-gui by cisco-system-traffic-generator.
the class ConnectionManager method handleResponse.
private String handleResponse(byte[] serverResponse, boolean writeToLog) throws UnsupportedEncodingException, IncorrectRPCMethodException, InvalidRPCResponseException {
if (serverResponse != null) {
String rpcResponse = new String(serverResponse, "UTF-8");
LOG.trace("Received Server response \n" + Util.toPrettyFormat(rpcResponse));
if (writeToLog) {
logProperty.setValue("Received Server response " + Util.toPrettyFormat(rpcResponse));
}
if (rpcResponse.contains("\"error\"")) {
try {
rpcResponse = Util.removeFirstBrackets(rpcResponse);
RPCError rpcError = new ObjectMapper().readValue(rpcResponse, RPCError.class);
String err = rpcError.getError().getSpecificOrMessage();
LOG.error(err);
LogsController.getInstance().appendText(LogType.ERROR, err);
throw new IncorrectRPCMethodException(err + "\n " + Util.toPrettyFormat(rpcResponse));
} catch (IOException ex) {
LOG.warn("Error parsing response", ex);
}
}
return rpcResponse;
} else {
throw new InvalidRPCResponseException();
}
}
Aggregations