Search in sources :

Example 6 with Echo

use of io.helidon.grpc.server.test.Echo in project helidon by oracle.

the class SecuredOutboundEchoService method echo.

/**
 * Make a web request passing this method's message parameter and send
 * the web response back to the caller .
 *
 * @param request   the echo request containing the message to echo
 * @param observer  the call response
 */
public void echo(Echo.EchoRequest request, StreamObserver<Echo.EchoResponse> observer) {
    try {
        SecurityContext securityContext = GrpcSecurity.SECURITY_CONTEXT.get();
        String message = request.getMessage();
        Response webResponse = client.target(url).path("/test").queryParam("message", message).request().property(ClientSecurity.PROPERTY_CONTEXT, securityContext).get();
        if (webResponse.getStatus() == 200) {
            String value = webResponse.readEntity(String.class);
            Echo.EchoResponse echoResponse = Echo.EchoResponse.newBuilder().setMessage(value).build();
            complete(observer, echoResponse);
        } else if (webResponse.getStatus() == Response.Status.FORBIDDEN.getStatusCode() || webResponse.getStatus() == Response.Status.UNAUTHORIZED.getStatusCode()) {
            observer.onError(Status.PERMISSION_DENIED.asException());
        } else {
            observer.onError(Status.UNKNOWN.withDescription("Received http response " + webResponse).asException());
        }
    } catch (Throwable thrown) {
        observer.onError(Status.UNKNOWN.withCause(thrown).asException());
    }
}
Also used : Response(jakarta.ws.rs.core.Response) Echo(io.helidon.grpc.server.test.Echo) SecurityContext(io.helidon.security.SecurityContext)

Example 7 with Echo

use of io.helidon.grpc.server.test.Echo in project helidon by oracle.

the class SslIT method shouldConnectWithClientCertsFor2WayUseConfig.

@Test
public void shouldConnectWithClientCertsFor2WayUseConfig() throws Exception {
    Resource tlsCaCert = Resource.create(CA_CERT);
    Resource tlsClientCert = Resource.create(CLIENT_CERT);
    Resource tlsClientKey = Resource.create(CLIENT_KEY);
    SslContext sslContext = clientSslContext(tlsCaCert, tlsClientCert, tlsClientKey);
    Channel channel = NettyChannelBuilder.forAddress("localhost", grpcServer_2WaySSLConfig.port()).negotiationType(NegotiationType.TLS).sslContext(sslContext).build();
    // call the gRPC Echo service
    Echo.EchoResponse response = EchoServiceGrpc.newBlockingStub(channel).echo(Echo.EchoRequest.newBuilder().setMessage("foo").build());
    assertThat(response.getMessage(), is("foo"));
    ((ManagedChannel) channel).shutdown().awaitTermination(5, TimeUnit.SECONDS);
}
Also used : Echo(io.helidon.grpc.server.test.Echo) ManagedChannel(io.grpc.ManagedChannel) Channel(io.grpc.Channel) Resource(io.helidon.common.configurable.Resource) SslContext(io.netty.handler.ssl.SslContext) Test(org.junit.jupiter.api.Test)

Aggregations

Echo (io.helidon.grpc.server.test.Echo)7 Test (org.junit.jupiter.api.Test)6 Channel (io.grpc.Channel)4 ManagedChannel (io.grpc.ManagedChannel)3 Resource (io.helidon.common.configurable.Resource)3 SslContext (io.netty.handler.ssl.SslContext)3 Context (io.helidon.common.context.Context)2 ManagedChannelBuilder (io.grpc.ManagedChannelBuilder)1 LogConfig (io.helidon.common.LogConfig)1 MediaType (io.helidon.common.http.MediaType)1 GrpcRouting (io.helidon.grpc.server.GrpcRouting)1 GrpcServer (io.helidon.grpc.server.GrpcServer)1 GrpcServerConfiguration (io.helidon.grpc.server.GrpcServerConfiguration)1 EchoServiceGrpc (io.helidon.grpc.server.test.EchoServiceGrpc)1 JsonpSupport (io.helidon.media.jsonp.JsonpSupport)1 MetricsSupport (io.helidon.metrics.MetricsSupport)1 SecurityContext (io.helidon.security.SecurityContext)1 WebClient (io.helidon.webclient.WebClient)1 Routing (io.helidon.webserver.Routing)1 WebServer (io.helidon.webserver.WebServer)1