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