use of io.fabric8.gateway.handlers.detecting.Protocol in project fabric8 by jboss-fuse.
the class ExtendedBurnIn method startDetectingGateway.
public DetectingGateway startDetectingGateway() {
String loadBalancerType = LoadBalancers.STICKY_LOAD_BALANCER;
int stickyLoadBalancerCacheSize = LoadBalancers.STICKY_LOAD_BALANCER_DEFAULT_CACHE_SIZE;
LoadBalancer serviceLoadBalancer = LoadBalancers.createLoadBalancer(loadBalancerType, stickyLoadBalancerCacheSize);
ArrayList<Protocol> protocols = new ArrayList<Protocol>();
protocols.add(new StompProtocol());
protocols.add(new MqttProtocol());
protocols.add(new AmqpProtocol());
protocols.add(new OpenwireProtocol());
protocols.add(new HttpProtocol());
protocols.add(new SslProtocol());
DetectingGateway gateway = new DetectingGateway();
gateway.setPort(0);
gateway.setVertx(vertx);
SslConfig sslConfig = new SslConfig(new File(basedir(), "src/test/resources/server.ks"), "password");
sslConfig.setKeyPassword("password");
gateway.setSslConfig(sslConfig);
gateway.setServiceMap(serviceMap);
gateway.setProtocols(protocols);
gateway.setServiceLoadBalancer(serviceLoadBalancer);
gateway.setDefaultVirtualHost("broker1");
gateway.setConnectionTimeout(5000);
if (httpGatewayServer != null) {
gateway.setHttpGateway(new InetSocketAddress("localhost", httpGatewayServer.getPort()));
}
gateway.init();
gateways.add(gateway);
return gateway;
}
use of io.fabric8.gateway.handlers.detecting.Protocol in project fabric8 by jboss-fuse.
the class MqttProtocol method snoopConnectionParameters.
@Override
public void snoopConnectionParameters(final SocketWrapper socket, final Buffer received, final Handler<ConnectionParameters> handler) {
final MqttProtocolDecoder h = new MqttProtocolDecoder(this);
h.errorHandler(new Handler<String>() {
@Override
public void handle(String error) {
LOG.info("STOMP protocol decoding error: " + error);
socket.close();
}
});
h.codecHandler(new Handler<MQTTFrame>() {
@Override
public void handle(MQTTFrame event) {
try {
if (event.messageType() == org.fusesource.mqtt.codec.CONNECT.TYPE) {
CONNECT connect = new CONNECT().decode(event);
ConnectionParameters parameters = new ConnectionParameters();
if (connect.clientId() != null) {
parameters.protocolClientId = connect.clientId().toString();
}
if (connect.userName() != null) {
parameters.protocolUser = connect.userName().toString();
// containing the virtual host info.
if (parameters.protocolUser.contains("/")) {
// Strip off the virtual host part of the username..
String[] parts = parameters.protocolUser.split("/", 2);
parameters.protocolVirtualHost = parts[0];
parameters.protocolUser = parts[1];
// Update the connect frame to strip out the virtual host from the username field...
connect.userName(new UTF8Buffer(parameters.protocolUser));
// re-write the received buffer /w the updated connect frame
Buffer tail = received.getBuffer((int) h.getBytesDecoded(), received.length());
BufferSupport.setLength(received, 0);
append(received, connect.encode());
received.appendBuffer(tail);
}
}
handler.handle(parameters);
} else {
LOG.info("Expected a CONNECT frame");
socket.close();
}
} catch (java.net.ProtocolException e) {
LOG.info("Invalid MQTT frame: " + e, e);
socket.close();
}
}
});
socket.readStream().dataHandler(h);
h.handle(received);
}
use of io.fabric8.gateway.handlers.detecting.Protocol in project fabric8 by jboss-fuse.
the class OpenwireProtocol method snoopConnectionParameters.
@Override
public void snoopConnectionParameters(final SocketWrapper socket, Buffer received, final Handler<ConnectionParameters> handler) {
OpenwireProtocolDecoder h = new OpenwireProtocolDecoder(this);
h.errorHandler(new Handler<String>() {
@Override
public void handle(String error) {
LOG.info("Openwire protocol decoding error: " + error);
socket.close();
}
});
h.codecHandler(new Handler<Command>() {
@Override
public void handle(Command event) {
if (event instanceof WireFormatInfo) {
WireFormatInfo info = (WireFormatInfo) event;
ConnectionParameters parameters = new ConnectionParameters();
try {
parameters.protocolVirtualHost = info.getHost();
} catch (IOException e) {
e.printStackTrace();
}
handler.handle(parameters);
} else {
LOG.info("Expected a WireFormatInfo frame");
socket.close();
}
}
});
socket.readStream().dataHandler(h);
h.handle(received);
}
use of io.fabric8.gateway.handlers.detecting.Protocol in project fabric8 by jboss-fuse.
the class DetectingGateway method handle.
public void handle(final SocketWrapper socket) {
try {
shutdownTracker.retain();
if (!socketsConnecting.add(socket)) {
throw new AssertionError("Socket existed in the socketsConnecting set");
}
} catch (Throwable e) {
LOG.debug("Could not accept connection from: " + socket.remoteAddress(), e);
socket.close();
// shutdownTracker.release();
return;
}
receivedConnectionAttempts.incrementAndGet();
if (connectionTimeout > 0) {
vertx.setTimer(connectionTimeout, new Handler<Long>() {
public void handle(Long timerID) {
if (socketsConnecting.contains(socket)) {
handleConnectFailure(socket, String.format("Gateway client '%s' protocol detection timeout.", socket.remoteAddress()));
}
}
});
}
ReadStream<ReadStream> readStream = socket.readStream();
readStream.exceptionHandler(new Handler<Throwable>() {
@Override
public void handle(Throwable e) {
handleConnectFailure(socket, String.format("Failed to route gateway client '%s' due to: %s", socket.remoteAddress(), e));
}
});
readStream.endHandler(new Handler<Void>() {
@Override
public void handle(Void event) {
handleConnectFailure(socket, String.format("Gateway client '%s' closed the connection before it could be routed.", socket.remoteAddress()));
}
});
readStream.dataHandler(new Handler<Buffer>() {
Buffer received = new Buffer();
{
LOG.debug("Inititalized new Handler[{}] for socket: {}", this, socket.remoteAddress());
}
@Override
public void handle(Buffer event) {
received.appendBuffer(event);
if (LOG.isTraceEnabled()) {
LOG.trace("Socket received following data: {}", event.copy().toString().replaceAll("\r", " "));
LOG.trace("Data handled by Handler {}", this.toString());
}
for (final Protocol protocol : protocols) {
if (protocol.matches(received)) {
if ("ssl".equals(protocol.getProtocolName())) {
LOG.info(String.format("SSL Connection from '%s'", socket.remoteAddress()));
String disabledCypherSuites = null;
String enabledCipherSuites = null;
if (sslConfig != null) {
disabledCypherSuites = sslConfig.getDisabledCypherSuites();
enabledCipherSuites = sslConfig.getEnabledCipherSuites();
}
if (sslContext == null) {
try {
if (sslConfig != null) {
sslContext = SSLContext.getInstance(sslConfig.getProtocol());
sslContext.init(sslConfig.getKeyManagers(), sslConfig.getTrustManagers(), null);
} else {
sslContext = SSLContext.getDefault();
}
} catch (Exception e) {
handleConnectFailure(socket, "Could initialize SSL: " + e);
return;
}
}
// lets wrap it up in a SslSocketWrapper.
SslSocketWrapper sslSocketWrapper = new SslSocketWrapper(socket);
sslSocketWrapper.putBackHeader(received);
sslSocketWrapper.initServer(sslContext, clientAuth, disabledCypherSuites, enabledCipherSuites);
// Undo initial connection accounting since we will be redoing @ the SSL level.
boolean removed = socketsConnecting.remove(socket);
assert removed;
receivedConnectionAttempts.decrementAndGet();
try {
DetectingGateway.this.handle(sslSocketWrapper);
} finally {
shutdownTracker.release();
}
return;
} else if ("http".equals(protocol.getProtocolName())) {
InetSocketAddress target = getHttpGateway();
if (target != null) {
try {
URI url = new URI("http://" + target.getHostString() + ":" + target.getPort());
LOG.info(String.format("Connecting '%s' to '%s:%d' using the http protocol", socket.remoteAddress(), url.getHost(), url.getPort()));
ConnectionParameters params = new ConnectionParameters();
params.protocol = "http";
createClient(params, socket, url, received);
return;
} catch (URISyntaxException e) {
handleConnectFailure(socket, "Could not build valid connect URI: " + e);
return;
}
} else {
handleConnectFailure(socket, "No http gateway available for the http protocol");
return;
}
} else {
protocol.snoopConnectionParameters(socket, received, new Handler<ConnectionParameters>() {
@Override
public void handle(ConnectionParameters connectionParameters) {
// this will install a new dataHandler on the socket.
if (connectionParameters.protocol == null)
connectionParameters.protocol = protocol.getProtocolName();
if (connectionParameters.protocolSchemes == null)
connectionParameters.protocolSchemes = protocol.getProtocolSchemes();
route(socket, connectionParameters, received);
}
});
return;
}
}
}
if (received.length() >= maxProtocolIdentificationLength) {
handleConnectFailure(socket, "Connection did not use one of the enabled protocols " + getProtocolNames());
}
}
});
}
use of io.fabric8.gateway.handlers.detecting.Protocol in project fabric8 by jboss-fuse.
the class DetectingGateway method route.
public void route(final SocketWrapper socket, ConnectionParameters params, final Buffer received) {
NetClient client = null;
if (params.protocolVirtualHost == null) {
params.protocolVirtualHost = defaultVirtualHost;
}
HashSet<String> schemes = new HashSet<String>(Arrays.asList(params.protocolSchemes));
if (params.protocolVirtualHost != null) {
List<ServiceDetails> services = serviceMap.getServices(params.protocolVirtualHost);
// Lets try again with the defaultVirtualHost
if (services.isEmpty() && !params.protocolVirtualHost.equals(defaultVirtualHost)) {
params.protocolVirtualHost = defaultVirtualHost;
services = serviceMap.getServices(params.protocolVirtualHost);
}
LOG.debug(String.format("%d services match the virtual host", services.size()));
if (!services.isEmpty()) {
ClientRequestFacade clientRequestFacade = clientRequestFacadeFactory.create(socket, params);
ServiceDetails serviceDetails = serviceLoadBalancer.choose(services, clientRequestFacade);
if (serviceDetails != null) {
List<String> urlStrings = serviceDetails.getServices();
LOG.debug("Selected service exposes the following URLS: {}", urlStrings);
for (String urlString : urlStrings) {
if (Strings.notEmpty(urlString)) {
// lets create a client for this request...
try {
URI uri = new URI(urlString);
// URL url = new URL(urlString);
String urlProtocol = uri.getScheme();
if (schemes.contains(urlProtocol)) {
if (!socket.remoteAddress().toString().equals(clientRequestFacade.getClientRequestKey())) {
LOG.info(String.format("Connecting client from '%s' (with key '%s') requesting virtual host '%s' to '%s:%d' using the %s protocol", socket.remoteAddress(), clientRequestFacade.getClientRequestKey(), params.protocolVirtualHost, uri.getHost(), uri.getPort(), params.protocol));
} else {
LOG.info(String.format("Connecting client from '%s' requesting virtual host '%s' to '%s:%d' using the %s protocol", socket.remoteAddress(), params.protocolVirtualHost, uri.getHost(), uri.getPort(), params.protocol));
}
client = createClient(params, socket, uri, received);
break;
}
} catch (URISyntaxException e) {
LOG.warn("Failed to parse URI: " + urlString + ". " + e, e);
}
}
}
}
}
}
if (client == null) {
// failed to route
handleConnectFailure(socket, String.format("No endpoint available for virtual host '%s' and protocol %s", params.protocolVirtualHost, params.protocol));
}
}
Aggregations