// 
// Decompiled by Procyon v0.6.0
// 

package com.nimbusds.jose.jwk.source;

import com.nimbusds.jose.KeySourceException;
import com.nimbusds.jose.util.Resource;
import java.io.IOException;
import com.nimbusds.jose.jwk.JWKSet;
import java.util.Objects;
import com.nimbusds.jose.util.ResourceRetriever;
import java.net.URL;
import com.nimbusds.jose.shaded.jcip.ThreadSafe;
import com.nimbusds.jose.proc.SecurityContext;

@ThreadSafe
public class URLBasedJWKSetSource<C extends SecurityContext> implements JWKSetSource<C>
{
    private final URL url;
    private final ResourceRetriever resourceRetriever;
    
    public URLBasedJWKSetSource(final URL url, final ResourceRetriever resourceRetriever) {
        Objects.requireNonNull(url, "The URL must not be null");
        this.url = url;
        Objects.requireNonNull(resourceRetriever, "The resource retriever must not be null");
        this.resourceRetriever = resourceRetriever;
    }
    
    public URL getJWKSetURL() {
        return this.url;
    }
    
    public ResourceRetriever getResourceRetriever() {
        return this.resourceRetriever;
    }
    
    @Override
    public JWKSet getJWKSet(final JWKSetCacheRefreshEvaluator refreshEvaluator, final long currentTime, final C context) throws KeySourceException {
        Resource resource;
        try {
            resource = this.getResourceRetriever().retrieveResource(this.getJWKSetURL());
        }
        catch (final IOException e) {
            throw new JWKSetRetrievalException("Couldn't retrieve JWK set from URL: " + e.getMessage(), e);
        }
        try {
            return JWKSet.parse(resource.getContent());
        }
        catch (final Exception e2) {
            throw new JWKSetParseException("Unable to parse JWK set", e2);
        }
    }
    
    @Override
    public void close() throws IOException {
    }
}
