1 18 19 package sync4j.framework.notification; 20 21 import java.security.NoSuchAlgorithmException ; 22 23 24 31 public class TriggerNotificationMessage { 32 33 private DigestNotificationMessage digest; 35 private TriggerHeaderNotificationMessage header; 36 private TriggerBodyNotificationMessage body; 37 38 39 41 42 public TriggerNotificationMessage() {} 43 44 public TriggerNotificationMessage(DigestNotificationMessage digest, 45 TriggerHeaderNotificationMessage header, 46 TriggerBodyNotificationMessage body) { 47 this.digest = digest; 48 this.header = header; 49 this.body = body; 50 } 51 52 public TriggerNotificationMessage(DigestNotificationMessage digest, 53 TriggerHeaderNotificationMessage header) { 54 this.digest = digest; 55 this.header = header; 56 } 57 58 59 61 66 public DigestNotificationMessage getDigest() { 67 return digest; 68 } 69 70 75 public void setDigest(DigestNotificationMessage digest) { 76 this.digest = digest; 77 } 78 79 84 public TriggerHeaderNotificationMessage getHeader() { 85 return header; 86 } 87 88 93 public void setHeader(TriggerHeaderNotificationMessage header) { 94 this.header = header; 95 } 96 97 102 public TriggerBodyNotificationMessage getBody() { 103 return body; 104 } 105 106 111 public void setBody(TriggerBodyNotificationMessage body) { 112 this.body = body; 113 } 114 115 public byte[] computeTriggerNotificationMessage() throws NotificationException { 116 byte[] toReturn = null; 117 118 byte[] headerMessage = null; 119 byte[] digestMessage = null; 120 byte[] bodyMessage = null; 121 122 123 124 125 126 if (header == null) { 127 throw new NotificationException("Header of the notification message could not be null"); 128 } 129 130 if (digest == null) { 131 throw new NotificationException("Digest of the notification message could not be null"); 132 } 133 134 135 headerMessage = header.buildByteMessageValue(); 136 137 if (body != null) { 138 bodyMessage = body.getBodyMessage(); 139 } 140 141 142 try { 143 digestMessage = digest.computeDigestMessage(mergeMessage(headerMessage, bodyMessage, null)); 144 } catch (NoSuchAlgorithmException ex) { 145 throw new NotificationException("Error during digest computing", ex); 146 } 147 148 149 toReturn = mergeMessage(digestMessage, headerMessage, bodyMessage); 150 151 return toReturn; 152 } 153 154 155 157 164 private byte[] mergeMessage(byte[] msg1, byte[] msg2, byte[] msg3) { 165 166 byte[] toReturnMsg = null; 167 168 int lengthMsg1 = 0; 169 int lengthMsg2 = 0; 170 int lengthMsg3 = 0; 171 172 if (msg1 != null) { 173 lengthMsg1 = msg1.length; 174 } else { 175 msg1 = new byte[0]; 176 } 177 178 if (msg2 != null) { 179 lengthMsg2 = msg2.length; 180 } else { 181 msg2 = new byte[0]; 182 } 183 184 if (msg3 != null) { 185 lengthMsg3 = msg3.length; 186 } else { 187 msg3 = new byte[0]; 188 } 189 190 int toReturnLength = lengthMsg1 + lengthMsg2 + lengthMsg3; 191 192 toReturnMsg = new byte[toReturnLength]; 193 194 System.arraycopy(msg1, 0, toReturnMsg, 0, lengthMsg1); 195 System.arraycopy(msg2, 0, toReturnMsg, lengthMsg1, lengthMsg2); 196 System.arraycopy(msg3, 0, toReturnMsg, lengthMsg1 + lengthMsg2, lengthMsg3); 197 198 return toReturnMsg; 199 } 200 201 } | Popular Tags |