use of org.apache.qpid.proton.engine.impl.EngineFactoryImpl in project fabric8 by jboss-fuse.
the class AmqpProtocol method experimentalSnoopConnectionParameters.
public void experimentalSnoopConnectionParameters(final NetSocket socket, final Buffer received, final Handler<ConnectionParameters> handler) {
final AmqpProtocolDecoder h = new AmqpProtocolDecoder(this);
final ConnectionParameters parameters = new ConnectionParameters();
h.errorHandler(new Handler<String>() {
@Override
public void handle(String error) {
LOG.info("STOMP protocol decoding error: " + error);
socket.close();
}
});
h.codecHandler(new Handler<AmqpEvent>() {
EngineFactory engineFactory = new EngineFactoryImpl();
Transport protonTransport = engineFactory.createTransport();
Connection protonConnection = engineFactory.createConnection();
Sasl sasl;
@Override
public void handle(AmqpEvent event) {
switch(event.type) {
case HEADER:
AmqpHeader header = (AmqpHeader) event.decodedFrame;
switch(header.getProtocolId()) {
case 0:
// amqpTransport.sendToAmqp(new AmqpHeader());
break;
// nothing to do..
case 3:
// Client will be using SASL for auth..
sasl = protonTransport.sasl();
// sasl.setMechanisms(new String[] { "ANONYMOUS", "PLAIN" });
sasl.server();
break;
default:
}
processEvent(event);
// Les send back the AMQP response headers so that the client
// can send us the SASL init or AMQP open frames.
Buffer buffer = toBuffer(protonTransport.getOutputBuffer());
protonTransport.outputConsumed();
socket.write(buffer);
break;
default:
processEvent(event);
}
}
private void processEvent(AmqpEvent event) {
byte[] buffer = event.encodedFrame.getBytes();
int offset = 0;
int remaining = buffer.length;
while (remaining > 0) {
try {
int count = protonTransport.input(buffer, offset, remaining);
offset += count;
remaining -= count;
} catch (Throwable e) {
LOG.info("Could not decode AMQP frame: " + e, e);
socket.close();
return;
}
if (sasl != null) {
// TODO: add timeout in case the client is waiting for SASL negotiation
if (sasl.getRemoteMechanisms().length > 0) {
parameters.protocolVirtualHost = getHostname(sasl);
if ("PLAIN".equals(sasl.getRemoteMechanisms()[0])) {
byte[] data = new byte[sasl.pending()];
sasl.recv(data, 0, data.length);
Buffer[] parts = split(new Buffer(data), (byte) 0);
if (parts.length > 0) {
parameters.protocolUser = parts[0].toString();
}
// We are done!
handler.handle(parameters);
}
}
}
if (protonConnection.getLocalState() == EndpointState.UNINITIALIZED && protonConnection.getRemoteState() != EndpointState.UNINITIALIZED) {
// If we get here them the connection was not using SASL.. we can get the hostname
// info from the open frame.
parameters.protocolVirtualHost = protonConnection.getRemoteHostname();
// We are done!
handler.handle(parameters);
}
}
}
});
socket.dataHandler(h);
h.handle(received);
}
Aggregations