Class MaskingLoggingFilter

  • All Implemented Interfaces:
    jakarta.ws.rs.client.ClientRequestFilter, jakarta.ws.rs.client.ClientResponseFilter, jakarta.ws.rs.ext.WriterInterceptor

    @Priority(-2147483648)
    public class MaskingLoggingFilter
    extends java.lang.Object
    implements jakarta.ws.rs.client.ClientRequestFilter, jakarta.ws.rs.client.ClientResponseFilter, jakarta.ws.rs.ext.WriterInterceptor
    This class logs request and response info masking HTTP header values that are known to contain sensitive information. This class was patterned after org.glassfish.jersey.logging.LoggingInterceptor, but written in such a way that it could be sub-classed and have its behavior modified.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      protected class  MaskingLoggingFilter.LoggingStream
      This class is responsible for logging the request entities, it will truncate at maxEntitySize and add "...more..." to the end of the entity log string.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.concurrent.atomic.AtomicLong _id  
      static java.util.List<java.lang.String> DEFAULT_MASKED_HEADER_NAMES
      Default list of header names that should be masked.
      protected static java.lang.String ENTITY_STREAM_PROPERTY
      Property name for the entity stream property
      protected java.util.logging.Level level  
      protected java.util.logging.Logger logger  
      protected static java.lang.String LOGGING_ID_PROPERTY
      Property name for the logging record id property
      protected java.util.Set<java.lang.String> maskedHeaderNames  
      protected int maxEntitySize  
      protected static java.lang.String REQUEST_PREFIX
      Prefix for request log entries.
      protected static java.lang.String RESPONSE_PREFIX
      Prefix for response log entries.
      protected static java.lang.String SECTION_PREFIX
      Prefix that marks the beginning of a request or response section.
    • Constructor Summary

      Constructors 
      Constructor Description
      MaskingLoggingFilter​(java.util.logging.Logger logger, java.util.logging.Level level)
      Creates a masking logging filter for the specified logger with entity logging disabled.
      MaskingLoggingFilter​(java.util.logging.Logger logger, java.util.logging.Level level, int maxEntitySize)
      Creates a masking logging filter for the specified logger.
      MaskingLoggingFilter​(java.util.logging.Logger logger, java.util.logging.Level level, int maxEntitySize, java.util.List<java.lang.String> maskedHeaderNames)
      Creates a masking logging filter for the specified logger.
      MaskingLoggingFilter​(java.util.logging.Logger logger, java.util.logging.Level level, java.util.List<java.lang.String> maskedHeaderNames)
      Creates a masking logging filter for the specified logger with entity logging disabled.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addMaskedHeaderName​(java.lang.String maskedHeaderName)
      Add a header name to the list of masked header names.
      protected java.lang.StringBuilder appendId​(java.lang.StringBuilder sb, long id)  
      void aroundWriteTo​(jakarta.ws.rs.ext.WriterInterceptorContext context)  
      protected void buildEntityLogString​(java.lang.StringBuilder sb, byte[] entity, int entitySize, java.nio.charset.Charset charset)  
      void filter​(jakarta.ws.rs.client.ClientRequestContext requestContext)  
      void filter​(jakarta.ws.rs.client.ClientRequestContext requestContext, jakarta.ws.rs.client.ClientResponseContext responseContext)  
      protected java.util.Set<java.util.Map.Entry<java.lang.String,​java.util.List<java.lang.String>>> getSortedHeaders​(java.util.Set<java.util.Map.Entry<java.lang.String,​java.util.List<java.lang.String>>> headers)  
      protected void log​(java.lang.StringBuilder sb)  
      protected void printHeaders​(java.lang.StringBuilder sb, long id, java.lang.String prefix, jakarta.ws.rs.core.MultivaluedMap<java.lang.String,​java.lang.String> headers)
      Logs each of the HTTP headers, masking the value of the header if the header key is in the list of masked header names.
      protected void printRequestLine​(java.lang.StringBuilder sb, java.lang.String note, long id, java.lang.String method, java.net.URI uri)  
      protected void printResponseLine​(java.lang.StringBuilder sb, java.lang.String note, long id, int status)  
      void setMaskedHeaderNames​(java.util.List<java.lang.String> maskedHeaderNames)
      Set the list of header names to mask values for.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_MASKED_HEADER_NAMES

        public static final java.util.List<java.lang.String> DEFAULT_MASKED_HEADER_NAMES
        Default list of header names that should be masked.
      • REQUEST_PREFIX

        protected static final java.lang.String REQUEST_PREFIX
        Prefix for request log entries.
        See Also:
        Constant Field Values
      • RESPONSE_PREFIX

        protected static final java.lang.String RESPONSE_PREFIX
        Prefix for response log entries.
        See Also:
        Constant Field Values
      • SECTION_PREFIX

        protected static final java.lang.String SECTION_PREFIX
        Prefix that marks the beginning of a request or response section.
        See Also:
        Constant Field Values
      • ENTITY_STREAM_PROPERTY

        protected static final java.lang.String ENTITY_STREAM_PROPERTY
        Property name for the entity stream property
      • LOGGING_ID_PROPERTY

        protected static final java.lang.String LOGGING_ID_PROPERTY
        Property name for the logging record id property
      • logger

        protected final java.util.logging.Logger logger
      • level

        protected final java.util.logging.Level level
      • maxEntitySize

        protected final int maxEntitySize
      • _id

        protected final java.util.concurrent.atomic.AtomicLong _id
      • maskedHeaderNames

        protected java.util.Set<java.lang.String> maskedHeaderNames
    • Constructor Detail

      • MaskingLoggingFilter

        public MaskingLoggingFilter​(java.util.logging.Logger logger,
                                    java.util.logging.Level level)
        Creates a masking logging filter for the specified logger with entity logging disabled.
        Parameters:
        logger - the logger to log messages to
        level - level at which the messages will be logged
      • MaskingLoggingFilter

        public MaskingLoggingFilter​(java.util.logging.Logger logger,
                                    java.util.logging.Level level,
                                    int maxEntitySize)
        Creates a masking logging filter for the specified logger.
        Parameters:
        logger - the logger to log messages to
        level - level at which the messages will be logged
        maxEntitySize - maximum number of entity bytes to be logged. When logging if the maxEntitySize is reached, the entity logging will be truncated at maxEntitySize and "...more..." will be added at the end of the log entry. If maxEntitySize is <= 0, entity logging will be disabled
      • MaskingLoggingFilter

        public MaskingLoggingFilter​(java.util.logging.Logger logger,
                                    java.util.logging.Level level,
                                    java.util.List<java.lang.String> maskedHeaderNames)
        Creates a masking logging filter for the specified logger with entity logging disabled.
        Parameters:
        logger - the logger to log messages to
        level - level at which the messages will be logged
        maskedHeaderNames - a list of header names that should have the values masked
      • MaskingLoggingFilter

        public MaskingLoggingFilter​(java.util.logging.Logger logger,
                                    java.util.logging.Level level,
                                    int maxEntitySize,
                                    java.util.List<java.lang.String> maskedHeaderNames)
        Creates a masking logging filter for the specified logger.
        Parameters:
        logger - the logger to log messages to
        level - level at which the messages will be logged
        maxEntitySize - maximum number of entity bytes to be logged. When logging if the maxEntitySize is reached, the entity logging will be truncated at maxEntitySize and "...more..." will be added at the end of the log entry. If maxEntitySize is <= 0, entity logging will be disabled
        maskedHeaderNames - a list of header names that should have the values masked
    • Method Detail

      • setMaskedHeaderNames

        public void setMaskedHeaderNames​(java.util.List<java.lang.String> maskedHeaderNames)
        Set the list of header names to mask values for. If null, will clear the header names to mask.
        Parameters:
        maskedHeaderNames - a list of header names that should have the values masked, if null, will clear the header names to mask
      • addMaskedHeaderName

        public void addMaskedHeaderName​(java.lang.String maskedHeaderName)
        Add a header name to the list of masked header names.
        Parameters:
        maskedHeaderName - the masked header name to add
      • log

        protected void log​(java.lang.StringBuilder sb)
      • appendId

        protected java.lang.StringBuilder appendId​(java.lang.StringBuilder sb,
                                                   long id)
      • printRequestLine

        protected void printRequestLine​(java.lang.StringBuilder sb,
                                        java.lang.String note,
                                        long id,
                                        java.lang.String method,
                                        java.net.URI uri)
      • printResponseLine

        protected void printResponseLine​(java.lang.StringBuilder sb,
                                         java.lang.String note,
                                         long id,
                                         int status)
      • getSortedHeaders

        protected java.util.Set<java.util.Map.Entry<java.lang.String,​java.util.List<java.lang.String>>> getSortedHeaders​(java.util.Set<java.util.Map.Entry<java.lang.String,​java.util.List<java.lang.String>>> headers)
      • printHeaders

        protected void printHeaders​(java.lang.StringBuilder sb,
                                    long id,
                                    java.lang.String prefix,
                                    jakarta.ws.rs.core.MultivaluedMap<java.lang.String,​java.lang.String> headers)
        Logs each of the HTTP headers, masking the value of the header if the header key is in the list of masked header names.
        Parameters:
        sb - the StringBuilder to build up the logging info in
        id - the ID for the logging line
        prefix - the logging line prefix character
        headers - a MultiValue map holding the header keys and values
      • buildEntityLogString

        protected void buildEntityLogString​(java.lang.StringBuilder sb,
                                            byte[] entity,
                                            int entitySize,
                                            java.nio.charset.Charset charset)
      • filter

        public void filter​(jakarta.ws.rs.client.ClientRequestContext requestContext)
                    throws java.io.IOException
        Specified by:
        filter in interface jakarta.ws.rs.client.ClientRequestFilter
        Throws:
        java.io.IOException
      • filter

        public void filter​(jakarta.ws.rs.client.ClientRequestContext requestContext,
                           jakarta.ws.rs.client.ClientResponseContext responseContext)
                    throws java.io.IOException
        Specified by:
        filter in interface jakarta.ws.rs.client.ClientResponseFilter
        Throws:
        java.io.IOException
      • aroundWriteTo

        public void aroundWriteTo​(jakarta.ws.rs.ext.WriterInterceptorContext context)
                           throws java.io.IOException,
                                  jakarta.ws.rs.WebApplicationException
        Specified by:
        aroundWriteTo in interface jakarta.ws.rs.ext.WriterInterceptor
        Throws:
        java.io.IOException
        jakarta.ws.rs.WebApplicationException