
/*
 Created as part of the StratusLab project (http://stratuslab.eu),
 co-funded by the European Commission under the Grant Agreement
 INSFO-RI-261552."

 Copyright (c) 2010, Centre Nationale de la Recherche Scientifique

 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 eu.stratuslab.authn;

import java.security.cert.X509Certificate;

import javax.security.auth.x500.X500Principal;
import javax.servlet.http.HttpServletRequest;

import org.apache.xmlrpc.common.XmlRpcHttpRequestConfigImpl;
import org.apache.xmlrpc.webserver.XmlRpcServletServer;

public class OneProxyServletServer extends XmlRpcServletServer {

	final private static String X509_ATTR_NAME = "javax.servlet.request.X509Certificate";

	@Override
	protected XmlRpcHttpRequestConfigImpl newConfig(HttpServletRequest pRequest) {
		return new OneProxyRequestConfigImpl(extractUserDn(pRequest));
	}

	private String extractUserDn(HttpServletRequest request) {

		Object c = request.getAttribute(X509_ATTR_NAME);

		if (c != null && c instanceof X509Certificate[]) {
			X509Certificate[] certs = (X509Certificate[]) c;
			X500Principal principal = certs[0].getSubjectX500Principal();
			return principal.getName();
		}

		return "";

	}

}

@@ -781,16 +781,44 @@
 authn;%0A%0A
+import java.io.IOException;%0A
 import j
@@ -851,16 +851,46 @@
 ificate;
+%0Aimport java.util.Enumeration;
 %0A%0Aimport
@@ -930,16 +930,55 @@
 ncipal;%0A
+import javax.servlet.ServletException;%0A
 import j
@@ -1014,16 +1014,97 @@
 Request;
+%0Aimport javax.servlet.http.HttpServletResponse;%0Aimport javax.servlet.http.Cookie;
 %0A%0Aimport
@@ -1862,14 +1862,1820 @@
 rn %22%22;%0A%0A
-
 %09%7D%0A%0A
+    @Override%0A    public void execute(HttpServletRequest request, HttpServletResponse response) %0A%09throws ServletException, IOException %7B%0A%0A%09super.execute(request, response);%0A%0A%09printRequest(request);%0A    %7D%0A%0A    public static void printRequest(HttpServletRequest request) %7B%0A%09%0A%09StringBuilder sb = new StringBuilder();%0A%09%0A        sb.append(%22session=%22 + request.getSession(true).getId() + %22%5Cn%22);%0A        sb.append(%22authtype=%22 + request.getAuthType() + %22%5Cn%22);%0A        sb.append(%22contextpath=%22 + request.getContextPath() + %22%5Cn%22);%0A        sb.append(%22principal=%22 + request.getUserPrincipal() + %22%5Cn%22);%0A%09%0A        sb.append(%22cookies%5Cn%22);%0A        Cookie%5B%5D cookies = request.getCookies();%0A        if (cookies!=null) %7B%0A            for (Cookie cookie : cookies) %7B%0A                sb.append(%22 -- %22 + formatCookie(cookie));%0A            %7D%0A        %7D%0A        sb.append(%22%5Cn%22);%0A        sb.append(formatHeaders(request));%0A%09%0A%09System.err.println(sb.toString());%0A    %7D%0A    %0A    public static String formatCookie(Cookie cookie) %7B%0A        StringBuilder sb = new StringBuilder();%0A        sb.append(cookie.getName() + %22 : %22);%0A        sb.append(cookie.getDomain() + %22 : %22);%0A        sb.append(cookie.getMaxAge() + %22 : %22);%0A        sb.append(cookie.getSecure() + %22 : %22);%0A        sb.append(cookie.getValue() + %22 : %22);%0A        return sb.toString();%0A    %7D%0A    %0A    public static String formatHeaders(HttpServletRequest request) %7B%0A        StringBuilder sb = new StringBuilder();%0A        sb.append(%22headers%5Cn%22);%0A        Enumeration%3CString%3E names = request.getHeaderNames();%0A        while (names.hasMoreElements()) %7B%0A            String name = names.nextElement();%0A            String value = request.getHeader(name);%0A            sb.append(%22 -- %22 + name + %22 = %22 + value + %22%5Cn%22);%0A        %7D%0A        sb.append(%22%5Cn%22);%0A        return sb.toString();%0A    %7D%0A%0A
 %7D%0A

add debugging output