Search in sources :

Example 11 with Context

use of io.helidon.common.context.Context in project helidon by oracle.

the class TestParent method createNewClient.

protected static WebClient createNewClient(WebClientService... clientServices) {
    Security security = Security.builder().addProvider(HttpBasicAuthProvider.builder().build()).build();
    SecurityContext securityContext = security.createContext("unit-test");
    Context context = Context.builder().id("unit-test").build();
    context.register(securityContext);
    WebClient.Builder builder = WebClient.builder().baseUri("http://localhost:" + webServer.port() + "/greet").config(CONFIG.get("client")).context(context).addMediaSupport(JsonpSupport.create());
    Stream.of(clientServices).forEach(builder::addService);
    return builder.build();
}
Also used : Context(io.helidon.common.context.Context) SecurityContext(io.helidon.security.SecurityContext) SecurityContext(io.helidon.security.SecurityContext) Security(io.helidon.security.Security) WebClient(io.helidon.webclient.WebClient)

Example 12 with Context

use of io.helidon.common.context.Context in project helidon by oracle.

the class JaegerDataPropagationProviderTest method dataPropagationTest.

@Test
void dataPropagationTest() {
    Context context = Context.create();
    Tracer tracer = new TestTracer();
    context.register(tracer);
    Span span = tracer.buildSpan("span").start();
    context.register(span);
    Scope scope = tracer.scopeManager().activate(span);
    context.register(scope);
    Contexts.runInContext(context, () -> {
        assertThat(closed(scope), is(false));
        JaegerDataPropagationProvider.JaegerContext data = provider.data();
        provider.propagateData(data);
        assertThat(closed(data.scope()), is(false));
        provider.clearData(data);
        assertThat(closed(data.scope()), is(true));
    });
}
Also used : Context(io.helidon.common.context.Context) SpanContext(io.opentracing.SpanContext) Scope(io.opentracing.Scope) Tracer(io.opentracing.Tracer) Span(io.opentracing.Span) Test(org.junit.jupiter.api.Test)

Example 13 with Context

use of io.helidon.common.context.Context in project helidon by oracle.

the class WebClientSecurity method request.

@Override
public Single<WebClientServiceRequest> request(WebClientServiceRequest request) {
    if ("true".equalsIgnoreCase(request.properties().get(OutboundConfig.PROPERTY_DISABLE_OUTBOUND))) {
        return Single.just(request);
    }
    Context requestContext = request.context();
    // context either from request or create a new one
    Optional<SecurityContext> maybeContext = requestContext.get(SecurityContext.class);
    SecurityContext context;
    if (null == security) {
        if (maybeContext.isEmpty()) {
            return Single.just(request);
        } else {
            context = maybeContext.get();
        }
    } else {
        // we have our own security - we need to use this instance for outbound,
        // so we cannot re-use the context
        context = createContext(request);
    }
    Span span = context.tracer().buildSpan("security:outbound").asChildOf(context.tracingSpan()).start();
    String explicitProvider = request.properties().get(PROVIDER_NAME);
    OutboundSecurityClientBuilder clientBuilder;
    try {
        SecurityEnvironment.Builder outboundEnv = context.env().derive().clearHeaders();
        outboundEnv.method(request.method().name()).path(request.path().toString()).targetUri(request.uri()).headers(request.headers().toMap());
        EndpointConfig.Builder outboundEp = context.endpointConfig().derive();
        Map<String, String> propMap = request.properties();
        for (String name : propMap.keySet()) {
            Optional.ofNullable(request.properties().get(name)).ifPresent(property -> outboundEp.addAtribute(name, property));
        }
        clientBuilder = context.outboundClientBuilder().outboundEnvironment(outboundEnv).outboundEndpointConfig(outboundEp).explicitProvider(explicitProvider);
    } catch (Exception e) {
        traceError(span, e, null);
        throw e;
    }
    return Single.create(clientBuilder.submit().thenApply(providerResponse -> processResponse(request, span, providerResponse)));
}
Also used : Context(io.helidon.common.context.Context) SecurityContext(io.helidon.security.SecurityContext) SpanContext(io.opentracing.SpanContext) OutboundSecurityResponse(io.helidon.security.OutboundSecurityResponse) Security(io.helidon.security.Security) WebClientServiceRequest(io.helidon.webclient.WebClientServiceRequest) WebClientService(io.helidon.webclient.spi.WebClientService) Tracer(io.opentracing.Tracer) Context(io.helidon.common.context.Context) SecurityContext(io.helidon.security.SecurityContext) UUID(java.util.UUID) Logger(java.util.logging.Logger) OutboundSecurityClientBuilder(io.helidon.security.OutboundSecurityClientBuilder) OutboundConfig(io.helidon.security.providers.common.OutboundConfig) WebClientRequestHeaders(io.helidon.webclient.WebClientRequestHeaders) Contexts(io.helidon.common.context.Contexts) Tags(io.opentracing.tag.Tags) SpanContext(io.opentracing.SpanContext) List(java.util.List) EndpointConfig(io.helidon.security.EndpointConfig) SecurityEnvironment(io.helidon.security.SecurityEnvironment) Map(java.util.Map) Optional(java.util.Optional) Single(io.helidon.common.reactive.Single) Span(io.opentracing.Span) SecurityEnvironment(io.helidon.security.SecurityEnvironment) Span(io.opentracing.Span) SecurityContext(io.helidon.security.SecurityContext) OutboundSecurityClientBuilder(io.helidon.security.OutboundSecurityClientBuilder) EndpointConfig(io.helidon.security.EndpointConfig)

Example 14 with Context

use of io.helidon.common.context.Context in project helidon by oracle.

the class SizeLogEntry method accept.

@Override
public void accept(ServerRequest req, ServerResponse res) {
    Context context = req.context();
    res.registerFilter(originalPublisher -> new ByteCountingPublisher(originalPublisher, context));
}
Also used : Context(io.helidon.common.context.Context)

Example 15 with Context

use of io.helidon.common.context.Context in project helidon by oracle.

the class AccessLogSupportTest method testHelidonFormat.

@Test
void testHelidonFormat() {
    AccessLogSupport accessLog = AccessLogSupport.create();
    ServerRequest request = mock(ServerRequest.class);
    Context context = Context.create();
    when(request.remoteAddress()).thenReturn(REMOTE_IP);
    when(request.context()).thenReturn(context);
    when(request.method()).thenReturn(Http.Method.PUT);
    HttpRequest.Path path = mock(HttpRequest.Path.class);
    when(path.toRawString()).thenReturn(PATH);
    when(request.path()).thenReturn(path);
    when(request.version()).thenReturn(Http.Version.V1_1);
    ServerResponse response = mock(ServerResponse.class);
    when(response.status()).thenReturn(Http.Status.I_AM_A_TEAPOT);
    AccessLogContext accessLogContext = mock(AccessLogContext.class);
    when(accessLogContext.requestDateTime()).thenReturn(BEGIN_TIME);
    String expectedTimestamp = TimestampLogEntry.create().doApply(accessLogContext);
    String logRecord = accessLog.createLogRecord(request, response, BEGIN_TIME, 0L, END_TIME, TIME_TAKEN_MICROS * 1000);
    // 192.168.0.104 - [18/Jun/2019:23:10:44 +0200] "GET /greet/test HTTP/1.1" 200 55 2248
    String expected = REMOTE_IP + " - " + expectedTimestamp + " \"" + METHOD + " " + PATH + " " + HTTP_VERSION + "\" " + STATUS_CODE + " " + CONTENT_LENGTH + " " + TIME_TAKEN_MICROS;
    assertThat(logRecord, is(expected));
}
Also used : Context(io.helidon.common.context.Context) HttpRequest(io.helidon.common.http.HttpRequest) ServerResponse(io.helidon.webserver.ServerResponse) ServerRequest(io.helidon.webserver.ServerRequest) Test(org.junit.jupiter.api.Test)

Aggregations

Context (io.helidon.common.context.Context)32 Test (org.junit.jupiter.api.Test)14 Contexts (io.helidon.common.context.Contexts)8 WebClient (io.helidon.webclient.WebClient)8 Optional (java.util.Optional)7 Http (io.helidon.common.http.Http)6 Single (io.helidon.common.reactive.Single)6 Config (io.helidon.config.Config)6 ServerRequest (io.helidon.webserver.ServerRequest)6 SpanContext (io.opentracing.SpanContext)6 Logger (java.util.logging.Logger)6 HttpRequest (io.helidon.common.http.HttpRequest)5 SecurityContext (io.helidon.security.SecurityContext)5 ServerResponse (io.helidon.webserver.ServerResponse)5 List (java.util.List)5 Map (java.util.Map)5 WebClientResponse (io.helidon.webclient.WebClientResponse)4 Span (io.opentracing.Span)4 Tracer (io.opentracing.Tracer)4 Collections (java.util.Collections)4