package org.owasp.appsensor.handler;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.xml.namespace.QName;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;

public class RegisterClientApplicationIdentificationHandler implements
		SOAPHandler<SOAPMessageContext> {

	/** default name for client application identification header */
	private static String HEADER_NAME = "X-Appsensor-Client-Application-Name";

	@Override
	public boolean handleMessage(SOAPMessageContext context) {

		//make sure this is an outbound request
		if ((Boolean)context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY)) {
			
			@SuppressWarnings("unchecked")
			Map<String, List<String>> httpHeaders = (Map<String, List<String>>) context.get(MessageContext.HTTP_REQUEST_HEADERS);
			if (null == httpHeaders) {
				httpHeaders = new HashMap<String, List<String>>();
			}
			
			httpHeaders.put(HEADER_NAME, Collections.singletonList("localhostme"));
			context.put(MessageContext.HTTP_REQUEST_HEADERS, httpHeaders);
		}
		return true;
	}

	@Override
	public boolean handleFault(SOAPMessageContext context) {
		return true;
	}

	@Override
	public void close(MessageContext context) {
		//
	}

	@Override
	public Set<QName> getHeaders() {
		return null;
	}

}

@@ -333,16 +333,386 @@
 ntext;%0A%0A
+import org.owasp.appsensor.AppSensorClient;%0A%0A/**%0A * This is the jax-ws SOAP handler that adds authentication info %0A * of client applications to outbound requests %0A * %0A * The authentication mechanism involves adding an HTTP request header %0A * for the username of the given client application. %0A * %0A * @author John Melton (jtmelton@gmail.com) http://www.jtmelton.com/%0A */%0A
 public c
@@ -1393,16 +1393,174 @@
 %09%09%7D%0A%09%09%09%0A
+%09%09%09String clientApplicationName = AppSensorClient.getInstance().getConfiguration().getServerConnection().getClientApplicationIdentificationHeaderValue();%0A%09%09%09%0A
 %09%09%09httpH
@@ -1613,21 +1613,29 @@
 ist(
-%22localhost
+clientApplicationNa
 me
-%22
 ));%0A

Update register client handler to read from config file instead of being hard-coded for test.