/*
 * Copyright 2005-2025 the original author or authors.
 *
 * 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
 *
 *      https://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 org.springframework.ws.transport.http;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Iterator;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;

import org.springframework.util.Assert;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.transport.WebServiceConnection;

/**
 * Implementation of {@link WebServiceConnection} that is based on Jakarta Commons
 * HttpClient. Exposes a {@link PostMethod}.
 *
 * @author Arjen Poutsma
 * @author Greg Turnquist
 * @since 1.0.0
 * @deprecated In favor of {@link HttpComponentsConnection}
 */
@Deprecated
public class CommonsHttpConnection extends AbstractHttpSenderConnection {

	private final HttpClient httpClient;

	private final PostMethod postMethod;

	private ByteArrayOutputStream requestBuffer;

	private MultiThreadedHttpConnectionManager connectionManager;

	protected CommonsHttpConnection(HttpClient httpClient, PostMethod postMethod) {
		Assert.notNull(httpClient, "httpClient must not be null");
		Assert.notNull(postMethod, "postMethod must not be null");
		this.httpClient = httpClient;
		this.postMethod = postMethod;
	}

	public PostMethod getPostMethod() {
		return this.postMethod;
	}

	@Override
	public void onClose() throws IOException {
		this.postMethod.releaseConnection();
		if (this.connectionManager != null) {
			this.connectionManager.shutdown();
		}
	}

	/*
	 * URI
	 */

	@Override
	public URI getUri() throws URISyntaxException {
		try {
			return new URI(this.postMethod.getURI().toString());
		}
		catch (URIException ex) {
			throw new URISyntaxException("", ex.getMessage());
		}
	}

	/*
	 * Sending request
	 */

	@Override
	protected void onSendBeforeWrite(WebServiceMessage message) throws IOException {
		this.requestBuffer = new ByteArrayOutputStream();
	}

	@Override
	public void addRequestHeader(String name, String value) throws IOException {
		this.postMethod.addRequestHeader(name, value);
	}

	@Override
	protected OutputStream getRequestOutputStream() throws IOException {
		return this.requestBuffer;
	}

	@Override
	protected void onSendAfterWrite(WebServiceMessage message) throws IOException {
		this.postMethod.setRequestEntity(new ByteArrayRequestEntity(this.requestBuffer.toByteArray()));
		this.requestBuffer = null;
		try {
			this.httpClient.executeMethod(this.postMethod);
		}
		catch (IllegalStateException ex) {
			if ("Connection factory has been shutdown.".equals(ex.getMessage())) {
				// The application context has been closed, resulting in a connection
				// factory shutdown and an ISE.
				// Let's create a new connection factory for this connection only.
				this.connectionManager = new MultiThreadedHttpConnectionManager();
				this.httpClient.setHttpConnectionManager(this.connectionManager);
				this.httpClient.executeMethod(this.postMethod);
			}
			else {
				throw ex;
			}
		}
	}

	/*
	 * Receiving response
	 */

	@Override
	protected int getResponseCode() throws IOException {
		return this.postMethod.getStatusCode();
	}

	@Override
	protected String getResponseMessage() throws IOException {
		return this.postMethod.getStatusText();
	}

	@Override
	protected long getResponseContentLength() throws IOException {
		return this.postMethod.getResponseContentLength();
	}

	@Override
	protected InputStream getRawResponseInputStream() throws IOException {
		return this.postMethod.getResponseBodyAsStream();
	}

	@Override
	public Iterator<String> getResponseHeaderNames() throws IOException {
		Header[] headers = this.postMethod.getResponseHeaders();
		String[] names = new String[headers.length];
		for (int i = 0; i < headers.length; i++) {
			names[i] = headers[i].getName();
		}
		return Arrays.asList(names).iterator();
	}

	@Override
	public Iterator<String> getResponseHeaders(String name) throws IOException {
		Header[] headers = this.postMethod.getResponseHeaders(name);
		String[] values = new String[headers.length];
		for (int i = 0; i < headers.length; i++) {
			values[i] = headers[i].getValue();
		}
		return Arrays.asList(values).iterator();
	}

}
