package org.deegree.securityproxy.authentication.wass;

import static org.springframework.security.core.authority.AuthorityUtils.createAuthorityList;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.deegree.securityproxy.filter.KvpRequestWrapper;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;

/**
 * 
 * TODO add class documentation here
 * 
 * @author <a href="mailto:goltz@lat-lon.de">Lyn Goltz</a>
 * @author last edited by: $Author: lyn $
 * 
 * @version $Revision: $, $Date: $
 */
public class AddParameterAnonymousAuthenticationFilter extends AbstractAuthenticationProcessingFilter {

    private static final Logger LOG = Logger.getLogger( AddParameterAnonymousAuthenticationFilter.class );

    private static final String ANONYMOUS_USER = "anonymousUser";

    private final String parameterKey;

    public AddParameterAnonymousAuthenticationFilter( String parameterKey ) {
        // TODO: fix path!
        super( "/test" );
        this.parameterKey = parameterKey;
    }

    @Override
    public void doFilter( ServletRequest req, ServletResponse res, FilterChain chain )
                            throws IOException, ServletException {
        LOG.debug( "Wrap request with query string " + ( (HttpServletRequest) req ).getQueryString() );
        KvpRequestWrapper request = new KvpRequestWrapper( (HttpServletRequest) req );
        super.doFilter( request, res, chain );
    }

    @Override
    public Authentication attemptAuthentication( HttpServletRequest request, HttpServletResponse response )
                            throws AuthenticationException, IOException, ServletException {
        Authentication authRequest = new AnonymousAuthenticationToken( ANONYMOUS_USER, ANONYMOUS_USER,
                                                                       createAuthorityList( "ROLE_ANONYMOUS" ) );
        return this.getAuthenticationManager().authenticate( authRequest );
    }

    @Override
    protected void successfulAuthentication( HttpServletRequest request, HttpServletResponse response,
                                             FilterChain chain, Authentication authResult )
                            throws IOException, ServletException {
        super.successfulAuthentication( request, response, chain, authResult );
        addParameter( request, authResult );
    }

    private void addParameter( HttpServletRequest request, Authentication authResult ) {
        String sessionId = (String) authResult.getCredentials();
        if ( sessionId != null ) {
            LOG.info( "Append parameter " + parameterKey + "=" + sessionId );
            ( (KvpRequestWrapper) request ).addParameter( parameterKey, sessionId );
        }
    }

}
@@ -587,16 +587,90 @@
 nToken;%0A
+import org.springframework.security.authentication.AuthenticationManager;%0A
 import o
@@ -891,45 +891,255 @@
 %0A * 
-%0A * TODO add class docum
+%7B@link AbstractAuthenticationProcessingFilter%7D implementation using a %7B@link AuthenticationManager%7D to authenticate a%0A * anonymous user. All requests are authenticated. After auth
 ent
+ic
 ation 
+t
 he
-re
+ credentials are added as parameter to the%0A * request.
 %0A * 
@@ -1696,22 +1696,94 @@
  // 
-TODO: fix path
+path is not used, cause method #requiresAuthentication() authentication is overwritten
 !%0A  
@@ -1801,12 +1801,18 @@
 ( %22/
-test
+unusedPath
 %22 );
@@ -2266,32 +2266,181 @@
 chain );%0A    %7D%0A%0A
+    @Override%0A    protected boolean requiresAuthentication( HttpServletRequest request, HttpServletResponse response ) %7B%0A        return true;%0A    %7D%0A%0A
     @Override%0A  
@@ -3225,64 +3225,29 @@
     
-super.successfulAuthentication( request, response, chain
+addParameter( request
 , au
@@ -3262,33 +3262,35 @@
  );%0A        
-addParame
+chain.doFil
 ter( request
@@ -3287,34 +3287,32 @@
 r( request, 
-authResult
+response
  );%0A    %7D%0A%0A 
@@ -3411,35 +3411,28 @@
     
-String sessionId = (String)
+Object credentials =
  aut
@@ -3474,25 +3474,60 @@
 f ( 
-sessionId != null
+credentials != null && credentials instanceof String
  ) %7B
@@ -3592,25 +3592,27 @@
 + %22=%22 + 
-sessionId
+credentials
  );%0A    
@@ -3683,17 +3683,28 @@
 ey, 
-sessionId
+(String) credentials
  );%0A

call chain.doFilter directly to avoid redirect; JavaDoc