use of software.amazon.awssdk.eventstreamrpc.model.EventStreamJsonMessage in project aws-iot-device-sdk-java-v2 by aws.
the class EventStreamRPCServiceModel method fromJson.
/**
* Uses this service's specific model class
* @param applicationModelType
* @param payload
* @return
*/
public EventStreamJsonMessage fromJson(final String applicationModelType, byte[] payload) {
final Optional<Class<? extends EventStreamJsonMessage>> clazz = getApplicationModelClass(applicationModelType);
if (!clazz.isPresent()) {
throw new UnmappedDataException(applicationModelType);
}
final EventStreamJsonMessage msg = fromJson(clazz.get(), payload);
msg.postFromJson();
return msg;
}
use of software.amazon.awssdk.eventstreamrpc.model.EventStreamJsonMessage in project aws-iot-device-sdk-java-v2 by aws.
the class GreengrassV2ClientTest method before.
@BeforeEach
public void before() throws IOException {
port = randomPort();
try (final EventLoopGroup elGroup = new EventLoopGroup(1);
SocketOptions socketOptions = new SocketOptions()) {
socketOptions.connectTimeoutMs = 3000;
socketOptions.domain = SocketOptions.SocketDomain.IPv4;
socketOptions.type = SocketOptions.SocketType.STREAM;
GreengrassCoreIPCService service = new GreengrassCoreIPCService();
service.setCreateLocalDeploymentHandler((c) -> new GeneratedAbstractCreateLocalDeploymentOperationHandler(c) {
@Override
protected void onStreamClosed() {
}
@Override
public CreateLocalDeploymentResponse handleRequest(CreateLocalDeploymentRequest request) {
return new CreateLocalDeploymentResponse().withDeploymentId("deployment");
}
@Override
public void handleStreamEvent(EventStreamJsonMessage streamRequestEvent) {
}
});
service.setSubscribeToTopicHandler((c) -> new GeneratedAbstractSubscribeToTopicOperationHandler(c) {
@Override
protected void onStreamClosed() {
subscriptionClosed.complete(null);
}
@Override
public SubscribeToTopicResponse handleRequest(SubscribeToTopicRequest request) {
new Thread(() -> {
sendStreamEvent(new SubscriptionResponseMessage().withBinaryMessage(new BinaryMessage().withMessage("message".getBytes(StandardCharsets.UTF_8))));
}).start();
return new SubscribeToTopicResponse().withTopicName(request.getTopic());
}
@Override
public void handleStreamEvent(EventStreamJsonMessage streamRequestEvent) {
}
});
service.setAuthenticationHandler((headers, bytes) -> {
authenticationRequest = new Gson().fromJson(new String(bytes), GreengrassEventStreamConnectMessage.class);
return () -> "connected";
});
service.setAuthorizationHandler(authenticationData -> Authorization.ACCEPT);
ipcServer = new RpcServer(elGroup, socketOptions, null, "127.0.0.1", port, service);
ipcServer.runServer();
client = GreengrassCoreIPCClientV2.builder().withPort(port).withSocketPath("127.0.0.1").withSocketDomain(SocketOptions.SocketDomain.IPv4).withAuthToken("myAuthToken").build();
}
}
use of software.amazon.awssdk.eventstreamrpc.model.EventStreamJsonMessage in project aws-iot-device-sdk-java-v2 by aws.
the class IpcServerTests method testStartStopIpcServer.
@Test
public void testStartStopIpcServer() throws InterruptedException {
final int port = randomPort();
final EventStreamRPCServiceHandler handler = new EventStreamRPCServiceHandler() {
@Override
protected EventStreamRPCServiceModel getServiceModel() {
return new EventStreamRPCServiceModel() {
@Override
public String getServiceName() {
return "TestService";
}
/**
* Retreives all operations on the service
*
* @return
*/
@Override
public Collection<String> getAllOperations() {
return new HashSet<>();
}
@Override
protected Optional<Class<? extends EventStreamJsonMessage>> getServiceClassType(String applicationModelType) {
return Optional.empty();
}
@Override
public OperationModelContext getOperationModelContext(String operationName) {
return null;
}
};
}
@Override
public Function<OperationContinuationHandlerContext, ? extends ServerConnectionContinuationHandler> getOperationHandler(String operationName) {
return null;
}
@Override
public boolean hasHandlerForOperation(String operation) {
return true;
}
};
handler.setAuthenticationHandler(TestAuthNZHandlers.getAuthNHandler());
handler.setAuthorizationHandler(TestAuthNZHandlers.getAuthZHandler());
try (final EventLoopGroup elGroup = new EventLoopGroup(1);
SocketOptions socketOptions = new SocketOptions()) {
socketOptions.connectTimeoutMs = 3000;
socketOptions.domain = SocketOptions.SocketDomain.IPv4;
socketOptions.type = SocketOptions.SocketType.STREAM;
try (final IpcServer ipcServer = new IpcServer(elGroup, socketOptions, null, "127.0.0.1", port, handler)) {
ipcServer.runServer();
Socket clientSocket = new Socket();
SocketAddress address = new InetSocketAddress("127.0.0.1", port);
clientSocket.connect(address, 3000);
// no real assertion to be made here as long as the above connection works...
clientSocket.close();
Thread.sleep(1_000);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
CrtResource.waitForNoResources();
}
use of software.amazon.awssdk.eventstreamrpc.model.EventStreamJsonMessage in project aws-iot-device-sdk-java-v2 by aws.
the class IpcServerTests method testIpcServerAuthNUnset.
@Test
public void testIpcServerAuthNUnset() {
final int port = randomPort();
final Set<String> OPERATION_SET = new HashSet<>();
OPERATION_SET.add("dummyOperationName");
final EventStreamRPCServiceHandler handler = new EventStreamRPCServiceHandler() {
@Override
protected EventStreamRPCServiceModel getServiceModel() {
return new EventStreamRPCServiceModel() {
@Override
public String getServiceName() {
return "TestService";
}
@Override
public Collection<String> getAllOperations() {
return new HashSet<>();
}
@Override
protected Optional<Class<? extends EventStreamJsonMessage>> getServiceClassType(String applicationModelType) {
return Optional.empty();
}
@Override
public OperationModelContext getOperationModelContext(String operationName) {
return null;
}
};
}
@Override
public Function<OperationContinuationHandlerContext, ? extends ServerConnectionContinuationHandler> getOperationHandler(String operationName) {
return null;
}
@Override
public boolean hasHandlerForOperation(String operation) {
return true;
}
};
// missing handler.setAuthenticationHandler(TestAuthNZHandlers.getAuthNHandler());
handler.setAuthorizationHandler(TestAuthNZHandlers.getAuthZHandler());
try (final EventLoopGroup elGroup = new EventLoopGroup(1);
SocketOptions socketOptions = new SocketOptions()) {
socketOptions.connectTimeoutMs = 3000;
socketOptions.domain = SocketOptions.SocketDomain.IPv4;
socketOptions.type = SocketOptions.SocketType.STREAM;
try (final IpcServer ipcServer = new IpcServer(elGroup, socketOptions, null, "127.0.0.1", port, handler)) {
Assertions.assertThrows(InvalidServiceConfigurationException.class, () -> {
ipcServer.runServer();
});
}
}
CrtResource.waitForNoResources();
}
use of software.amazon.awssdk.eventstreamrpc.model.EventStreamJsonMessage in project aws-iot-device-sdk-java-v2 by aws.
the class IpcServerTests method testIpcServerAuthZUnset.
@Test
public void testIpcServerAuthZUnset() {
final int port = randomPort();
final Set<String> OPERATION_SET = new HashSet<>();
OPERATION_SET.add("dummyOperationName");
final EventStreamRPCServiceHandler handler = new EventStreamRPCServiceHandler() {
@Override
protected EventStreamRPCServiceModel getServiceModel() {
return new EventStreamRPCServiceModel() {
@Override
public String getServiceName() {
return "TestService";
}
@Override
public Collection<String> getAllOperations() {
return new HashSet<>();
}
@Override
protected Optional<Class<? extends EventStreamJsonMessage>> getServiceClassType(String applicationModelType) {
return Optional.empty();
}
@Override
public OperationModelContext getOperationModelContext(String operationName) {
return null;
}
};
}
@Override
public Function<OperationContinuationHandlerContext, ? extends ServerConnectionContinuationHandler> getOperationHandler(String operationName) {
return null;
}
@Override
public boolean hasHandlerForOperation(String operation) {
return true;
}
};
handler.setAuthenticationHandler(TestAuthNZHandlers.getAuthNHandler());
try (final EventLoopGroup elGroup = new EventLoopGroup(1);
SocketOptions socketOptions = new SocketOptions()) {
socketOptions.connectTimeoutMs = 3000;
socketOptions.domain = SocketOptions.SocketDomain.IPv4;
socketOptions.type = SocketOptions.SocketType.STREAM;
try (final IpcServer ipcServer = new IpcServer(elGroup, socketOptions, null, "127.0.0.1", port, handler)) {
Assertions.assertThrows(InvalidServiceConfigurationException.class, () -> {
ipcServer.runServer();
});
}
}
CrtResource.waitForNoResources();
}
Aggregations