View Javadoc

1   /*
2    * $Id: IdempotentSecureHashMessageFilter.java 22146 2011-06-08 07:40:29Z dirk.olmes $
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  public class IdempotentSecureHashMessageFilter extends IdempotentMessageFilter
36  {
37      private String messageDigestAlgorithm = "SHA-256";
38  
39      private final SerializableToByteArray objectToByteArray = new SerializableToByteArray();
40      private final ByteArrayToHexString byteArrayToHexString = new ByteArrayToHexString();
41  
42      @Override
43      protected String getIdForEvent(MuleEvent event) throws MessagingException
44      {
45          try
46          {
47              Object payload = event.getMessage().getPayload();
48              byte[] bytes = (byte[]) objectToByteArray.transform(payload);
49              MessageDigest md = MessageDigest.getInstance(messageDigestAlgorithm);
50              byte[] digestedBytes = md.digest(bytes);
51              return (String)byteArrayToHexString.transform(digestedBytes);
52          }
53          catch (NoSuchAlgorithmException nsa)
54          {
55              throw new RoutingException(event,this, nsa);
56          }
57          catch (TransformerException te)
58          {
59              throw new RoutingException(event, this, te);
60          }
61      }
62  
63      public String getMessageDigestAlgorithm()
64      {
65          return messageDigestAlgorithm;
66      }
67  
68      public void setMessageDigestAlgorithm(String messageDigestAlgorithm)
69      {
70          this.messageDigestAlgorithm = messageDigestAlgorithm;
71      }
72  }