3838import java .io .IOException ;
3939import java .io .InputStream ;
4040import java .io .UncheckedIOException ;
41+ import java .lang .reflect .Type ;
4142import java .net .URI ;
4243import java .time .Duration ;
4344import java .time .Instant ;
@@ -123,6 +124,7 @@ public class AccessGrantClient {
123124 private static final Set <String > ACCESS_GRANT_TYPES = getAccessGrantTypes ();
124125 private static final Set <String > ACCESS_REQUEST_TYPES = getAccessRequestTypes ();
125126 private static final Set <String > ACCESS_DENIAL_TYPES = getAccessDenialTypes ();
127+ private static final Type JSON_TYPE_REF = new HashMap <String , Object >(){}.getClass ().getGenericSuperclass ();
126128
127129 private final Client client ;
128130 private final ClientCache <URI , Metadata > metadataCache ;
@@ -319,8 +321,7 @@ public CompletionStage<AccessCredentialVerification> verify(final AccessCredenti
319321 final Map <String , Object > presentation = new HashMap <>();
320322
321323 try (final InputStream is = new ByteArrayInputStream (credential .serialize ().getBytes (UTF_8 ))) {
322- final Map <String , Object > data = jsonService .fromJson (is ,
323- new HashMap <String , Object >(){}.getClass ().getGenericSuperclass ());
324+ final Map <String , Object > data = jsonService .fromJson (is , JSON_TYPE_REF );
324325 Utils .getCredentialsFromPresentation (data , credential .getTypes ()).stream ().findFirst ()
325326 .ifPresent (c -> presentation .put (VERIFIABLE_CREDENTIAL , c ));
326327 } catch (final IOException ex ) {
@@ -424,38 +425,45 @@ static String getPageQueryParam(final URI uri) {
424425 return null ;
425426 }
426427
427- @ SuppressWarnings ("unchecked" )
428428 <T extends AccessCredential > List <T > processFilterResponseBody (final InputStream input ,
429429 final Set <String > validTypes , final Class <T > clazz ) throws IOException {
430430
431431 final List <T > page = new ArrayList <>();
432- final Map <String , Object > data = jsonService .fromJson (input ,
433- new HashMap <String , Object >(){}.getClass ().getGenericSuperclass ());
434- System .out .println ("JSON: " + data );
435- Utils .asSet (data .get ("items" )).ifPresent (items -> {
432+ final Map <String , Object > data = jsonService .fromJson (input , JSON_TYPE_REF );
433+ Utils .asList (data .get ("items" )).ifPresent (items -> {
436434 for (final Object item : items ) {
437- Utils .asMap (item ).ifPresent (credential ->
435+ Utils .asMap (item ).ifPresent (credential -> {
438436 Utils .asSet (credential .get (TYPE )).ifPresent (types -> {
439437 types .retainAll (validTypes );
440438 if (!types .isEmpty ()) {
441439 final Map <String , Object > presentation = new HashMap <>();
442440 presentation .put (CONTEXT , Arrays .asList (VC_CONTEXT_URI ));
443441 presentation .put (TYPE , Arrays .asList ("VerifiablePresentation" ));
444442 presentation .put (VERIFIABLE_CREDENTIAL , Arrays .asList (credential ));
445- if (AccessGrant .class .equals (clazz )) {
446- page .add ((T ) AccessGrant .of (new String (serialize (presentation ), UTF_8 )));
447- } else if (AccessRequest .class .equals (clazz )) {
448- page .add ((T ) AccessRequest .of (new String (serialize (presentation ), UTF_8 )));
449- } else if (AccessDenial .class .equals (clazz )) {
450- page .add ((T ) AccessDenial .of (new String (serialize (presentation ), UTF_8 )));
443+ final T c = cast (presentation , clazz );
444+ if (c != null ) {
445+ page .add (c );
451446 }
452447 }
453- }));
448+ });
449+ });
454450 }
455451 });
456452 return page ;
457453 }
458454
455+ @ SuppressWarnings ("unchecked" )
456+ <T extends AccessCredential > T cast (final Map <String , Object > data , final Class <T > clazz ) {
457+ if (AccessGrant .class .equals (clazz )) {
458+ return (T ) AccessGrant .of (new String (serialize (data ), UTF_8 ));
459+ } else if (AccessRequest .class .equals (clazz )) {
460+ return (T ) AccessRequest .of (new String (serialize (data ), UTF_8 ));
461+ } else if (AccessDenial .class .equals (clazz )) {
462+ return (T ) AccessDenial .of (new String (serialize (data ), UTF_8 ));
463+ }
464+ return null ;
465+ }
466+
459467 /**
460468 * Perform an Access Credentials query and returns 0 to N matching access credentials.
461469 *
@@ -606,11 +614,9 @@ public <T extends AccessCredential> CompletionStage<T> fetch(final URI identifie
606614 });
607615 }
608616
609- @ SuppressWarnings ("unchecked" )
610617 <T extends AccessCredential > T processVerifiableCredential (final InputStream input , final Set <String > validTypes ,
611618 final Class <T > clazz ) throws IOException {
612- final Map <String , Object > data = jsonService .fromJson (input ,
613- new HashMap <String , Object >(){}.getClass ().getGenericSuperclass ());
619+ final Map <String , Object > data = jsonService .fromJson (input , JSON_TYPE_REF );
614620 final Set <String > types = Utils .asSet (data .get (TYPE )).orElseThrow (() ->
615621 new AccessGrantException ("Invalid Access Grant: no 'type' field" ));
616622 types .retainAll (validTypes );
@@ -619,22 +625,17 @@ <T extends AccessCredential> T processVerifiableCredential(final InputStream inp
619625 presentation .put (CONTEXT , Arrays .asList (VC_CONTEXT_URI ));
620626 presentation .put (TYPE , Arrays .asList ("VerifiablePresentation" ));
621627 presentation .put (VERIFIABLE_CREDENTIAL , Arrays .asList (data ));
622- if (AccessGrant .class .isAssignableFrom (clazz )) {
623- return (T ) AccessGrant .of (new String (serialize (presentation ), UTF_8 ));
624- } else if (AccessRequest .class .isAssignableFrom (clazz )) {
625- return (T ) AccessRequest .of (new String (serialize (presentation ), UTF_8 ));
626- } else if (AccessDenial .class .isAssignableFrom (clazz )) {
627- return (T ) AccessDenial .of (new String (serialize (presentation ), UTF_8 ));
628+ final T credential = cast (presentation , clazz );
629+ if (credential != null ) {
630+ return credential ;
628631 }
629632 }
630633 throw new AccessGrantException ("Invalid Access Grant: missing supported type" );
631634 }
632635
633- @ SuppressWarnings ("unchecked" )
634636 <T extends AccessCredential > List <T > processQueryResponse (final InputStream input , final Set <String > validTypes ,
635637 final Class <T > clazz ) throws IOException {
636- final Map <String , Object > data = jsonService .fromJson (input ,
637- new HashMap <String , Object >(){}.getClass ().getGenericSuperclass ());
638+ final Map <String , Object > data = jsonService .fromJson (input , JSON_TYPE_REF );
638639 final List <T > grants = new ArrayList <>();
639640 for (final Object item : getCredentials (data )) {
640641 Utils .asMap (item ).ifPresent (credential ->
@@ -645,12 +646,9 @@ <T extends AccessCredential> List<T> processQueryResponse(final InputStream inpu
645646 presentation .put (CONTEXT , Arrays .asList (VC_CONTEXT_URI ));
646647 presentation .put (TYPE , Arrays .asList ("VerifiablePresentation" ));
647648 presentation .put (VERIFIABLE_CREDENTIAL , Arrays .asList (credential ));
648- if (AccessGrant .class .equals (clazz )) {
649- grants .add ((T ) AccessGrant .of (new String (serialize (presentation ), UTF_8 )));
650- } else if (AccessRequest .class .equals (clazz )) {
651- grants .add ((T ) AccessRequest .of (new String (serialize (presentation ), UTF_8 )));
652- } else if (AccessDenial .class .equals (clazz )) {
653- grants .add ((T ) AccessDenial .of (new String (serialize (presentation ), UTF_8 )));
649+ final T c = cast (presentation , clazz );
650+ if (c != null ) {
651+ grants .add (c );
654652 }
655653 }
656654 }));
@@ -672,8 +670,7 @@ CompletionStage<Metadata> v1Metadata() {
672670 try (final InputStream input = res .body ()) {
673671 final int httpStatus = res .statusCode ();
674672 if (isSuccess (httpStatus )) {
675- final Map <String , Object > data = jsonService .fromJson (input ,
676- new HashMap <String , Object >(){}.getClass ().getGenericSuperclass ());
673+ final Map <String , Object > data = jsonService .fromJson (input , JSON_TYPE_REF );
677674 return data ;
678675 }
679676 throw new AccessGrantException (
0 commit comments