Search in sources :

Example 1 with RPCError

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;
}
Also used : SizeLimitExceededException(javax.naming.SizeLimitExceededException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RPCError(com.exalttech.trex.remote.models.common.RPCError) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with RPCError

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();
    }
}
Also used : RPCError(com.exalttech.trex.remote.models.common.RPCError) IOException(java.io.IOException) IncorrectRPCMethodException(com.exalttech.trex.remote.exceptions.IncorrectRPCMethodException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) InvalidRPCResponseException(com.exalttech.trex.remote.exceptions.InvalidRPCResponseException)

Aggregations

RPCError (com.exalttech.trex.remote.models.common.RPCError)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 IOException (java.io.IOException)2 IncorrectRPCMethodException (com.exalttech.trex.remote.exceptions.IncorrectRPCMethodException)1 InvalidRPCResponseException (com.exalttech.trex.remote.exceptions.InvalidRPCResponseException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 SizeLimitExceededException (javax.naming.SizeLimitExceededException)1