Coverage Report - org.mule.routing.IdempotentSecureHashMessageFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
IdempotentSecureHashMessageFilter
0%
0/13
N/A
0
 
 1  
 /*
 2  
  * $Id: IdempotentSecureHashMessageFilter.java 20321 2010-11-24 15:21:24Z dfeist $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.routing;
 12  
 
 13  
 import org.mule.api.MessagingException;
 14  
 import org.mule.api.MuleEvent;
 15  
 import org.mule.api.routing.RoutingException;
 16  
 import org.mule.api.transformer.TransformerException;
 17  
 import org.mule.transformer.simple.ByteArrayToHexString;
 18  
 import org.mule.transformer.simple.SerializableToByteArray;
 19  
 
 20  
 import java.security.MessageDigest;
 21  
 import java.security.NoSuchAlgorithmException;
 22  
 
 23  
 /**
 24  
  * <code>IdempotentSecureHashMessageFilter</code> ensures that only unique messages are
 25  
  * received by a service. It does this by calculating the SHA-256 hash of the message
 26  
  * itself. This provides a value with an infinitesimally small chance of a collision. This
 27  
  * can be used to filter message duplicates. Please keep in mind that the hash is
 28  
  * calculated over the entire byte array representing the message, so any leading or
 29  
  * trailing spaces or extraneous bytes (like padding) can produce different hash values
 30  
  * for the same semantic message content. Care should be taken to ensure that messages do
 31  
  * not contain extraneous bytes. This class is useful when the message does not support
 32  
  * unique identifiers.
 33  
  */
 34  
 
 35  0
 public class IdempotentSecureHashMessageFilter extends IdempotentMessageFilter
 36  
 {
 37  0
     private String messageDigestAlgorithm = "SHA-256";
 38  
 
 39  0
     private final SerializableToByteArray objectToByteArray = new SerializableToByteArray();
 40  0
     private final ByteArrayToHexString byteArrayToHexString = new ByteArrayToHexString();
 41  
 
 42  
     @Override
 43  
     protected String getIdForEvent(MuleEvent event) throws MessagingException
 44  
     {
 45  
         try
 46  
         {
 47  0
             MessageDigest md = MessageDigest.getInstance(messageDigestAlgorithm);
 48  0
             return (String)byteArrayToHexString.transform(md.digest((byte[]) objectToByteArray.transform(event.getMessage()
 49  
                 .getPayload())));
 50  
         }
 51  0
         catch (NoSuchAlgorithmException nsa)
 52  
         {
 53  0
             throw new RoutingException(event,this, nsa);
 54  
         }
 55  0
         catch (TransformerException te)
 56  
         {
 57  0
             throw new RoutingException(event, this, te);
 58  
         }
 59  
     }
 60  
 
 61  
     public String getMessageDigestAlgorithm()
 62  
     {
 63  0
         return messageDigestAlgorithm;
 64  
     }
 65  
 
 66  
     public void setMessageDigestAlgorithm(String messageDigestAlgorithm)
 67  
     {
 68  0
         this.messageDigestAlgorithm = messageDigestAlgorithm;
 69  0
     }
 70  
 }