From 3d6fced58f3785415dc2c39a9177ccfa43d8c5f3 Mon Sep 17 00:00:00 2001 From: Jonas Fonseca Date: Mon, 8 Dec 2008 11:23:25 +0100 Subject: [PATCH] client/spi: Make AuthenticationService take SessionService argument --- .../main/java/remote/apps/cli/Authenticator.java | 5 ++-- .../cli/src/main/java/remote/apps/cli/Command.java | 6 +++-- .../java/remote/axis/spi/AxisServiceContext.java | 17 ++++--------- .../client/spi/service/AuthenticationService.java | 5 +++- .../java/remote/client/xrt/SessionBuilder.java | 13 +++++----- .../main/java/remote/client/xrt/SessionModel.java | 17 +++++++------ .../java/remote/client/xrt/SessionModelTest.java | 28 +++++++++++++++++++--- 7 files changed, 56 insertions(+), 35 deletions(-) diff --git a/apps/cli/src/main/java/remote/apps/cli/Authenticator.java b/apps/cli/src/main/java/remote/apps/cli/Authenticator.java index 101a56b..87c7bdf 100644 --- a/apps/cli/src/main/java/remote/apps/cli/Authenticator.java +++ b/apps/cli/src/main/java/remote/apps/cli/Authenticator.java @@ -11,6 +11,7 @@ import remote.client.authentication.AuthenticationHandler; import remote.client.authentication.AuthenticationNameCallback; import remote.client.authentication.AuthenticationPasswordCallback; import remote.client.spi.ServiceManager; +import remote.client.spi.ServiceSession; import remote.client.spi.service.AuthenticationService; class Authenticator implements AuthenticationHandler, @@ -127,9 +128,9 @@ class Authenticator implements AuthenticationHandler, visitor.onAuthenticatorComplete(); } - void authenticate(ServiceManager manager) + void authenticate(ServiceManager manager, ServiceSession session) { - manager.get(AuthenticationService.class).authenticate(this, this); + manager.get(AuthenticationService.class).authenticate(session, this, this); } public void onSuccess(Boolean result) diff --git a/apps/cli/src/main/java/remote/apps/cli/Command.java b/apps/cli/src/main/java/remote/apps/cli/Command.java index e871a0a..75713c5 100644 --- a/apps/cli/src/main/java/remote/apps/cli/Command.java +++ b/apps/cli/src/main/java/remote/apps/cli/Command.java @@ -233,8 +233,10 @@ public abstract class Command { public void onEvent(T event) { - if (event instanceof ServiceSession.Event.Init) - auth.authenticate(getServiceManager()); + if (event instanceof ServiceSession.Event.Init) { + ServiceSession session = ((ServiceSession.Event) event).getSession(); + auth.authenticate(getServiceManager(), session); + } } } diff --git a/axis/spi/src/main/java/remote/axis/spi/AxisServiceContext.java b/axis/spi/src/main/java/remote/axis/spi/AxisServiceContext.java index 24406ce..e652a4d 100644 --- a/axis/spi/src/main/java/remote/axis/spi/AxisServiceContext.java +++ b/axis/spi/src/main/java/remote/axis/spi/AxisServiceContext.java @@ -81,6 +81,8 @@ class AxisServiceContext implements ServiceContext, AuthenticationService, { if (session == null) throw new ServiceException("No session attached"); + if (!session.isConnected()) + throw new ServiceException("Session not connected"); return session.getId(); } @@ -140,8 +142,10 @@ class AxisServiceContext implements ServiceContext, AuthenticationService, return result; } - public Task authenticate(final AuthenticationHandler handler, Callback callback) + public Task authenticate(ServiceSession session, final AuthenticationHandler handler, + Callback callback) { + this.session = session; return manager.addRequest(callback, new AxisRequest() { public Boolean run() throws Throwable @@ -209,17 +213,6 @@ class AxisServiceContext implements ServiceContext, AuthenticationService, public void onEvent(T event) { - if (event instanceof ServiceSession.Event) { - ServiceSession ses = ((ServiceSession.Event) event).getSession(); - - if (event instanceof ServiceSession.Event.Init) { - if (session == null && !ses.isAuthenticated()) - session = ses; - } else if (event instanceof ServiceSession.Event.Exit) { - if (session == ses) - session = null; - } - } } public void shutdown() diff --git a/client/spi/src/main/java/remote/client/spi/service/AuthenticationService.java b/client/spi/src/main/java/remote/client/spi/service/AuthenticationService.java index 0b35b94..f57e7f6 100644 --- a/client/spi/src/main/java/remote/client/spi/service/AuthenticationService.java +++ b/client/spi/src/main/java/remote/client/spi/service/AuthenticationService.java @@ -2,6 +2,7 @@ package remote.client.spi.service; import remote.client.authentication.AuthenticationHandler; import remote.client.spi.Service; +import remote.client.spi.ServiceSession; /** * Service for session authentication. @@ -21,10 +22,12 @@ public interface AuthenticationService extends Service { * request credentials from the client. On success, the result is a * Boolean object indicating whether the session was authenticated. * + * @param session that needs authentication. * @param handler for requesting credentials from the client. * @param callback for communicating the response. * @return the service task. */ - Task authenticate(AuthenticationHandler handler, Callback callback); + Task authenticate(ServiceSession session, AuthenticationHandler handler, + Callback callback); } diff --git a/client/xrt/src/main/java/remote/client/xrt/SessionBuilder.java b/client/xrt/src/main/java/remote/client/xrt/SessionBuilder.java index 7c89eda..3563a8d 100644 --- a/client/xrt/src/main/java/remote/client/xrt/SessionBuilder.java +++ b/client/xrt/src/main/java/remote/client/xrt/SessionBuilder.java @@ -4,6 +4,7 @@ import remote.client.control.MoteManager; import remote.client.session.Session; import remote.client.session.SessionListener; import remote.client.spi.ServiceManager; +import remote.client.spi.ServiceSession; /** * Builder for creating a session. @@ -13,8 +14,8 @@ public class SessionBuilder { private final RuntimeSystem system; private SessionListener listener; private ServiceManager services; + private ServiceSession session; private MoteManager motes; - private boolean authenticated = false; /** * Get the service manager. @@ -76,14 +77,14 @@ public class SessionBuilder { return this; } - public boolean authenticated() + public ServiceSession session() { - return authenticated; + return session; } - public SessionBuilder authenticated(boolean authenticated) + public SessionBuilder session(ServiceSession session) { - this.authenticated = authenticated; + this.session = session; return this; } @@ -106,7 +107,7 @@ public class SessionBuilder { if (!valid()) throw new IllegalStateException(); - return new SessionModel(services, motes, authenticated, listener) { + return new SessionModel(services, motes, session, listener) { @Override protected RuntimeSystem system() diff --git a/client/xrt/src/main/java/remote/client/xrt/SessionModel.java b/client/xrt/src/main/java/remote/client/xrt/SessionModel.java index d147dbb..fc41f17 100644 --- a/client/xrt/src/main/java/remote/client/xrt/SessionModel.java +++ b/client/xrt/src/main/java/remote/client/xrt/SessionModel.java @@ -5,6 +5,7 @@ import remote.client.control.MoteManager; import remote.client.session.Session; import remote.client.session.SessionListener; import remote.client.spi.ServiceManager; +import remote.client.spi.ServiceSession; import remote.client.spi.service.AuthenticationService; /** @@ -16,17 +17,16 @@ abstract class SessionModel extends RuntimeModel implements Session { private final MoteManager moteManager; private final ServiceManager service; private final SessionListener listener; - private boolean connected = false; - private boolean authenticated = false; private AuthenticationService.Task task; + private ServiceSession serviceSession; SessionModel(ServiceManager serviceManager, MoteManager moteManager, - boolean authenticated, SessionListener listener) + ServiceSession serviceSession, SessionListener listener) { this.service = serviceManager; this.moteManager = moteManager; this.listener = listener; - this.authenticated = authenticated; + this.serviceSession = serviceSession; } public MoteManager getMoteManager() @@ -47,17 +47,17 @@ abstract class SessionModel extends RuntimeModel implements Session { public boolean isConnected() { - return connected; + return serviceSession != null && serviceSession.isConnected(); } public boolean isAuthenticated() { - return authenticated; + return serviceSession != null && serviceSession.isAuthenticated(); } public void authenticate(AuthenticationHandler handler) { - if (authenticated) + if (isAuthenticated()) throw new AlreadyAuthenticatedException(); task = new AuthenticationRequest(handler); } @@ -68,13 +68,12 @@ abstract class SessionModel extends RuntimeModel implements Session { { super(system()); setTask(service.get(AuthenticationService.class). - authenticate(handler, this)); + authenticate(serviceSession, handler, this)); } @Override protected void success(Boolean result) { - authenticated = result.booleanValue(); } } diff --git a/client/xrt/src/test/java/remote/client/xrt/SessionModelTest.java b/client/xrt/src/test/java/remote/client/xrt/SessionModelTest.java index 6d8f6d8..e94f078 100644 --- a/client/xrt/src/test/java/remote/client/xrt/SessionModelTest.java +++ b/client/xrt/src/test/java/remote/client/xrt/SessionModelTest.java @@ -14,7 +14,10 @@ import remote.client.control.MoteSite; import remote.client.session.SessionListener; import remote.client.session.Session; import remote.client.spi.Service; +import remote.client.spi.Service.Callback; +import remote.client.spi.Service.Task; import remote.client.spi.ServiceManager; +import remote.client.spi.ServiceSession; import remote.client.spi.service.AuthenticationService; import static org.junit.Assert.*; @@ -64,7 +67,7 @@ public class SessionModelTest { TestServiceTask authRequest = null; boolean connected = false; - public Task authenticate(AuthenticationHandler handler, Callback callback) + public Task authenticate(ServiceSession session, AuthenticationHandler handler, Callback callback) { authRequest = new TestServiceTask(callback); return authRequest; @@ -82,11 +85,11 @@ public class SessionModelTest { authRequest = null; } - } Session session = null; ServiceManager services = null; + ServiceSession serviceSession; TestService service = null; TestSessionListener listener = null; private TestRuntimeSystem system; @@ -142,7 +145,26 @@ public class SessionModelTest { } }; - session = new SessionModel(services, motes, false, listener) { + serviceSession = new ServiceSession() { + + public String getId() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public boolean isConnected() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public boolean isAuthenticated() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + + }; + session = new SessionModel(services, motes, serviceSession, listener) { @Override protected RuntimeSystem system() -- 2.11.4.GIT