/*
 * The contents of this file are subject to the Mozilla Public 
 * License Version 1.1 (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.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an 
 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express 
 * or implied. See the License for the specific language governing
 * rights and limitations under the License.
 *
 *
 * The Original Code is OIOSAML Java Service Provider.
 * 
 * The Initial Developer of the Original Code is Trifork A/S. Portions 
 * created by Trifork A/S are Copyright (C) 2008 Danish National IT 
 * and Telecom Agency (http://www.itst.dk). All Rights Reserved.
 * 
 * Contributor(s):
 *   Joakim Recht <jre@trifork.com>
 *   Rolf Njor Jensen <rolf@trifork.com>
 *
 */
package dk.itst.oiosaml.sp.service;

import java.security.Principal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import dk.itst.oiosaml.sp.OIOPrincipal;
import dk.itst.oiosaml.sp.UserAssertion;
import dk.itst.oiosaml.sp.service.util.Constants;

public class SAMLHttpServletRequest extends HttpServletRequestWrapper {

	private final UserAssertion assertion;
	private final String hostname;
	private String relayState;

	public SAMLHttpServletRequest(HttpServletRequest request, UserAssertion assertion, String hostname) {
		super(request);
		this.assertion = assertion;
		this.hostname = hostname;
	}

	public SAMLHttpServletRequest(HttpServletRequest servletRequest, String hostname, String relayState) {
		this(servletRequest, (UserAssertion)null, hostname);
		this.relayState = relayState;
	}

	@Override
	public String getRemoteUser() {
		if (assertion != null) {
			return assertion.getSubject();
		} else {
			return super.getRemoteUser();
		}
	}
	
	@Override
	public Principal getUserPrincipal() {
		if (assertion != null) {
			return new OIOPrincipal(assertion);
		} else {
			return super.getUserPrincipal();
		}
	}
	
	@Override
	public StringBuffer getRequestURL() {
		String url = super.getRequestURL().toString();
		
		String mod = hostname + url.substring(url.indexOf('/', 8));
		return new StringBuffer(mod);
	}

	@Override
	public String getParameter(String name) {
		if (Constants.SAML_RELAYSTATE.equals(name) && relayState != null) {
			return relayState;
		}
		return super.getParameter(name);
	}
}

@@ -2435,10 +2435,310 @@
 me);%0A%09%7D%0A
+%09%0A%09@Override%0A%09public String getQueryString() %7B%0A%09%09if (relayState == null) return super.getQueryString();%0A%09%09%0A%09%09String q = super.getQueryString();%0A%09%09if (q == null) %7B%0A%09%09%09q = %22%22;%0A%09%09%7D%0A%09%09return new StringBuilder(q).append(%22&%22).append(Constants.SAML_RELAYSTATE).append(%22=%22).append(relayState).toString();%0A%09%7D%0A
 %7D%0A

make sure RelayState is also added to the queryString - this is necessary when discovery mode is active