JBoss Remoting SVN: r5774 - in remoting3/trunk/jboss-remoting/src: test/java/org/jboss/remoting3/test and 1 other directory.
by jboss-remoting-commits@lists.jboss.org
Author: david.lloyd(a)jboss.com
Date: 2010-02-28 17:59:17 -0500 (Sun, 28 Feb 2010)
New Revision: 5774
Added:
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/security/SimpleServerAuthenticationProvider.java
Modified:
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/InvocationTestBase.java
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemoteTestCase.java
Log:
Add simple server authentication provider for standalone usages
Added: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/security/SimpleServerAuthenticationProvider.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/security/SimpleServerAuthenticationProvider.java (rev 0)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/security/SimpleServerAuthenticationProvider.java 2010-02-28 22:59:17 UTC (rev 5774)
@@ -0,0 +1,159 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.remoting3.security;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.NameCallback;
+import javax.security.auth.callback.PasswordCallback;
+import javax.security.auth.callback.UnsupportedCallbackException;
+import javax.security.sasl.AuthenticationException;
+import javax.security.sasl.AuthorizeCallback;
+import javax.security.sasl.RealmCallback;
+
+/**
+ * A server authentication handler which maintains a simple map of user names and passwords.
+ */
+public final class SimpleServerAuthenticationProvider implements ServerAuthenticationProvider {
+
+ private static final RemotingPermission ADD_USER_PERM = new RemotingPermission("addServerUser");
+
+ private final Map<String, Map<String, Entry>> map = new HashMap<String, Map<String, Entry>>();
+
+ /** {@inheritDoc} */
+ public CallbackHandler getCallbackHandler() {
+ return new CallbackHandler() {
+ public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException {
+ String userName = null;
+ String realmName = null;
+ for (Callback callback : callbacks) {
+ if (callback instanceof NameCallback) {
+ final NameCallback nameCallback = (NameCallback) callback;
+ final String defaultName = nameCallback.getDefaultName();
+ userName = defaultName.toLowerCase().trim();
+ nameCallback.setName(userName);
+ } else if (callback instanceof RealmCallback) {
+ final RealmCallback realmCallback = (RealmCallback) callback;
+ final String defaultRealm = realmCallback.getDefaultText();
+ if (defaultRealm != null) {
+ realmName = defaultRealm.toLowerCase().trim();
+ realmCallback.setText(realmName);
+ }
+ } else if (callback instanceof PasswordCallback) {
+ final PasswordCallback passwordCallback = (PasswordCallback) callback;
+ // retrieve the record based on user and realm (if any)
+ Entry entry = null;
+ if (realmName == null) {
+ // scan all realms
+ synchronized (map) {
+ for (Map<String, Entry> realmMap : map.values()) {
+ if (realmMap.containsKey(userName)) {
+ entry = realmMap.get(userName);
+ break;
+ }
+ }
+ }
+ } else {
+ synchronized (map) {
+ final Map<String, Entry> realmMap = map.get(realmName);
+ if (realmMap != null) {
+ entry = realmMap.get(userName);
+ }
+ }
+ }
+ if (entry == null) {
+ throw new AuthenticationException("No matching user found");
+ }
+ passwordCallback.setPassword(entry.getPassword());
+ } else if (callback instanceof AuthorizeCallback) {
+ final AuthorizeCallback authorizeCallback = (AuthorizeCallback) callback;
+ authorizeCallback.setAuthorized(authorizeCallback.getAuthenticationID().equals(authorizeCallback.getAuthorizationID()));
+ } else {
+ throw new UnsupportedCallbackException(callback, "Callback not supported: " + callback);
+ }
+ }
+ }
+ };
+ }
+
+ /**
+ * Add a user to the authentication table.
+ *
+ * @param userName the user name
+ * @param userRealm the user realm
+ * @param password the password
+ */
+ public void addUser(String userName, String userRealm, char[] password) {
+ if (userName == null) {
+ throw new IllegalArgumentException("userName is null");
+ }
+ if (userRealm == null) {
+ throw new IllegalArgumentException("userRealm is null");
+ }
+ if (password == null) {
+ throw new IllegalArgumentException("password is null");
+ }
+ final SecurityManager sm = System.getSecurityManager();
+ if (sm != null) {
+ sm.checkPermission(ADD_USER_PERM);
+ }
+ final String canonUserRealm = userRealm.toLowerCase().trim();
+ final String canonUserName = userName.toLowerCase().trim();
+ synchronized (map) {
+ Map<String, Entry> realmMap = map.get(canonUserRealm);
+ if (realmMap == null) {
+ realmMap = new HashMap<String, Entry>();
+ map.put(canonUserRealm, realmMap);
+ }
+ realmMap.put(canonUserName, new Entry(canonUserName, canonUserRealm, password));
+ }
+ }
+
+ private static final class Entry {
+ private final String userName;
+ private final String userRealm;
+ private final char[] password;
+
+ private Entry(final String userName, final String userRealm, final char[] password) {
+ this.userName = userName;
+ this.userRealm = userRealm;
+ this.password = password;
+ }
+
+ String getUserName() {
+ return userName;
+ }
+
+ String getUserRealm() {
+ return userRealm;
+ }
+
+ char[] getPassword() {
+ return password;
+ }
+ }
+}
Modified: remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/InvocationTestBase.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/InvocationTestBase.java 2010-02-28 22:05:37 UTC (rev 5773)
+++ remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/InvocationTestBase.java 2010-02-28 22:59:17 UTC (rev 5774)
@@ -86,6 +86,7 @@
return new RequestListener<InvocationTestObject, InvocationTestObject>() {
public void handleRequest(final RequestContext<InvocationTestObject> objectRequestContext, final InvocationTestObject request) throws RemoteExecutionException {
try {
+ log.info("Got request %s, sending reply %s", request, replyObj);
objectRequestContext.sendReply(replyObj);
} catch (IOException e) {
throw new RemoteExecutionException(e);
@@ -182,6 +183,7 @@
public void handleRequest(final RequestContext<InvocationTestObject> objectRequestContext, final ClientConnector request) throws RemoteExecutionException {
try {
assertEquals(replyObj, ((ClientConnector<InvocationTestObject, InvocationTestObject>)request).getFutureClient().get().invoke(requestObj));
+ log.info("Got request %s, sending reply %s", request, replyObj);
objectRequestContext.sendReply(replyObj);
} catch (Throwable e) {
throw new RemoteExecutionException(e);
@@ -202,6 +204,7 @@
client.invoke(connection.createClientConnector(new RequestListener<InvocationTestObject, InvocationTestObject>() {
public void handleRequest(final RequestContext<InvocationTestObject> requestContext, final InvocationTestObject request) throws RemoteExecutionException {
try {
+ log.info("Got request %s, sending reply %s", request, replyObj);
requestContext.sendReply(replyObj);
} catch (IOException e) {
throw new RemoteExecutionException(e);
Modified: remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemoteTestCase.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemoteTestCase.java 2010-02-28 22:05:37 UTC (rev 5773)
+++ remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemoteTestCase.java 2010-02-28 22:59:17 UTC (rev 5774)
@@ -29,7 +29,7 @@
import org.jboss.remoting3.CloseHandler;
import org.jboss.remoting3.Connection;
import org.jboss.remoting3.RemotingOptions;
-import org.jboss.remoting3.security.ServerAuthenticationProvider;
+import org.jboss.remoting3.security.SimpleServerAuthenticationProvider;
import org.jboss.remoting3.spi.NetworkServerProvider;
import org.jboss.remoting3.spi.ProtocolServiceType;
import org.jboss.xnio.AcceptingServer;
@@ -44,15 +44,6 @@
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
-import javax.security.auth.callback.Callback;
-import javax.security.auth.callback.CallbackHandler;
-import javax.security.auth.callback.NameCallback;
-import javax.security.auth.callback.PasswordCallback;
-import javax.security.auth.callback.UnsupportedCallbackException;
-import javax.security.sasl.AuthenticationException;
-import javax.security.sasl.AuthorizeCallback;
-import javax.security.sasl.RealmCallback;
-
@Test(suiteName = "Remote tests")
public final class RemoteTestCase extends InvocationTestBase {
@@ -61,36 +52,9 @@
enter();
try {
super.setUp();
- endpoint.addProtocolService(ProtocolServiceType.SERVER_AUTHENTICATION_PROVIDER, "test", new ServerAuthenticationProvider() {
- public CallbackHandler getCallbackHandler() {
- return new CallbackHandler() {
- public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException {
- for (Callback callback : callbacks) {
- if (callback instanceof NameCallback) {
- final NameCallback nameCallback = (NameCallback) callback;
- final String defaultName = nameCallback.getDefaultName();
- if (defaultName != null) {
- nameCallback.setName(defaultName);
- }
- if (!"user".equals(nameCallback.getName())) {
- throw new AuthenticationException("Invalid user name");
- }
- } else if (callback instanceof PasswordCallback) {
- final PasswordCallback passwordCallback = (PasswordCallback) callback;
- passwordCallback.setPassword("password".toCharArray());
- } else if (callback instanceof RealmCallback) {
- // allow
- } else if (callback instanceof AuthorizeCallback) {
- final AuthorizeCallback authorizeCallback = (AuthorizeCallback) callback;
- authorizeCallback.setAuthorized(authorizeCallback.getAuthenticationID().equals(authorizeCallback.getAuthorizationID()));
- } else {
- throw new UnsupportedCallbackException(callback, "Callback not supported: " + callback);
- }
- }
- }
- };
- }
- });
+ final SimpleServerAuthenticationProvider authenticationProvider = new SimpleServerAuthenticationProvider();
+ authenticationProvider.addUser("user", "endpoint", "password".toCharArray());
+ endpoint.addProtocolService(ProtocolServiceType.SERVER_AUTHENTICATION_PROVIDER, "test", authenticationProvider);
} finally {
exit();
}
14 years, 9 months
JBoss Remoting SVN: r5773 - in remoting3/trunk: jboss-remoting/src/main/java/org/jboss/remoting3/remote and 4 other directories.
by jboss-remoting-commits@lists.jboss.org
Author: david.lloyd(a)jboss.com
Date: 2010-02-28 17:05:37 -0500 (Sun, 28 Feb 2010)
New Revision: 5773
Modified:
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/HandleableCloseable.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ClientOpenListener.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerOpenListener.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/AbstractHandleableCloseable.java
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/InvocationTestBase.java
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemoteTestCase.java
remoting3/trunk/jboss-remoting/src/test/resources/logging.properties
remoting3/trunk/samples/src/main/java/org/jboss/remoting3/samples/socket/server/SocketServerRequestHandler.java
Log:
Clean up close path a little bit
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/HandleableCloseable.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/HandleableCloseable.java 2010-02-28 21:14:06 UTC (rev 5772)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/HandleableCloseable.java 2010-02-28 22:05:37 UTC (rev 5773)
@@ -43,6 +43,18 @@
void close() throws IOException;
/**
+ * Wait for a resource close to complete.
+ *
+ * @throws InterruptedException if the operation is interrupted
+ */
+ void awaitClosed() throws InterruptedException;
+
+ /**
+ * Wait for a resource close to complete.
+ */
+ void awaitClosedUninterruptibly();
+
+ /**
* Add a handler that will be called upon close. If the resource is already closed, the handler will be called
* immediately.
*
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ClientOpenListener.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ClientOpenListener.java 2010-02-28 21:14:06 UTC (rev 5772)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ClientOpenListener.java 2010-02-28 22:05:37 UTC (rev 5773)
@@ -75,6 +75,7 @@
try {
res = channel.write(buffer);
} catch (IOException e1) {
+ RemoteConnectionHandler.log.trace(e1, "Failed to send client greeting message");
IoUtils.safeClose(connection);
connection.free(buffer);
return;
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerOpenListener.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerOpenListener.java 2010-02-28 21:14:06 UTC (rev 5772)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerOpenListener.java 2010-02-28 22:05:37 UTC (rev 5773)
@@ -100,6 +100,7 @@
try {
res = channel.write(buffer);
} catch (IOException e1) {
+ RemoteConnectionHandler.log.trace(e1, "Failed to send server greeting message");
IoUtils.safeClose(connection);
connection.free(buffer);
return;
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/AbstractHandleableCloseable.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/AbstractHandleableCloseable.java 2010-02-28 21:14:06 UTC (rev 5772)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/AbstractHandleableCloseable.java 2010-02-28 22:05:37 UTC (rev 5773)
@@ -120,21 +120,7 @@
this.closeHandlers = null;
break;
}
- case CLOSING: {
- if (Thread.currentThread() != closingThread) {
- while (state != State.CLOSED) {
- try {
- closeLock.wait();
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- throw new InterruptedIOException("Close interrupted");
- }
- }
- } else {
- // reentrant close always goes through unblocked
- }
- return;
- }
+ case CLOSING:
case CLOSED: return;
default: throw new IllegalStateException();
}
@@ -158,6 +144,33 @@
}
}
+ /** {@inheritDoc} */
+ public void awaitClosed() throws InterruptedException {
+ synchronized (closeLock) {
+ while (state != State.CLOSED) {
+ closeLock.wait();
+ }
+ }
+ }
+
+ /** {@inheritDoc} */
+ public void awaitClosedUninterruptibly() {
+ boolean intr = false;
+ try {
+ synchronized (closeLock) {
+ while (state != State.CLOSED) {
+ try {
+ closeLock.wait();
+ } catch (InterruptedException e) {
+ intr = true;
+ }
+ }
+ }
+ } finally {
+ if (intr) Thread.currentThread().interrupt();
+ }
+ }
+
/**
* {@inheritDoc}
*/
Modified: remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/InvocationTestBase.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/InvocationTestBase.java 2010-02-28 21:14:06 UTC (rev 5772)
+++ remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/InvocationTestBase.java 2010-02-28 22:05:37 UTC (rev 5773)
@@ -106,12 +106,15 @@
assertEquals(replyObj, client.invoke(requestObj));
} finally {
IoUtils.safeClose(client);
+ client.awaitClosedUninterruptibly();
}
} finally {
IoUtils.safeClose(connection);
+ connection.awaitClosedUninterruptibly();
}
} finally {
IoUtils.safeClose(registration);
+ registration.awaitClosedUninterruptibly();
}
} finally {
exit();
@@ -151,12 +154,15 @@
assertEquals(replyObj, client.send(requestObj).get());
} finally {
IoUtils.safeClose(client);
+ client.awaitClosedUninterruptibly();
}
} finally {
IoUtils.safeClose(connection);
+ connection.awaitClosedUninterruptibly();
}
} finally {
IoUtils.safeClose(registration);
+ registration.awaitClosedUninterruptibly();
}
} finally {
exit();
@@ -208,12 +214,15 @@
}, InvocationTestObject.class, InvocationTestObject.class));
} finally {
IoUtils.safeClose(client);
+ client.awaitClosedUninterruptibly();
}
} finally {
IoUtils.safeClose(connection);
+ connection.awaitClosedUninterruptibly();
}
} finally {
IoUtils.safeClose(registration);
+ registration.awaitClosedUninterruptibly();
}
} catch (UnsupportedOperationException e) {
throw new SkipException("Skipping test due to unsupported createClientConnector");
@@ -244,6 +253,7 @@
enter();
try {
Xnio.getInstance().close();
+ System.runFinalization();
} finally {
exit();
}
Modified: remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemoteTestCase.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemoteTestCase.java 2010-02-28 21:14:06 UTC (rev 5772)
+++ remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemoteTestCase.java 2010-02-28 22:05:37 UTC (rev 5773)
@@ -101,11 +101,11 @@
final ChannelListener<ConnectedStreamChannel<InetSocketAddress>> listener = provider.getServerListener(OptionMap.builder().set(RemotingOptions.AUTHENTICATION_PROVIDER, "test").setSequence(Options.SASL_MECHANISMS, "DIGEST-MD5").getMap());
final Xnio xnio = Xnio.getInstance();
try {
-// final AcceptingServer<InetSocketAddress, ?, ?> server = xnio.createSslTcpServer(listener, OptionMap.EMPTY);
+// final AcceptingServer<InetSocketAddress, ?, ?> server = xnio.createSslTcpServer(listener, OptionMap.builder().setSequence(Options.SSL_ENABLED_CIPHER_SUITES, "TLS_RSA_WITH_AES_128_CBC_SHA").getMap());
final AcceptingServer<InetSocketAddress, ?, ?> server = xnio.createTcpServer(listener, OptionMap.EMPTY);
final IoFuture<? extends BoundChannel<InetSocketAddress>> future = server.bind(new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 0));
final InetSocketAddress localAddress = future.get().getLocalAddress();
- final Connection connection = endpoint.connect(new URI("remote", null, localAddress.getAddress().getHostAddress(), localAddress.getPort(), null, null, null), OptionMap.EMPTY, "user", null, "password".toCharArray()).get();
+ final Connection connection = endpoint.connect(new URI("remote", null, localAddress.getAddress().getHostAddress(), localAddress.getPort(), null, null, null), OptionMap.builder().setSequence(Options.SSL_ENABLED_CIPHER_SUITES, "TLS_RSA_WITH_AES_128_CBC_SHA").getMap(), "user", null, "password".toCharArray()).get();
connection.addCloseHandler(new CloseHandler<Connection>() {
public void handleClose(final Connection closed) {
IoUtils.safeClose(server);
Modified: remoting3/trunk/jboss-remoting/src/test/resources/logging.properties
===================================================================
--- remoting3/trunk/jboss-remoting/src/test/resources/logging.properties 2010-02-28 21:14:06 UTC (rev 5772)
+++ remoting3/trunk/jboss-remoting/src/test/resources/logging.properties 2010-02-28 22:05:37 UTC (rev 5773)
@@ -21,26 +21,24 @@
#
# Additional logger names to configure (root logger is always configured)
-#loggers=org.foo.bar, org.foo.baz
+loggers=javax.security.sasl
-# Root logger level
-logger.level=TRACE
-
-# Declare handlers for the root logger
+# Root logger configuration
+logger.level=DEBUG
logger.handlers=CONSOLE
-# Declare handlers for additional loggers
-#logger.org.foo.bar.handlers=XXX, YYY
+# Configure javax.security.sasl to be less verbose by default
+logger.javax.security.sasl.level=INFO
# Console handler configuration
handler.CONSOLE=org.jboss.logmanager.handlers.ConsoleHandler
handler.CONSOLE.target=SYSTEM_ERR
handler.CONSOLE.properties=autoFlush
-handler.CONSOLE.level=TRACE
+handler.CONSOLE.level=DEBUG
handler.CONSOLE.autoFlush=true
handler.CONSOLE.formatter=PATTERN
-# The log format pattern for both logs
+# The log format pattern
formatter.PATTERN=org.jboss.logmanager.formatters.PatternFormatter
formatter.PATTERN.properties=pattern
formatter.PATTERN.pattern=%d{HH:mm:ss,SSS} %-5p (%t) [%c] %m%n
Modified: remoting3/trunk/samples/src/main/java/org/jboss/remoting3/samples/socket/server/SocketServerRequestHandler.java
===================================================================
--- remoting3/trunk/samples/src/main/java/org/jboss/remoting3/samples/socket/server/SocketServerRequestHandler.java 2010-02-28 21:14:06 UTC (rev 5772)
+++ remoting3/trunk/samples/src/main/java/org/jboss/remoting3/samples/socket/server/SocketServerRequestHandler.java 2010-02-28 22:05:37 UTC (rev 5773)
@@ -157,7 +157,15 @@
return null;
}
- public String toString() {
+ public void awaitClosed() throws InterruptedException {
+ throw new UnsupportedOperationException();
+ }
+
+ public void awaitClosedUninterruptibly() {
+ throw new UnsupportedOperationException();
+ }
+
+ public String toString() {
return "SocketServerRequestHandler[" + super.toString() + "]";
}
14 years, 9 months
JBoss Remoting SVN: r5772 - in remoting3/trunk/jboss-remoting/src: main/java/org/jboss/remoting3/remote and 4 other directories.
by jboss-remoting-commits@lists.jboss.org
Author: david.lloyd(a)jboss.com
Date: 2010-02-28 16:14:06 -0500 (Sun, 28 Feb 2010)
New Revision: 5772
Added:
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/AbstractClientMessageHandler.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/AbstractMessageHandler.java
Modified:
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/EndpointImpl.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/RemotingOptions.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ClientAuthenticationHandler.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ClientGreetingHandler.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ClientOpenListener.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/InboundRequestInputHandler.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundRequestHandler.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteConnection.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteConnectionHandler.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteMessageHandler.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteProtocol.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/SaslUtils.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerAuthenticationHandler.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerGreetingHandler.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerInitialAuthenticationHandler.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerOpenListener.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/security/SimpleClientCallbackHandler.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/AbstractHandleableCloseable.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/ConnectionProviderContext.java
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemoteTestCase.java
remoting3/trunk/jboss-remoting/src/test/resources/remoting.properties
Log:
Make sure DIGEST-MD5 and CRAM-MD5 both work
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/EndpointImpl.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/EndpointImpl.java 2010-02-28 15:51:38 UTC (rev 5771)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/EndpointImpl.java 2010-02-28 21:14:06 UTC (rev 5772)
@@ -534,10 +534,19 @@
}
public IoFuture<? extends Connection> connect(final URI destination, final OptionMap connectOptions) throws IOException {
- return connect(destination, connectOptions, new SimpleClientCallbackHandler(connectOptions.get(RemotingOptions.AUTH_USER_NAME), connectOptions.get(RemotingOptions.AUTH_REALM), null));
+ final String uriUserInfo = destination.getUserInfo();
+ final OptionMap finalMap;
+ if (uriUserInfo != null) {
+ final OptionMap.Builder builder = OptionMap.builder().addAll(connectOptions);
+ builder.set(RemotingOptions.AUTH_USER_NAME, uriUserInfo);
+ finalMap = builder.getMap();
+ } else {
+ finalMap = connectOptions;
+ }
+ return doConnect(destination, connectOptions, new SimpleClientCallbackHandler(finalMap.get(RemotingOptions.AUTH_USER_NAME), finalMap.get(RemotingOptions.AUTH_REALM), null));
}
- public IoFuture<? extends Connection> connect(final URI destination, final OptionMap connectOptions, final CallbackHandler callbackHandler) throws IOException {
+ private IoFuture<? extends Connection> doConnect(final URI destination, final OptionMap connectOptions, final CallbackHandler callbackHandler) throws IOException {
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(CONNECT_PERM);
@@ -556,10 +565,28 @@
return futureResult.getIoFuture();
}
+ public IoFuture<? extends Connection> connect(final URI destination, final OptionMap connectOptions, final CallbackHandler callbackHandler) throws IOException {
+ final String uriUserInfo = destination.getUserInfo();
+ final OptionMap finalMap;
+ if (uriUserInfo != null) {
+ final OptionMap.Builder builder = OptionMap.builder().addAll(connectOptions);
+ builder.set(RemotingOptions.AUTH_USER_NAME, uriUserInfo);
+ finalMap = builder.getMap();
+ } else {
+ finalMap = connectOptions;
+ }
+ return doConnect(destination, finalMap, callbackHandler);
+ }
+
public IoFuture<? extends Connection> connect(final URI destination, final OptionMap connectOptions, final String userName, final String realmName, final char[] password) throws IOException {
- final String actualUserName = userName != null ? userName : connectOptions.get(RemotingOptions.AUTH_USER_NAME);
+ final String uriUserInfo = destination.getUserInfo();
+ final String actualUserName = userName != null ? userName : uriUserInfo != null ? uriUserInfo : connectOptions.get(RemotingOptions.AUTH_USER_NAME);
final String actualUserRealm = realmName != null ? realmName : connectOptions.get(RemotingOptions.AUTH_REALM);
- return connect(destination, connectOptions, new SimpleClientCallbackHandler(actualUserName, actualUserRealm, password));
+ final OptionMap.Builder builder = OptionMap.builder().addAll(connectOptions);
+ if (actualUserName != null) builder.set(RemotingOptions.AUTH_USER_NAME, actualUserName);
+ if (actualUserRealm != null) builder.set(RemotingOptions.AUTH_REALM, actualUserRealm);
+ final OptionMap finalMap = builder.getMap();
+ return doConnect(destination, finalMap, new SimpleClientCallbackHandler(actualUserName, actualUserRealm, password));
}
public ConnectionProviderRegistration addConnectionProvider(final String uriScheme, final ConnectionProviderFactory providerFactory) {
@@ -695,6 +722,10 @@
public <T> T getProtocolServiceProvider(final ProtocolServiceType<T> serviceType, final String name) {
return getMapFor(serviceType).get(name);
}
+
+ public String getEndpointName() {
+ return getName();
+ }
}
private class ConnectionProviderRegistrationImpl extends AbstractHandleableCloseable<Registration> implements ConnectionProviderRegistration {
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/RemotingOptions.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/RemotingOptions.java 2010-02-28 15:51:38 UTC (rev 5771)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/RemotingOptions.java 2010-02-28 21:14:06 UTC (rev 5772)
@@ -143,9 +143,4 @@
* Specify the name of a preregistered server authentication provider to use.
*/
public static final Option<String> AUTHENTICATION_PROVIDER = Option.simple(RemotingOptions.class, "AUTHENTICATION_PROVIDER", String.class);
-
- /**
- * Specify a set of SASL server mechanisms to allow. If not specified, no mechanisms will be excluded.
- */
- public static final Option<Sequence<String>> SASL_SERVER_MECHANISMS = Option.sequence(RemotingOptions.class, "SASL_SERVER_MECHANISMS", String.class);
}
Added: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/AbstractClientMessageHandler.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/AbstractClientMessageHandler.java (rev 0)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/AbstractClientMessageHandler.java 2010-02-28 21:14:06 UTC (rev 5772)
@@ -0,0 +1,41 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.remoting3.remote;
+
+import java.io.IOException;
+import org.jboss.remoting3.spi.ConnectionHandlerFactory;
+import org.jboss.xnio.Result;
+
+abstract class AbstractClientMessageHandler extends AbstractMessageHandler {
+ private final Result<ConnectionHandlerFactory> result;
+
+ protected AbstractClientMessageHandler(final RemoteConnection remoteConnection, final Result<ConnectionHandlerFactory> result) {
+ super(remoteConnection);
+ this.result = result;
+ }
+
+ public void handleException(final IOException e) {
+ result.setException(e);
+ super.handleException(e);
+ }
+}
Added: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/AbstractMessageHandler.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/AbstractMessageHandler.java (rev 0)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/AbstractMessageHandler.java 2010-02-28 21:14:06 UTC (rev 5772)
@@ -0,0 +1,50 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.remoting3.remote;
+
+import java.io.IOException;
+import org.jboss.xnio.IoUtils;
+import org.jboss.xnio.channels.MessageHandler;
+
+abstract class AbstractMessageHandler implements MessageHandler {
+ protected final RemoteConnection remoteConnection;
+
+ protected AbstractMessageHandler(final RemoteConnection remoteConnection) {
+ this.remoteConnection = remoteConnection;
+ }
+
+ public void handleEof() {
+ try {
+ remoteConnection.getChannel().shutdownReads();
+ } catch (IOException e) {
+ RemoteConnectionHandler.log.trace(e, "Failed to shut down reads for %s", remoteConnection);
+ }
+ remoteConnection.readDone();
+ IoUtils.safeClose(remoteConnection);
+ }
+
+ public void handleException(final IOException e) {
+ RemoteConnectionHandler.log.trace(e, "Received exception from %s", remoteConnection);
+ IoUtils.safeClose(remoteConnection);
+ }
+}
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ClientAuthenticationHandler.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ClientAuthenticationHandler.java 2010-02-28 15:51:38 UTC (rev 5771)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ClientAuthenticationHandler.java 2010-02-28 21:14:06 UTC (rev 5772)
@@ -22,7 +22,6 @@
package org.jboss.remoting3.remote;
-import java.io.EOFException;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.jboss.marshalling.MarshallerFactory;
@@ -35,18 +34,18 @@
import org.jboss.xnio.Buffers;
import org.jboss.xnio.IoUtils;
import org.jboss.xnio.Result;
-import org.jboss.xnio.channels.MessageHandler;
import javax.security.sasl.SaslClient;
import javax.security.sasl.SaslException;
-final class ClientAuthenticationHandler implements MessageHandler {
+final class ClientAuthenticationHandler extends AbstractClientMessageHandler {
private final RemoteConnection remoteConnection;
private final SaslClient saslClient;
private final Result<ConnectionHandlerFactory> factoryResult;
- public ClientAuthenticationHandler(final RemoteConnection remoteConnection, final SaslClient saslClient, final Result<ConnectionHandlerFactory> factoryResult) {
+ ClientAuthenticationHandler(final RemoteConnection remoteConnection, final SaslClient saslClient, final Result<ConnectionHandlerFactory> factoryResult) {
+ super(remoteConnection, factoryResult);
this.remoteConnection = remoteConnection;
this.saslClient = saslClient;
this.factoryResult = factoryResult;
@@ -55,54 +54,92 @@
public void handleMessage(final ByteBuffer buffer) {
final byte msgType = buffer.get();
switch (msgType) {
- case RemoteProtocol.AUTH_CHALLENGE:
- case RemoteProtocol.AUTH_COMPLETE: {
+ case RemoteProtocol.AUTH_CHALLENGE: {
+ RemoteConnectionHandler.log.trace("Received challenge message");
+ final boolean clientComplete = saslClient.isComplete();
+ if (clientComplete) {
+ RemoteConnectionHandler.log.trace("Received extra auth challenge message on %s after completion", remoteConnection);
+ factoryResult.setException(new SaslException("Received extra auth message after completion"));
+ IoUtils.safeClose(remoteConnection);
+ return;
+ }
final byte[] response;
+ final byte[] challenge = Buffers.take(buffer, buffer.remaining());
try {
- response = saslClient.evaluateChallenge(Buffers.take(buffer, buffer.remaining()));
+ response = saslClient.evaluateChallenge(challenge);
+ if (msgType == RemoteProtocol.AUTH_COMPLETE && response != null && response.length > 0) {
+ RemoteConnectionHandler.log.trace("Received extra auth message on %s", remoteConnection);
+ factoryResult.setException(new SaslException("Received extra auth message after completion"));
+ IoUtils.safeClose(remoteConnection);
+ return;
+ }
} catch (SaslException e) {
- // todo log it
+ RemoteConnectionHandler.log.trace(e, "Authentication error");
factoryResult.setException(e);
+ try {
+ remoteConnection.shutdownWritesBlocking();
+ } catch (IOException e1) {
+ RemoteConnectionHandler.log.trace(e, "Unable to shut down writes");
+ }
+ return;
+ }
+ try {
+ RemoteConnectionHandler.log.trace("Sending SASL response");
+ remoteConnection.sendAuthMessage(RemoteProtocol.AUTH_RESPONSE, response);
+ } catch (IOException e) {
+ factoryResult.setException(e);
+ RemoteConnectionHandler.log.trace("Failed to send auth response message on %s", remoteConnection);
IoUtils.safeClose(remoteConnection);
return;
}
- if (msgType == RemoteProtocol.AUTH_COMPLETE) {
- if ((response != null || response.length > 0)) {
- // todo log extraneous message
+ return;
+ }
+ case RemoteProtocol.AUTH_COMPLETE: {
+ RemoteConnectionHandler.log.trace("Received auth complete message");
+ final boolean clientComplete = saslClient.isComplete();
+ final byte[] challenge = Buffers.take(buffer, buffer.remaining());
+ if (! clientComplete) try {
+ final byte[] response = saslClient.evaluateChallenge(challenge);
+ if (response != null && response.length > 0) {
+ RemoteConnectionHandler.log.trace("Received extra auth message on %s", remoteConnection);
+ factoryResult.setException(new SaslException("Received extra auth message after completion"));
IoUtils.safeClose(remoteConnection);
return;
}
- // auth complete.
- factoryResult.setResult(new ConnectionHandlerFactory() {
- public ConnectionHandler createInstance(final ConnectionHandlerContext connectionContext) {
- // this happens immediately.
- final MarshallerFactory marshallerFactory = Marshalling.getMarshallerFactory("river");
- final MarshallingConfiguration marshallingConfiguration = new MarshallingConfiguration();
- final RemoteConnectionHandler connectionHandler = new RemoteConnectionHandler(connectionContext, remoteConnection, marshallerFactory, marshallingConfiguration);
- remoteConnection.addCloseHandler(new CloseHandler<Object>() {
- public void handleClose(final Object closed) {
- IoUtils.safeClose(connectionHandler);
- }
- });
- remoteConnection.setMessageHandler(new RemoteMessageHandler(connectionHandler, remoteConnection));
- return connectionHandler;
- }
- });
+ if (! saslClient.isComplete()) {
+ RemoteConnectionHandler.log.trace("Client not complete after processing auth complete message on %s", remoteConnection);
+ factoryResult.setException(new SaslException("Client not complete after processing auth complete message"));
+ IoUtils.safeClose(remoteConnection);
+ return;
+ }
+ } catch (SaslException e) {
+ RemoteConnectionHandler.log.trace(e, "Authentication error");
+ factoryResult.setException(e);
+ try {
+ remoteConnection.shutdownWritesBlocking();
+ } catch (IOException e1) {
+ RemoteConnectionHandler.log.trace(e, "Unable to shut down writes");
+ }
return;
}
- break;
+ // auth complete.
+ factoryResult.setResult(new ConnectionHandlerFactory() {
+ public ConnectionHandler createInstance(final ConnectionHandlerContext connectionContext) {
+ // this happens immediately.
+ final MarshallerFactory marshallerFactory = Marshalling.getMarshallerFactory("river");
+ final MarshallingConfiguration marshallingConfiguration = new MarshallingConfiguration();
+ final RemoteConnectionHandler connectionHandler = new RemoteConnectionHandler(connectionContext, remoteConnection, marshallerFactory, marshallingConfiguration);
+ remoteConnection.addCloseHandler(new CloseHandler<Object>() {
+ public void handleClose(final Object closed) {
+ IoUtils.safeClose(connectionHandler);
+ }
+ });
+ remoteConnection.setMessageHandler(new RemoteMessageHandler(connectionHandler, remoteConnection));
+ return connectionHandler;
+ }
+ });
+ return;
}
}
}
-
- public void handleEof() {
- factoryResult.setException(new EOFException("End of file on input"));
- IoUtils.safeClose(remoteConnection);
- }
-
- public void handleException(final IOException e) {
- // todo log it
- factoryResult.setException(e);
- IoUtils.safeClose(remoteConnection);
- }
}
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ClientGreetingHandler.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ClientGreetingHandler.java 2010-02-28 15:51:38 UTC (rev 5771)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ClientGreetingHandler.java 2010-02-28 21:14:06 UTC (rev 5772)
@@ -22,7 +22,6 @@
package org.jboss.remoting3.remote;
-import java.io.EOFException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
@@ -31,23 +30,21 @@
import org.jboss.remoting3.RemotingOptions;
import org.jboss.remoting3.spi.ConnectionHandlerFactory;
import org.jboss.xnio.Buffers;
-import org.jboss.xnio.IoUtils;
-import org.jboss.xnio.Option;
import org.jboss.xnio.OptionMap;
import org.jboss.xnio.Result;
-import org.jboss.xnio.channels.MessageHandler;
import javax.security.auth.callback.CallbackHandler;
import javax.security.sasl.Sasl;
import javax.security.sasl.SaslClient;
import javax.security.sasl.SaslException;
-final class ClientGreetingHandler implements MessageHandler {
+final class ClientGreetingHandler extends AbstractClientMessageHandler {
private final RemoteConnection connection;
private final Result<ConnectionHandlerFactory> factoryResult;
private final CallbackHandler callbackHandler;
- public ClientGreetingHandler(final RemoteConnection connection, final Result<ConnectionHandlerFactory> factoryResult, final CallbackHandler callbackHandler) {
+ ClientGreetingHandler(final RemoteConnection connection, final Result<ConnectionHandlerFactory> factoryResult, final CallbackHandler callbackHandler) {
+ super(connection, factoryResult);
this.connection = connection;
this.factoryResult = factoryResult;
this.callbackHandler = callbackHandler;
@@ -55,6 +52,7 @@
public void handleMessage(final ByteBuffer buffer) {
List<String> saslMechs = new ArrayList<String>();
+ String remoteEndpointName = "endpoint";
switch (buffer.get()) {
case RemoteProtocol.GREETING: {
while (buffer.hasRemaining()) {
@@ -71,6 +69,10 @@
saslMechs.add(Buffers.getModifiedUtf8(Buffers.slice(buffer, len)));
break;
}
+ case RemoteProtocol.GREETING_ENDPOINT_NAME: {
+ remoteEndpointName = Buffers.getModifiedUtf8(Buffers.slice(buffer, len));
+ break;
+ }
default: {
// unknown, skip it for forward compatibility.
Buffers.skip(buffer, len);
@@ -81,37 +83,49 @@
// OK now send our authentication request
final OptionMap optionMap = connection.getOptionMap();
final String userName = optionMap.get(RemotingOptions.AUTH_USER_NAME);
- final String hostName = connection.getChannel().getPeerAddress().getHostName();
final Map<String, ?> propertyMap = SaslUtils.createPropertyMap(optionMap);
final SaslClient saslClient;
try {
- saslClient = Sasl.createSaslClient(saslMechs.toArray(new String[saslMechs.size()]), userName == null ? "anonymous" : userName, "remote", hostName, propertyMap, callbackHandler);
+ saslClient = Sasl.createSaslClient(saslMechs.toArray(new String[saslMechs.size()]), userName, "remote", remoteEndpointName, propertyMap, callbackHandler);
} catch (SaslException e) {
factoryResult.setException(e);
- // todo log exception @ error
- // todo send "goodbye" & close
- IoUtils.safeClose(connection);
+ RemoteConnectionHandler.log.trace(e, "Client connect authentication error");
+ try {
+ remoteConnection.shutdownWritesBlocking();
+ } catch (IOException e1) {
+ RemoteConnectionHandler.log.trace(e1, "Failed to shutdown writes on %s", remoteConnection);
+ }
return;
}
+ final String mechanismName = saslClient.getMechanismName();
+ RemoteConnectionHandler.log.trace("Sasl mechanism selected: %s", mechanismName);
+ final ByteBuffer outBuf = connection.allocate();
+ try {
+ outBuf.putInt(0);
+ outBuf.put(RemoteProtocol.AUTH_REQUEST);
+ Buffers.putModifiedUtf8(outBuf, mechanismName);
+ outBuf.flip();
+ connection.sendBlocking(outBuf);
+ connection.flushBlocking();
+ } catch (IOException e) {
+ RemoteConnectionHandler.log.trace(e, "Failed to send auth request on %s", remoteConnection);
+ factoryResult.setException(e);
+ return;
+ } finally {
+ connection.free(outBuf);
+ }
connection.setMessageHandler(new ClientAuthenticationHandler(connection, saslClient, factoryResult));
return;
}
default: {
- // todo log invalid greeting
- IoUtils.safeClose(connection);
+ RemoteConnectionHandler.log.warn("Received invalid greeting packet on %s", remoteConnection);
+ try {
+ remoteConnection.shutdownWritesBlocking();
+ } catch (IOException e1) {
+ RemoteConnectionHandler.log.trace(e1, "Failed to shutdown writes on %s", remoteConnection);
+ }
return;
}
}
}
-
- public void handleEof() {
- factoryResult.setException(new EOFException("End of file on input"));
- IoUtils.safeClose(connection);
- }
-
- public void handleException(final IOException e) {
- // todo log it
- factoryResult.setException(e);
- IoUtils.safeClose(connection);
- }
}
\ No newline at end of file
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ClientOpenListener.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ClientOpenListener.java 2010-02-28 15:51:38 UTC (rev 5771)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ClientOpenListener.java 2010-02-28 21:14:06 UTC (rev 5772)
@@ -43,7 +43,7 @@
private final Result<ConnectionHandlerFactory> factoryResult;
private final CallbackHandler callbackHandler;
- public ClientOpenListener(final OptionMap optionMap, final ConnectionProviderContext connectionProviderContext, final Result<ConnectionHandlerFactory> factoryResult, final CallbackHandler callbackHandler) {
+ ClientOpenListener(final OptionMap optionMap, final ConnectionProviderContext connectionProviderContext, final Result<ConnectionHandlerFactory> factoryResult, final CallbackHandler callbackHandler) {
this.optionMap = optionMap;
this.connectionProviderContext = connectionProviderContext;
this.factoryResult = factoryResult;
@@ -60,24 +60,39 @@
// Send client greeting packet...
final ByteBuffer buffer = connection.allocate();
- try {
- // length placeholder
- buffer.putInt(0);
- // version ID
- GreetingUtils.writeByte(buffer, RemoteProtocol.GREETING_VERSION, RemoteProtocol.VERSION);
- // that's it!
- buffer.flip();
- connection.sendBlocking(buffer);
- } catch (IOException e1) {
- // todo log it
- factoryResult.setException(e1);
- IoUtils.safeClose(connection);
- } finally {
- connection.free(buffer);
- }
+ // length placeholder
+ buffer.putInt(0);
+ // version ID
+ GreetingUtils.writeByte(buffer, RemoteProtocol.GREETING_VERSION, RemoteProtocol.VERSION);
+ // that's it!
+ buffer.flip();
+ buffer.putInt(0, buffer.remaining() - 4);
+ channel.getWriteSetter().set(new ChannelListener<ConnectedStreamChannel<InetSocketAddress>>() {
+ public void handleEvent(final ConnectedStreamChannel<InetSocketAddress> channel) {
+ for (;;) {
+ while (buffer.hasRemaining()) {
+ final int res;
+ try {
+ res = channel.write(buffer);
+ } catch (IOException e1) {
+ IoUtils.safeClose(connection);
+ connection.free(buffer);
+ return;
+ }
+ if (res == 0) {
+ channel.resumeWrites();
+ return;
+ }
+ }
+ connection.free(buffer);
+ channel.resumeReads();
+ return;
+ }
+ }
+ });
connection.setMessageHandler(new ClientGreetingHandler(connection, factoryResult, callbackHandler));
- // start up the read cycle
- channel.resumeReads();
+ // and send the greeting
+ channel.resumeWrites();
}
}
\ No newline at end of file
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/InboundRequestInputHandler.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/InboundRequestInputHandler.java 2010-02-28 15:51:38 UTC (rev 5771)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/InboundRequestInputHandler.java 2010-02-28 21:14:06 UTC (rev 5772)
@@ -56,7 +56,7 @@
}
public void close() throws IOException {
- // todo: stream was closed, no action needed
+ // no operation
}
public String toString() {
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundRequestHandler.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundRequestHandler.java 2010-02-28 15:51:38 UTC (rev 5771)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundRequestHandler.java 2010-02-28 21:14:06 UTC (rev 5772)
@@ -81,7 +81,7 @@
try {
connectionHandler.getRemoteConnection().sendBlocking(buf);
} catch (IOException e1) {
- // todo log it
+ RemoteConnectionHandler.log.trace("Send failed: %s", e1);
}
}
return outboundRequest;
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteConnection.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteConnection.java 2010-02-28 15:51:38 UTC (rev 5771)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteConnection.java 2010-02-28 21:14:06 UTC (rev 5772)
@@ -34,17 +34,16 @@
import org.jboss.xnio.OptionMap;
import org.jboss.xnio.Pool;
import org.jboss.xnio.channels.Channels;
-import org.jboss.xnio.channels.ConnectedChannel;
import org.jboss.xnio.channels.ConnectedStreamChannel;
import org.jboss.xnio.channels.MessageHandler;
-import javax.security.auth.callback.CallbackHandler;
-
final class RemoteConnection extends AbstractHandleableCloseable<RemoteConnection> implements Closeable {
private final ConnectedStreamChannel<InetSocketAddress> channel;
private final Pool<ByteBuffer> bufferPool = Buffers.createHeapByteBufferAllocator(4096);
private final MessageHandler.Setter messageHandlerSetter;
private final OptionMap optionMap;
+ private boolean readDone;
+ private final Object writeLock = new Object();
RemoteConnection(final Executor executor, final ConnectedStreamChannel<InetSocketAddress> channel, final OptionMap optionMap) {
super(executor);
@@ -53,7 +52,28 @@
this.optionMap = optionMap;
}
- public void close() throws IOException {
+ protected void closeAction() throws IOException {
+ synchronized (writeLock) {
+ try {
+ shutdownWritesBlocking();
+ } catch (IOException e) {
+ readDone = true;
+ writeLock.notifyAll();
+ IoUtils.safeClose(channel);
+ return;
+ }
+ while (! readDone) {
+ try {
+ writeLock.wait();
+ } catch (InterruptedException e) {
+ readDone = true;
+ writeLock.notifyAll();
+ IoUtils.safeClose(channel);
+ Thread.currentThread().interrupt();
+ throw new InterruptedIOException();
+ }
+ }
+ }
channel.close();
}
@@ -61,7 +81,7 @@
return optionMap;
}
- ConnectedChannel<InetSocketAddress> getChannel() {
+ ConnectedStreamChannel<InetSocketAddress> getChannel() {
return channel;
}
@@ -93,54 +113,60 @@
}
void sendBlockingNoClose(final ByteBuffer buffer) throws IOException {
- buffer.putInt(0, buffer.remaining() - 4);
- boolean intr = false;
- try {
- while (buffer.hasRemaining()) {
- if (channel.write(buffer) == 0) {
- try {
- channel.awaitWritable();
- } catch (InterruptedIOException e) {
- intr = Thread.interrupted();
+ synchronized (writeLock) {
+ buffer.putInt(0, buffer.remaining() - 4);
+ boolean intr = false;
+ try {
+ while (buffer.hasRemaining()) {
+ if (channel.write(buffer) == 0) {
+ try {
+ channel.awaitWritable();
+ } catch (InterruptedIOException e) {
+ intr = Thread.interrupted();
+ }
}
}
+ } finally {
+ if (intr) Thread.currentThread().interrupt();
}
- } finally {
- if (intr) Thread.currentThread().interrupt();
}
}
void flushBlocking() throws IOException {
- try {
- while (! channel.flush()) {
- channel.awaitWritable();
+ synchronized (writeLock) {
+ try {
+ while (! channel.flush()) {
+ channel.awaitWritable();
+ }
+ } catch (IOException e) {
+ IoUtils.safeClose(this);
+ throw e;
+ } catch (RuntimeException e) {
+ IoUtils.safeClose(this);
+ throw e;
+ } catch (Error e) {
+ IoUtils.safeClose(this);
+ throw e;
}
- } catch (IOException e) {
- IoUtils.safeClose(this);
- throw e;
- } catch (RuntimeException e) {
- IoUtils.safeClose(this);
- throw e;
- } catch (Error e) {
- IoUtils.safeClose(this);
- throw e;
}
}
void shutdownWritesBlocking() throws IOException {
- try {
- while (! channel.shutdownWrites()) {
- channel.awaitWritable();
+ synchronized (writeLock) {
+ try {
+ while (! channel.shutdownWrites()) {
+ channel.awaitWritable();
+ }
+ } catch (IOException e) {
+ IoUtils.safeClose(channel);
+ throw e;
+ } catch (RuntimeException e) {
+ IoUtils.safeClose(channel);
+ throw e;
+ } catch (Error e) {
+ IoUtils.safeClose(channel);
+ throw e;
}
- } catch (IOException e) {
- IoUtils.safeClose(this);
- throw e;
- } catch (RuntimeException e) {
- IoUtils.safeClose(this);
- throw e;
- } catch (Error e) {
- IoUtils.safeClose(this);
- throw e;
}
}
@@ -175,4 +201,13 @@
void shutdownReads() throws IOException {
channel.shutdownReads();
}
+
+ void readDone() {
+ synchronized (writeLock) {
+ readDone = true;
+ writeLock.notifyAll();
+ }
+ }
+
+
}
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteConnectionHandler.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteConnectionHandler.java 2010-02-28 15:51:38 UTC (rev 5771)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteConnectionHandler.java 2010-02-28 21:14:06 UTC (rev 5772)
@@ -31,6 +31,7 @@
import org.jboss.marshalling.util.IntKeyMap;
import org.jboss.remoting3.IndeterminateOutcomeException;
import org.jboss.remoting3.ServiceOpenException;
+import org.jboss.remoting3.spi.AbstractHandleableCloseable;
import org.jboss.remoting3.spi.ConnectionHandler;
import org.jboss.remoting3.spi.ConnectionHandlerContext;
import org.jboss.remoting3.spi.RequestHandler;
@@ -41,10 +42,9 @@
import org.jboss.xnio.IoUtils;
import org.jboss.xnio.Pool;
import org.jboss.xnio.Result;
-import org.jboss.xnio.channels.Channels;
import org.jboss.xnio.log.Logger;
-final class RemoteConnectionHandler implements ConnectionHandler {
+final class RemoteConnectionHandler extends AbstractHandleableCloseable<RemoteConnectionHandler> implements ConnectionHandler {
static final Logger log = Logger.getLogger("org.jboss.remoting.remote");
@@ -67,6 +67,7 @@
private final AtomicBoolean closed = new AtomicBoolean();
public RemoteConnectionHandler(final ConnectionHandlerContext connectionContext, final RemoteConnection remoteConnection, final MarshallerFactory marshallerFactory, final MarshallingConfiguration marshallingConfiguration) {
+ super(connectionContext.getConnectionProviderContext().getExecutor());
this.connectionContext = connectionContext;
this.remoteConnection = remoteConnection;
this.marshallerFactory = marshallerFactory;
@@ -107,35 +108,33 @@
throw new UnsupportedOperationException();
}
- public void close() throws IOException {
- if (! closed.getAndSet(true)) {
- try {
- remoteConnection.close();
- } finally {
- // other actions here
- for (IntKeyMap.Entry<OutboundClient> entry : outboundClients) {
- final OutboundClient outboundClient = entry.getValue();
- synchronized (outboundClient) {
- IoUtils.safeClose(outboundClient.getRequestHandler());
- }
+ protected void closeAction() throws IOException {
+ try {
+ remoteConnection.close();
+ } finally {
+ // other actions here
+ for (IntKeyMap.Entry<OutboundClient> entry : outboundClients) {
+ final OutboundClient outboundClient = entry.getValue();
+ synchronized (outboundClient) {
+ IoUtils.safeClose(outboundClient.getRequestHandler());
}
- for (IntKeyMap.Entry<InboundClient> entry : inboundClients) {
- final InboundClient inboundClient = entry.getValue();
- synchronized (inboundClient) {
- IoUtils.safeClose(inboundClient.getHandler());
- }
+ }
+ for (IntKeyMap.Entry<InboundClient> entry : inboundClients) {
+ final InboundClient inboundClient = entry.getValue();
+ synchronized (inboundClient) {
+ IoUtils.safeClose(inboundClient.getHandler());
}
- for (IntKeyMap.Entry<OutboundRequest> entry : outboundRequests) {
- final OutboundRequest outboundRequest = entry.getValue();
- synchronized (outboundRequest) {
- SpiUtils.safeHandleException(outboundRequest.getInboundReplyHandler(), new IndeterminateOutcomeException("Connection closed"));
- }
+ }
+ for (IntKeyMap.Entry<OutboundRequest> entry : outboundRequests) {
+ final OutboundRequest outboundRequest = entry.getValue();
+ synchronized (outboundRequest) {
+ SpiUtils.safeHandleException(outboundRequest.getInboundReplyHandler(), new IndeterminateOutcomeException("Connection closed"));
}
- for (IntKeyMap.Entry<InboundRequest> entry : inboundRequests) {
- final InboundRequest inboundRequest = entry.getValue();
- synchronized (inboundRequest) {
- inboundRequest.getCancellable().cancel();
- }
+ }
+ for (IntKeyMap.Entry<InboundRequest> entry : inboundRequests) {
+ final InboundRequest inboundRequest = entry.getValue();
+ synchronized (inboundRequest) {
+ inboundRequest.getCancellable().cancel();
}
}
}
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteMessageHandler.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteMessageHandler.java 2010-02-28 15:51:38 UTC (rev 5771)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteMessageHandler.java 2010-02-28 21:14:06 UTC (rev 5772)
@@ -38,12 +38,13 @@
import org.jboss.xnio.OptionMap;
import org.jboss.xnio.Pool;
-final class RemoteMessageHandler implements org.jboss.xnio.channels.MessageHandler {
+final class RemoteMessageHandler extends AbstractMessageHandler implements org.jboss.xnio.channels.MessageHandler {
private final RemoteConnection connection;
private final RemoteConnectionHandler remoteConnectionHandler;
RemoteMessageHandler(final RemoteConnectionHandler remoteConnectionHandler, final RemoteConnection connection) {
+ super(connection);
this.remoteConnectionHandler = remoteConnectionHandler;
this.connection = connection;
}
@@ -309,12 +310,4 @@
}
}
}
-
- public void handleEof() {
- IoUtils.safeClose(remoteConnectionHandler);
- }
-
- public void handleException(final IOException e) {
- IoUtils.safeClose(remoteConnectionHandler);
- }
}
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteProtocol.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteProtocol.java 2010-02-28 15:51:38 UTC (rev 5771)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteProtocol.java 2010-02-28 21:14:06 UTC (rev 5772)
@@ -66,6 +66,7 @@
static final byte GREETING_VERSION = 0; // sent by client & server
static final byte GREETING_SASL_MECH = 1; // sent by server
+ static final byte GREETING_ENDPOINT_NAME = 2; // sent by client & server
/**
* Create an instance of the connection provider for the "remote" protocol.
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/SaslUtils.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/SaslUtils.java 2010-02-28 15:51:38 UTC (rev 5771)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/SaslUtils.java 2010-02-28 21:14:06 UTC (rev 5772)
@@ -68,15 +68,20 @@
}
private static void addQopList(OptionMap optionMap, Option<Sequence<SaslQop>> option, Map<String, Object> map, String propName) {
- final Sequence<SaslQop> seq = optionMap.get(option);
+ final Sequence<SaslQop> value = optionMap.get(option);
+ if (value == null) return;
+ final Sequence<SaslQop> seq = value;
final StringBuilder builder = new StringBuilder();
final Iterator<SaslQop> iterator = seq.iterator();
- while (iterator.hasNext()) {
+ if (!iterator.hasNext()) {
+ return;
+ } else do {
builder.append(iterator.next());
if (iterator.hasNext()) {
builder.append(',');
}
- }
+ } while (iterator.hasNext());
+ map.put(propName, builder.toString());
}
private static final Set<String> SECURE_QOP;
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerAuthenticationHandler.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerAuthenticationHandler.java 2010-02-28 15:51:38 UTC (rev 5771)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerAuthenticationHandler.java 2010-02-28 21:14:06 UTC (rev 5772)
@@ -34,17 +34,17 @@
import org.jboss.remoting3.spi.ConnectionProviderContext;
import org.jboss.xnio.Buffers;
import org.jboss.xnio.IoUtils;
-import org.jboss.xnio.channels.MessageHandler;
import javax.security.sasl.SaslException;
import javax.security.sasl.SaslServer;
-final class ServerAuthenticationHandler implements MessageHandler {
+final class ServerAuthenticationHandler extends AbstractMessageHandler {
private final RemoteConnection remoteConnection;
private final SaslServer saslServer;
private final ConnectionProviderContext connectionProviderContext;
ServerAuthenticationHandler(final RemoteConnection remoteConnection, final SaslServer saslServer, final ConnectionProviderContext connectionProviderContext) {
+ super(remoteConnection);
this.saslServer = saslServer;
this.remoteConnection = remoteConnection;
this.connectionProviderContext = connectionProviderContext;
@@ -53,17 +53,20 @@
public void handleMessage(final ByteBuffer buffer) {
switch (buffer.get()) {
case RemoteProtocol.AUTH_RESPONSE: {
+ RemoteConnectionHandler.log.trace("Received SASL response");
final byte[] challenge;
try {
try {
challenge = saslServer.evaluateResponse(Buffers.take(buffer, buffer.remaining()));
} catch (SaslException e) {
- // todo log it
+ RemoteConnectionHandler.log.trace(e, "Server authentication failed");
remoteConnection.sendAuthReject("Authentication failed");
+ remoteConnection.flushBlocking();
return;
}
final boolean complete = saslServer.isComplete();
if (complete) {
+ RemoteConnectionHandler.log.trace("Sending SASL complete");
remoteConnection.sendAuthMessage(RemoteProtocol.AUTH_COMPLETE, challenge);
connectionProviderContext.accept(new ConnectionHandlerFactory() {
public ConnectionHandler createInstance(final ConnectionHandlerContext connectionContext) {
@@ -80,15 +83,17 @@
}
});
} else {
+ RemoteConnectionHandler.log.trace("Sending subsequent SASL challenge");
remoteConnection.sendAuthMessage(RemoteProtocol.AUTH_CHALLENGE, challenge);
}
} catch (IOException e) {
- // todo log it; close channel
+ RemoteConnectionHandler.log.trace(e, "Failed to send auth message");
IoUtils.safeClose(remoteConnection);
}
+ break;
}
default: {
- // todo log invalid msg
+ RemoteConnectionHandler.log.warn("Server received invalid message on %s", remoteConnection);
IoUtils.safeClose(remoteConnection);
}
}
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerGreetingHandler.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerGreetingHandler.java 2010-02-28 15:51:38 UTC (rev 5771)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerGreetingHandler.java 2010-02-28 21:14:06 UTC (rev 5772)
@@ -30,16 +30,16 @@
import org.jboss.remoting3.spi.ConnectionProviderContext;
import org.jboss.xnio.Buffers;
import org.jboss.xnio.IoUtils;
-import org.jboss.xnio.channels.MessageHandler;
-final class ServerGreetingHandler implements MessageHandler {
+final class ServerGreetingHandler extends AbstractMessageHandler {
private final RemoteConnection connection;
private final ConnectionProviderContext connectionProviderContext;
private final Set<String> saslMechs;
private final ServerAuthenticationProvider provider;
private final Map<String, Object> propertyMap;
- public ServerGreetingHandler(final RemoteConnection connection, final ConnectionProviderContext connectionProviderContext, final Set<String> saslMechs, final ServerAuthenticationProvider provider, final Map<String, Object> propertyMap) {
+ ServerGreetingHandler(final RemoteConnection connection, final ConnectionProviderContext connectionProviderContext, final Set<String> saslMechs, final ServerAuthenticationProvider provider, final Map<String, Object> propertyMap) {
+ super(connection);
this.connection = connection;
this.connectionProviderContext = connectionProviderContext;
this.saslMechs = saslMechs;
@@ -71,18 +71,9 @@
return;
}
default: {
- // todo log invalid greeting
+ RemoteConnectionHandler.log.warn("Server received invalid greeting message");
IoUtils.safeClose(connection);
}
}
}
-
- public void handleEof() {
- IoUtils.safeClose(connection);
- }
-
- public void handleException(final IOException e) {
- // todo log it
- IoUtils.safeClose(connection);
- }
}
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerInitialAuthenticationHandler.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerInitialAuthenticationHandler.java 2010-02-28 15:51:38 UTC (rev 5771)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerInitialAuthenticationHandler.java 2010-02-28 21:14:06 UTC (rev 5772)
@@ -26,26 +26,27 @@
import java.nio.ByteBuffer;
import java.util.Map;
import java.util.Set;
+import org.jboss.remoting3.RemotingOptions;
import org.jboss.remoting3.security.ServerAuthenticationProvider;
import org.jboss.remoting3.spi.ConnectionProviderContext;
import org.jboss.xnio.Buffers;
import org.jboss.xnio.IoUtils;
-import org.jboss.xnio.channels.MessageHandler;
import javax.security.sasl.Sasl;
import javax.security.sasl.SaslServer;
-final class ServerInitialAuthenticationHandler implements MessageHandler {
+final class ServerInitialAuthenticationHandler extends AbstractMessageHandler {
private final RemoteConnection remoteConnection;
private final Map<String, ?> saslPropertyMap;
- private final Set<String> allowedMechsMap;
+ private final Set<String> allowedMechs;
private final ServerAuthenticationProvider authenticationProvider;
private final ConnectionProviderContext connectionProviderContext;
- ServerInitialAuthenticationHandler(final RemoteConnection remoteConnection, final Map<String, ?> saslPropertyMap, final Set<String> allowedMechsMap, final ServerAuthenticationProvider authenticationProvider, final ConnectionProviderContext connectionProviderContext) {
+ ServerInitialAuthenticationHandler(final RemoteConnection remoteConnection, final Map<String, ?> saslPropertyMap, final Set<String> allowedMechs, final ServerAuthenticationProvider authenticationProvider, final ConnectionProviderContext connectionProviderContext) {
+ super(remoteConnection);
this.remoteConnection = remoteConnection;
this.saslPropertyMap = saslPropertyMap;
- this.allowedMechsMap = allowedMechsMap;
+ this.allowedMechs = allowedMechs;
this.authenticationProvider = authenticationProvider;
this.connectionProviderContext = connectionProviderContext;
}
@@ -56,37 +57,29 @@
try {
// mech name
final String name = Buffers.getModifiedUtf8(buffer);
- if (allowedMechsMap.contains(name)) {
- final SaslServer server = Sasl.createSaslServer(name, "remote", remoteConnection.getChannel().getLocalAddress().getHostName(), saslPropertyMap, authenticationProvider.getCallbackHandler());
+ if (allowedMechs.contains(name)) {
+ RemoteConnectionHandler.log.trace("Selected SASL mechanism %s", name);
+ final String realm = connectionProviderContext.getEndpointName();
+ final SaslServer server = Sasl.createSaslServer(name, "remote", realm, saslPropertyMap, authenticationProvider.getCallbackHandler());
remoteConnection.setMessageHandler(new ServerAuthenticationHandler(remoteConnection, server, connectionProviderContext));
- remoteConnection.sendAuthMessage(RemoteProtocol.AUTH_CHALLENGE, SaslUtils.EMPTY);
+ RemoteConnectionHandler.log.trace("Sending initial challenge");
+ remoteConnection.sendAuthMessage(RemoteProtocol.AUTH_CHALLENGE, server.evaluateResponse(SaslUtils.EMPTY));
return;
} else {
+ RemoteConnectionHandler.log.trace("Rejected invalid SASL mechanism %s", name);
remoteConnection.sendAuthReject("Invalid mechanism name");
return;
}
} catch (IOException e) {
IoUtils.safeClose(remoteConnection);
- // todo log it
+ RemoteConnectionHandler.log.trace("Failed to send auth message: %s", e);
return;
}
}
default: {
- // todo log invalid msg
+ RemoteConnectionHandler.log.warn("Server received invalid auth request message");
IoUtils.safeClose(remoteConnection);
}
}
}
-
- public void handleEof() {
- try {
- remoteConnection.shutdownReads();
- } catch (IOException e) {
- // todo log it
- }
- }
-
- public void handleException(final IOException e) {
- // todo log it
- }
}
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerOpenListener.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerOpenListener.java 2010-02-28 15:51:38 UTC (rev 5771)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerOpenListener.java 2010-02-28 21:14:06 UTC (rev 5772)
@@ -63,7 +63,7 @@
final RemoteConnection connection = new RemoteConnection(connectionProviderContext.getExecutor(), channel, optionMap);
// Calculate available server mechanisms
- final Sequence<String> mechs = optionMap.get(RemotingOptions.SASL_SERVER_MECHANISMS);
+ final Sequence<String> mechs = optionMap.get(Options.SASL_MECHANISMS);
final Set<String> includes = mechs != null ? new HashSet<String>(mechs) : null;
final Set<String> serverMechanisms = new LinkedHashSet<String>();
final Map<String, Object> propertyMap = SaslUtils.createPropertyMap(optionMap);
@@ -79,24 +79,42 @@
// Send server greeting packet...
final ByteBuffer buffer = connection.allocate();
- try {
- // length placeholder
- buffer.putInt(0);
- // version ID
- GreetingUtils.writeByte(buffer, RemoteProtocol.GREETING_VERSION, RemoteProtocol.VERSION);
- // SASL server mechs
- for (String name : serverMechanisms) {
- GreetingUtils.writeString(buffer, RemoteProtocol.GREETING_SASL_MECH, name);
+ // length placeholder
+ buffer.putInt(0);
+ // version ID
+ GreetingUtils.writeByte(buffer, RemoteProtocol.GREETING_VERSION, RemoteProtocol.VERSION);
+ // SASL server mechs
+ for (String name : serverMechanisms) {
+ GreetingUtils.writeString(buffer, RemoteProtocol.GREETING_SASL_MECH, name);
+ RemoteConnectionHandler.log.trace("Offering SASL mechanism %s", name);
+ }
+ GreetingUtils.writeString(buffer, RemoteProtocol.GREETING_ENDPOINT_NAME, connectionProviderContext.getEndpointName());
+ // that's it!
+ buffer.flip();
+ buffer.putInt(0, buffer.remaining() - 4);
+ channel.getWriteSetter().set(new ChannelListener<ConnectedStreamChannel<InetSocketAddress>>() {
+ public void handleEvent(final ConnectedStreamChannel<InetSocketAddress> channel) {
+ for (;;) {
+ while (buffer.hasRemaining()) {
+ final int res;
+ try {
+ res = channel.write(buffer);
+ } catch (IOException e1) {
+ IoUtils.safeClose(connection);
+ connection.free(buffer);
+ return;
+ }
+ if (res == 0) {
+ channel.resumeWrites();
+ return;
+ }
+ }
+ connection.free(buffer);
+ channel.resumeReads();
+ return;
+ }
}
- // that's it!
- buffer.flip();
- connection.sendBlocking(buffer);
- } catch (Exception e1) {
- // todo log it
- IoUtils.safeClose(connection);
- } finally {
- connection.free(buffer);
- }
+ });
final String authProvider = optionMap.get(RemotingOptions.AUTHENTICATION_PROVIDER);
if (authProvider == null) {
// todo log no valid auth provider
@@ -108,7 +126,7 @@
IoUtils.safeClose(connection);
}
connection.setMessageHandler(new ServerGreetingHandler(connection, connectionProviderContext, serverMechanisms, provider, propertyMap));
- // start up the read cycle
- channel.resumeReads();
+ // and send the greeting
+ channel.resumeWrites();
}
}
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/security/SimpleClientCallbackHandler.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/security/SimpleClientCallbackHandler.java 2010-02-28 15:51:38 UTC (rev 5771)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/security/SimpleClientCallbackHandler.java 2010-02-28 21:14:06 UTC (rev 5772)
@@ -71,12 +71,28 @@
*/
public void handle(final Callback[] callbacks) throws UnsupportedCallbackException {
MAIN: for (Callback callback : callbacks) {
- if (callback instanceof NameCallback && actualUserName != null) {
+ if (callback instanceof NameCallback) {
final NameCallback nameCallback = (NameCallback) callback;
- nameCallback.setName(actualUserName);
- } else if (callback instanceof RealmCallback && actualUserRealm != null) {
+ final String defaultName = nameCallback.getDefaultName();
+ log.trace("User name requested; prompt '%s', default is '%s', ours is '%s'", nameCallback.getPrompt(), defaultName, actualUserName);
+ if (actualUserName == null) {
+ if (defaultName != null) {
+ nameCallback.setName(defaultName);
+ }
+ } else {
+ nameCallback.setName(actualUserName);
+ }
+ } else if (callback instanceof RealmCallback) {
final RealmCallback realmCallback = (RealmCallback) callback;
- realmCallback.setText(actualUserRealm);
+ final String defaultRealm = realmCallback.getDefaultText();
+ log.trace("Realm requested; prompt '%s', default is '%s', ours is '%s'", realmCallback.getPrompt(), defaultRealm, actualUserRealm);
+ if (actualUserRealm == null) {
+ if (defaultRealm != null) {
+ realmCallback.setText(defaultRealm);
+ }
+ } else {
+ realmCallback.setText(actualUserRealm);
+ }
} else if (callback instanceof RealmChoiceCallback && actualUserRealm != null) {
final RealmChoiceCallback realmChoiceCallback = (RealmChoiceCallback) callback;
final String[] choices = realmChoiceCallback.getChoices();
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/AbstractHandleableCloseable.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/AbstractHandleableCloseable.java 2010-02-28 15:51:38 UTC (rev 5771)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/AbstractHandleableCloseable.java 2010-02-28 21:14:06 UTC (rev 5772)
@@ -23,6 +23,7 @@
package org.jboss.remoting3.spi;
import java.io.IOException;
+import java.io.InterruptedIOException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.IdentityHashMap;
@@ -49,9 +50,16 @@
private final StackTraceElement[] backtrace;
private final Object closeLock = new Object();
- private boolean closed;
+ private Thread closingThread;
+ private State state = State.OPEN;
private Map<Key, CloseHandler<? super T>> closeHandlers = null;
+ enum State {
+ OPEN,
+ CLOSING,
+ CLOSED,
+ }
+
static {
boolean b = false;
try {
@@ -87,7 +95,7 @@
*/
protected boolean isOpen() {
synchronized (closeLock) {
- return ! closed;
+ return state == State.OPEN;
}
}
@@ -104,12 +112,32 @@
public void close() throws IOException {
final Map<Key, CloseHandler<? super T>> closeHandlers;
synchronized (closeLock) {
- if (closed) {
- return;
+ switch (state) {
+ case OPEN: {
+ state = State.CLOSING;
+ closingThread = Thread.currentThread();
+ closeHandlers = this.closeHandlers;
+ this.closeHandlers = null;
+ break;
+ }
+ case CLOSING: {
+ if (Thread.currentThread() != closingThread) {
+ while (state != State.CLOSED) {
+ try {
+ closeLock.wait();
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ throw new InterruptedIOException("Close interrupted");
+ }
+ }
+ } else {
+ // reentrant close always goes through unblocked
+ }
+ return;
+ }
+ case CLOSED: return;
+ default: throw new IllegalStateException();
}
- closed = true;
- closeHandlers = this.closeHandlers;
- this.closeHandlers = null;
}
if (closeHandlers != null) {
log.trace("Closed %s", this);
@@ -119,7 +147,15 @@
}
}
}
- closeAction();
+ try {
+ closeAction();
+ } finally {
+ synchronized (closeLock) {
+ state = State.CLOSED;
+ closingThread = null;
+ closeLock.notifyAll();
+ }
+ }
}
/**
@@ -130,7 +166,7 @@
throw new NullPointerException("handler is null");
}
synchronized (closeLock) {
- if (! closed) {
+ if (state == State.OPEN) {
final Key key = new KeyImpl<T>(this);
final Map<Key, CloseHandler<? super T>> closeHandlers = this.closeHandlers;
if (closeHandlers == null) {
@@ -215,7 +251,7 @@
*/
protected void checkOpen() throws NotOpenException {
synchronized (closeLock) {
- if (closed) {
+ if (state != State.OPEN) {
throw new NotOpenException(toString() + " is not open");
}
}
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/ConnectionProviderContext.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/ConnectionProviderContext.java 2010-02-28 15:51:38 UTC (rev 5771)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/ConnectionProviderContext.java 2010-02-28 21:14:06 UTC (rev 5772)
@@ -66,4 +66,11 @@
* @return the provider, or {@code null} if none was matched
*/
<T> T getProtocolServiceProvider(ProtocolServiceType<T> serviceType, String name);
+
+ /**
+ * Get the endpoint's name.
+ *
+ * @return the endpoint name
+ */
+ String getEndpointName();
}
Modified: remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemoteTestCase.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemoteTestCase.java 2010-02-28 15:51:38 UTC (rev 5771)
+++ remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemoteTestCase.java 2010-02-28 21:14:06 UTC (rev 5772)
@@ -37,6 +37,7 @@
import org.jboss.xnio.IoFuture;
import org.jboss.xnio.IoUtils;
import org.jboss.xnio.OptionMap;
+import org.jboss.xnio.Options;
import org.jboss.xnio.Xnio;
import org.jboss.xnio.channels.BoundChannel;
import org.jboss.xnio.channels.ConnectedStreamChannel;
@@ -49,6 +50,7 @@
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.sasl.AuthenticationException;
+import javax.security.sasl.AuthorizeCallback;
import javax.security.sasl.RealmCallback;
@Test(suiteName = "Remote tests")
@@ -66,7 +68,11 @@
for (Callback callback : callbacks) {
if (callback instanceof NameCallback) {
final NameCallback nameCallback = (NameCallback) callback;
- if (! nameCallback.getName().equals("user")) {
+ final String defaultName = nameCallback.getDefaultName();
+ if (defaultName != null) {
+ nameCallback.setName(defaultName);
+ }
+ if (!"user".equals(nameCallback.getName())) {
throw new AuthenticationException("Invalid user name");
}
} else if (callback instanceof PasswordCallback) {
@@ -74,8 +80,11 @@
passwordCallback.setPassword("password".toCharArray());
} else if (callback instanceof RealmCallback) {
// allow
+ } else if (callback instanceof AuthorizeCallback) {
+ final AuthorizeCallback authorizeCallback = (AuthorizeCallback) callback;
+ authorizeCallback.setAuthorized(authorizeCallback.getAuthenticationID().equals(authorizeCallback.getAuthorizationID()));
} else {
- throw new UnsupportedCallbackException(callback);
+ throw new UnsupportedCallbackException(callback, "Callback not supported: " + callback);
}
}
}
@@ -89,14 +98,14 @@
protected Connection getConnection() throws IOException {
final NetworkServerProvider provider = endpoint.getConnectionProviderInterface("remote", NetworkServerProvider.class);
- final ChannelListener<ConnectedStreamChannel<InetSocketAddress>> listener = provider.getServerListener(OptionMap.builder().set(RemotingOptions.AUTHENTICATION_PROVIDER, "test").getMap());
+ final ChannelListener<ConnectedStreamChannel<InetSocketAddress>> listener = provider.getServerListener(OptionMap.builder().set(RemotingOptions.AUTHENTICATION_PROVIDER, "test").setSequence(Options.SASL_MECHANISMS, "DIGEST-MD5").getMap());
final Xnio xnio = Xnio.getInstance();
try {
- final AcceptingServer<InetSocketAddress, ?, ?> server = xnio.createSslTcpServer(listener, OptionMap.EMPTY);
-// final AcceptingServer<InetSocketAddress, ?, ?> server = xnio.createTcpServer(listener, OptionMap.EMPTY);
+// final AcceptingServer<InetSocketAddress, ?, ?> server = xnio.createSslTcpServer(listener, OptionMap.EMPTY);
+ final AcceptingServer<InetSocketAddress, ?, ?> server = xnio.createTcpServer(listener, OptionMap.EMPTY);
final IoFuture<? extends BoundChannel<InetSocketAddress>> future = server.bind(new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 0));
final InetSocketAddress localAddress = future.get().getLocalAddress();
- final Connection connection = endpoint.connect(new URI("remote", null, localAddress.getAddress().getHostAddress(), localAddress.getPort(), null, null, null), OptionMap.EMPTY, "user", "realm", "password".toCharArray()).get();
+ final Connection connection = endpoint.connect(new URI("remote", null, localAddress.getAddress().getHostAddress(), localAddress.getPort(), null, null, null), OptionMap.EMPTY, "user", null, "password".toCharArray()).get();
connection.addCloseHandler(new CloseHandler<Connection>() {
public void handleClose(final Connection closed) {
IoUtils.safeClose(server);
Modified: remoting3/trunk/jboss-remoting/src/test/resources/remoting.properties
===================================================================
--- remoting3/trunk/jboss-remoting/src/test/resources/remoting.properties 2010-02-28 15:51:38 UTC (rev 5771)
+++ remoting3/trunk/jboss-remoting/src/test/resources/remoting.properties 2010-02-28 21:14:06 UTC (rev 5772)
@@ -20,4 +20,4 @@
# 02110-1301 USA, or see the FSF site: http://www.fsf.org.
#
-#remote.ssl.enable=false
+remote.ssl.enable=false
14 years, 9 months
JBoss Remoting SVN: r5771 - in remoting3/trunk/jboss-remoting/src: main/java/org/jboss/remoting3/remote and 3 other directories.
by jboss-remoting-commits@lists.jboss.org
Author: david.lloyd(a)jboss.com
Date: 2010-02-28 10:51:38 -0500 (Sun, 28 Feb 2010)
New Revision: 5771
Added:
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ClientAuthenticationHandler.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ClientGreetingHandler.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ClientOpenListener.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/GreetingUtils.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteConnection.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/SaslClientContext.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/SaslContext.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/SaslServerContext.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/SaslUnwrappingMessageHandler.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/SaslUtils.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerAuthenticationHandler.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerGreetingHandler.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerInitialAuthenticationHandler.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerOpenListener.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/security/ServerAuthenticationProvider.java
Removed:
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteOpenListener.java
Modified:
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/CloseHandler.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/EndpointImpl.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/RemotingOptions.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/InboundReplyInputHandler.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/InboundRequestInputHandler.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundReplyBufferWriter.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundReplyHandler.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundRequestBufferWriter.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundRequestHandler.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteConnectionHandler.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteConnectionProvider.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteMessageHandler.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteProtocol.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/ProtocolServiceType.java
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/InvocationTestBase.java
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/LocalTestCase.java
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemoteTestCase.java
Log:
Add authentication layer
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/CloseHandler.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/CloseHandler.java 2010-02-28 00:58:32 UTC (rev 5770)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/CloseHandler.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -22,6 +22,8 @@
package org.jboss.remoting3;
+import java.util.EventListener;
+
/**
* A handler which is notified of a resource close.
*
@@ -30,7 +32,7 @@
* @apiviz.exclude
* @remoting.implement
*/
-public interface CloseHandler<T> {
+public interface CloseHandler<T> extends EventListener {
/**
* Receive a notification that the resource was closed.
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/EndpointImpl.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/EndpointImpl.java 2010-02-28 00:58:32 UTC (rev 5770)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/EndpointImpl.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -591,7 +591,8 @@
return expectedType.cast(provider.getProviderInterface());
}
- private <T> Registration addMarshallingRegistration(final String name, final T target, final ConcurrentMap<String, T> map, final String descr) throws DuplicateRegistrationException {
+ public <T> Registration addProtocolService(final ProtocolServiceType<T> type, final String name, final T provider) throws DuplicateRegistrationException {
+ final ConcurrentMap<String, T> map = getMapFor(type);
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(ADD_MARSHALLING_PROTOCOL_PERM);
@@ -599,16 +600,12 @@
if ("default".equals(name)) {
throw new IllegalArgumentException("'default' is not an allowed name");
}
- if (map.putIfAbsent(name, target) != null) {
- throw new DuplicateRegistrationException(descr + " '" + name + "' is already registered");
+ if (map.putIfAbsent(name, provider) != null) {
+ throw new DuplicateRegistrationException(type.getDescription() + " '" + name + "' is already registered");
}
- return new MapRegistration<T>(map, name, target);
+ return new MapRegistration<T>(map, name, provider);
}
- public <T> Registration addProtocolService(final ProtocolServiceType<T> type, final String name, final T provider) throws DuplicateRegistrationException {
- return addMarshallingRegistration(name, provider, getMapFor(type), type.getDescription());
- }
-
public String toString() {
return "endpoint \"" + name + "\" <" + Integer.toHexString(hashCode()) + ">";
}
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/RemotingOptions.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/RemotingOptions.java 2010-02-28 00:58:32 UTC (rev 5770)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/RemotingOptions.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -138,4 +138,14 @@
* Specify whether a local connection or client should call by reference (the usual default) or value.
*/
public static final Option<Boolean> CALL_BY_VALUE = Option.simple(RemotingOptions.class, "CALL_BY_VALUE", Boolean.class);
+
+ /**
+ * Specify the name of a preregistered server authentication provider to use.
+ */
+ public static final Option<String> AUTHENTICATION_PROVIDER = Option.simple(RemotingOptions.class, "AUTHENTICATION_PROVIDER", String.class);
+
+ /**
+ * Specify a set of SASL server mechanisms to allow. If not specified, no mechanisms will be excluded.
+ */
+ public static final Option<Sequence<String>> SASL_SERVER_MECHANISMS = Option.sequence(RemotingOptions.class, "SASL_SERVER_MECHANISMS", String.class);
}
Added: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ClientAuthenticationHandler.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ClientAuthenticationHandler.java (rev 0)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ClientAuthenticationHandler.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -0,0 +1,108 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.remoting3.remote;
+
+import java.io.EOFException;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import org.jboss.marshalling.MarshallerFactory;
+import org.jboss.marshalling.Marshalling;
+import org.jboss.marshalling.MarshallingConfiguration;
+import org.jboss.remoting3.CloseHandler;
+import org.jboss.remoting3.spi.ConnectionHandler;
+import org.jboss.remoting3.spi.ConnectionHandlerContext;
+import org.jboss.remoting3.spi.ConnectionHandlerFactory;
+import org.jboss.xnio.Buffers;
+import org.jboss.xnio.IoUtils;
+import org.jboss.xnio.Result;
+import org.jboss.xnio.channels.MessageHandler;
+
+import javax.security.sasl.SaslClient;
+import javax.security.sasl.SaslException;
+
+final class ClientAuthenticationHandler implements MessageHandler {
+
+ private final RemoteConnection remoteConnection;
+ private final SaslClient saslClient;
+ private final Result<ConnectionHandlerFactory> factoryResult;
+
+ public ClientAuthenticationHandler(final RemoteConnection remoteConnection, final SaslClient saslClient, final Result<ConnectionHandlerFactory> factoryResult) {
+ this.remoteConnection = remoteConnection;
+ this.saslClient = saslClient;
+ this.factoryResult = factoryResult;
+ }
+
+ public void handleMessage(final ByteBuffer buffer) {
+ final byte msgType = buffer.get();
+ switch (msgType) {
+ case RemoteProtocol.AUTH_CHALLENGE:
+ case RemoteProtocol.AUTH_COMPLETE: {
+ final byte[] response;
+ try {
+ response = saslClient.evaluateChallenge(Buffers.take(buffer, buffer.remaining()));
+ } catch (SaslException e) {
+ // todo log it
+ factoryResult.setException(e);
+ IoUtils.safeClose(remoteConnection);
+ return;
+ }
+ if (msgType == RemoteProtocol.AUTH_COMPLETE) {
+ if ((response != null || response.length > 0)) {
+ // todo log extraneous message
+ IoUtils.safeClose(remoteConnection);
+ return;
+ }
+ // auth complete.
+ factoryResult.setResult(new ConnectionHandlerFactory() {
+ public ConnectionHandler createInstance(final ConnectionHandlerContext connectionContext) {
+ // this happens immediately.
+ final MarshallerFactory marshallerFactory = Marshalling.getMarshallerFactory("river");
+ final MarshallingConfiguration marshallingConfiguration = new MarshallingConfiguration();
+ final RemoteConnectionHandler connectionHandler = new RemoteConnectionHandler(connectionContext, remoteConnection, marshallerFactory, marshallingConfiguration);
+ remoteConnection.addCloseHandler(new CloseHandler<Object>() {
+ public void handleClose(final Object closed) {
+ IoUtils.safeClose(connectionHandler);
+ }
+ });
+ remoteConnection.setMessageHandler(new RemoteMessageHandler(connectionHandler, remoteConnection));
+ return connectionHandler;
+ }
+ });
+ return;
+ }
+ break;
+ }
+ }
+ }
+
+ public void handleEof() {
+ factoryResult.setException(new EOFException("End of file on input"));
+ IoUtils.safeClose(remoteConnection);
+ }
+
+ public void handleException(final IOException e) {
+ // todo log it
+ factoryResult.setException(e);
+ IoUtils.safeClose(remoteConnection);
+ }
+}
Added: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ClientGreetingHandler.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ClientGreetingHandler.java (rev 0)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ClientGreetingHandler.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -0,0 +1,117 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.remoting3.remote;
+
+import java.io.EOFException;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.jboss.remoting3.RemotingOptions;
+import org.jboss.remoting3.spi.ConnectionHandlerFactory;
+import org.jboss.xnio.Buffers;
+import org.jboss.xnio.IoUtils;
+import org.jboss.xnio.Option;
+import org.jboss.xnio.OptionMap;
+import org.jboss.xnio.Result;
+import org.jboss.xnio.channels.MessageHandler;
+
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.sasl.Sasl;
+import javax.security.sasl.SaslClient;
+import javax.security.sasl.SaslException;
+
+final class ClientGreetingHandler implements MessageHandler {
+ private final RemoteConnection connection;
+ private final Result<ConnectionHandlerFactory> factoryResult;
+ private final CallbackHandler callbackHandler;
+
+ public ClientGreetingHandler(final RemoteConnection connection, final Result<ConnectionHandlerFactory> factoryResult, final CallbackHandler callbackHandler) {
+ this.connection = connection;
+ this.factoryResult = factoryResult;
+ this.callbackHandler = callbackHandler;
+ }
+
+ public void handleMessage(final ByteBuffer buffer) {
+ List<String> saslMechs = new ArrayList<String>();
+ switch (buffer.get()) {
+ case RemoteProtocol.GREETING: {
+ while (buffer.hasRemaining()) {
+ final byte type = buffer.get();
+ final int len = buffer.get() & 0xff;
+ switch (type) {
+ case RemoteProtocol.GREETING_VERSION: {
+ // We only support version zero, so knowing the other side's version is not useful presently
+ buffer.get();
+ if (len > 1) Buffers.skip(buffer, len - 1);
+ break;
+ }
+ case RemoteProtocol.GREETING_SASL_MECH: {
+ saslMechs.add(Buffers.getModifiedUtf8(Buffers.slice(buffer, len)));
+ break;
+ }
+ default: {
+ // unknown, skip it for forward compatibility.
+ Buffers.skip(buffer, len);
+ break;
+ }
+ }
+ }
+ // OK now send our authentication request
+ final OptionMap optionMap = connection.getOptionMap();
+ final String userName = optionMap.get(RemotingOptions.AUTH_USER_NAME);
+ final String hostName = connection.getChannel().getPeerAddress().getHostName();
+ final Map<String, ?> propertyMap = SaslUtils.createPropertyMap(optionMap);
+ final SaslClient saslClient;
+ try {
+ saslClient = Sasl.createSaslClient(saslMechs.toArray(new String[saslMechs.size()]), userName == null ? "anonymous" : userName, "remote", hostName, propertyMap, callbackHandler);
+ } catch (SaslException e) {
+ factoryResult.setException(e);
+ // todo log exception @ error
+ // todo send "goodbye" & close
+ IoUtils.safeClose(connection);
+ return;
+ }
+ connection.setMessageHandler(new ClientAuthenticationHandler(connection, saslClient, factoryResult));
+ return;
+ }
+ default: {
+ // todo log invalid greeting
+ IoUtils.safeClose(connection);
+ return;
+ }
+ }
+ }
+
+ public void handleEof() {
+ factoryResult.setException(new EOFException("End of file on input"));
+ IoUtils.safeClose(connection);
+ }
+
+ public void handleException(final IOException e) {
+ // todo log it
+ factoryResult.setException(e);
+ IoUtils.safeClose(connection);
+ }
+}
\ No newline at end of file
Added: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ClientOpenListener.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ClientOpenListener.java (rev 0)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ClientOpenListener.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -0,0 +1,83 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.remoting3.remote;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.nio.ByteBuffer;
+import org.jboss.remoting3.spi.ConnectionHandlerFactory;
+import org.jboss.remoting3.spi.ConnectionProviderContext;
+import org.jboss.xnio.ChannelListener;
+import org.jboss.xnio.IoUtils;
+import org.jboss.xnio.OptionMap;
+import org.jboss.xnio.Options;
+import org.jboss.xnio.Result;
+import org.jboss.xnio.channels.ConnectedStreamChannel;
+
+import javax.security.auth.callback.CallbackHandler;
+
+final class ClientOpenListener implements ChannelListener<ConnectedStreamChannel<InetSocketAddress>> {
+
+ private final OptionMap optionMap;
+ private final ConnectionProviderContext connectionProviderContext;
+ private final Result<ConnectionHandlerFactory> factoryResult;
+ private final CallbackHandler callbackHandler;
+
+ public ClientOpenListener(final OptionMap optionMap, final ConnectionProviderContext connectionProviderContext, final Result<ConnectionHandlerFactory> factoryResult, final CallbackHandler callbackHandler) {
+ this.optionMap = optionMap;
+ this.connectionProviderContext = connectionProviderContext;
+ this.factoryResult = factoryResult;
+ this.callbackHandler = callbackHandler;
+ }
+
+ public void handleEvent(final ConnectedStreamChannel<InetSocketAddress> channel) {
+ try {
+ channel.setOption(Options.TCP_NODELAY, Boolean.TRUE);
+ } catch (IOException e) {
+ // ignore
+ }
+ final RemoteConnection connection = new RemoteConnection(connectionProviderContext.getExecutor(), channel, optionMap);
+
+ // Send client greeting packet...
+ final ByteBuffer buffer = connection.allocate();
+ try {
+ // length placeholder
+ buffer.putInt(0);
+ // version ID
+ GreetingUtils.writeByte(buffer, RemoteProtocol.GREETING_VERSION, RemoteProtocol.VERSION);
+ // that's it!
+ buffer.flip();
+ connection.sendBlocking(buffer);
+ } catch (IOException e1) {
+ // todo log it
+ factoryResult.setException(e1);
+ IoUtils.safeClose(connection);
+ } finally {
+ connection.free(buffer);
+ }
+
+ connection.setMessageHandler(new ClientGreetingHandler(connection, factoryResult, callbackHandler));
+ // start up the read cycle
+ channel.resumeReads();
+ }
+}
\ No newline at end of file
Added: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/GreetingUtils.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/GreetingUtils.java (rev 0)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/GreetingUtils.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -0,0 +1,53 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.remoting3.remote;
+
+import java.nio.ByteBuffer;
+import org.jboss.xnio.Buffers;
+
+final class GreetingUtils {
+
+ private GreetingUtils() {
+ }
+
+ static void writeString(ByteBuffer buffer, byte type, String data) {
+ buffer.put(type);
+ buffer.put((byte) 0); // length placeholder
+ int s = buffer.position();
+ Buffers.putModifiedUtf8(buffer, data);
+ final int len = buffer.position() - s;
+ if (len > 255) {
+ // truncate long name
+ buffer.position(s + 255);
+ buffer.put(s-1, (byte) 255);
+ } else {
+ buffer.put(s-1, (byte) len);
+ }
+ }
+
+ static void writeByte(ByteBuffer buffer, byte type, byte value) {
+ buffer.put(type);
+ buffer.put((byte) 1);
+ buffer.put(value);
+ }
+}
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/InboundReplyInputHandler.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/InboundReplyInputHandler.java 2010-02-28 00:58:32 UTC (rev 5770)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/InboundReplyInputHandler.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -43,8 +43,9 @@
buffer.put(RemoteProtocol.REQUEST_ACK_CHUNK);
buffer.putInt(rid);
buffer.flip();
- connectionHandler.sendBlocking(buffer);
- connectionHandler.flushBlocking();
+ final RemoteConnection connection = connectionHandler.getRemoteConnection();
+ connection.sendBlocking(buffer);
+ connection.flushBlocking();
} finally {
connectionHandler.getBufferPool().free(buffer);
}
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/InboundRequestInputHandler.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/InboundRequestInputHandler.java 2010-02-28 00:58:32 UTC (rev 5770)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/InboundRequestInputHandler.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -45,8 +45,9 @@
buffer.put(RemoteProtocol.REQUEST_ACK_CHUNK);
buffer.putInt(rid);
buffer.flip();
- connectionHandler.sendBlocking(buffer);
- connectionHandler.flushBlocking();
+ final RemoteConnection connection = connectionHandler.getRemoteConnection();
+ connection.sendBlocking(buffer);
+ connection.flushBlocking();
} catch (IOException e) {
RemoteConnectionHandler.log.trace(e, "Failed to acknowledge chunk for %s", this);
} finally {
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundReplyBufferWriter.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundReplyBufferWriter.java 2010-02-28 00:58:32 UTC (rev 5770)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundReplyBufferWriter.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -73,14 +73,14 @@
buffer.put(7, (byte) (buffer.get(3) | RemoteProtocol.MSG_FLAG_LAST));
}
RemoteConnectionHandler.log.trace("Sending buffer %s for %s", buffer, this);
- connectionHandler.sendBlocking(buffer);
+ connectionHandler.getRemoteConnection().sendBlocking(buffer);
} finally {
connectionHandler.getBufferPool().free(buffer);
}
}
public void flush() throws IOException {
- inboundRequest.getRemoteConnectionHandler().flushBlocking();
+ inboundRequest.getRemoteConnectionHandler().getRemoteConnection().flushBlocking();
}
public String toString() {
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundReplyHandler.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundReplyHandler.java 2010-02-28 00:58:32 UTC (rev 5770)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundReplyHandler.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -71,7 +71,7 @@
buffer.put(RemoteProtocol.REPLY_EXCEPTION_ABORT);
buffer.putInt(rid);
buffer.flip();
- connectionHandler.sendBlocking(buffer);
+ connectionHandler.getRemoteConnection().sendBlocking(buffer);
} finally {
bufferPool.free(buffer);
}
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundRequestBufferWriter.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundRequestBufferWriter.java 2010-02-28 00:58:32 UTC (rev 5770)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundRequestBufferWriter.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -73,13 +73,13 @@
}
}
RemoteConnectionHandler.log.trace("Sending buffer %s for %s", buffer, this);
- remoteConnectionHandler.sendBlocking(buffer);
+ remoteConnectionHandler.getRemoteConnection().sendBlocking(buffer);
} finally {
remoteConnectionHandler.getBufferPool().free(buffer);
}
}
public void flush() throws IOException {
- outboundRequest.getRemoteConnectionHandler().flushBlocking();
+ outboundRequest.getRemoteConnectionHandler().getRemoteConnection().flushBlocking();
}
}
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundRequestHandler.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundRequestHandler.java 2010-02-28 00:58:32 UTC (rev 5770)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundRequestHandler.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -79,7 +79,7 @@
buf.putInt(rid);
buf.flip();
try {
- connectionHandler.sendBlocking(buf);
+ connectionHandler.getRemoteConnection().sendBlocking(buf);
} catch (IOException e1) {
// todo log it
}
@@ -100,7 +100,7 @@
buf.put(RemoteProtocol.CLIENT_CLOSED);
buf.putInt(outboundClient.getId());
buf.flip();
- connectionHandler.sendBlocking(buf);
+ connectionHandler.getRemoteConnection().sendBlocking(buf);
} finally {
bufferPool.free(buf);
}
Added: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteConnection.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteConnection.java (rev 0)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteConnection.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -0,0 +1,178 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.remoting3.remote;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.InterruptedIOException;
+import java.net.InetSocketAddress;
+import java.nio.ByteBuffer;
+import java.util.concurrent.Executor;
+import org.jboss.remoting3.spi.AbstractHandleableCloseable;
+import org.jboss.xnio.Buffers;
+import org.jboss.xnio.IoUtils;
+import org.jboss.xnio.OptionMap;
+import org.jboss.xnio.Pool;
+import org.jboss.xnio.channels.Channels;
+import org.jboss.xnio.channels.ConnectedChannel;
+import org.jboss.xnio.channels.ConnectedStreamChannel;
+import org.jboss.xnio.channels.MessageHandler;
+
+import javax.security.auth.callback.CallbackHandler;
+
+final class RemoteConnection extends AbstractHandleableCloseable<RemoteConnection> implements Closeable {
+ private final ConnectedStreamChannel<InetSocketAddress> channel;
+ private final Pool<ByteBuffer> bufferPool = Buffers.createHeapByteBufferAllocator(4096);
+ private final MessageHandler.Setter messageHandlerSetter;
+ private final OptionMap optionMap;
+
+ RemoteConnection(final Executor executor, final ConnectedStreamChannel<InetSocketAddress> channel, final OptionMap optionMap) {
+ super(executor);
+ this.channel = channel;
+ messageHandlerSetter = Channels.createMessageReader(channel, optionMap);
+ this.optionMap = optionMap;
+ }
+
+ public void close() throws IOException {
+ channel.close();
+ }
+
+ OptionMap getOptionMap() {
+ return optionMap;
+ }
+
+ ConnectedChannel<InetSocketAddress> getChannel() {
+ return channel;
+ }
+
+ ByteBuffer allocate() {
+ return bufferPool.allocate();
+ }
+
+ void free(ByteBuffer buffer) {
+ bufferPool.free(buffer);
+ }
+
+ void setMessageHandler(MessageHandler handler) {
+ messageHandlerSetter.set(handler);
+ }
+
+ void sendBlocking(final ByteBuffer buffer) throws IOException {
+ try {
+ sendBlockingNoClose(buffer);
+ } catch (IOException e) {
+ IoUtils.safeClose(this);
+ throw e;
+ } catch (RuntimeException e) {
+ IoUtils.safeClose(this);
+ throw e;
+ } catch (Error e) {
+ IoUtils.safeClose(this);
+ throw e;
+ }
+ }
+
+ void sendBlockingNoClose(final ByteBuffer buffer) throws IOException {
+ buffer.putInt(0, buffer.remaining() - 4);
+ boolean intr = false;
+ try {
+ while (buffer.hasRemaining()) {
+ if (channel.write(buffer) == 0) {
+ try {
+ channel.awaitWritable();
+ } catch (InterruptedIOException e) {
+ intr = Thread.interrupted();
+ }
+ }
+ }
+ } finally {
+ if (intr) Thread.currentThread().interrupt();
+ }
+ }
+
+ void flushBlocking() throws IOException {
+ try {
+ while (! channel.flush()) {
+ channel.awaitWritable();
+ }
+ } catch (IOException e) {
+ IoUtils.safeClose(this);
+ throw e;
+ } catch (RuntimeException e) {
+ IoUtils.safeClose(this);
+ throw e;
+ } catch (Error e) {
+ IoUtils.safeClose(this);
+ throw e;
+ }
+ }
+
+ void shutdownWritesBlocking() throws IOException {
+ try {
+ while (! channel.shutdownWrites()) {
+ channel.awaitWritable();
+ }
+ } catch (IOException e) {
+ IoUtils.safeClose(this);
+ throw e;
+ } catch (RuntimeException e) {
+ IoUtils.safeClose(this);
+ throw e;
+ } catch (Error e) {
+ IoUtils.safeClose(this);
+ throw e;
+ }
+ }
+
+ void sendAuthReject(final String msg) throws IOException {
+ final ByteBuffer buf = allocate();
+ try {
+ buf.putInt(0);
+ buf.put(RemoteProtocol.AUTH_REJECTED);
+ Buffers.putModifiedUtf8(buf, msg);
+ buf.flip();
+ sendBlocking(buf);
+ flushBlocking();
+ } finally {
+ free(buf);
+ }
+ }
+
+ void sendAuthMessage(final byte msgType, final byte[] message) throws IOException {
+ final ByteBuffer buf = allocate();
+ try {
+ buf.putInt(0);
+ buf.put(msgType);
+ if (message != null) buf.put(message);
+ buf.flip();
+ sendBlocking(buf);
+ flushBlocking();
+ } finally {
+ free(buf);
+ }
+ }
+
+ void shutdownReads() throws IOException {
+ channel.shutdownReads();
+ }
+}
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteConnectionHandler.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteConnectionHandler.java 2010-02-28 00:58:32 UTC (rev 5770)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteConnectionHandler.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -23,7 +23,6 @@
package org.jboss.remoting3.remote;
import java.io.IOException;
-import java.io.InterruptedIOException;
import java.nio.ByteBuffer;
import java.util.Random;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -42,7 +41,7 @@
import org.jboss.xnio.IoUtils;
import org.jboss.xnio.Pool;
import org.jboss.xnio.Result;
-import org.jboss.xnio.channels.StreamChannel;
+import org.jboss.xnio.channels.Channels;
import org.jboss.xnio.log.Logger;
final class RemoteConnectionHandler implements ConnectionHandler {
@@ -56,7 +55,7 @@
private final MarshallingConfiguration marshallingConfiguration;
private final ConnectionHandlerContext connectionContext;
- private final StreamChannel channel;
+ private final RemoteConnection remoteConnection;
private final Random random = new Random();
private final IntKeyMap<OutboundClient> outboundClients = new IntKeyMap<OutboundClient>();
@@ -67,63 +66,13 @@
private final AtomicBoolean closed = new AtomicBoolean();
- public RemoteConnectionHandler(final ConnectionHandlerContext connectionContext, final StreamChannel channel, final MarshallerFactory marshallerFactory, final MarshallingConfiguration marshallingConfiguration) {
+ public RemoteConnectionHandler(final ConnectionHandlerContext connectionContext, final RemoteConnection remoteConnection, final MarshallerFactory marshallerFactory, final MarshallingConfiguration marshallingConfiguration) {
this.connectionContext = connectionContext;
- this.channel = channel;
+ this.remoteConnection = remoteConnection;
this.marshallerFactory = marshallerFactory;
this.marshallingConfiguration = marshallingConfiguration;
}
- void sendBlocking(final ByteBuffer buffer) throws IOException {
- try {
- sendBlockingNoClose(buffer);
- } catch (IOException e) {
- IoUtils.safeClose(this);
- throw e;
- } catch (RuntimeException e) {
- IoUtils.safeClose(this);
- throw e;
- } catch (Error e) {
- IoUtils.safeClose(this);
- throw e;
- }
- }
-
- void sendBlockingNoClose(final ByteBuffer buffer) throws IOException {
- buffer.putInt(0, buffer.remaining() - 4);
- boolean intr = false;
- try {
- while (buffer.hasRemaining()) {
- if (channel.write(buffer) == 0) {
- try {
- channel.awaitWritable();
- } catch (InterruptedIOException e) {
- intr = Thread.interrupted();
- }
- }
- }
- } finally {
- if (intr) Thread.currentThread().interrupt();
- }
- }
-
- void flushBlocking() throws IOException {
- try {
- while (! channel.flush()) {
- channel.awaitWritable();
- }
- } catch (IOException e) {
- IoUtils.safeClose(this);
- throw e;
- } catch (RuntimeException e) {
- IoUtils.safeClose(this);
- throw e;
- } catch (Error e) {
- IoUtils.safeClose(this);
- throw e;
- }
- }
-
public Cancellable open(final String serviceType, final String groupName, final Result<RequestHandler> result) {
final OutboundClient outboundClient;
int id;
@@ -143,7 +92,7 @@
Buffers.putModifiedUtf8(buffer, groupName);
buffer.put((byte) 0);
buffer.flip();
- sendBlocking(buffer);
+ remoteConnection.sendBlocking(buffer);
} catch (IOException e) {
result.setException(e);
} catch (Throwable e) {
@@ -161,7 +110,7 @@
public void close() throws IOException {
if (! closed.getAndSet(true)) {
try {
- channel.close();
+ remoteConnection.close();
} finally {
// other actions here
for (IntKeyMap.Entry<OutboundClient> entry : outboundClients) {
@@ -208,10 +157,6 @@
return connectionContext;
}
- StreamChannel getChannel() {
- return channel;
- }
-
Random getRandom() {
return random;
}
@@ -235,4 +180,8 @@
AtomicBoolean getClosed() {
return closed;
}
+
+ RemoteConnection getRemoteConnection() {
+ return remoteConnection;
+ }
}
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteConnectionProvider.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteConnectionProvider.java 2010-02-28 00:58:32 UTC (rev 5770)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteConnectionProvider.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -22,15 +22,17 @@
package org.jboss.remoting3.remote;
-import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.UnknownHostException;
+import org.jboss.remoting3.RemotingOptions;
+import org.jboss.remoting3.security.ServerAuthenticationProvider;
import org.jboss.remoting3.spi.ConnectionHandlerFactory;
import org.jboss.remoting3.spi.ConnectionProvider;
import org.jboss.remoting3.spi.ConnectionProviderContext;
import org.jboss.remoting3.spi.NetworkServerProvider;
+import org.jboss.remoting3.spi.ProtocolServiceType;
import org.jboss.xnio.Cancellable;
import org.jboss.xnio.ChannelListener;
import org.jboss.xnio.Connector;
@@ -68,7 +70,7 @@
// Open a client channel
final IoFuture<? extends ConnectedStreamChannel<InetSocketAddress>> futureChannel;
try {
- futureChannel = connector.connectTo(new InetSocketAddress(InetAddress.getByName(host), port), new RemoteOpenListener(false, connectOptions, connectionProviderContext, result), null);
+ futureChannel = connector.connectTo(new InetSocketAddress(InetAddress.getByName(host), port), new ClientOpenListener(connectOptions, connectionProviderContext, result, callbackHandler), null);
} catch (UnknownHostException e) {
result.setException(e);
return IoUtils.nullCancellable();
@@ -82,20 +84,12 @@
private class ProviderInterface implements NetworkServerProvider {
public ChannelListener<ConnectedStreamChannel<InetSocketAddress>> getServerListener(final OptionMap optionMap) {
- return new RemoteOpenListener(true, optionMap, connectionProviderContext, new Result<ConnectionHandlerFactory>() {
- public boolean setResult(final ConnectionHandlerFactory result) {
- connectionProviderContext.accept(result);
- return true;
- }
-
- public boolean setException(final IOException exception) {
- return true;
- }
-
- public boolean setCancelled() {
- return true;
- }
- });
+ final String providerName = optionMap.get(RemotingOptions.AUTHENTICATION_PROVIDER);
+ final ServerAuthenticationProvider authenticationProvider = connectionProviderContext.getProtocolServiceProvider(ProtocolServiceType.SERVER_AUTHENTICATION_PROVIDER, providerName == null ? "default" : providerName);
+ if (authenticationProvider == null) {
+ throw new IllegalArgumentException("Missing authentication provider: " + (providerName == null ? "default" : providerName));
+ }
+ return new ServerOpenListener(optionMap, connectionProviderContext);
}
}
}
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteMessageHandler.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteMessageHandler.java 2010-02-28 00:58:32 UTC (rev 5770)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteMessageHandler.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -40,10 +40,12 @@
final class RemoteMessageHandler implements org.jboss.xnio.channels.MessageHandler {
- private RemoteConnectionHandler remoteConnectionHandler;
+ private final RemoteConnection connection;
+ private final RemoteConnectionHandler remoteConnectionHandler;
- public RemoteMessageHandler(final RemoteConnectionHandler remoteConnectionHandler) {
+ RemoteMessageHandler(final RemoteConnectionHandler remoteConnectionHandler, final RemoteConnection connection) {
this.remoteConnectionHandler = remoteConnectionHandler;
+ this.connection = connection;
}
public void handleMessage(final ByteBuffer buffer) {
@@ -75,7 +77,7 @@
outBuf.putInt(id);
outBuf.flip();
try {
- connectionHandler.sendBlocking(outBuf);
+ connection.sendBlocking(outBuf);
} catch (IOException e) {
// the channel has suddenly failed
RemoteConnectionHandler.log.trace("Send failed: %s", e);
@@ -302,7 +304,7 @@
return;
}
default: {
- RemoteConnectionHandler.log.error("Received invalid packet type on %s, closing", connectionHandler.getChannel());
+ RemoteConnectionHandler.log.error("Received invalid packet type on %s, closing", connectionHandler);
IoUtils.safeClose(connectionHandler);
}
}
Deleted: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteOpenListener.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteOpenListener.java 2010-02-28 00:58:32 UTC (rev 5770)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteOpenListener.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -1,73 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2010, JBoss Inc., and individual contributors as indicated
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-
-package org.jboss.remoting3.remote;
-
-import java.io.IOException;
-import java.net.InetSocketAddress;
-import org.jboss.marshalling.MarshallerFactory;
-import org.jboss.marshalling.Marshalling;
-import org.jboss.marshalling.MarshallingConfiguration;
-import org.jboss.remoting3.spi.ConnectionHandler;
-import org.jboss.remoting3.spi.ConnectionHandlerContext;
-import org.jboss.remoting3.spi.ConnectionHandlerFactory;
-import org.jboss.remoting3.spi.ConnectionProviderContext;
-import org.jboss.xnio.ChannelListener;
-import org.jboss.xnio.OptionMap;
-import org.jboss.xnio.Options;
-import org.jboss.xnio.Result;
-import org.jboss.xnio.channels.Channels;
-import org.jboss.xnio.channels.ConnectedStreamChannel;
-
-final class RemoteOpenListener implements ChannelListener<ConnectedStreamChannel<InetSocketAddress>> {
-
- private final boolean server;
- private final OptionMap optionMap;
- private final ConnectionProviderContext connectionProviderContext;
- private final Result<ConnectionHandlerFactory> factoryResult;
-
- public RemoteOpenListener(final boolean server, final OptionMap optionMap, final ConnectionProviderContext connectionProviderContext, final Result<ConnectionHandlerFactory> factoryResult) {
- this.server = server;
- this.optionMap = optionMap;
- this.connectionProviderContext = connectionProviderContext;
- this.factoryResult = factoryResult;
- }
-
- public void handleEvent(final ConnectedStreamChannel<InetSocketAddress> channel) {
- try {
- channel.setOption(Options.TCP_NODELAY, Boolean.TRUE);
- } catch (IOException e) {
- // ignore
- }
- // TODO: For now, just build a pre-set-up connection without a negotiation phase
- factoryResult.setResult(new ConnectionHandlerFactory() {
- public ConnectionHandler createInstance(final ConnectionHandlerContext connectionContext) {
- final MarshallerFactory marshallerFactory = Marshalling.getMarshallerFactory("river");
- final MarshallingConfiguration marshallingConfiguration = new MarshallingConfiguration();
- final RemoteConnectionHandler connectionHandler = new RemoteConnectionHandler(connectionContext, channel, marshallerFactory, marshallingConfiguration);
- Channels.createMessageReader(channel, optionMap).set(new RemoteMessageHandler(connectionHandler));
- channel.resumeReads();
- return connectionHandler;
- }
- });
- }
-}
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteProtocol.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteProtocol.java 2010-02-28 00:58:32 UTC (rev 5770)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteProtocol.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -33,6 +33,11 @@
*/
public final class RemoteProtocol {
+ /**
+ * The highest-supported version of the remote protocol supported by this implementation.
+ */
+ public static final byte VERSION = 0;
+
static final int MSG_FLAG_FIRST = 1;
static final int MSG_FLAG_LAST = 2;
@@ -49,6 +54,19 @@
static final byte REPLY_ACK_CHUNK = 10;
static final byte REPLY_EXCEPTION_ABORT = 11;
+ static final byte AUTH_REQUEST = 12;
+ static final byte AUTH_CHALLENGE = 13;
+ static final byte AUTH_RESPONSE = 14;
+ static final byte AUTH_COMPLETE = 15;
+ static final byte AUTH_REJECTED = 16;
+
+ static final byte ALIVE = 99;
+
+ // Greeting types
+
+ static final byte GREETING_VERSION = 0; // sent by client & server
+ static final byte GREETING_SASL_MECH = 1; // sent by server
+
/**
* Create an instance of the connection provider for the "remote" protocol.
*
Added: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/SaslClientContext.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/SaslClientContext.java (rev 0)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/SaslClientContext.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -0,0 +1,60 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.remoting3.remote;
+
+import java.nio.ByteBuffer;
+
+import javax.security.sasl.SaslClient;
+import javax.security.sasl.SaslException;
+
+final class SaslClientContext implements SaslContext {
+ private final SaslClient saslClient;
+
+ SaslClientContext(final SaslClient saslClient) {
+ this.saslClient = saslClient;
+ }
+
+ public String getMechanismName() {
+ return saslClient.getMechanismName();
+ }
+
+ public Object getNegotiatedProperty(final String name) {
+ return saslClient.getNegotiatedProperty(name);
+ }
+
+ public ByteBuffer unwrap(final ByteBuffer src) throws SaslException {
+ final byte[] unwrapped;
+ if (src.hasArray()) {
+ final byte[] orig = src.array();
+ final int start = src.arrayOffset() + src.position();
+ final int len = src.remaining();
+ unwrapped = saslClient.unwrap(orig, start, len);
+ } else {
+ final int len = src.remaining();
+ final byte[] orig = new byte[len];
+ src.get(orig);
+ unwrapped = saslClient.unwrap(orig, 0, len);
+ }
+ return ByteBuffer.wrap(unwrapped);
+ }
+}
Added: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/SaslContext.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/SaslContext.java (rev 0)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/SaslContext.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -0,0 +1,35 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.remoting3.remote;
+
+import java.nio.ByteBuffer;
+
+import javax.security.sasl.SaslException;
+
+interface SaslContext {
+ String getMechanismName();
+
+ Object getNegotiatedProperty(String name);
+
+ ByteBuffer unwrap(ByteBuffer src) throws SaslException;
+}
Added: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/SaslServerContext.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/SaslServerContext.java (rev 0)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/SaslServerContext.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -0,0 +1,60 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.remoting3.remote;
+
+import java.nio.ByteBuffer;
+
+import javax.security.sasl.SaslException;
+import javax.security.sasl.SaslServer;
+
+final class SaslServerContext implements SaslContext {
+ private final SaslServer saslServer;
+
+ SaslServerContext(final SaslServer saslServer) {
+ this.saslServer = saslServer;
+ }
+
+ public String getMechanismName() {
+ return saslServer.getMechanismName();
+ }
+
+ public Object getNegotiatedProperty(final String name) {
+ return saslServer.getNegotiatedProperty(name);
+ }
+
+ public ByteBuffer unwrap(final ByteBuffer src) throws SaslException {
+ final byte[] unwrapped;
+ if (src.hasArray()) {
+ final byte[] orig = src.array();
+ final int start = src.arrayOffset() + src.position();
+ final int len = src.remaining();
+ unwrapped = saslServer.unwrap(orig, start, len);
+ } else {
+ final int len = src.remaining();
+ final byte[] orig = new byte[len];
+ src.get(orig);
+ unwrapped = saslServer.unwrap(orig, 0, len);
+ }
+ return ByteBuffer.wrap(unwrapped);
+ }
+}
Added: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/SaslUnwrappingMessageHandler.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/SaslUnwrappingMessageHandler.java (rev 0)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/SaslUnwrappingMessageHandler.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -0,0 +1,59 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.remoting3.remote;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import org.jboss.xnio.channels.MessageHandler;
+
+import javax.security.sasl.SaslException;
+
+final class SaslUnwrappingMessageHandler implements MessageHandler, MessageHandler.Setter {
+ private final SaslContext saslContext;
+ private volatile MessageHandler delegate;
+
+ SaslUnwrappingMessageHandler(final SaslContext saslContext, final MessageHandler delegate) {
+ this.saslContext = saslContext;
+ this.delegate = delegate;
+ }
+
+ public void handleMessage(final ByteBuffer buffer) {
+ try {
+ delegate.handleMessage(saslContext.unwrap(buffer));
+ } catch (SaslException e) {
+ delegate.handleException(e);
+ }
+ }
+
+ public void handleEof() {
+ delegate.handleEof();
+ }
+
+ public void handleException(final IOException e) {
+ delegate.handleException(e);
+ }
+
+ public void set(final MessageHandler messageHandler) {
+ delegate = messageHandler;
+ }
+}
Added: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/SaslUtils.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/SaslUtils.java (rev 0)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/SaslUtils.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -0,0 +1,108 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.remoting3.remote;
+
+import java.nio.ByteBuffer;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+import org.jboss.xnio.Buffers;
+import org.jboss.xnio.Option;
+import org.jboss.xnio.OptionMap;
+import org.jboss.xnio.Options;
+import org.jboss.xnio.SaslQop;
+import org.jboss.xnio.Sequence;
+
+import javax.security.sasl.Sasl;
+import javax.security.sasl.SaslClient;
+import javax.security.sasl.SaslException;
+
+final class SaslUtils {
+
+ private SaslUtils() {
+ }
+
+ static final byte[] EMPTY = new byte[0];
+
+ static Map<String, Object> createPropertyMap(OptionMap optionMap) {
+ final Map<String,Object> propertyMap = new HashMap<String, Object>();
+
+ add(optionMap, Options.SASL_POLICY_FORWARD_SECRECY, propertyMap, Sasl.POLICY_FORWARD_SECRECY);
+ add(optionMap, Options.SASL_POLICY_NOACTIVE, propertyMap, Sasl.POLICY_NOACTIVE);
+ add(optionMap, Options.SASL_POLICY_NOANONYMOUS, propertyMap, Sasl.POLICY_NOANONYMOUS);
+ add(optionMap, Options.SASL_POLICY_NODICTIONARY, propertyMap, Sasl.POLICY_NODICTIONARY);
+ add(optionMap, Options.SASL_POLICY_NOPLAINTEXT, propertyMap, Sasl.POLICY_NOPLAINTEXT);
+ add(optionMap, Options.SASL_POLICY_PASS_CREDENTIALS, propertyMap, Sasl.POLICY_PASS_CREDENTIALS);
+ add(optionMap, Options.SASL_REUSE, propertyMap, Sasl.REUSE);
+ add(optionMap, Options.SASL_SERVER_AUTH, propertyMap, Sasl.SERVER_AUTH);
+ addQopList(optionMap, Options.SASL_QOP, propertyMap, Sasl.QOP);
+ add(optionMap, Options.SASL_STRENGTH, propertyMap, Sasl.STRENGTH);
+ return propertyMap;
+ }
+
+ private static void add(OptionMap optionMap, Option<?> option, Map<String, Object> map, String propName) {
+ final Object value = optionMap.get(option);
+ if (value != null) map.put(propName, value.toString().toLowerCase());
+ }
+
+ private static void addQopList(OptionMap optionMap, Option<Sequence<SaslQop>> option, Map<String, Object> map, String propName) {
+ final Sequence<SaslQop> seq = optionMap.get(option);
+ final StringBuilder builder = new StringBuilder();
+ final Iterator<SaslQop> iterator = seq.iterator();
+ while (iterator.hasNext()) {
+ builder.append(iterator.next());
+ if (iterator.hasNext()) {
+ builder.append(',');
+ }
+ }
+ }
+
+ private static final Set<String> SECURE_QOP;
+
+ static {
+ final Set<String> set = new HashSet<String>();
+ set.add("auth-int");
+ set.add("auth-conf");
+ SECURE_QOP = set;
+ }
+
+ static boolean isSecureQop(Object qop) {
+ return SECURE_QOP.contains(qop);
+ }
+
+ static void wrapFramed(SaslClient saslClient, ByteBuffer message) throws SaslException {
+ final byte[] result;
+ if (message.hasArray()) {
+ result = saslClient.wrap(message.array(), message.arrayOffset() + 4, message.position());
+ } else {
+ final int end = message.position();
+ message.position(4);
+ final byte[] bytes = Buffers.take(message, end - 4);
+ result = saslClient.wrap(bytes, 0, bytes.length);
+ }
+ message.position(4);
+ message.put(result);
+ }
+}
Added: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerAuthenticationHandler.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerAuthenticationHandler.java (rev 0)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerAuthenticationHandler.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -0,0 +1,104 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.remoting3.remote;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import org.jboss.marshalling.MarshallerFactory;
+import org.jboss.marshalling.Marshalling;
+import org.jboss.marshalling.MarshallingConfiguration;
+import org.jboss.remoting3.CloseHandler;
+import org.jboss.remoting3.spi.ConnectionHandler;
+import org.jboss.remoting3.spi.ConnectionHandlerContext;
+import org.jboss.remoting3.spi.ConnectionHandlerFactory;
+import org.jboss.remoting3.spi.ConnectionProviderContext;
+import org.jboss.xnio.Buffers;
+import org.jboss.xnio.IoUtils;
+import org.jboss.xnio.channels.MessageHandler;
+
+import javax.security.sasl.SaslException;
+import javax.security.sasl.SaslServer;
+
+final class ServerAuthenticationHandler implements MessageHandler {
+ private final RemoteConnection remoteConnection;
+ private final SaslServer saslServer;
+ private final ConnectionProviderContext connectionProviderContext;
+
+ ServerAuthenticationHandler(final RemoteConnection remoteConnection, final SaslServer saslServer, final ConnectionProviderContext connectionProviderContext) {
+ this.saslServer = saslServer;
+ this.remoteConnection = remoteConnection;
+ this.connectionProviderContext = connectionProviderContext;
+ }
+
+ public void handleMessage(final ByteBuffer buffer) {
+ switch (buffer.get()) {
+ case RemoteProtocol.AUTH_RESPONSE: {
+ final byte[] challenge;
+ try {
+ try {
+ challenge = saslServer.evaluateResponse(Buffers.take(buffer, buffer.remaining()));
+ } catch (SaslException e) {
+ // todo log it
+ remoteConnection.sendAuthReject("Authentication failed");
+ return;
+ }
+ final boolean complete = saslServer.isComplete();
+ if (complete) {
+ remoteConnection.sendAuthMessage(RemoteProtocol.AUTH_COMPLETE, challenge);
+ connectionProviderContext.accept(new ConnectionHandlerFactory() {
+ public ConnectionHandler createInstance(final ConnectionHandlerContext connectionContext) {
+ final MarshallerFactory marshallerFactory = Marshalling.getMarshallerFactory("river");
+ final MarshallingConfiguration marshallingConfiguration = new MarshallingConfiguration();
+ final RemoteConnectionHandler connectionHandler = new RemoteConnectionHandler(connectionContext, remoteConnection, marshallerFactory, marshallingConfiguration);
+ remoteConnection.addCloseHandler(new CloseHandler<Object>() {
+ public void handleClose(final Object closed) {
+ IoUtils.safeClose(connectionHandler);
+ }
+ });
+ remoteConnection.setMessageHandler(new RemoteMessageHandler(connectionHandler, remoteConnection));
+ return connectionHandler;
+ }
+ });
+ } else {
+ remoteConnection.sendAuthMessage(RemoteProtocol.AUTH_CHALLENGE, challenge);
+ }
+ } catch (IOException e) {
+ // todo log it; close channel
+ IoUtils.safeClose(remoteConnection);
+ }
+ }
+ default: {
+ // todo log invalid msg
+ IoUtils.safeClose(remoteConnection);
+ }
+ }
+ }
+
+ public void handleEof() {
+ IoUtils.safeClose(remoteConnection);
+ }
+
+ public void handleException(final IOException e) {
+ IoUtils.safeClose(remoteConnection);
+ }
+}
Added: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerGreetingHandler.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerGreetingHandler.java (rev 0)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerGreetingHandler.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -0,0 +1,88 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.remoting3.remote;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Map;
+import java.util.Set;
+import org.jboss.remoting3.security.ServerAuthenticationProvider;
+import org.jboss.remoting3.spi.ConnectionProviderContext;
+import org.jboss.xnio.Buffers;
+import org.jboss.xnio.IoUtils;
+import org.jboss.xnio.channels.MessageHandler;
+
+final class ServerGreetingHandler implements MessageHandler {
+ private final RemoteConnection connection;
+ private final ConnectionProviderContext connectionProviderContext;
+ private final Set<String> saslMechs;
+ private final ServerAuthenticationProvider provider;
+ private final Map<String, Object> propertyMap;
+
+ public ServerGreetingHandler(final RemoteConnection connection, final ConnectionProviderContext connectionProviderContext, final Set<String> saslMechs, final ServerAuthenticationProvider provider, final Map<String, Object> propertyMap) {
+ this.connection = connection;
+ this.connectionProviderContext = connectionProviderContext;
+ this.saslMechs = saslMechs;
+ this.provider = provider;
+ this.propertyMap = propertyMap;
+ }
+
+ public void handleMessage(final ByteBuffer buffer) {
+ switch (buffer.get()) {
+ case RemoteProtocol.GREETING: {
+ while (buffer.hasRemaining()) {
+ final byte type = buffer.get();
+ final int len = buffer.get() & 0xff;
+ switch (type) {
+ case RemoteProtocol.GREETING_VERSION: {
+ // We only support version zero, so knowing the other side's version is not useful presently
+ buffer.get();
+ if (len > 1) Buffers.skip(buffer, len - 1);
+ break;
+ }
+ default: {
+ // unknown, skip it for forward compatibility.
+ Buffers.skip(buffer, len);
+ break;
+ }
+ }
+ }
+ connection.setMessageHandler(new ServerInitialAuthenticationHandler(connection, propertyMap, saslMechs, provider, connectionProviderContext));
+ return;
+ }
+ default: {
+ // todo log invalid greeting
+ IoUtils.safeClose(connection);
+ }
+ }
+ }
+
+ public void handleEof() {
+ IoUtils.safeClose(connection);
+ }
+
+ public void handleException(final IOException e) {
+ // todo log it
+ IoUtils.safeClose(connection);
+ }
+}
Added: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerInitialAuthenticationHandler.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerInitialAuthenticationHandler.java (rev 0)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerInitialAuthenticationHandler.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -0,0 +1,92 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.remoting3.remote;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Map;
+import java.util.Set;
+import org.jboss.remoting3.security.ServerAuthenticationProvider;
+import org.jboss.remoting3.spi.ConnectionProviderContext;
+import org.jboss.xnio.Buffers;
+import org.jboss.xnio.IoUtils;
+import org.jboss.xnio.channels.MessageHandler;
+
+import javax.security.sasl.Sasl;
+import javax.security.sasl.SaslServer;
+
+final class ServerInitialAuthenticationHandler implements MessageHandler {
+ private final RemoteConnection remoteConnection;
+ private final Map<String, ?> saslPropertyMap;
+ private final Set<String> allowedMechsMap;
+ private final ServerAuthenticationProvider authenticationProvider;
+ private final ConnectionProviderContext connectionProviderContext;
+
+ ServerInitialAuthenticationHandler(final RemoteConnection remoteConnection, final Map<String, ?> saslPropertyMap, final Set<String> allowedMechsMap, final ServerAuthenticationProvider authenticationProvider, final ConnectionProviderContext connectionProviderContext) {
+ this.remoteConnection = remoteConnection;
+ this.saslPropertyMap = saslPropertyMap;
+ this.allowedMechsMap = allowedMechsMap;
+ this.authenticationProvider = authenticationProvider;
+ this.connectionProviderContext = connectionProviderContext;
+ }
+
+ public void handleMessage(final ByteBuffer buffer) {
+ switch (buffer.get()) {
+ case RemoteProtocol.AUTH_REQUEST: {
+ try {
+ // mech name
+ final String name = Buffers.getModifiedUtf8(buffer);
+ if (allowedMechsMap.contains(name)) {
+ final SaslServer server = Sasl.createSaslServer(name, "remote", remoteConnection.getChannel().getLocalAddress().getHostName(), saslPropertyMap, authenticationProvider.getCallbackHandler());
+ remoteConnection.setMessageHandler(new ServerAuthenticationHandler(remoteConnection, server, connectionProviderContext));
+ remoteConnection.sendAuthMessage(RemoteProtocol.AUTH_CHALLENGE, SaslUtils.EMPTY);
+ return;
+ } else {
+ remoteConnection.sendAuthReject("Invalid mechanism name");
+ return;
+ }
+ } catch (IOException e) {
+ IoUtils.safeClose(remoteConnection);
+ // todo log it
+ return;
+ }
+ }
+ default: {
+ // todo log invalid msg
+ IoUtils.safeClose(remoteConnection);
+ }
+ }
+ }
+
+ public void handleEof() {
+ try {
+ remoteConnection.shutdownReads();
+ } catch (IOException e) {
+ // todo log it
+ }
+ }
+
+ public void handleException(final IOException e) {
+ // todo log it
+ }
+}
Copied: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerOpenListener.java (from rev 5760, remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteOpenListener.java)
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerOpenListener.java (rev 0)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/ServerOpenListener.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -0,0 +1,114 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.remoting3.remote;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.nio.ByteBuffer;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.LinkedHashSet;
+import java.util.Map;
+import java.util.Set;
+import org.jboss.remoting3.RemotingOptions;
+import org.jboss.remoting3.security.ServerAuthenticationProvider;
+import org.jboss.remoting3.spi.ConnectionProviderContext;
+import org.jboss.remoting3.spi.ProtocolServiceType;
+import org.jboss.xnio.ChannelListener;
+import org.jboss.xnio.IoUtils;
+import org.jboss.xnio.OptionMap;
+import org.jboss.xnio.Options;
+import org.jboss.xnio.Sequence;
+import org.jboss.xnio.channels.ConnectedStreamChannel;
+
+import javax.security.sasl.Sasl;
+import javax.security.sasl.SaslServerFactory;
+
+final class ServerOpenListener implements ChannelListener<ConnectedStreamChannel<InetSocketAddress>> {
+
+ private final OptionMap optionMap;
+ private final ConnectionProviderContext connectionProviderContext;
+
+ ServerOpenListener(final OptionMap optionMap, final ConnectionProviderContext connectionProviderContext) {
+ this.optionMap = optionMap;
+ this.connectionProviderContext = connectionProviderContext;
+ }
+
+ public void handleEvent(final ConnectedStreamChannel<InetSocketAddress> channel) {
+ try {
+ channel.setOption(Options.TCP_NODELAY, Boolean.TRUE);
+ } catch (IOException e) {
+ // ignore
+ }
+ final RemoteConnection connection = new RemoteConnection(connectionProviderContext.getExecutor(), channel, optionMap);
+
+ // Calculate available server mechanisms
+ final Sequence<String> mechs = optionMap.get(RemotingOptions.SASL_SERVER_MECHANISMS);
+ final Set<String> includes = mechs != null ? new HashSet<String>(mechs) : null;
+ final Set<String> serverMechanisms = new LinkedHashSet<String>();
+ final Map<String, Object> propertyMap = SaslUtils.createPropertyMap(optionMap);
+ final Enumeration<SaslServerFactory> e = Sasl.getSaslServerFactories();
+ while (e.hasMoreElements()) {
+ final SaslServerFactory saslServerFactory = e.nextElement();
+ for (String name : saslServerFactory.getMechanismNames(propertyMap)) {
+ if (includes == null || includes.contains(name)) {
+ serverMechanisms.add(name);
+ }
+ }
+ }
+
+ // Send server greeting packet...
+ final ByteBuffer buffer = connection.allocate();
+ try {
+ // length placeholder
+ buffer.putInt(0);
+ // version ID
+ GreetingUtils.writeByte(buffer, RemoteProtocol.GREETING_VERSION, RemoteProtocol.VERSION);
+ // SASL server mechs
+ for (String name : serverMechanisms) {
+ GreetingUtils.writeString(buffer, RemoteProtocol.GREETING_SASL_MECH, name);
+ }
+ // that's it!
+ buffer.flip();
+ connection.sendBlocking(buffer);
+ } catch (Exception e1) {
+ // todo log it
+ IoUtils.safeClose(connection);
+ } finally {
+ connection.free(buffer);
+ }
+ final String authProvider = optionMap.get(RemotingOptions.AUTHENTICATION_PROVIDER);
+ if (authProvider == null) {
+ // todo log no valid auth provider
+ IoUtils.safeClose(connection);
+ }
+ final ServerAuthenticationProvider provider = connectionProviderContext.getProtocolServiceProvider(ProtocolServiceType.SERVER_AUTHENTICATION_PROVIDER, authProvider);
+ if (provider == null) {
+ // todo log no valid auth provider
+ IoUtils.safeClose(connection);
+ }
+ connection.setMessageHandler(new ServerGreetingHandler(connection, connectionProviderContext, serverMechanisms, provider, propertyMap));
+ // start up the read cycle
+ channel.resumeReads();
+ }
+}
Added: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/security/ServerAuthenticationProvider.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/security/ServerAuthenticationProvider.java (rev 0)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/security/ServerAuthenticationProvider.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -0,0 +1,32 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.remoting3.security;
+
+import javax.security.auth.callback.CallbackHandler;
+
+/**
+ *
+ */
+public interface ServerAuthenticationProvider {
+ CallbackHandler getCallbackHandler();
+}
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/ProtocolServiceType.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/ProtocolServiceType.java 2010-02-28 00:58:32 UTC (rev 5770)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/ProtocolServiceType.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -29,6 +29,7 @@
import org.jboss.marshalling.ClassExternalizerFactory;
import org.jboss.marshalling.ProviderDescriptor;
import java.io.Serializable;
+import org.jboss.remoting3.security.ServerAuthenticationProvider;
public final class ProtocolServiceType<T> implements Serializable {
@@ -77,6 +78,8 @@
public static final ProtocolServiceType<ClassExternalizerFactory> CLASS_EXTERNALIZER_FACTORY;
+ public static final ProtocolServiceType<ServerAuthenticationProvider> SERVER_AUTHENTICATION_PROVIDER;
+
private static final ProtocolServiceType<?>[] SERVICE_TYPES;
public static ProtocolServiceType<?>[] getServiceTypes() {
@@ -96,6 +99,7 @@
CLASS_RESOLVER = new ProtocolServiceType<ClassResolver>(ClassResolver.class, "CLASS_RESOLVER", "Class resolver", index++),
OBJECT_RESOLVER = new ProtocolServiceType<ObjectResolver>(ObjectResolver.class, "OBJECT_RESOLVER", "Object resolver", index++),
CLASS_EXTERNALIZER_FACTORY = new ProtocolServiceType<ClassExternalizerFactory>(ClassExternalizerFactory.class, "CLASS_EXTERNALIZER_FACTORY", "Class externalizer factory", index++),
+ SERVER_AUTHENTICATION_PROVIDER = new ProtocolServiceType<ServerAuthenticationProvider>(ServerAuthenticationProvider.class, "SERVER_AUTHENTICATION_PROVIDER", "Server authentication provider", index++)
};
}
Modified: remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/InvocationTestBase.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/InvocationTestBase.java 2010-02-28 00:58:32 UTC (rev 5770)
+++ remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/InvocationTestBase.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -62,13 +62,15 @@
}
}
- private static void enter() {
- log.info("Entering: %s", new Throwable().getStackTrace()[1].getMethodName());
+ static void enter() {
+ final StackTraceElement e = new Throwable().getStackTrace()[1];
+ log.info("Entering: %s#%s", e.getClassName(), e.getMethodName());
}
- private static void exit() {
- log.info("Exiting: %s", new Throwable().getStackTrace()[1].getMethodName());
- log.info("-----------------------------------------");
+ static void exit() {
+ final StackTraceElement e = new Throwable().getStackTrace()[1];
+ log.info("Exiting: %s#%s", e.getClassName(), e.getMethodName());
+ log.info("-------------------------------------------------------------");
}
protected abstract Connection getConnection() throws IOException;
Modified: remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/LocalTestCase.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/LocalTestCase.java 2010-02-28 00:58:32 UTC (rev 5770)
+++ remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/LocalTestCase.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -28,7 +28,7 @@
import org.jboss.xnio.OptionMap;
import org.testng.annotations.Test;
-@Test
+@Test(suiteName = "Local tests")
public final class LocalTestCase extends InvocationTestBase {
protected Connection getConnection() throws IOException {
Modified: remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemoteTestCase.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemoteTestCase.java 2010-02-28 00:58:32 UTC (rev 5770)
+++ remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemoteTestCase.java 2010-02-28 15:51:38 UTC (rev 5771)
@@ -28,7 +28,10 @@
import java.net.URI;
import org.jboss.remoting3.CloseHandler;
import org.jboss.remoting3.Connection;
+import org.jboss.remoting3.RemotingOptions;
+import org.jboss.remoting3.security.ServerAuthenticationProvider;
import org.jboss.remoting3.spi.NetworkServerProvider;
+import org.jboss.remoting3.spi.ProtocolServiceType;
import org.jboss.xnio.AcceptingServer;
import org.jboss.xnio.ChannelListener;
import org.jboss.xnio.IoFuture;
@@ -37,21 +40,63 @@
import org.jboss.xnio.Xnio;
import org.jboss.xnio.channels.BoundChannel;
import org.jboss.xnio.channels.ConnectedStreamChannel;
+import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
-@Test
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.NameCallback;
+import javax.security.auth.callback.PasswordCallback;
+import javax.security.auth.callback.UnsupportedCallbackException;
+import javax.security.sasl.AuthenticationException;
+import javax.security.sasl.RealmCallback;
+
+@Test(suiteName = "Remote tests")
public final class RemoteTestCase extends InvocationTestBase {
+ @BeforeTest
+ public void setUp() throws IOException {
+ enter();
+ try {
+ super.setUp();
+ endpoint.addProtocolService(ProtocolServiceType.SERVER_AUTHENTICATION_PROVIDER, "test", new ServerAuthenticationProvider() {
+ public CallbackHandler getCallbackHandler() {
+ return new CallbackHandler() {
+ public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException {
+ for (Callback callback : callbacks) {
+ if (callback instanceof NameCallback) {
+ final NameCallback nameCallback = (NameCallback) callback;
+ if (! nameCallback.getName().equals("user")) {
+ throw new AuthenticationException("Invalid user name");
+ }
+ } else if (callback instanceof PasswordCallback) {
+ final PasswordCallback passwordCallback = (PasswordCallback) callback;
+ passwordCallback.setPassword("password".toCharArray());
+ } else if (callback instanceof RealmCallback) {
+ // allow
+ } else {
+ throw new UnsupportedCallbackException(callback);
+ }
+ }
+ }
+ };
+ }
+ });
+ } finally {
+ exit();
+ }
+ }
+
protected Connection getConnection() throws IOException {
final NetworkServerProvider provider = endpoint.getConnectionProviderInterface("remote", NetworkServerProvider.class);
- final ChannelListener<ConnectedStreamChannel<InetSocketAddress>> listener = provider.getServerListener(OptionMap.EMPTY);
+ final ChannelListener<ConnectedStreamChannel<InetSocketAddress>> listener = provider.getServerListener(OptionMap.builder().set(RemotingOptions.AUTHENTICATION_PROVIDER, "test").getMap());
final Xnio xnio = Xnio.getInstance();
try {
final AcceptingServer<InetSocketAddress, ?, ?> server = xnio.createSslTcpServer(listener, OptionMap.EMPTY);
// final AcceptingServer<InetSocketAddress, ?, ?> server = xnio.createTcpServer(listener, OptionMap.EMPTY);
final IoFuture<? extends BoundChannel<InetSocketAddress>> future = server.bind(new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 0));
final InetSocketAddress localAddress = future.get().getLocalAddress();
- final Connection connection = endpoint.connect(new URI("remote", null, localAddress.getAddress().getHostAddress(), localAddress.getPort(), null, null, null), OptionMap.EMPTY).get();
+ final Connection connection = endpoint.connect(new URI("remote", null, localAddress.getAddress().getHostAddress(), localAddress.getPort(), null, null, null), OptionMap.EMPTY, "user", "realm", "password".toCharArray()).get();
connection.addCloseHandler(new CloseHandler<Connection>() {
public void handleClose(final Connection closed) {
IoUtils.safeClose(server);
14 years, 9 months
JBoss Remoting SVN: r5770 - in remoting3/trunk/jboss-remoting/src/test: resources and 1 other directory.
by jboss-remoting-commits@lists.jboss.org
Author: david.lloyd(a)jboss.com
Date: 2010-02-27 19:58:32 -0500 (Sat, 27 Feb 2010)
New Revision: 5770
Modified:
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemoteTestCase.java
remoting3/trunk/jboss-remoting/src/test/resources/remoting.properties
Log:
Switch to SSL
Modified: remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemoteTestCase.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemoteTestCase.java 2010-02-28 00:57:54 UTC (rev 5769)
+++ remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemoteTestCase.java 2010-02-28 00:58:32 UTC (rev 5770)
@@ -47,8 +47,8 @@
final ChannelListener<ConnectedStreamChannel<InetSocketAddress>> listener = provider.getServerListener(OptionMap.EMPTY);
final Xnio xnio = Xnio.getInstance();
try {
-// final AcceptingServer<InetSocketAddress, ?, ?> server = xnio.createSslTcpServer(listener, OptionMap.EMPTY);
- final AcceptingServer<InetSocketAddress, ?, ?> server = xnio.createTcpServer(listener, OptionMap.EMPTY);
+ final AcceptingServer<InetSocketAddress, ?, ?> server = xnio.createSslTcpServer(listener, OptionMap.EMPTY);
+// final AcceptingServer<InetSocketAddress, ?, ?> server = xnio.createTcpServer(listener, OptionMap.EMPTY);
final IoFuture<? extends BoundChannel<InetSocketAddress>> future = server.bind(new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 0));
final InetSocketAddress localAddress = future.get().getLocalAddress();
final Connection connection = endpoint.connect(new URI("remote", null, localAddress.getAddress().getHostAddress(), localAddress.getPort(), null, null, null), OptionMap.EMPTY).get();
Modified: remoting3/trunk/jboss-remoting/src/test/resources/remoting.properties
===================================================================
--- remoting3/trunk/jboss-remoting/src/test/resources/remoting.properties 2010-02-28 00:57:54 UTC (rev 5769)
+++ remoting3/trunk/jboss-remoting/src/test/resources/remoting.properties 2010-02-28 00:58:32 UTC (rev 5770)
@@ -20,4 +20,4 @@
# 02110-1301 USA, or see the FSF site: http://www.fsf.org.
#
-remote.ssl.enable=false
+#remote.ssl.enable=false
14 years, 9 months
JBoss Remoting SVN: r5769 - remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test.
by jboss-remoting-commits@lists.jboss.org
Author: david.lloyd(a)jboss.com
Date: 2010-02-27 19:57:54 -0500 (Sat, 27 Feb 2010)
New Revision: 5769
Modified:
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/InvocationTestBase.java
Log:
Skip test for unfinished feature
Modified: remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/InvocationTestBase.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/InvocationTestBase.java 2010-02-28 00:14:45 UTC (rev 5768)
+++ remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/InvocationTestBase.java 2010-02-28 00:57:54 UTC (rev 5769)
@@ -38,6 +38,7 @@
import org.jboss.xnio.IoUtils;
import org.jboss.xnio.Xnio;
import org.jboss.xnio.log.Logger;
+import org.testng.SkipException;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
@@ -212,6 +213,8 @@
} finally {
IoUtils.safeClose(registration);
}
+ } catch (UnsupportedOperationException e) {
+ throw new SkipException("Skipping test due to unsupported createClientConnector");
} finally {
exit();
}
14 years, 9 months
JBoss Remoting SVN: r5768 - remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote.
by jboss-remoting-commits@lists.jboss.org
Author: david.lloyd(a)jboss.com
Date: 2010-02-27 19:14:45 -0500 (Sat, 27 Feb 2010)
New Revision: 5768
Modified:
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteConnectionHandler.java
Log:
Throw exception for now for ClientConnector over remote:
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteConnectionHandler.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteConnectionHandler.java 2010-02-28 00:08:16 UTC (rev 5767)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteConnectionHandler.java 2010-02-28 00:14:45 UTC (rev 5768)
@@ -155,7 +155,7 @@
}
public RequestHandlerConnector createConnector(final RequestHandler localHandler) {
- return null;
+ throw new UnsupportedOperationException();
}
public void close() throws IOException {
14 years, 9 months
JBoss Remoting SVN: r5767 - in remoting3/trunk/jboss-remoting: src/test/java/org/jboss/remoting3/test and 1 other directory.
by jboss-remoting-commits@lists.jboss.org
Author: david.lloyd(a)jboss.com
Date: 2010-02-27 19:08:16 -0500 (Sat, 27 Feb 2010)
New Revision: 5767
Modified:
remoting3/trunk/jboss-remoting/pom.xml
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/CloseableTestCase.java
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/EndpointTestCase.java
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/InvocationTestBase.java
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/LocalTestCase.java
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemoteTestCase.java
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/StreamsTestCase.java
Log:
Switch to testNG
Modified: remoting3/trunk/jboss-remoting/pom.xml
===================================================================
--- remoting3/trunk/jboss-remoting/pom.xml 2010-02-27 23:26:07 UTC (rev 5766)
+++ remoting3/trunk/jboss-remoting/pom.xml 2010-02-28 00:08:16 UTC (rev 5767)
@@ -65,9 +65,10 @@
<scope>test</scope>
</dependency>
<dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.2</version>
+ <groupId>org.testng</groupId>
+ <artifactId>testng</artifactId>
+ <version>5.8</version>
+ <classifier>jdk15</classifier>
<scope>test</scope>
</dependency>
<dependency>
Modified: remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/CloseableTestCase.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/CloseableTestCase.java 2010-02-27 23:26:07 UTC (rev 5766)
+++ remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/CloseableTestCase.java 2010-02-28 00:08:16 UTC (rev 5767)
@@ -27,16 +27,20 @@
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
-import junit.framework.TestCase;
import org.jboss.remoting3.CloseHandler;
import org.jboss.remoting3.spi.AbstractHandleableCloseable;
import org.jboss.xnio.IoUtils;
import org.jboss.xnio.log.Logger;
+import org.testng.annotations.Test;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+
/**
*
*/
-public final class CloseableTestCase extends TestCase {
+@Test
+public final class CloseableTestCase {
public static final Logger log = Logger.getLogger(CloseableTestCase.class);
Modified: remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/EndpointTestCase.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/EndpointTestCase.java 2010-02-27 23:26:07 UTC (rev 5766)
+++ remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/EndpointTestCase.java 2010-02-28 00:08:16 UTC (rev 5767)
@@ -26,7 +26,6 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
-import junit.framework.TestCase;
import org.jboss.remoting3.Client;
import org.jboss.remoting3.Endpoint;
import org.jboss.remoting3.RemoteExecutionException;
@@ -36,11 +35,16 @@
import org.jboss.xnio.IoUtils;
import org.jboss.xnio.OptionMap;
import org.jboss.xnio.log.Logger;
+import org.testng.annotations.Test;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+
/**
*
*/
-public final class EndpointTestCase extends TestCase {
+@Test
+public final class EndpointTestCase {
private static final Logger log = Logger.getLogger(EndpointTestCase.class);
Modified: remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/InvocationTestBase.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/InvocationTestBase.java 2010-02-27 23:26:07 UTC (rev 5766)
+++ remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/InvocationTestBase.java 2010-02-28 00:08:16 UTC (rev 5767)
@@ -23,7 +23,6 @@
package org.jboss.remoting3.test;
import java.io.IOException;
-import junit.framework.TestCase;
import org.jboss.remoting3.Client;
import org.jboss.remoting3.ClientConnector;
import org.jboss.remoting3.ClientContext;
@@ -39,12 +38,20 @@
import org.jboss.xnio.IoUtils;
import org.jboss.xnio.Xnio;
import org.jboss.xnio.log.Logger;
+import org.testng.annotations.AfterTest;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.Test;
-public abstract class InvocationTestBase extends TestCase {
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.fail;
+
+@Test
+public abstract class InvocationTestBase {
private static final Logger log = Logger.getLogger(InvocationTestBase.class);
protected Endpoint endpoint;
+ @BeforeTest
public void setUp() throws IOException {
enter();
try {
@@ -60,6 +67,7 @@
private static void exit() {
log.info("Exiting: %s", new Throwable().getStackTrace()[1].getMethodName());
+ log.info("-----------------------------------------");
}
protected abstract Connection getConnection() throws IOException;
@@ -226,6 +234,7 @@
}
}
+ @AfterTest
public void tearDown() throws IOException {
enter();
try {
Modified: remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/LocalTestCase.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/LocalTestCase.java 2010-02-27 23:26:07 UTC (rev 5766)
+++ remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/LocalTestCase.java 2010-02-28 00:08:16 UTC (rev 5767)
@@ -26,7 +26,9 @@
import java.net.URI;
import org.jboss.remoting3.Connection;
import org.jboss.xnio.OptionMap;
+import org.testng.annotations.Test;
+@Test
public final class LocalTestCase extends InvocationTestBase {
protected Connection getConnection() throws IOException {
Modified: remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemoteTestCase.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemoteTestCase.java 2010-02-27 23:26:07 UTC (rev 5766)
+++ remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/RemoteTestCase.java 2010-02-28 00:08:16 UTC (rev 5767)
@@ -37,7 +37,9 @@
import org.jboss.xnio.Xnio;
import org.jboss.xnio.channels.BoundChannel;
import org.jboss.xnio.channels.ConnectedStreamChannel;
+import org.testng.annotations.Test;
+@Test
public final class RemoteTestCase extends InvocationTestBase {
protected Connection getConnection() throws IOException {
Modified: remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/StreamsTestCase.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/StreamsTestCase.java 2010-02-27 23:26:07 UTC (rev 5766)
+++ remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/StreamsTestCase.java 2010-02-28 00:08:16 UTC (rev 5767)
@@ -27,15 +27,21 @@
import java.util.Arrays;
import java.util.Iterator;
import java.util.Vector;
-import junit.framework.TestCase;
import org.jboss.remoting3.stream.ObjectSink;
import org.jboss.remoting3.stream.ObjectSource;
import org.jboss.remoting3.stream.Streams;
+import org.testng.annotations.Test;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
+
/**
*
*/
-public final class StreamsTestCase extends TestCase {
+@Test
+public final class StreamsTestCase {
public void testCollectionObjectSink() throws Throwable {
final ArrayList<String> strings = new ArrayList<String>();
14 years, 9 months
JBoss Remoting SVN: r5766 - remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test.
by jboss-remoting-commits@lists.jboss.org
Author: david.lloyd(a)jboss.com
Date: 2010-02-27 18:26:07 -0500 (Sat, 27 Feb 2010)
New Revision: 5766
Modified:
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/InvocationTestObject.java
Log:
Implement equals/hashCode on test object so equals assertions work across remote connections
Modified: remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/InvocationTestObject.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/InvocationTestObject.java 2010-02-27 23:20:58 UTC (rev 5765)
+++ remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/InvocationTestObject.java 2010-02-27 23:26:07 UTC (rev 5766)
@@ -26,4 +26,22 @@
public final class InvocationTestObject implements Serializable {
private static final long serialVersionUID = 7228470862155215008L;
+
+ private final int frob = 12345;
+
+ public int getFrob() {
+ return frob;
+ }
+
+ public boolean equals(final Object obj) {
+ return obj instanceof InvocationTestObject && equals((InvocationTestObject) obj);
+ }
+
+ public boolean equals(final InvocationTestObject obj) {
+ return obj != null && obj.frob == frob;
+ }
+
+ public int hashCode() {
+ return frob;
+ }
}
14 years, 9 months
JBoss Remoting SVN: r5765 - remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote.
by jboss-remoting-commits@lists.jboss.org
Author: david.lloyd(a)jboss.com
Date: 2010-02-27 18:20:58 -0500 (Sat, 27 Feb 2010)
New Revision: 5765
Modified:
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/InboundRequest.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/InboundRequestInputHandler.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundClient.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundReplyBufferWriter.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundRequest.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundRequestBufferWriter.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteConnectionHandler.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteMessageHandler.java
Log:
Improve trace logging
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/InboundRequest.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/InboundRequest.java 2010-02-27 21:48:46 UTC (rev 5764)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/InboundRequest.java 2010-02-27 23:20:58 UTC (rev 5765)
@@ -34,11 +34,13 @@
private OutboundReplyHandler replyHandler;
private final NioByteInput byteInput;
private final RemoteConnectionHandler remoteConnectionHandler;
+ private final int rid;
private State state = State.RECEIVING;
InboundRequest(final RemoteConnectionHandler remoteConnectionHandler, final int rid) {
this.remoteConnectionHandler = remoteConnectionHandler;
byteInput = new NioByteInput(new InboundRequestInputHandler(this, rid));
+ this.rid = rid;
}
void ack() {
@@ -79,4 +81,8 @@
RemoteConnectionHandler getRemoteConnectionHandler() {
return remoteConnectionHandler;
}
+
+ public String toString() {
+ return "Inbound request ID " + rid;
+ }
}
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/InboundRequestInputHandler.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/InboundRequestInputHandler.java 2010-02-27 21:48:46 UTC (rev 5764)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/InboundRequestInputHandler.java 2010-02-27 23:20:58 UTC (rev 5765)
@@ -47,6 +47,8 @@
buffer.flip();
connectionHandler.sendBlocking(buffer);
connectionHandler.flushBlocking();
+ } catch (IOException e) {
+ RemoteConnectionHandler.log.trace(e, "Failed to acknowledge chunk for %s", this);
} finally {
bufferPool.free(buffer);
}
@@ -55,4 +57,8 @@
public void close() throws IOException {
// todo: stream was closed, no action needed
}
+
+ public String toString() {
+ return "Inbound request input handler for request ID " + rid;
+ }
}
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundClient.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundClient.java 2010-02-27 21:48:46 UTC (rev 5764)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundClient.java 2010-02-27 23:20:58 UTC (rev 5765)
@@ -36,6 +36,7 @@
private final String serviceType;
private final String groupName;
private RemoteConnectionHandler remoteConnectionHandler;
+ private RequestHandler requestHandler;
OutboundClient(final RemoteConnectionHandler remoteConnectionHandler, final int id, final Result<RequestHandler> result, final String serviceType, final String groupName) {
this.remoteConnectionHandler = remoteConnectionHandler;
@@ -88,6 +89,15 @@
return id;
}
+ RequestHandler getRequestHandler() {
+ return requestHandler;
+ }
+
+ void setResult(final RequestHandler requestHandler) {
+ result.setResult(requestHandler);
+ this.requestHandler = requestHandler;
+ }
+
enum State {
REPLY_WAIT,
ESTABLISHED,
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundReplyBufferWriter.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundReplyBufferWriter.java 2010-02-27 21:48:46 UTC (rev 5764)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundReplyBufferWriter.java 2010-02-27 23:20:58 UTC (rev 5765)
@@ -46,6 +46,7 @@
final RemoteConnectionHandler connectionHandler = inboundRequest.getRemoteConnectionHandler();
final Pool<ByteBuffer> bufferPool = connectionHandler.getBufferPool();
final ByteBuffer buffer = bufferPool.allocate();
+ RemoteConnectionHandler.log.trace("Allocated buffer %s for %s", buffer, this);
buffer.putInt(RemoteConnectionHandler.LENGTH_PLACEHOLDER);
buffer.put(exception ? RemoteProtocol.REPLY_EXCEPTION : RemoteProtocol.REPLY);
buffer.putInt(id);
@@ -55,6 +56,7 @@
} else {
buffer.put((byte)0);
}
+ RemoteConnectionHandler.log.trace("Prepopulated buffer %s for %s", buffer, this);
return buffer;
}
@@ -70,6 +72,7 @@
if (eof) {
buffer.put(7, (byte) (buffer.get(3) | RemoteProtocol.MSG_FLAG_LAST));
}
+ RemoteConnectionHandler.log.trace("Sending buffer %s for %s", buffer, this);
connectionHandler.sendBlocking(buffer);
} finally {
connectionHandler.getBufferPool().free(buffer);
@@ -79,4 +82,8 @@
public void flush() throws IOException {
inboundRequest.getRemoteConnectionHandler().flushBlocking();
}
+
+ public String toString() {
+ return "Outbound reply buffer writer for " + inboundRequest;
+ }
}
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundRequest.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundRequest.java 2010-02-27 21:48:46 UTC (rev 5764)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundRequest.java 2010-02-27 23:20:58 UTC (rev 5765)
@@ -108,4 +108,8 @@
ReplyHandler getInboundReplyHandler() {
return inboundReplyHandler;
}
+
+ public String toString() {
+ return "Outbound Request for client ID " + cid;
+ }
}
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundRequestBufferWriter.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundRequestBufferWriter.java 2010-02-27 21:48:46 UTC (rev 5764)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/OutboundRequestBufferWriter.java 2010-02-27 23:20:58 UTC (rev 5765)
@@ -41,6 +41,7 @@
public ByteBuffer getBuffer() {
final ByteBuffer buffer = outboundRequest.getRemoteConnectionHandler().getBufferPool().allocate();
+ RemoteConnectionHandler.log.trace("Allocated buffer %s for %s", buffer, this);
buffer.putInt(RemoteConnectionHandler.LENGTH_PLACEHOLDER);
buffer.put(RemoteProtocol.REQUEST);
buffer.putInt(rid);
@@ -51,7 +52,7 @@
} else {
buffer.put((byte)0);
}
- RemoteConnectionHandler.log.trace("Allocated buffer %s for %s", buffer, this);
+ RemoteConnectionHandler.log.trace("Prepopulated buffer %s for %s", buffer, this);
return buffer;
}
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteConnectionHandler.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteConnectionHandler.java 2010-02-27 21:48:46 UTC (rev 5764)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteConnectionHandler.java 2010-02-27 23:20:58 UTC (rev 5765)
@@ -167,7 +167,7 @@
for (IntKeyMap.Entry<OutboundClient> entry : outboundClients) {
final OutboundClient outboundClient = entry.getValue();
synchronized (outboundClient) {
- // todo close the request handler
+ IoUtils.safeClose(outboundClient.getRequestHandler());
}
}
for (IntKeyMap.Entry<InboundClient> entry : inboundClients) {
Modified: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteMessageHandler.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteMessageHandler.java 2010-02-27 21:48:46 UTC (rev 5764)
+++ remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/remote/RemoteMessageHandler.java 2010-02-27 23:20:58 UTC (rev 5765)
@@ -118,7 +118,7 @@
synchronized (client) {
// todo assert client state == waiting
client.setState(OutboundClient.State.ESTABLISHED);
- client.getResult().setResult(new OutboundRequestHandler(client));
+ client.setResult(new OutboundRequestHandler(client));
}
return;
}
@@ -150,13 +150,15 @@
synchronized (inboundRequests) {
if ((flags & RemoteProtocol.MSG_FLAG_FIRST) != 0) {
cid = buffer.getInt();
- inboundRequest = new InboundRequest(connectionHandler, 0);
+ inboundRequest = new InboundRequest(connectionHandler, rid);
start = true;
// todo - check for duplicate
inboundRequests.put(rid, inboundRequest);
+ RemoteConnectionHandler.log.trace("Received first request message %s for %s", buffer, inboundRequest);
} else {
cid = 0;
inboundRequest = inboundRequests.get(rid);
+ RemoteConnectionHandler.log.trace("Received subsequent request message %s for %s", buffer, inboundRequest);
}
if (inboundRequest == null) {
RemoteConnectionHandler.log.trace("Received request for unknown request ID %d", Integer.valueOf(rid));
@@ -220,10 +222,12 @@
}
synchronized (outboundRequest) {
if ((flags & RemoteProtocol.MSG_FLAG_FIRST) != 0) {
+ RemoteConnectionHandler.log.trace("Received first reply message %s for %s", buffer, outboundRequest);
// todo - check for duplicate
outboundRequest.setByteInput(byteInput = new NioByteInput(new InboundReplyInputHandler(outboundRequest, rid)));
connectionHandler.getConnectionContext().getConnectionProviderContext().getExecutor().execute(new InboundReplyTask(connectionHandler, outboundRequest));
} else {
+ RemoteConnectionHandler.log.trace("Received subsequent reply message %s for %s", buffer, outboundRequest);
byteInput = outboundRequest.getByteInput();
}
}
14 years, 9 months