001/* 002 * nimbus-jose-jwt 003 * 004 * Copyright 2012-2022, Connect2id Ltd. 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use 007 * this file except in compliance with the License. You may obtain a copy of the 008 * License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software distributed 013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 015 * specific language governing permissions and limitations under the License. 016 */ 017 018package com.nimbusds.jose.jwk.source; 019 020 021import java.util.Objects; 022 023import net.jcip.annotations.ThreadSafe; 024 025import com.nimbusds.jose.KeySourceException; 026import com.nimbusds.jose.jwk.JWKSet; 027import com.nimbusds.jose.proc.SecurityContext; 028import com.nimbusds.jose.util.health.HealthReport; 029import com.nimbusds.jose.util.health.HealthReportListener; 030import com.nimbusds.jose.util.health.HealthStatus; 031 032 033/** 034 * Decorates a {@linkplain JWKSetSource} with health status reporting. 035 * 036 * @author Thomas Rørvik Skjølberg 037 * @author Vladimir Dzhuvinov 038 * @version 2022-11-22 039 */ 040@ThreadSafe 041public class JWKSetSourceWithHealthStatusReporting<C extends SecurityContext> extends JWKSetSourceWrapper<C> { 042 043 044 private final HealthReportListener<JWKSetSourceWithHealthStatusReporting<C>, C> healthReportListener; 045 046 047 /** 048 * Creates a new JWK set source with health status reporting to the 049 * specified listener. 050 * 051 * @param source The JWK set source to wrap. Must not be 052 * {@code null}. 053 * @param healthReportListener The health report listener. Must not be 054 * {@code null}. 055 */ 056 public JWKSetSourceWithHealthStatusReporting(final JWKSetSource<C> source, 057 final HealthReportListener<JWKSetSourceWithHealthStatusReporting<C>, C> healthReportListener) { 058 super(source); 059 Objects.requireNonNull(healthReportListener); 060 this.healthReportListener = healthReportListener; 061 } 062 063 064 @Override 065 public JWKSet getJWKSet(final JWKSetCacheRefreshEvaluator refreshEvaluator, final long currentTime, final C context) 066 throws KeySourceException { 067 068 JWKSet jwkSet; 069 try { 070 jwkSet = getSource().getJWKSet(refreshEvaluator, currentTime, context); 071 healthReportListener.notify(new HealthReport<>(this, HealthStatus.HEALTHY, currentTime, context)); 072 } catch (Exception e) { 073 healthReportListener.notify(new HealthReport<>(this, HealthStatus.NOT_HEALTHY, e, currentTime, context)); 074 throw e; 075 } 076 077 return jwkSet; 078 } 079}