/**
 * Copyright 2008-2009 DRIVER PROJECT (ICM UW)
 * Original author: Marek Horst
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package eu.dnetlib.data.index;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

/**
 * Simple class for sending SOAP messages.
 * @author mhorst
 *
 */
public class SOAPSender {

//	public final static String DEFAULT_SERVER = "http://88.156.223.164:8181/IDataProvider";
	public final static String DEFAULT_SERVER = "http://localhost:8080/indexService/IndexService";
//	public final static String DEFAULT_SERVER = "http://213.135.50.24:38383/indexService/IndexService";
	
//	public final static String DEFAULT_SERVER = "http://localhost:8080/indexService/ResultSet";
	
//	public final static String SOAP_ACTION = "getBulkData";
	public final static String SOAP_ACTION = "indexLookup";
//	public final static String SOAP_ACTION = "getResult";

	public static void main(String[] args) throws ResourceNotFoundException {

		try {
			URL u = new URL(DEFAULT_SERVER);
			URLConnection uc = u.openConnection();
			HttpURLConnection connection = (HttpURLConnection) uc;
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestMethod("POST");
			connection.setRequestProperty("SOAPAction", SOAP_ACTION);

			OutputStream out = connection.getOutputStream();
			Writer wout = new OutputStreamWriter(out);
			
			ClasspathResourceLoader loader = new ClasspathResourceLoader();
			String sourceFilePath = "pl/edu/icm/driver/index/soap.message";
			String messageContent = read(loader.getResourceStream(sourceFilePath));
			
			long startTime = System.currentTimeMillis();
			
			wout.write(messageContent);
			wout.flush();
			wout.close();

			System.out.println("Response message: "
					+ connection.getResponseMessage());
			System.out
					.println("Response code: " + connection.getResponseCode());
			
			InputStream in = connection.getInputStream();
			int c;
			while ((c = in.read()) != -1)
				System.out.write(c);
			in.close();

			System.out.println("\noperation time: "+(System.currentTimeMillis()-startTime) + " ms");
			
		} catch (IOException e) {
			System.err.println(e);
		}

	}
	
	public static String read(InputStream in) throws IOException {
	    StringBuffer out = new StringBuffer();
	    byte[] b = new byte[4096];
	    for (int n; (n = in.read(b)) != -1;) {
	        out.append(new String(b, 0, n));
	    }
	    return out.toString();
	}

	
}
