/*
 * JBoss, the OpenSource webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.test.webservice.attachmentstepbystep;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.rmi.RemoteException;
import javax.activation.DataHandler;



/**
 * Implementation des méthodes du service
 */
public class MyServiceImpl implements MyService  
{
    
    // CTOR
    public MyServiceImpl() {
        super();
    }
    
    public DataHandler myService(DataHandler mimepart) throws RemoteException
    {
        URL url = null;
        String result="";
        try {
            // print the recieved file
            InputStream is = mimepart.getInputStream();
            if (is != null) {
                BufferedReader in =  new BufferedReader(new InputStreamReader(is));
                
                String line="";
                line = in.readLine();
                result = "" + line;
                while (line != null)
                {
                    System.out.println(line);
                    line = in.readLine();
                    result = result + line;
                }
                in.close();
            }
            // the file to return if size of received file == 192
            if (result.length() == 190)
            {
                url = new URL("http://" + System.getProperty("jboss.bind.address", "localhost") + ":8080/myservice/resources/attachment_server2client.txt");
            }
            else
            {
                throw new Exception("The received file isn't 190 bytes length as expected. Length = " + result.length());
            }
        }
        catch (Exception ex) {
            System.err.println(ex);
        }
        return new DataHandler(url);
    }

    
}
