Hi All,
I had hard time trying to find a send SOAP DIME attachment
code for the web service. And here it is, I wrote one,
but I use to easy way to deploy the service.
I just simply change the extension .java to .jws,
so, can you all tell me whether it will be a problem or not ?
And please review my code below, I debugged and no error,
but I am not sure if it is working right.
Basiclly 2 operations:
public String generateID(int artID)
public File detachFile(String filename)
Please comment on this code, I am trying to make it
more robust, so, that I can redo it and post it to share with
everybody.
thanks,
Derek
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.io.*;
import java.security.*;
import java.security.NoSuchAlgorithmException;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.soap.AttachmentPart;
import org.apache.axis.AxisFault;
import org.apache.axis.Message;
import org.apache.axis.MessageContext;
import org.apache.axis.attachments.Attachments;
import org.apache.axis.attachments.AttachmentsImpl;
import org.apache.log4j.Logger;
public class AttachmentYPMG
{
private static final Logger _logger = Logger.getLogger(AttachmentYPMG.class);
public AttachmentYPMG()
{
}
public String generateID(int artID)
{
String artid = Integer.toString(artID);
String md5_hash_string = plainStringToMD5(artid);
return md5_hash_string;
}
private String plainStringToMD5(String input) {
// Some stuff we will use later
MessageDigest md = null;
byte[] byteHash = null;
StringBuffer resultString = new StringBuffer();
// Bad things can happen here
try {
// Choose between MD5 and SHA1
md = MessageDigest.getInstance("MD5");
} catch(NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgorithmException caught!");
System.exit( -1);
}
// Reset is always good
md.reset();
// We really need some conversion here
md.update(input.getBytes());
// There goes the hash
byteHash = md.digest();
// Now here comes the best part
for(int i = 0; i < byteHash.length; i++) {
resultString.append(Integer.toHexString(0xFF & byteHash));
}
// That's it!
return(resultString.toString());
}
public File detachFile(String filename)
{
InputStream is = null;
FileOutputStream os = null;
File file = null;
int totalAttachments ;
try
{
//Get all the attachments
AttachmentPart[] attachments = getMessageAttachments();
/*
* getMessageAttachments() as provided by Steve Loughran in his mail
* to axis-user group
* http://www.mail-archive.com/axis-user@xml.apache.org/msg08732.html
*/
//Put the logic in a loop for totalAttachments for multiple
// attachments.
totalAttachments = attachments.length;
_logger.debug("saveFile(String filename = " + filename + ") - " +
"Total Attachments Received Are: "+ totalAttachments);
//Extract the first attachment. (Since in this case we have only one attachment sent)
DataHandler dh = attachments[0].getDataHandler();
//Extract the file name of the first attachment.
String name = filename;
_logger.debug("saveFile(String filename = " + filename + ") - File received on server is: " + name);
//Get the streams to file and from attachment, then stream to disk
is = dh.getInputStream();
file = new File(name);
os = new FileOutputStream(file);
this.writeBuffersAndClose(is, os);
} catch (Exception e)
{
_logger.error("detachFile(String filename = " + filename + ")", e);
//throw new AttachmentException(e);
}
//if(file!= null)
return file;
//else
//throw new AttachmentException("The attachment was not saved");
}
/**
* extract attachments from the current request
*
* @return a list of attachmentparts or an empty array for no attachments
* support in this axis buid/runtime
*/
private AttachmentPart[] getMessageAttachments() throws AxisFault
{
/*
* Reusing the method implementation for AttachmentPart[]
* getMessageAttachments() as provided by Steve Loughran in his mail to
* axis-user group
* http://www.mail-archive.com/axis-user@xml.apache.org/msg08732.html
*/
MessageContext msgContext = MessageContext.getCurrentContext();
Message reqMsg = msgContext.getRequestMessage();
Attachments messageAttachments = reqMsg.getAttachmentsImpl();
if (null == messageAttachments)
{
System.out.println("no attachment support");
return new AttachmentPart[0];
}
int attachmentCount = messageAttachments.getAttachmentCount();
AttachmentPart attachments[] = new AttachmentPart[attachmentCount];
Iterator it = messageAttachments.getAttachments().iterator();
int count = 0;
while (it.hasNext())
{
AttachmentPart part = (AttachmentPart) it.next();
attachments[count++] = part;
}
return attachments;
}
/**
* Simple method for writing one stream from another.
*
* @param is
* @param os
* @throws IOException
*/
private void writeBuffersAndClose(InputStream is, OutputStream os)
throws IOException
{
int i = 0;
byte [] buffer = new byte[1024];
while (i != -1)
{
i = is.read(buffer, 0, buffer.length);
if(i > 0)
os.write(buffer, 0, buffer.length);
}
is.close();
os.close();
}
}