/*
   Copyright 2016 Nationale-Nederlanden

   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 nl.nn.adapterframework.extensions.api;

import nl.nn.adapterframework.configuration.ConfigurationException;
import nl.nn.adapterframework.parameters.Parameter;
import nl.nn.adapterframework.parameters.ParameterList;
import nl.nn.adapterframework.soap.SoapWrapperPipe;

import org.apache.commons.lang.StringUtils;

/**
 * Extension to SoapWrapperPipe for API Management.
 *
 * @author Peter Leeuwenburgh
 */

public class ApiSoapWrapperPipe extends SoapWrapperPipe {
	protected final static String CONVERSATIONID = "conversationId";

	@Override
	public void configure() throws ConfigurationException {
		if ("wrap".equalsIgnoreCase(getDirection())) {
			if (StringUtils.isEmpty(getSoapHeaderSessionKey())) {
				setSoapHeaderSessionKey(DEFAULT_SOAP_HEADER_SESSION_KEY);
			}
			if (StringUtils.isEmpty(getSoapHeaderStyleSheet())) {
				setSoapHeaderStyleSheet("/xml/xsl/api/soapHeader.xsl");
			}
			addParameters();
		}
		super.configure();
	}

	private void addParameters() {
		ParameterList parameterList = getParameterList();
		Parameter p;
		if (parameterList.findParameter(CONVERSATIONID) == null) {
			p = new Parameter();
			p.setName(CONVERSATIONID);
			p.setSessionKey(getSoapHeaderSessionKey());
			p.setXpathExpression("MessageHeader/HeaderFields/ConversationId");
			p.setRemoveNamespaces(true);
			// only forward the existing ConversationId, don't create a new one
			// p.setPattern("{hostname}_{uid}");
			// p.setDefaultValueMethods("pattern");
			addParameter(p);
		}
	}
}

@@ -867,16 +867,65 @@
 perPipe;
+%0Aimport nl.nn.adapterframework.util.AppConstants;
 %0A%0Aimport
@@ -1130,20 +1130,20 @@
 ted 
-final 
 static
+ final
  Str
@@ -1180,16 +1180,122 @@
 tionId%22;
+%0A%09protected static final String FROM_IN = %22from_in%22;%0A%09protected static final String FROM_OUT = %22from_out%22;
 %0A%0A%09@Over
@@ -2241,13 +2241,374 @@
 p);%0A%09%09%7D%0A
+%09%09if (parameterList.findParameter(FROM_IN) == null) %7B%0A%09%09%09p = new Parameter();%0A%09%09%09p.setName(FROM_IN);%0A%09%09%09p.setSessionKey(getSoapHeaderSessionKey());%0A%09%09%09p.setXpathExpression(%22MessageHeader/From%22);%0A%09%09%09addParameter(p);%0A%09%09%7D%0A%0A%09%09p = new Parameter();%0A%09%09p.setName(FROM_OUT);%0A%09%09p.setValue(AppConstants.getInstance().getProperty(%22instance.name%22, %22%22));%0A%09%09addParameter(p);%0A%0A
 %09%7D%0A%7D%0A

Add from field next to conversationId field