package org.rzo.netty.ahessian.auth;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.logging.InternalLogger;
import org.jboss.netty.logging.InternalLoggerFactory;
public class AuthTokenList implements AuthToken
{
private Map<ByteArrayWrapper, AuthToken> _tokens;
int _receivedLength = 0;
byte [] _receivedBytes;
AuthToken _currentToken;
boolean _uniqueLogon;
private static final InternalLogger logger =
InternalLoggerFactory.getInstance(SimpleAuthToken.class);
public AuthTokenList(Map<ByteArrayWrapper, AuthToken> tokens, int bytesLength, boolean uniqueLogon)
{
_tokens = tokens;
_receivedBytes = new byte[bytesLength];
_uniqueLogon = uniqueLogon;
}
public int authenticate(ChannelHandlerContext ctx, MessageEvent e)
{
ChannelBuffer b = (ChannelBuffer) e.getMessage();
int toCopy = Math.min(_receivedBytes.length-_receivedLength, b.readableBytes());
byte[] bytes = new byte[toCopy];
b.readBytes(bytes);
System.arraycopy(bytes, 0, _receivedBytes, _receivedLength, bytes.length);
_receivedLength += toCopy;
if (_receivedLength == _receivedBytes.length)
{
_currentToken = _tokens.get(new ByteArrayWrapper(_receivedBytes));
if (_currentToken != null && (_uniqueLogon || _currentToken.isLoggedOn()))
{
logger.info("authenticated");
((SimpleAuthToken)_currentToken).setLoggedOn(true);
if (b.readableBytes() != 0)
ctx.sendUpstream(e);
return PASSED;
}
else
{
_currentToken = null;
return FAILED;
}
}
else
return NOT_COMPLETE;
}
public void sendPassword(ChannelHandlerContext ctx)
{
;
}
public boolean isLoggedOn()
{
return _currentToken != null;
}
public void setLoggedOn(boolean value)
{
if (!value && _currentToken != null)
{
((SimpleAuthToken)_currentToken).setLoggedOn(false);
_currentToken = null;
}
}
public void disconnected()
{
setLoggedOn(false);
}
}