private static Object deserializeHeaders ( HttpHeaders headers , SerializerAdapter serializer , HttpResponseDecodeData decodeData ) throws IOException { final Type deserializedHeadersType = decodeData . headersType ( ) ; if ( deserializedHeadersType == null ) { return null ; } else { final String headersJsonString = serializer . serialize ( headers , SerializerEncoding . JSON ) ; Object deserializedHeaders = serializer . deserialize ( headersJsonString , deserializedHeadersType , SerializerEncoding . JSON ) ; final Class < ? > deserializedHeadersClass = TypeUtil . getRawClass ( deserializedHeadersType ) ; final Field [ ] declaredFields = deserializedHeadersClass . getDeclaredFields ( ) ; for ( final Field declaredField : declaredFields ) { if ( declaredField . isAnnotationPresent ( HeaderCollection . class ) ) { final Type declaredFieldType = declaredField . getGenericType ( ) ; if ( TypeUtil . isTypeOrSubTypeOf ( declaredField . getType ( ) , Map . class ) ) { final Type [ ] mapTypeArguments = TypeUtil . getTypeArguments ( declaredFieldType ) ; if ( mapTypeArguments . length == 2 && mapTypeArguments [ 0 ] == String . class && mapTypeArguments [ 1 ] == String . class ) { final HeaderCollection headerCollectionAnnotation = declaredField . getAnnotation ( HeaderCollection . class ) ; final String headerCollectionPrefix = headerCollectionAnnotation . value ( ) . toLowerCase ( Locale . ROOT ) ; final int headerCollectionPrefixLength = headerCollectionPrefix . length ( ) ; if ( headerCollectionPrefixLength > 0 ) { final Map < String , String > headerCollection = new HashMap <> ( ) ; for ( final HttpHeader header : headers ) { final String headerName = header . name ( ) ; if ( headerName . toLowerCase ( Locale . ROOT ) . startsWith ( headerCollectionPrefix ) ) { headerCollection . put ( headerName . substring ( headerCollectionPrefixLength ) , header . value ( ) ) ; } } final boolean declaredFieldAccessibleBackup = declaredField . isAccessible ( ) ; try { if ( ! declaredFieldAccessibleBackup ) { declaredField . setAccessible ( true ) ; } declaredField . set ( deserializedHeaders , headerCollection ) ; } catch ( IllegalAccessException ignored ) { } finally { if ( ! declaredFieldAccessibleBackup ) { declaredField . setAccessible ( declaredFieldAccessibleBackup ) ; } } } } } } } return deserializedHeaders ; } }
Deserialize the provided headers returned from a REST API to an entity instance declared as the model to hold Matching headers .