Search in sources :

Example 36 with Command

use of com.canoo.dp.impl.remoting.legacy.communication.Command in project dolphin-platform by canoo.

the class DolphinContextCommunicationHandler method handle.

public void handle(final HttpServletRequest request, final HttpServletResponse response) {
    Assert.requireNonNull(request, "request");
    Assert.requireNonNull(response, "response");
    final HttpSession httpSession = Assert.requireNonNull(request.getSession(), "request.getSession()");
    final ClientSession clientSession = sessionProvider.getCurrentClientSession();
    if (clientSession == null) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        LOG.error("No client session provided for request in http session {}", httpSession.getId());
        return;
    }
    final String userAgent = request.getHeader("user-agent");
    LOG.trace("receiving RPM request for client session {} in http session {} from client with user-agent {}", clientSession.getId(), httpSession.getId(), userAgent);
    final List<Command> commands = new ArrayList<>();
    try {
        commands.addAll(readCommands(request));
    } catch (final Exception e) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        LOG.error("Can not parse request! (DolphinContext " + clientSession.getId() + ")", e);
        return;
    }
    LOG.trace("Request for DolphinContext {} in http session {} contains {} commands", clientSession.getId(), httpSession.getId(), commands.size());
    try {
        DolphinContext context = getOrCreateContext(clientSession, commands);
        final List<Command> results = new ArrayList<>();
        try {
            results.addAll(handle(context, commands));
        } catch (final Exception e) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            LOG.error("Can not withoutResult the the received commands (DolphinContext " + context.getId() + ")", e);
            return;
        }
        LOG.trace("Sending RPM response for client session {} in http session {} from client with user-agent {}", context.getId(), httpSession.getId(), userAgent);
        LOG.trace("RPM response for client session {} in http session {} contains {} commands", context.getId(), httpSession.getId(), results.size());
        try {
            writeCommands(results, response);
        } catch (final Exception e) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            LOG.error("Can not writeRequestContent response!", e);
            return;
        }
    } catch (final Exception e) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        LOG.error("Can not find or create matching dolphin context in session " + httpSession.getId(), e);
        return;
    }
}
Also used : CreateContextCommand(com.canoo.dp.impl.remoting.commands.CreateContextCommand) Command(com.canoo.dp.impl.remoting.legacy.communication.Command) HttpSession(javax.servlet.http.HttpSession) ClientSession(com.canoo.platform.server.client.ClientSession) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 37 with Command

use of com.canoo.dp.impl.remoting.legacy.communication.Command in project dolphin-platform by canoo.

the class TestDolphinCommandHandler method testInvocation.

@Test
public void testInvocation() throws Exception {
    // Given:
    final DolphinTestConfiguration configuration = createDolphinTestConfiguration();
    final ServerModelStore serverModelStore = configuration.getServerModelStore();
    final ClientModelStore clientModelStore = configuration.getClientModelStore();
    final DolphinCommandHandler dolphinCommandHandler = new DolphinCommandHandler(configuration.getClientConnector());
    final String modelId = UUID.randomUUID().toString();
    clientModelStore.createModel(modelId, null, new ClientAttribute("myAttribute", "UNKNOWN"));
    configuration.getServerConnector().register(new DolphinServerAction() {

        @Override
        public void registerIn(ActionRegistry registry) {
            registry.register(TestChangeCommand.class, new CommandHandler() {

                @Override
                public void handleCommand(Command command, List response) {
                    serverModelStore.findPresentationModelById(modelId).getAttribute("myAttribute").setValue("Hello World");
                }
            });
        }
    });
    // When:
    dolphinCommandHandler.invokeDolphinCommand(new TestChangeCommand()).get();
    // Then:
    assertEquals(clientModelStore.findPresentationModelById(modelId).getAttribute("myAttribute").getValue(), "Hello World");
}
Also used : ClientAttribute(com.canoo.dp.impl.client.legacy.ClientAttribute) ServerModelStore(com.canoo.dp.impl.server.legacy.ServerModelStore) CommandHandler(com.canoo.dp.impl.server.legacy.communication.CommandHandler) DolphinCommandHandler(com.canoo.dp.impl.client.DolphinCommandHandler) ActionRegistry(com.canoo.dp.impl.server.legacy.communication.ActionRegistry) DolphinCommandHandler(com.canoo.dp.impl.client.DolphinCommandHandler) Command(com.canoo.dp.impl.remoting.legacy.communication.Command) List(java.util.List) ClientModelStore(com.canoo.dp.impl.client.legacy.ClientModelStore) DolphinServerAction(com.canoo.dp.impl.server.legacy.action.DolphinServerAction) Test(org.testng.annotations.Test) AbstractDolphinBasedTest(com.canoo.dolphin.client.util.AbstractDolphinBasedTest)

Example 38 with Command

use of com.canoo.dp.impl.remoting.legacy.communication.Command in project dolphin-platform by canoo.

the class TestDolphinPlatformHttpClientConnector method testSimpleCall.

@Test
public void testSimpleCall() throws DolphinRemotingException, URISyntaxException {
    PlatformClient.init(new HeadlessToolkit());
    PlatformClient.getClientConfiguration().setHttpURLConnectionFactory(new HttpURLConnectionFactory() {

        @Override
        public HttpURLConnection create(URI url) throws IOException {
            return new HttpURLConnection(url.toURL()) {

                @Override
                public void disconnect() {
                }

                @Override
                public boolean usingProxy() {
                    return false;
                }

                @Override
                public void connect() throws IOException {
                }

                @Override
                public int getResponseCode() throws IOException {
                    return HttpStatus.HTTP_OK;
                }

                @Override
                public OutputStream getOutputStream() throws IOException {
                    return new ByteArrayOutputStream();
                }

                @Override
                public InputStream getInputStream() throws IOException {
                    String response = "[{\"pmId\":\"p1\",\"clientSideOnly\":false,\"id\":\"CreatePresentationModel\",\"attributes\":[],\"pmType\":null,\"className\":\"com.canoo.dp.impl.remoting.legacy.communication.CreatePresentationModelCommand\"}]";
                    return new ByteArrayInputStream(response.getBytes("UTF-8"));
                }

                @Override
                public String getHeaderField(String name) {
                    if (PlatformConstants.CLIENT_ID_HTTP_HEADER_NAME.equals(name)) {
                        return "TEST-ID";
                    }
                    return super.getHeaderField(name);
                }
            };
        }
    });
    final ClientModelStore clientModelStore = new ClientModelStore(new DefaultModelSynchronizer(() -> null));
    final DolphinPlatformHttpClientConnector connector = new DolphinPlatformHttpClientConnector(getDummyURL(), PlatformClient.getClientConfiguration(), clientModelStore, new JsonCodec(), new SimpleExceptionHandler(), PlatformClient.getService(HttpClient.class));
    final CreatePresentationModelCommand command = new CreatePresentationModelCommand();
    command.setPmId("p1");
    Command rawCommand = command;
    final List<Command> result = connector.transmit(Collections.singletonList(rawCommand));
    Assert.assertEquals(result.size(), 1);
    Assert.assertTrue(result.get(0) instanceof CreatePresentationModelCommand);
    Assert.assertEquals(((CreatePresentationModelCommand) result.get(0)).getPmId(), "p1");
}
Also used : DolphinPlatformHttpClientConnector(com.canoo.dp.impl.client.DolphinPlatformHttpClientConnector) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) DefaultModelSynchronizer(com.canoo.dp.impl.client.legacy.DefaultModelSynchronizer) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SimpleExceptionHandler(com.canoo.dp.impl.client.legacy.communication.SimpleExceptionHandler) HttpURLConnectionFactory(com.canoo.platform.core.http.HttpURLConnectionFactory) JsonCodec(com.canoo.dp.impl.remoting.legacy.communication.JsonCodec) ByteArrayInputStream(java.io.ByteArrayInputStream) CreateContextCommand(com.canoo.dp.impl.remoting.commands.CreateContextCommand) Command(com.canoo.dp.impl.remoting.legacy.communication.Command) CreatePresentationModelCommand(com.canoo.dp.impl.remoting.legacy.communication.CreatePresentationModelCommand) HttpClient(com.canoo.platform.core.http.HttpClient) CreatePresentationModelCommand(com.canoo.dp.impl.remoting.legacy.communication.CreatePresentationModelCommand) HeadlessToolkit(com.canoo.platform.client.HeadlessToolkit) ClientModelStore(com.canoo.dp.impl.client.legacy.ClientModelStore) Test(org.testng.annotations.Test)

Example 39 with Command

use of com.canoo.dp.impl.remoting.legacy.communication.Command in project dolphin-platform by canoo.

the class DefaultModelSynchronizer method onMetadataChanged.

@Override
public void onMetadataChanged(final PropertyChangeEvent evt) {
    final Command command = new ChangeAttributeMetadataCommand(((Attribute) evt.getSource()).getId(), evt.getPropertyName(), evt.getNewValue());
    send(command);
}
Also used : Command(com.canoo.dp.impl.remoting.legacy.communication.Command) CreatePresentationModelCommand(com.canoo.dp.impl.remoting.legacy.communication.CreatePresentationModelCommand) PresentationModelDeletedCommand(com.canoo.dp.impl.remoting.legacy.communication.PresentationModelDeletedCommand) ChangeAttributeMetadataCommand(com.canoo.dp.impl.remoting.legacy.communication.ChangeAttributeMetadataCommand) ValueChangedCommand(com.canoo.dp.impl.remoting.legacy.communication.ValueChangedCommand) ChangeAttributeMetadataCommand(com.canoo.dp.impl.remoting.legacy.communication.ChangeAttributeMetadataCommand)

Example 40 with Command

use of com.canoo.dp.impl.remoting.legacy.communication.Command in project dolphin-platform by canoo.

the class DefaultModelSynchronizer method onDeleted.

@Override
public void onDeleted(final ClientPresentationModel model) {
    final Command command = new PresentationModelDeletedCommand(model.getId());
    send(command);
}
Also used : PresentationModelDeletedCommand(com.canoo.dp.impl.remoting.legacy.communication.PresentationModelDeletedCommand) Command(com.canoo.dp.impl.remoting.legacy.communication.Command) CreatePresentationModelCommand(com.canoo.dp.impl.remoting.legacy.communication.CreatePresentationModelCommand) PresentationModelDeletedCommand(com.canoo.dp.impl.remoting.legacy.communication.PresentationModelDeletedCommand) ChangeAttributeMetadataCommand(com.canoo.dp.impl.remoting.legacy.communication.ChangeAttributeMetadataCommand) ValueChangedCommand(com.canoo.dp.impl.remoting.legacy.communication.ValueChangedCommand)

Aggregations

Command (com.canoo.dp.impl.remoting.legacy.communication.Command)43 CreatePresentationModelCommand (com.canoo.dp.impl.remoting.legacy.communication.CreatePresentationModelCommand)25 ValueChangedCommand (com.canoo.dp.impl.remoting.legacy.communication.ValueChangedCommand)23 Test (org.testng.annotations.Test)22 EmptyCommand (com.canoo.dp.impl.remoting.legacy.communication.EmptyCommand)19 CallActionCommand (com.canoo.dp.impl.remoting.commands.CallActionCommand)18 StartLongPollCommand (com.canoo.dp.impl.remoting.legacy.commands.StartLongPollCommand)10 InterruptLongPollCommand (com.canoo.dp.impl.remoting.legacy.commands.InterruptLongPollCommand)8 ChangeAttributeMetadataCommand (com.canoo.dp.impl.remoting.legacy.communication.ChangeAttributeMetadataCommand)8 ArrayList (java.util.ArrayList)8 PresentationModelDeletedCommand (com.canoo.dp.impl.remoting.legacy.communication.PresentationModelDeletedCommand)7 CreateContextCommand (com.canoo.dp.impl.remoting.commands.CreateContextCommand)5 LinkedList (java.util.LinkedList)5 ServerModelStore (com.canoo.dp.impl.server.legacy.ServerModelStore)4 ClientAttribute (com.canoo.dp.impl.client.legacy.ClientAttribute)3 ClientModelStore (com.canoo.dp.impl.client.legacy.ClientModelStore)3 DestroyContextCommand (com.canoo.dp.impl.remoting.commands.DestroyContextCommand)3 AttributeMetadataChangedCommand (com.canoo.dp.impl.remoting.legacy.communication.AttributeMetadataChangedCommand)3 DeletePresentationModelCommand (com.canoo.dp.impl.remoting.legacy.communication.DeletePresentationModelCommand)3 CommandHandler (com.canoo.dp.impl.server.legacy.communication.CommandHandler)3