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;
}
}
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");
}
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");
}
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);
}
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);
}
Aggregations