We're working on a "file archive" service. This service will need to let its clients upload files for archiving and later retrive them. (Ok, its more complex than that but that's the relevant bit...)
What I have thus far is working OK but I would appreciate some more experienced eyes on it and their opinions of my approach. I'm always anxious to hear a Better Way.
I started with this interface:
public interface EchoService
{
public void echo();
}That was given to java2wsdl and the resulting wsdl was then given to wsdl2java. I wrote the following EchoClient to use the generated service locator and binding stub:
public class EchoClient
{
private static Logger log = Logger.getLogger(EchoClient.class);public static void main(String [] args)
{
try
{
System.err.println("Sending /etc/profile");
EchoServiceSoapBindingStub c = (EchoServiceSoapBindingStub) (new EchoServiceServiceLocator().getEchoService());
DataHandler dh = new DataHandler(new FileDataSource("/etc/profile"));
c.addAttachment(dh);
c.echo(); Object [] attachments = c.getAttachments();
System.err.println("Hello World");
for(int i = 0 ; i < attachments.length ; ++i)
{
AttachmentPart part = (AttachmentPart) attachments[i];
System.err.println("getSize = " + part.getSize());
System.err.println("getAttachmentFile = " + part.getAttachmentFile());
System.err.println("getContentLocation = " + part.getContentLocation());
dh = part.getDataHandler();
System.err.println("dh.getName() = " + dh.getName());
// Create a File using dh.getName() and do whatever with its contents.
// Then delete that file so that we don't fillup our /tmp space.
}
}
catch(Exception e)
{
log.error("oops", e);
}
}
}
I then implemented this simple server object to handle the 'echo' requests:
public class EchoService
{
private static Logger log = Logger.getLogger(EchoService.class);
public void echo()
{
log.info("Hello World");
try
{
MessageContext msgContext = MessageContext.getCurrentContext();
Message reqMsg = msgContext.getRequestMessage();
Attachments messageAttachments = reqMsg.getAttachmentsImpl();
if (messageAttachments == null)
{
log.info("No Attachments");
return;
}
log.info(
messageAttachments.getAttachmentCount() + " attachments");
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();
log.info("getSize = " + part.getSize());
log.info("getAttachmentFile = " + part.getAttachmentFile());
log.info("getContentLocation = " + part.getContentLocation());
DataHandler dh = part.getDataHandler();
log.info("dh.getName() = " + dh.getName());
// Create a File using dh.getName() and do whatever with its contents.
// Then delete that file so that we don't fillup our /tmp space.
}
Message resp = msgContext.getResponseMessage();
log.info("Response message is " + resp);
AttachmentPart part = (AttachmentPart) resp.createAttachmentPart();
part.setContent("I like cheese", "text/plain");
resp.addAttachmentPart(part);
}
catch (Exception e)
{
log.error(e);
}
}
}
Thanks in advance! James
