-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3645023
commit 92ee1f1
Showing
2 changed files
with
128 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
...a/no/nav/vedtak/felles/integrasjon/rest/jersey/PropagatingThreadPoolExecutorProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |