1 16 17 package org.apache.axis.handlers; 18 19 20 import org.apache.axis.AxisFault; 21 import org.apache.axis.Message; 22 import org.apache.axis.MessageContext; 23 import org.apache.axis.components.logger.LogFactory; 24 import org.apache.axis.soap.SOAPConstants; 25 import org.apache.axis.utils.Messages; 26 import org.apache.commons.logging.Log; 27 28 29 34 public class MD5AttachHandler extends org.apache.axis.handlers.BasicHandler { 35 protected static Log log = 36 LogFactory.getLog(MD5AttachHandler.class.getName()); 37 38 public void invoke(MessageContext msgContext) throws AxisFault { 39 log.debug("Enter: MD5AttachHandler::invoke"); 40 try { 41 Message msg = msgContext.getRequestMessage(); 43 SOAPConstants soapConstants = msgContext.getSOAPConstants(); 44 org.apache.axis.message.SOAPEnvelope env = (org.apache.axis.message.SOAPEnvelope) msg.getSOAPEnvelope(); 45 org.apache.axis.message.SOAPBodyElement sbe = env.getFirstBody(); org.w3c.dom.Element sbElement = sbe.getAsDOM(); 47 org.w3c.dom.Node n = sbElement.getFirstChild(); 49 50 for (; n != null && !(n instanceof org.w3c.dom.Element ); n = n.getNextSibling()); 51 org.w3c.dom.Element paramElement = (org.w3c.dom.Element ) n; 52 String href = paramElement.getAttribute(soapConstants.getAttrHref()); 54 org.apache.axis.Part ap = msg.getAttachmentsImpl().getAttachmentByReference(href); 55 javax.activation.DataHandler dh = org.apache.axis.attachments.AttachmentUtils.getActivationDataHandler(ap); 56 org.w3c.dom.Node timeNode = paramElement.getFirstChild(); 57 long startTime = -1; 58 59 if (timeNode != null && timeNode instanceof org.w3c.dom.Text ) { 60 String startTimeStr = ((org.w3c.dom.Text ) timeNode).getData(); 61 62 startTime = Long.parseLong(startTimeStr); 63 } 64 66 long receivedTime = System.currentTimeMillis(); 67 long elapsedTime = -1; 68 69 if (startTime > 0) elapsedTime = receivedTime - startTime; 72 String elapsedTimeStr = elapsedTime + ""; 73 75 java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5"); 76 java.io.InputStream attachmentStream = dh.getInputStream(); 77 int bread = 0; 78 byte[] buf = new byte[64 * 1024]; 79 80 do { 81 bread = attachmentStream.read(buf); 82 if (bread > 0) { 83 md.update(buf, 0, bread); 84 } 85 } 86 while (bread > -1); 87 attachmentStream.close(); 88 buf = null; 89 String contentType = dh.getContentType(); 91 92 if (contentType != null && contentType.length() != 0) { 93 md.update( contentType.getBytes("US-ASCII")); 94 } 95 96 sbe = env.getFirstBody(); 97 sbElement = sbe.getAsDOM(); 98 n = sbElement.getFirstChild(); 100 for (; n != null && !(n instanceof org.w3c.dom.Element ); n = n.getNextSibling()); 101 paramElement = (org.w3c.dom.Element ) n; 102 String MD5String = org.apache.axis.encoding.Base64.encode(md.digest()); 104 String senddata = " elapsedTime=" + elapsedTimeStr + " MD5=" + MD5String; 105 106 paramElement.appendChild( paramElement.getOwnerDocument().createTextNode(senddata)); 108 109 sbe = new org.apache.axis.message.SOAPBodyElement(sbElement); 110 env.clearBody(); 111 env.addBodyElement(sbe); 112 msg = new Message( env ); 113 114 msgContext.setResponseMessage( msg ); 115 } 116 catch ( Exception e ) { 117 log.error( Messages.getMessage("exception00"), e ); 118 throw AxisFault.makeFault(e); 119 } 120 121 log.debug("Exit: MD5AttachHandler::invoke"); 122 } 123 124 } 125 | Popular Tags |