001package com.nimbusds.jose.jwk; 002 003 004import java.util.*; 005 006import net.jcip.annotations.Immutable; 007 008 009/** 010 * Selects (filters) one or more JSON Web Keys (JWKs) from a JWK set. 011 * 012 * @author Vladimir Dzhuvinov 013 * @version 2015-04-15 014 */ 015@Immutable 016public final class JWKSelector { 017 018 019 /** 020 * The JWK matcher. 021 */ 022 private final JWKMatcher matcher; 023 024 025 /** 026 * Creates a new JWK selector (filter). 027 * 028 * @param matcher Specifies the JWK matching criteria. Must not be 029 * {@code null}. 030 */ 031 public JWKSelector(final JWKMatcher matcher) { 032 033 if (matcher == null) { 034 throw new IllegalArgumentException("The JWK matcher must not be null"); 035 } 036 037 this.matcher = matcher; 038 } 039 040 041 /** 042 * Returns the JWK matcher. 043 * 044 * @return The JWK matcher. 045 */ 046 public JWKMatcher getMatcher() { 047 048 return matcher; 049 } 050 051 052 /** 053 * Selects the keys from the specified JWK set according to the 054 * matcher's criteria. 055 * 056 * @param jwkSet The JWK set. May be {@code null}. 057 * 058 * @return The selected keys, ordered by their position in the JWK set, 059 * empty list if none were matched or the JWK is {@code null}. 060 */ 061 public List<JWK> select(final JWKSet jwkSet) { 062 063 List<JWK> selectedKeys = new LinkedList<>(); 064 065 if (jwkSet == null) 066 return selectedKeys; 067 068 for (JWK key: jwkSet.getKeys()) { 069 070 if (matcher.matches(key)) { 071 selectedKeys.add(key); 072 } 073 } 074 075 return selectedKeys; 076 } 077}