<commit_before>/*
   Copyright 2013 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.esb;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import nl.nn.adapterframework.configuration.ConfigurationException;
import nl.nn.adapterframework.core.Adapter;
import nl.nn.adapterframework.core.IPipeLineSession;
import nl.nn.adapterframework.core.PipeForward;
import nl.nn.adapterframework.core.PipeLine;
import nl.nn.adapterframework.core.PipeRunException;
import nl.nn.adapterframework.core.PipeRunResult;
import nl.nn.adapterframework.pipes.FixedForwardPipe;
import nl.nn.adapterframework.receivers.GenericReceiver;
import nl.nn.adapterframework.soap.Wsdl;
import nl.nn.adapterframework.util.AppConstants;
import nl.nn.adapterframework.util.DateUtils;
import nl.nn.adapterframework.util.Dir2Xml;
import nl.nn.adapterframework.util.FileUtils;
import nl.nn.adapterframework.util.Misc;
import nl.nn.adapterframework.util.TransformerPool;
import nl.nn.adapterframework.util.XmlUtils;

import org.apache.commons.lang.StringUtils;

public class WsdlGeneratorPipe extends FixedForwardPipe {

	private String sessionKey = "file";
	private String propertiesFileName = "wsdl.properties";

	public PipeRunResult doPipe(Object input, IPipeLineSession session)
			throws PipeRunException {
		InputStream inputStream = (InputStream) session.get("file");
		if (inputStream == null) {
			throw new PipeRunException(this, getLogPrefix(session)
					+ "got null value from session under key ["
					+ getSessionKey() + "]");
		}

		File tempDir;
		String fileName;
		try {
			tempDir = FileUtils.createTempDir(null, "WEB-INF" + File.separator
					+ "classes");
			fileName = (String) session.get("fileName");
			if (FileUtils.extensionEqualsIgnoreCase(fileName, "zip")) {
				FileUtils.unzipStream(inputStream, tempDir);
			} else {
				File file = new File(tempDir, fileName);
				Misc.streamToFile(inputStream, file);
				file.deleteOnExit();
			}
		} catch (IOException e) {
			throw new PipeRunException(this, getLogPrefix(session)
					+ " Exception on uploading and unzipping/writing file", e);
		}

		File propertiesFile = new File(tempDir, getPropertiesFileName());
		PipeLine pipeLine;
		try {
			if (propertiesFile.exists()) {
				pipeLine = createPipeLineFromPropertiesFile(propertiesFile);
			} else {
				File xsdFile = FileUtils.getFirstFile(tempDir);
				pipeLine = createPipeLineFromXsdFile(xsdFile);
			}
		} catch (Exception e) {
			throw new PipeRunException(this, getLogPrefix(session)
					+ " Exception on generating wsdl", e);
		}

		Object result = null;
		OutputStream zipOut = null;
		OutputStream fullWsdlOut = null;
		try {
			Adapter adapter = new Adapter();
			String fileBaseName = FileUtils.getBaseName(fileName).replaceAll(
					" ", "_");
			adapter.setName(fileBaseName);
			GenericReceiver genericReceiver = new GenericReceiver();
			EsbJmsListener esbJmsListener = new EsbJmsListener();
			esbJmsListener.setQueueConnectionFactoryName("jms/qcf_"
					+ fileBaseName);<commit_msg>Bugfix NPE for ESB WsdlGeneratorPipe<commit_after>/*
   Copyright 2013 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.esb;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import nl.nn.adapterframework.configuration.Configuration;
import nl.nn.adapterframework.configuration.ConfigurationException;
import nl.nn.adapterframework.core.Adapter;
import nl.nn.adapterframework.core.IPipeLineSession;
import nl.nn.adapterframework.core.PipeForward;
import nl.nn.adapterframework.core.PipeLine;
import nl.nn.adapterframework.core.PipeRunException;
import nl.nn.adapterframework.core.PipeRunResult;
import nl.nn.adapterframework.pipes.FixedForwardPipe;
import nl.nn.adapterframework.receivers.GenericReceiver;
import nl.nn.adapterframework.soap.Wsdl;
import nl.nn.adapterframework.util.AppConstants;
import nl.nn.adapterframework.util.DateUtils;
import nl.nn.adapterframework.util.Dir2Xml;
import nl.nn.adapterframework.util.FileUtils;
import nl.nn.adapterframework.util.Misc;
import nl.nn.adapterframework.util.TransformerPool;
import nl.nn.adapterframework.util.XmlUtils;

import org.apache.commons.lang.StringUtils;

public class WsdlGeneratorPipe extends FixedForwardPipe {

	private String sessionKey = "file";
	private String propertiesFileName = "wsdl.properties";

	public PipeRunResult doPipe(Object input, IPipeLineSession session)
			throws PipeRunException {
		InputStream inputStream = (InputStream) session.get("file");
		if (inputStream == null) {
			throw new PipeRunException(this, getLogPrefix(session)
					+ "got null value from session under key ["
					+ getSessionKey() + "]");
		}

		File tempDir;
		String fileName;
		try {
			tempDir = FileUtils.createTempDir(null, "WEB-INF" + File.separator
					+ "classes");
			fileName = (String) session.get("fileName");
			if (FileUtils.extensionEqualsIgnoreCase(fileName, "zip")) {
				FileUtils.unzipStream(inputStream, tempDir);
			} else {
				File file = new File(tempDir, fileName);
				Misc.streamToFile(inputStream, file);
				file.deleteOnExit();
			}
		} catch (IOException e) {
			throw new PipeRunException(this, getLogPrefix(session)
					+ " Exception on uploading and unzipping/writing file", e);
		}

		File propertiesFile = new File(tempDir, getPropertiesFileName());
		PipeLine pipeLine;
		try {
			if (propertiesFile.exists()) {
				pipeLine = createPipeLineFromPropertiesFile(propertiesFile);
			} else {
				File xsdFile = FileUtils.getFirstFile(tempDir);
				pipeLine = createPipeLineFromXsdFile(xsdFile);
			}
		} catch (Exception e) {
			throw new PipeRunException(this, getLogPrefix(session)
					+ " Exception on generating wsdl", e);
		}

		Object result = null;
		OutputStream zipOut = null;
		OutputStream fullWsdlOut = null;
		try {
			Adapter adapter = new Adapter();
			adapter.setConfiguration(new Configuration(null));
			String fileBaseName = FileUtils.getBaseName(fileName).replaceAll(
					" ", "_");
			adapter.setName(fileBaseName);
			GenericReceiver genericReceiver = new GenericReceiver();
			EsbJmsListener esbJmsListener = new EsbJmsListener();
			esbJmsListener.setQueueConnectionFactoryName("jms/qcf_"
					+ fileBaseName);
{'fraction_non_alphanumeric': 0.09574749075541468, 'fraction_numerical': 0.002377179080824089, 'mean_word_length': 4.741470811220622, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '261979', 'n_tokens_mistral': 2431, 'n_tokens_neox': 2229, 'n_words': 553}