/*****************************************************************************
 * Copyright (C) The Apache Software Foundation. All rights reserved.        *
 * ------------------------------------------------------------------------- *
 * This software is published under the terms of the Apache Software License *
 * version 1.1, a copy of which has been included  with this distribution in *
 * the LICENSE file.                                                         *
 *****************************************************************************/
package org.apache.cocoon.components.language.markup.xsp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import org.apache.avalon.framework.component.ComponentException;
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.components.xscript.XScriptManager;
import org.apache.cocoon.components.xscript.XScriptObject;
import org.apache.cocoon.components.xscript.XScriptObjectInlineXML;

import java.net.URLConnection;
import java.io.*;

import org.xml.sax.InputSource;

/**
 * Helper for the SOAP logicsheet.
 *
 * @author <a href="mailto:ovidiu@cup.hp.com">Ovidiu Predescu</a>
 * @since July 16, 2001
 */
public class SOAPHelper
{
  XScriptManager xscriptManager;
  URL url;
  String action = "";
  XScriptObject xscriptObject;
  char[] buf = new char[4096];

  public SOAPHelper(ComponentManager manager, String urlContext, String url,
                    String action, XScriptObject xscriptObject)
    throws MalformedURLException, ComponentException
  {
    this.xscriptManager = (XScriptManager)manager.lookup(XScriptManager.ROLE);
    URL context = new URL(urlContext);
    this.url = new URL(context, url);
    this.action = action;
    this.xscriptObject = xscriptObject;
  }

  public XScriptObject invoke()
    throws ProcessingException
  {

    try {
      if (action == null || action.equals(""))
        action = "\"\"";
		URLConnection con = url.openConnection();
		con.setDoInput(true);
		con.setDoOutput(true);
		con.setUseCaches(false);
		//con.setRequestProperty("CONTENT_LENGTH", "" + postDataFile.length());
		con.setRequestProperty("Content-type", "text/xml; charset=\"utf-8\"");
		con.setRequestProperty("SOAPAction", action);

		//BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(postDataFile)));
		InputSource saxInputStream = xscriptObject.getInputSource();
		InputStream is = saxInputStream.getByteStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);

		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(con.getOutputStream()));

		int nread = 0;
		int nwrote = 0;
		while ((nread = br.read(buf, 0, buf.length)) > 0){
			bw.write(buf, 0, nread);
			nwrote += nread;
		}
		bw.flush();
		bw.close();
		br.close();
System.out.println("Wrote " + nwrote + " bytes to " + url);

		br = new BufferedReader(new InputStreamReader(con.getInputStream()));
		CharArrayWriter caw = new CharArrayWriter();

		nwrote = 0;
		while ((nread = br.read(buf, 0, buf.length)) > 0){
			caw.write(buf, 0, nread);
			nwrote += nread;
		}
System.out.println("Read " + nwrote + " bytes from " + url);

        return new XScriptObjectInlineXML(xscriptManager, caw.toString());
    }
    catch (Exception ex) {
      throw new ProcessingException("Error invoking remote service: " + ex,
                                    ex);
    }
  }
}

