Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
janolaveide committed Jun 4, 2021
1 parent 3645023 commit 92ee1f1
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import no.nav.vedtak.exception.TekniskException;
import no.nav.vedtak.felles.integrasjon.rest.HttpRequestRetryHandler;
import no.nav.vedtak.felles.integrasjon.rest.OidcRestClientResponseHandler.StringResponseHandler;
import no.nav.vedtak.felles.integrasjon.rest.jersey.tokenx.PropagatingThreadPoolExecutorProvider;
import no.nav.vedtak.felles.integrasjon.rest.RestClientSupportProdusent;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package no.nav.vedtak.felles.integrasjon.rest.jersey;

import static no.nav.security.token.support.jaxrs.JaxrsTokenValidationContextHolder.getHolder;
import static no.nav.vedtak.sikkerhet.context.SubjectHandler.getSubjectHandler;
import static org.slf4j.MDC.getCopyOfContextMap;

import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.security.auth.Subject;

import org.glassfish.jersey.client.ClientAsyncExecutor;
import org.glassfish.jersey.spi.ThreadPoolExecutorProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.binder.jvm.ExecutorServiceMetrics;
import no.nav.security.token.support.core.context.TokenValidationContext;
import no.nav.security.token.support.jaxrs.JaxrsTokenValidationContextHolder;
import no.nav.vedtak.sikkerhet.context.ThreadLocalSubjectHandler;

@ClientAsyncExecutor
class PropagatingThreadPoolExecutorProvider extends ThreadPoolExecutorProvider {

private static final Logger LOG = LoggerFactory.getLogger(PropagatingThreadPoolExecutorProvider.class);

public PropagatingThreadPoolExecutorProvider() {
super(PropagatingThreadPoolExecutorProvider.class.getSimpleName());
}

@Override
public ThreadPoolExecutor createExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory, RejectedExecutionHandler handler) {
return new PropagatingThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS, workQueue, threadFactory, handler);
}

static class PropagatingThreadPoolExecutor extends ThreadPoolExecutor {
PropagatingThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory, RejectedExecutionHandler handler) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
new ExecutorServiceMetrics(this, getClass().getSimpleName(), Tags.empty()).bindTo(Metrics.globalRegistry);
}

@Override
public void execute(Runnable task) {
super.execute(propagate(task));
}

private static Runnable propagate(Runnable task) {
return new PropagatingRunnable(task, getSubjectHandler().getSubject(), getHolder().getTokenValidationContext(), getCopyOfContextMap());
}
}

private static class PropagatingRunnable implements Runnable {

private static final Logger LOG = LoggerFactory.getLogger(PropagatingRunnable.class);
private final Runnable task;
private final TokenValidationContext ctx;
private final Map<String, String> mdc;
private final Subject subject;

PropagatingRunnable(Runnable task, Subject subject, TokenValidationContext ctx, Map<String, String> mdc) {
this.task = task;
this.subject = subject;
this.ctx = ctx;
this.mdc = mdc;
}

@Override
public void run() {
propagate();
try {
task.run();
} finally {
cleanup();
}
}

private void propagate() {
propagateMDCIfSet();
propagateSubjectIfSet();
propagateContextIfSet();
}

private void propagateContextIfSet() {
if (ctx != null && ctx.hasValidToken()) {
LOG.trace("Propagerer context");
JaxrsTokenValidationContextHolder.getHolder().setTokenValidationContext(ctx);
} else {
LOG.trace("Ingen context å propagere");
}
}

private void propagateSubjectIfSet() {
if (subject != null) {
LOG.trace("Propagerer subject fra subject handler");
ThreadLocalSubjectHandler.class.cast(getSubjectHandler()).setSubject(subject);
} else {
LOG.trace("Intet subject å propagere");
}
}

private void propagateMDCIfSet() {
MDC.clear();
if (mdc != null) {
LOG.trace("Propagerer {} verdier fra MDC", mdc.size());
MDC.setContextMap(mdc);
} else {
LOG.trace("Ingen MDC å propagere");
}
}
}

private static void cleanup() {
LOG.trace("Rydder opp i tråden");
JaxrsTokenValidationContextHolder.getHolder().setTokenValidationContext(null);
MDC.clear();
ThreadLocalSubjectHandler.class.cast(getSubjectHandler()).setSubject(null);
LOG.trace("Ryddet opp i tråden OK");
}
}

0 comments on commit 92ee1f1

Please sign in to comment.