use of io.helidon.common.context.Context in project helidon by oracle.
the class Slf4jMdcTest method testThreadPropagationWithEmptyMdc.
@Test
public void testThreadPropagationWithEmptyMdc() {
Context context = Context.create();
ExecutorService executor = Contexts.wrap(Executors.newFixedThreadPool(1));
Contexts.runInContext(context, () -> {
try {
Boolean value = executor.submit(new TestEmptyMdc()).get();
assertThat(value, is(true));
} catch (Exception e) {
throw new ExecutorException("failed to execute", e);
}
});
}
use of io.helidon.common.context.Context in project helidon by oracle.
the class MpTracingInterceptor method locateParent.
private Optional<SpanContext> locateParent() {
// first check if we are in an active span
Span active = tracer.activeSpan();
if (null != active) {
// in case there is an active span (such as from JAX-RS resource or from another bean), use it as a parent
return Optional.of(active.context());
}
Optional<Context> context = Contexts.context();
return context.flatMap(ctx -> ctx.get(SpanContext.class)).or(() -> context.flatMap(ctx -> ctx.get(ClientTracingFilter.class, SpanContext.class)));
}
use of io.helidon.common.context.Context in project helidon by oracle.
the class ClientSecurityFilter method doFilter.
private void doFilter(ClientRequestContext requestContext) {
// find the context - if not cannot propagate
Optional<SecurityContext> securityContext = findContext(requestContext);
if (securityContext.isPresent()) {
outboundSecurity(requestContext, securityContext.get());
} else {
LOGGER.finest("Security context not available, using empty one. You can define it using " + "property \"" + ClientSecurity.PROPERTY_CONTEXT + "\" on request");
// use current context, or create a new one if we run outside of Helidon context
Context context = Contexts.context().orElseGet(() -> Context.builder().id("security-" + CONTEXT_COUNTER.incrementAndGet()).build());
// create a new security context for current request (not authenticated)
Optional<SecurityContext> newSecurityContext = context.get(Security.class).map(it -> it.createContext(context.id()));
if (newSecurityContext.isPresent()) {
// run in the context we obtained above with the new security context
// we may still propagate security information (such as when we explicitly configure outbound
// security in outbound target of a provider
Contexts.runInContext(context, () -> outboundSecurity(requestContext, newSecurityContext.get()));
} else {
// we cannot do anything - security is not available in global or current context, cannot propagate
LOGGER.finest("Security is not available in global or current context, cannot propagate identity.");
}
}
}
use of io.helidon.common.context.Context in project helidon by oracle.
the class IdcsMtRoleMapperRxProvider method getGrantsFromServer.
/**
* Get grants from IDCS server. The result is cached.
*
* @param idcsTenantId ID of the IDCS tenant
* @param idcsAppName Name of IDCS application
* @param subject subject to get grants for
* @return optional list of grants from server
*/
protected Single<List<? extends Grant>> getGrantsFromServer(String idcsTenantId, String idcsAppName, Subject subject) {
String subjectName = subject.principal().getName();
String subjectType = (String) subject.principal().abacAttribute("sub_type").orElse(defaultIdcsSubjectType());
RoleMapTracing tracing = SecurityTracing.get().roleMapTracing("idcs");
return Single.create(getAppToken(idcsTenantId, tracing)).flatMapSingle(maybeAppToken -> {
if (maybeAppToken.isEmpty()) {
return Single.error(new SecurityException("Application token not available"));
}
return Single.just(maybeAppToken.get());
}).flatMapSingle(appToken -> {
JsonObjectBuilder requestBuilder = JSON.createObjectBuilder().add("mappingAttributeValue", subjectName).add("subjectType", subjectType).add("appName", idcsAppName).add("includeMemberships", true);
JsonArrayBuilder arrayBuilder = JSON.createArrayBuilder();
arrayBuilder.add("urn:ietf:params:scim:schemas:oracle:idcs:Asserter");
requestBuilder.add("schemas", arrayBuilder);
Context parentContext = Contexts.context().orElseGet(Contexts::globalContext);
Context childContext = Context.builder().parent(parentContext).build();
tracing.findParent().ifPresent(childContext::register);
WebClientRequestBuilder post = oidcConfig().generalWebClient().post().context(childContext).uri(multitenantEndpoints.assertEndpoint(idcsTenantId)).headers(it -> {
it.add(Http.Header.AUTHORIZATION, "Bearer " + appToken);
return it;
});
return processRoleRequest(post, requestBuilder.build(), subjectName);
});
}
use of io.helidon.common.context.Context in project helidon by oracle.
the class HelloBean method getHello.
/**
* Runs in Jersey/Helidon thread.
*
* @return Hello string.
*/
public String getHello() {
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 "Hello World";
}
Aggregations