Search in sources :

Example 1 with CallActionCommand

use of com.canoo.dp.impl.remoting.commands.CallActionCommand in project dolphin-platform by canoo.

the class ControllerProxyImpl method invoke.

@Override
public CompletableFuture<Void> invoke(final String actionName, final Param... params) {
    if (destroyed) {
        throw new IllegalStateException("The controller was already destroyed");
    }
    final ClientControllerActionCallBean bean = platformBeanRepository.createControllerActionCallBean(controllerId, actionName, params);
    final CallActionCommand callActionCommand = new CallActionCommand();
    callActionCommand.setControllerId(controllerId);
    callActionCommand.setActionName(actionName);
    if (params != null) {
        for (Param param : params) {
            Object value = param.getValue();
            if (value == null) {
                callActionCommand.addParam(param.getName(), null);
            } else {
                try {
                    callActionCommand.addParam(param.getName(), converters.getConverter(value.getClass()).convertToDolphin(value));
                } catch (ValueConverterException e) {
                    throw new MappingException("Error in value conversion of param '" + param.getName() + "' for action '" + actionName + "'", e);
                }
            }
        }
    }
    final CompletableFuture<Void> result = new CompletableFuture<>();
    clientConnector.send(callActionCommand, () -> {
        if (bean.isError()) {
            result.completeExceptionally(new ControllerActionException("Error on calling action on the server. Please check the server log."));
        } else {
            result.complete(null);
        }
        bean.unregister();
    });
    return result;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ControllerActionException(com.canoo.platform.remoting.client.ControllerActionException) CallActionCommand(com.canoo.dp.impl.remoting.commands.CallActionCommand) Param(com.canoo.platform.remoting.client.Param) ValueConverterException(com.canoo.platform.remoting.spi.converter.ValueConverterException) MappingException(com.canoo.dp.impl.remoting.MappingException)

Example 2 with CallActionCommand

use of com.canoo.dp.impl.remoting.commands.CallActionCommand in project dolphin-platform by canoo.

the class TestOptimizedJsonCodec method shouldEncodeCallActionCommand.

@Test
public void shouldEncodeCallActionCommand() {
    final CallActionCommand command = new CallActionCommand();
    command.setControllerId("4711");
    command.setActionName("action");
    final String actual = OptimizedJsonCodec.getInstance().encode(Collections.<Command>singletonList(command));
    assertThat(actual, is("[{\"c_id\":\"4711\",\"n\":\"action\",\"p\":[],\"id\":\"CallAction\"}]"));
}
Also used : CallActionCommand(com.canoo.dp.impl.remoting.commands.CallActionCommand) Test(org.testng.annotations.Test)

Example 3 with CallActionCommand

use of com.canoo.dp.impl.remoting.commands.CallActionCommand in project dolphin-platform by canoo.

the class CallActionCommandEncoder method decode.

@Override
public CallActionCommand decode(final JsonObject jsonObject) {
    Assert.requireNonNull(jsonObject, "jsonObject");
    try {
        final CallActionCommand command = new CallActionCommand();
        command.setControllerId(getStringElement(jsonObject, CONTROLLER_ID));
        command.setActionName(getStringElement(jsonObject, NAME));
        final JsonArray jsonArray = jsonObject.getAsJsonArray(PARAMS);
        if (jsonArray != null) {
            for (final JsonElement jsonElement : jsonArray) {
                final JsonObject paramObject = jsonElement.getAsJsonObject();
                command.addParam(getStringElement(paramObject, NAME), ValueEncoder.decodeValue(paramObject.get(VALUE)));
            }
        }
        return command;
    } catch (final Exception ex) {
        throw new JsonParseException("Illegal JSON detected", ex);
    }
}
Also used : JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) CallActionCommand(com.canoo.dp.impl.remoting.commands.CallActionCommand) JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException) JsonParseException(com.google.gson.JsonParseException)

Example 4 with CallActionCommand

use of com.canoo.dp.impl.remoting.commands.CallActionCommand in project dolphin-platform by canoo.

the class TestOptimizedJsonCodec method shouldEncodeCallActionWithParamsCommand.

@Test
public void shouldEncodeCallActionWithParamsCommand() {
    final CallActionCommand command = new CallActionCommand();
    command.setControllerId("4711");
    command.setActionName("action");
    command.addParam("A", 1);
    command.addParam("B", 7.6);
    command.addParam("C", true);
    command.addParam("D", null);
    command.addParam("E", "Hello");
    final String actual = OptimizedJsonCodec.getInstance().encode(Collections.<Command>singletonList(command));
    assertThat(actual, is("[{\"c_id\":\"4711\",\"n\":\"action\",\"p\":[{\"n\":\"A\",\"v\":1},{\"n\":\"B\",\"v\":7.6},{\"n\":\"C\",\"v\":true},{\"n\":\"D\",\"v\":null},{\"n\":\"E\",\"v\":\"Hello\"}],\"id\":\"CallAction\"}]"));
}
Also used : CallActionCommand(com.canoo.dp.impl.remoting.commands.CallActionCommand) Test(org.testng.annotations.Test)

Aggregations

CallActionCommand (com.canoo.dp.impl.remoting.commands.CallActionCommand)4 Test (org.testng.annotations.Test)2 MappingException (com.canoo.dp.impl.remoting.MappingException)1 ControllerActionException (com.canoo.platform.remoting.client.ControllerActionException)1 Param (com.canoo.platform.remoting.client.Param)1 ValueConverterException (com.canoo.platform.remoting.spi.converter.ValueConverterException)1 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParseException (com.google.gson.JsonParseException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1