/* Copyright 2000 Fraunhofer Gesellschaft
 * Leonrodstr. 54, 80636 Munich, Germany.
 * All rights reserved.
 *
 * You shall use this software only in accordance with
 * the terms of the license agreement you entered into
 * with Fraunhofer Gesellschaft.
 */
package confuoco.test;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;

import java.io.*;
import java.net.URL;
import java.util.Date;
import java.util.Hashtable;

import confuoco.content.*;

import org.apache.axis.MessageContext;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory;
import org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory;
import org.apache.axis.transport.http.HTTPConstants;

/**
 * DOCUMENT ME!
 *
 * @author $author$
 * @version $Revision: 1.4 $
 */
public class FileUploadServiceClient
{
    /**
     * DOCUMENT ME!
     *
     * @param args DOCUMENT ME!
     *
     * @throws Exception DOCUMENT ME!
     */
    public static void main(String[] args) throws Exception
    {
        
    	String filename              = "jakarta.zip";
        
        DataHandler dhSource = new DataHandler(new FileDataSource(filename));

        System.out.println("contentType:  " + dhSource.getContentType());
        
        Service service = new Service();
        Call call       = (Call)service.createCall();
        call.setTargetEndpointAddress(
            new URL("http://localhost:8080/axis/services/FileUploadService"));
        call.setOperationName(
            new QName("http://webservice.ttp.confuoco", "upload"));
        call.addParameter("name", XMLType.XSD_STRING, ParameterMode.IN);
        call.addParameter("date", XMLType.XSD_DATETIME, ParameterMode.IN);
        QName qnameAttachment =
            new QName("http://webservice.ttp.confuoco", "DataHandler");
        call.registerTypeMapping(
            dhSource.getClass(),
            qnameAttachment,
            JAFDataHandlerSerializerFactory.class,
            JAFDataHandlerDeserializerFactory.class);
        call.addParameter("handler", qnameAttachment, ParameterMode.IN);
        call.setReturnType(XMLType.AXIS_VOID);
        Hashtable chunkedTable = new Hashtable();
        chunkedTable.put(
            HTTPConstants.HEADER_TRANSFER_ENCODING,
            HTTPConstants.HEADER_TRANSFER_ENCODING_CHUNKED);

        call.setProperty(
            MessageContext.HTTP_TRANSPORT_VERSION,
            HTTPConstants.HEADER_PROTOCOL_V11);
        call.setProperty(HTTPConstants.REQUEST_HEADERS, chunkedTable);

        call.setProperty(
            Call.ATTACHMENT_ENCAPSULATION_FORMAT,
            Call.ATTACHMENT_ENCAPSULATION_FORMAT_DIME);
        call.invoke(new Object[]{ filename, new Date(), dhSource });
    }

    /**
     * DOCUMENT ME!
     *
     * @param file DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     *
     * @throws IOException DOCUMENT ME!
     */
    public byte[] getBytesFromFile(File file) throws IOException
    {
        InputStream is = new FileInputStream(file);

        // Get the size of the file
        long length = file.length();

        // You cannot create an array using a long type.
        // It needs to be an int type.
        // Before converting to an int type, check
        // to ensure that file is not larger than Integer.MAX_VALUE.
        if (length > Integer.MAX_VALUE)
        {
            System.out.println("File is too large.");
            // File is too large
        }

        // Create the byte array to hold the data
        byte[] bytes = new byte[(int)length];

        // Read in the bytes
        int offset  = 0;
        int numRead = 0;
        while (
            (offset < bytes.length)
            && ((numRead = is.read(bytes, offset, bytes.length - offset)) >= 0))
        {
            offset += numRead;
        }

        // Ensure all the bytes have been read in
        if (offset < bytes.length)
        {
            throw new IOException(
                "Could not completely read file " + file.getName());
        }

        // Close the input stream and return bytes
        is.close();
        return bytes;
    }
}
