/**
 * 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 eu.dnetlib.data.index.ws.commons.profile.ProfileHeader;
import eu.dnetlib.data.index.ws.commons.profile.blackboard.Blackboard;
import eu.dnetlib.data.index.ws.commons.profile.blackboard.BlackboardLastAction;
import eu.dnetlib.data.index.ws.commons.profile.blackboard.Message;
import eu.dnetlib.data.index.ws.commons.profile.utils.ProfileMarshaller;


public class SimpleSOAPNotifyTest {

	public final static String DEFAULT_SERVER = "http://88.156.223.164:8080/indexService/IndexService";

	public final static String SOAP_ACTION = "notify";

	public static void main(String[] args) {

		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);

			String subscrId = "17d0450c-45db-4d65-84c8-45d224bfddf7";
			String topic = "xxx";
//			TODO escape xml chars
			String isId = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
					+ "<wsa:EndpointReference xmlns:wsa=\"http://www.driver.org/schema\" xmlns:driver=\"http://www.driver.org\" xmlns:wsaw=\"http://www.w3.org/2006/02/addressing/wsdl\" xmlns:wsdl=\"http://www.w3.org/2005/08/wsdl-instance\">"
					+ "<wsa:Address>http://146.48.87.182:8010/SOAP/IS_SN</wsa:Address>"
					+ "<wsa:Metadata wsdl:wsdlLocation=\"http://146.48.87.182:8010/SOAP/IS_SN?WSDL\"><wsaw:ServiceName>theISSN</wsaw:ServiceName></wsa:Metadata></wsa:EndpointReference>";
			isId = "<![CDATA[" + isId + "]]>";
			
//			--setting bb content
			ProfileHeader profileHeader = new ProfileHeader("someResourceIdentifier",
					ProfileHeader.INDEX_SERVICE_TYPE,"anyKind");
			String predefinedId = "predefinedId";
			Blackboard blackboard = new Blackboard();
			blackboard.setLastRequest(new BlackboardLastAction("someDate",predefinedId));
			Message bbMessage = new Message();
			bbMessage.setId(predefinedId);
			bbMessage.setAction(Message.Action.CREATE);
			bbMessage.setActionStatus(Message.ActionStatus.ASSIGNED);
			blackboard.addMessage(bbMessage);
			Message message = new Message();
			message.setAction(Message.Action.CREATE);
			message.setActionStatus(Message.ActionStatus.ASSIGNED);
			String messageStr = ProfileMarshaller.generateBBProfile(profileHeader, blackboard);
			messageStr = "<![CDATA[" + messageStr + "]]>";
//			--
			OutputStream out = connection.getOutputStream();
			Writer wout = new OutputStreamWriter(out);
			wout.write("<?xml version='1.0'?>\r\n");
			wout.write("<soap:Envelope "
							+ "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" >" 
							+ "<soap:Body>" 
							+ "<notify>" 
							+ "<subscrId>" + subscrId + "</subscrId>" 
							+ "<topic>" + topic	+ "</topic>" 
							+ "<isId>" + isId + "</isId>"
							+ "<message>" + messageStr + "</message>" 
							+ "</notify>"
							+ "</soap:Body>" + "</soap:Envelope>" + "\r\n");

			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();

		} catch (IOException e) {
			System.err.println(e);
		}

	}
}