Coverage Report - org.mule.routing.inbound.IdempotentSecureHashReceiver
 
Classes in this File Line Coverage Branch Coverage Complexity
IdempotentSecureHashReceiver
0%
0/9
N/A
6
 
 1  
 /*
 2  
  * $Id: IdempotentSecureHashReceiver.java 8952 2007-10-05 23:11:13Z holger $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.inbound;
 12  
 
 13  
 import org.mule.transformers.simple.ByteArrayToHexString;
 14  
 import org.mule.transformers.simple.SerializableToByteArray;
 15  
 import org.mule.umo.MessagingException;
 16  
 import org.mule.umo.UMOEvent;
 17  
 import org.mule.umo.routing.RoutingException;
 18  
 import org.mule.umo.transformer.TransformerException;
 19  
 
 20  
 import java.security.MessageDigest;
 21  
 import java.security.NoSuchAlgorithmException;
 22  
 
 23  
 /**
 24  
  * <code>IdempotentSecureHashReceiver</code> ensures that only unique messages are
 25  
  * received by a component. 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 IdempotentSecureHashReceiver extends IdempotentReceiver
 36  
 {
 37  
     private static final 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 Object getIdForEvent(UMOEvent event) throws MessagingException
 44  
     {
 45  
         try
 46  
         {
 47  0
             MessageDigest md = MessageDigest.getInstance(messageDigestAlgorithm);
 48  0
             return byteArrayToHexString.transform(md.digest((byte[]) objectToByteArray.transform(event.getMessage()
 49  
                 .getPayload())));
 50  
         }
 51  0
         catch (NoSuchAlgorithmException nsa)
 52  
         {
 53  0
             throw new RoutingException(event.getMessage(), event.getEndpoint(), nsa);
 54  
         }
 55  0
         catch (TransformerException te)
 56  
         {
 57  0
             throw new RoutingException(event.getMessage(), event.getEndpoint(), te);
 58  
         }
 59  
     }
 60  
 }