logahawk.formatters
Class HashedMessageFormatter

java.lang.Object
  extended by logahawk.formatters.HashedMessageFormatter
All Implemented Interfaces:
MessageFormatter
Direct Known Subclasses:
RollingHashedMessageFormatter

@ThreadSafe
public class HashedMessageFormatter
extends Object
implements MessageFormatter

Calculates a hash of the log message, and prepends that has to the message. This formatter uses another MessageFormatter to format the actual message, and this class handles the hashing. The purpose of this MessageFormatteris to provide integrity for individual log messages. This cannot determine whether log messages have been added, removed, or reordered. (See RollingHashedMessageFormatter


Nested Class Summary
protected static class HashedMessageFormatter.HashMessagePair
          Helper class that holds the hash and original message separately.
 
Field Summary
protected  String hashPrefix
           
protected  String hashSuffix
           
protected  MessageDigest messageDigest
          Used to generate the hash.
protected  MessageFormatter messageFormatter
           
 
Constructor Summary
HashedMessageFormatter(MessageFormatter messageFormatter, MessageDigest messageDigest)
           
 
Method Summary
protected  String createDigest(String message)
          Creates a digest from the provided message.
protected  String encode(byte[] digest)
          Encodes the digest hash bytes into something smaller for a log message.
 String format(LogMeta meta, String message)
          The log message is first created by the constructor provided MessageFormatter.
 String getHashPrefix()
          Returns the prefix for the Hash.
 String getHashSuffix()
          Returns the suffix for the Hash.
 void setHashPrefix(String value)
           
 void setHashSuffix(String value)
           
protected  List<HashedMessageFormatter.HashMessagePair> splitMessage(List<String> messages)
          Given a list of log messages, split the hash and message and return them inside of HashedMessageFormatter.HashMessagePairs.
 int verify(List<String> messages)
          Verifies that the hash contained within the messages is the correct hash.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

hashPrefix

protected volatile String hashPrefix

hashSuffix

protected volatile String hashSuffix

messageDigest

protected final MessageDigest messageDigest
Used to generate the hash. This will be reused for every message, and reset before each usage.


messageFormatter

protected final MessageFormatter messageFormatter
Constructor Detail

HashedMessageFormatter

public HashedMessageFormatter(MessageFormatter messageFormatter,
                              MessageDigest messageDigest)
Method Detail

format

public String format(LogMeta meta,
                     String message)
The log message is first created by the constructor provided MessageFormatter. Then that message is digested. That digest is prepended to the message, and that final message is returned.

Specified by:
format in interface MessageFormatter

getHashPrefix

public String getHashPrefix()
Returns the prefix for the Hash.


setHashPrefix

public void setHashPrefix(String value)

getHashSuffix

public String getHashSuffix()
Returns the suffix for the Hash.


setHashSuffix

public void setHashSuffix(String value)

createDigest

protected String createDigest(String message)
Creates a digest from the provided message. This method may not be idempotent depending on the implementation.


encode

protected String encode(byte[] digest)
Encodes the digest hash bytes into something smaller for a log message. This method does not change the MessageDigest

Parameters:
digest - The byte[] buffer to encode, usually the results of a MessageDigest.
Returns:
A char[] containing hexadecimal characters
Since:
1.4

splitMessage

protected List<HashedMessageFormatter.HashMessagePair> splitMessage(List<String> messages)
Given a list of log messages, split the hash and message and return them inside of HashedMessageFormatter.HashMessagePairs. This method exists to simplify verify(List) to make derived implementations simpler.


verify

public int verify(List<String> messages)
           throws IllegalArgumentException
Verifies that the hash contained within the messages is the correct hash.

Returns:
The index of the first invalid message. If the returned index equals the length of the provided List, then all messages are valid.
Throws:
IllegalArgumentException - Thrown if the message does not contain a hash in the expected location.