KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > handlers > MD5AttachHandler


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.<t_X>env.get
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55
56 package org.jboss.axis.handlers;
57
58
59 import org.jboss.axis.AxisFault;
60 import org.jboss.axis.Message;
61 import org.jboss.axis.MessageContext;
62 import org.jboss.axis.soap.SOAPConstants;
63 import org.jboss.axis.utils.Messages;
64 import org.jboss.logging.Logger;
65
66
67 /**
68  * @author Doug Davis (dug@us.ibm.com)
69  * @author Rick Rineholt
70  */

71 public class MD5AttachHandler extends org.jboss.axis.handlers.BasicHandler
72 {
73    private static Logger log = Logger.getLogger(MD5AttachHandler.class.getName());
74
75    public void invoke(MessageContext msgContext) throws AxisFault
76    {
77       log.debug("Enter: EchoHandler::invoke");
78       try
79       {
80          // log.debug("IN MD5");
81
Message msg = msgContext.getRequestMessage();
82          SOAPConstants soapConstants = msgContext.getSOAPConstants();
83          org.jboss.axis.message.SOAPEnvelopeAxisImpl env = (org.jboss.axis.message.SOAPEnvelopeAxisImpl)msg.getSOAPEnvelope();
84          org.jboss.axis.message.SOAPBodyElementAxisImpl sbe = env.getFirstBody();//env.getBodyByName("ns1", "addedfile");
85
org.w3c.dom.Element JavaDoc sbElement = sbe.getAsDOM();
86          //get the first level accessor ie parameter
87
org.w3c.dom.Node JavaDoc n = sbElement.getFirstChild();
88
89          for (; n != null && !(n instanceof org.w3c.dom.Element JavaDoc); n = n.getNextSibling()) ;
90          org.w3c.dom.Element JavaDoc paramElement = (org.w3c.dom.Element JavaDoc)n;
91          //Get the href associated with the attachment.
92
String JavaDoc href = paramElement.getAttribute(soapConstants.getAttrHref());
93          org.jboss.axis.Part ap = msg.getAttachmentsImpl().getAttachmentByReference(href);
94          javax.activation.DataHandler JavaDoc dh = org.jboss.axis.attachments.AttachmentUtils.getActivationDataHandler(ap);
95          org.w3c.dom.Node JavaDoc timeNode = paramElement.getFirstChild();
96          long startTime = -1;
97
98          if (timeNode != null && timeNode instanceof org.w3c.dom.Text JavaDoc)
99          {
100             String JavaDoc startTimeStr = ((org.w3c.dom.Text JavaDoc)timeNode).getData();
101
102             startTime = Long.parseLong(startTimeStr);
103          }
104          // log.debug("GOTIT");
105

106          long receivedTime = System.currentTimeMillis();
107          long elapsedTime = -1;
108
109          // log.debug("startTime=" + startTime);
110
// log.debug("receivedTime=" + receivedTime);
111
if (startTime > 0) elapsedTime = receivedTime - startTime;
112          String JavaDoc elapsedTimeStr = elapsedTime + "";
113          // log.debug("elapsedTimeStr=" + elapsedTimeStr);
114

115          java.security.MessageDigest JavaDoc md = java.security.MessageDigest.getInstance("MD5");
116          java.io.InputStream JavaDoc attachmentStream = dh.getInputStream();
117          int bread = 0;
118          byte[] buf = new byte[64 * 1024];
119
120          do
121          {
122             bread = attachmentStream.read(buf);
123             if (bread > 0)
124             {
125                md.update(buf, 0, bread);
126             }
127          }
128          while (bread > -1);
129          attachmentStream.close();
130          buf = null;
131          //Add the mime type to the digest.
132
String JavaDoc contentType = dh.getContentType();
133
134          if (contentType != null && contentType.length() != 0)
135          {
136             md.update(contentType.getBytes("US-ASCII"));
137          }
138
139          sbe = env.getFirstBody();
140          sbElement = sbe.getAsDOM();
141          //get the first level accessor ie parameter
142
n = sbElement.getFirstChild();
143          for (; n != null && !(n instanceof org.w3c.dom.Element JavaDoc); n = n.getNextSibling()) ;
144          paramElement = (org.w3c.dom.Element JavaDoc)n;
145          // paramElement.setAttribute(soapConstants.getAttrHref(), respHref);
146
String JavaDoc MD5String = org.jboss.axis.encoding.Base64.encode(md.digest());
147          String JavaDoc senddata = " elapsedTime=" + elapsedTimeStr + " MD5=" + MD5String;
148
149          // log.debug("senddata=" + senddata);
150
paramElement.appendChild(paramElement.getOwnerDocument().createTextNode(senddata));
151
152          sbe = new org.jboss.axis.message.SOAPBodyElementAxisImpl(sbElement);
153          env.clearBody();
154          env.addBodyElement(sbe);
155          msg = new Message(env);
156
157          msgContext.setResponseMessage(msg);
158       }
159       catch (Exception JavaDoc e)
160       {
161          log.error(Messages.getMessage("exception00"), e);
162          throw AxisFault.makeFault(e);
163       }
164
165       log.debug("Exit: EchoHandler::invoke");
166    }
167
168 }
169
Popular Tags