use of io.helidon.common.context.Context in project helidon by oracle.
the class AccessLogSupportTest method testCustomFormat.
@Test
void testCustomFormat() {
AccessLogSupport accessLog = AccessLogSupport.builder().add(TimestampLogEntry.builder().formatter(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")).build()).add(HeaderLogEntry.create("Referer")).build();
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);
RequestHeaders headers = mock(RequestHeaders.class);
when(headers.all("Referer")).thenReturn(Arrays.asList("first", "second"));
when(request.headers()).thenReturn(headers);
ServerResponse response = mock(ServerResponse.class);
when(response.status()).thenReturn(Http.Status.I_AM_A_TEAPOT);
String logRecord = accessLog.createLogRecord(request, response, BEGIN_TIME, 0L, END_TIME, TIME_TAKEN_MICROS * 1000);
String expected = "20071203101530 \"first,second\"";
assertThat(logRecord, is(expected));
}
use of io.helidon.common.context.Context in project helidon by oracle.
the class AccessLogSupportTest method testCommonFormat.
@Test
void testCommonFormat() {
AccessLogSupport accessLog = AccessLogSupport.builder().commonLogFormat().build();
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;
assertThat(logRecord, is(expected));
}
use of io.helidon.common.context.Context in project helidon by oracle.
the class SecurityHandler method processSecurity.
private void processSecurity(SecurityContext securityContext, ServerRequest req, ServerResponse res) {
// authentication and authorization
// start security span
SecurityTracing tracing = SecurityTracing.get();
tracing.securityContext(securityContext);
// extract headers
extractQueryParams(securityContext, req);
securityContext.endpointConfig(securityContext.endpointConfig().derive().configMap(configMap).customObjects(customObjects.orElse(new ClassToInstanceStore<>())).build());
Optional<Context> context = Contexts.context();
processAuthentication(res, securityContext, tracing.atnTracing()).thenCompose(atnResult -> {
if (atnResult.proceed) {
// authentication was OK or disabled, we should continue
return processAuthorization(req, res, securityContext, tracing.atzTracing());
} else {
// authentication told us to stop processing
return CompletableFuture.completedFuture(AtxResult.STOP);
}
}).thenAccept(atzResult -> {
if (atzResult.proceed) {
// authorization was OK, we can continue processing
tracing.logProceed();
tracing.finish();
// propagate context information in call to next
context.ifPresentOrElse(c -> Contexts.runInContext(c, (Runnable) req::next), req::next);
} else {
tracing.logDeny();
tracing.finish();
}
}).exceptionally(throwable -> {
tracing.error(throwable);
LOGGER.log(Level.SEVERE, "Unexpected exception during security processing", throwable);
abortRequest(res, null, Http.Status.INTERNAL_SERVER_ERROR_500.code(), Map.of());
return null;
});
// auditing
res.whenSent().thenAccept(sr -> processAudit(req, sr, securityContext));
}
use of io.helidon.common.context.Context in project helidon by oracle.
the class AbstractTracingFilter method filter.
@Override
public void filter(ContainerRequestContext requestContext) {
if (!tracingEnabled(requestContext)) {
return;
}
Context context = Contexts.context().orElseThrow(() -> new IllegalStateException("Context must be available in Jersey"));
String spanName = spanName(requestContext);
SpanTracingConfig spanConfig = TracingConfigUtil.spanConfig(ClientTracingFilter.JAX_RS_TRACING_COMPONENT, spanName, context);
if (spanConfig.enabled()) {
spanName = spanConfig.newName().orElse(spanName);
Tracer tracer = context.get(Tracer.class).orElseGet(GlobalTracer::get);
SpanContext parentSpan = context.get(ServerRequest.class, SpanContext.class).orElseGet(() -> context.get(SpanContext.class).orElse(null));
Tracer.SpanBuilder spanBuilder = tracer.buildSpan(spanName).withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER).withTag(Tags.HTTP_METHOD.getKey(), requestContext.getMethod()).withTag(Tags.HTTP_URL.getKey(), url(requestContext)).withTag(Tags.COMPONENT.getKey(), "jaxrs");
if (null != parentSpan) {
spanBuilder.asChildOf(parentSpan);
}
configureSpan(spanBuilder);
Span span = spanBuilder.start();
Scope spanScope = tracer.scopeManager().activate(span);
context.register(span);
context.register(spanScope);
context.register(ClientTracingFilter.class, span.context());
requestContext.setProperty(SPAN_PROPERTY, span);
requestContext.setProperty(SPAN_SCOPE_PROPERTY, spanScope);
if (context.get(TracingContext.class).isEmpty()) {
context.register(TracingContext.create(tracer, requestContext.getHeaders()));
}
context.get(TracingContext.class).ifPresent(tctx -> tctx.parentSpan(span.context()));
if (null == parentSpan) {
// register current span as the parent span for other (unless already exists)
context.register(span.context());
}
}
}
use of io.helidon.common.context.Context in project helidon by oracle.
the class HelloBean method getHelloAsync.
/**
* Runs via FT async thread.
*
* @return Hello string.
*/
@Asynchronous
public CompletionStage<String> getHelloAsync() {
Context context = Contexts.context().orElse(null);
Objects.requireNonNull(context);
// application scoped context
Objects.requireNonNull(context.get(RegistryFactory.class).orElse(null));
// request scoped context
Objects.requireNonNull(context.get(MyMessage.class).orElse(null));
return CompletableFuture.completedFuture("Hello World");
}
Aggregations