import com.lmtas.swa.client.*;

import java.lang.*;
import java.util.*;
import java.io.*;

import org.apache.axis.Message;
import org.apache.axis.MessageContext;
import org.apache.axis.transport.http.HTTPConstants;
import org.apache.axis.attachments.Attachments;
import org.apache.axis.attachments.AttachmentPart;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;

public class swaClient {

    public static void main (String[] args) {
        System.out.println("Test it");

        SWAServiceLocator locator = new SWAServiceLocator();
        locator.setSWAServicePortEndpointAddress("http://192.189.248.2:8080/swa/services/SWAServicePort");

        try {
            System.out.println("\n");

            SWAServiceBindingStub stub2 = (SWAServiceBindingStub) locator.getSWAServicePort();

            DimReqData drqd = new DimReqData("Give me data");
            DimResData drsd = new DimResData();
            drsd = stub2.getDime(drqd);
            System.out.println("getDime message: " + drsd.getFileId());

            Object [] messageAttachments = stub2.getAttachments();

            if (messageAttachments == null) {
                System.out.println("There are no attachments for this message");
            }
            else {
                System.out.println("There are " + messageAttachments.length + " attachments for this message");
                for (int k = 0; k < messageAttachments.length; ++k) {
                    try {
                        AttachmentPart attachPart = (AttachmentPart) messageAttachments[k]; 
                        DataHandler dh = attachPart.getDataHandler();

                        System.out.println("ID: " + attachPart.getContentId() );

                        File myFile = new File(attachPart.getContentId());
                        FileOutputStream myFOS = new FileOutputStream(myFile);
                        dh.writeTo(myFOS);
                    }
                    catch (Exception e) {
                        System.out.println("Error: " + e.getMessage());
                        e.printStackTrace();
                    }
                }
            }
            System.out.println("\n");

        }
        catch (Throwable t) {
            t.printStackTrace();
        }
    }

}
