package com.xpn.xwiki.user.impl.xwiki;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.xpn.xwiki.user.api.XWikiUser;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;

/*
  Implements a authentication mecanism which is trusting the App Server authentication
  If it fails it falls back to the standard XWiki authentication
 */
public class AppServerTrustedAuthServiceImpl  extends XWikiAuthServiceImpl {
    private static final Log log = LogFactory.getLog(AppServerTrustedAuthServiceImpl.class);

    public XWikiUser checkAuth(XWikiContext context) throws XWikiException {
        String user = context.getRequest().getRemoteUser();
        if ((user == null) || user.equals("")) {
            return super.checkAuth(context);
        }
        else {
            if (log.isDebugEnabled())
                log.debug("Launching create user for " + user);            
            createUser(user, context);
            if (log.isDebugEnabled())
                log.debug("Create user done for " + user);
            user = "XWiki." + user;
        }
        context.setUser(user);
        return new XWikiUser(user);
    }

    /**
     * We cannot authenticate locally since we need to trust the app server for authentication
     * @param username
     * @param password
     * @param context
     * @return
     * @throws XWikiException
     */
    public XWikiUser checkAuth(String username, String password, String rememberme, XWikiContext context) throws XWikiException {
        String user = context.getRequest().getRemoteUser();
        if ((user == null) || user.equals("")) {
            return super.checkAuth(username, password, rememberme, context);
        }
        else {
            createUser(user, context);
            user = "XWiki." + user;
        }
        context.setUser(user);
        return new XWikiUser(user);
    }
}
@@ -234,18 +234,20 @@
 ion;%0A%0A/*
+*
 %0A 
+*
  Impleme
@@ -323,18 +323,17 @@
 tication
-%0A 
+.
  If it f
@@ -353,16 +353,19 @@
  back to
+%0A *
  the sta
@@ -390,16 +390,38 @@
 tication
+.%0A * %0A * @version $Id$
 %0A */%0Apub
@@ -462,17 +462,16 @@
 iceImpl 
- 
 extends 
@@ -490,17 +490,17 @@
 viceImpl
- 
+%0A
 %7B%0A    pr
@@ -587,16 +587,30 @@
 lass);%0A%0A
+    @Override%0A
     publ
@@ -667,32 +667,36 @@
 s XWikiException
+%0A   
  %7B%0A        Strin
@@ -841,32 +841,24 @@
 );%0A        %7D
-%0A       
  else %7B%0A    
@@ -954,28 +954,16 @@
 + user);
-            
 %0A       
@@ -1324,122 +1324,23 @@
    *
- @param username%0A     * @param password%0A     * @param context%0A     * @return%0A     * @throws XWikiException%0A     */
+/%0A    @Override
 %0A   
@@ -1433,32 +1433,40 @@
 Context context)
+%0A       
  throws XWikiExc
@@ -1471,16 +1471,20 @@
 xception
+%0A   
  %7B%0A     
@@ -1673,24 +1673,16 @@
        %7D
-%0A       
  else %7B%0A
@@ -1789,32 +1789,33 @@
 .setUser(user);%0A
+%0A
         return n
@@ -1841,8 +1841,9 @@
 %0A    %7D%0A%7D
+%0A

Apply codestyle