/*******************************************************************************
 * Copyright (c) 2013 - 2016 QPark Consulting  S.a r.l.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0.
 * The Eclipse Public License is available at
 * http://www.eclipse.org/legal/epl-v10.html.
 ******************************************************************************/
package com.qpark.eip.core.spring.security;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.util.Comparator;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;

import org.apache.commons.codec.binary.Base64;
import org.springframework.ws.transport.http.HttpUrlConnectionMessageSender;

/**
 * @author bhausen
 */
public class EipHttpUrlConnectionMessageSender
		extends HttpUrlConnectionMessageSender {
	/** The user name of the basic HTTP-authentication. */
	private String userName;

	/** The password of the basic HTTP-authentication. */
	private String password;

	/** base64(userName:password) */
	private String base64UserNamePassword;

	/**
	 * @return the password.
	 */
	public String getPassword() {
		return this.password;
	}

	/**
	 * @return the userName.
	 */
	public String getUserName() {
		return this.userName;
	}

	/**
	 * Initialize the HTTP connection.
	 *
	 * @throws UnsupportedEncodingException
	 */
	@PostConstruct
	public void init() throws UnsupportedEncodingException {
		if (this.userName != null) {
			if (this.password == null) {
				this.password = "";
			}
			this.base64UserNamePassword = new String(
					Base64.encodeBase64String(new StringBuffer(this.userName)
							.append(":").append(this.password).toString()
							.getBytes("UTF-8"))).replaceAll("\n", "");
		}
	}

	/**
	 * @see org.springframework.ws.transport.http.HttpsUrlConnectionMessageSender#prepareConnection(java.net.HttpURLConnection)
	 */
	@Override
	protected void prepareConnection(final HttpURLConnection connection)
			throws IOException {
		/* call the super method. */
		super.prepareConnection(connection);

		/* Setup the basic Authentication. */
		if (this.userName != null) {
			this.logger.debug(String.format(
					"prepareConnection add request header '%s' basic AUTH userName '%s'",
					"Authorization", this.userName));
			connection.setRequestProperty("Authorization",
					new StringBuffer(128).append("Basic ")
							.append(this.base64UserNamePassword).toString());
			this.logger.debug(String.format(
					"prepareConnection request Headers: %s",
					connection.getRequestProperties().entrySet().stream()
							.sorted(Comparator
									.comparing(v -> String.valueOf(v.getKey())))
							.map(v -> String.format("%s: %s", v.getKey(),
									v.getValue().stream()
											.collect(Collectors.joining(","))))
							.collect(Collectors.joining(", "))));
		}
	}

	/**
	 * @param password
	 *            the password to set.
	 */
	public void setPassword(final String password) {
		this.password = password;
	}

	/**
	 * @param userName
	 *            the userName to set.
	 */
	public void setUserName(final String userName) {
		this.userName = userName;
	}
}

@@ -609,16 +609,42 @@
 arator;%0A
+import java.util.Objects;%0A
 import j
@@ -671,16 +671,16 @@
 ectors;%0A
-
 %0Aimport 
@@ -1036,17 +1036,16 @@
 erName;%0A
-%0A
 %09/** The
@@ -1113,25 +1113,92 @@
 g password;%0A
+%09/** The HTTP request content type. */%0A%09private String contentType;
 %0A
-
 %09/** base64(
@@ -1260,16 +1260,241 @@
 sword;%0A%0A
+%09/**%0A%09 * @param contentType%0A%09 */%0A%09public void setContentType(String contentType) %7B%0A%09%09this.contentType = contentType;%0A%09%7D%0A%0A%09/**%0A%09 * @return the contentType.%0A%09 */%0A%09public String getContentType() %7B%0A%09%09return this.contentType;%0A%09%7D%0A%0A
 %09/**%0A%09 *
@@ -2834,16 +2834,124 @@
 ing());%0A
+%09%09%09if (Objects.nonNull(contentType)) %7B%0A%09%09%09%09connection.setRequestProperty(%22Content-Type%22, contentType);%0A%09%09%09%7D%0A
 %09%09%09this.
@@ -3371,31 +3371,16 @@
 password
-%0A%09 *           
  the pas
@@ -3477,32 +3477,32 @@
 sword;%0A%09%7D%0A%0A%09/**%0A
+
 %09 * @param userN
@@ -3508,23 +3508,8 @@
 Name
-%0A%09 *           
  the

Set content type for HTTP request