use of org.vertx.java.core.Handler in project fabric8 by jboss-fuse.
the class DetectingGateway method createClient.
/**
* Creates a new client for the given URL and handler
*/
private NetClient createClient(final ConnectionParameters params, final SocketWrapper socketFromClient, final URI url, final Buffer received) {
final NetClient netClient = vertx.createNetClient();
socketFromClient.readStream().pause();
return netClient.connect(url.getPort(), url.getHost(), new Handler<AsyncResult<NetSocket>>() {
public void handle(final AsyncResult<NetSocket> asyncSocket) {
if (!asyncSocket.succeeded()) {
handleConnectFailure(socketFromClient, String.format("Could not connect to '%s'", url));
} else {
socketFromClient.readStream().resume();
final NetSocket socketToServer = asyncSocket.result();
successfulConnectionAttempts.incrementAndGet();
boolean removed = socketsConnecting.remove(socketFromClient);
assert removed;
final ConnectedSocketInfo connectedInfo = new ConnectedSocketInfo(params, url, socketFromClient, netClient);
boolean added = socketsConnected.add(connectedInfo);
assert added;
Handler<Void> endHandler = new Handler<Void>() {
@Override
public void handle(Void event) {
handleShutdown(connectedInfo);
}
};
Handler<Throwable> exceptionHandler = new Handler<Throwable>() {
@Override
public void handle(Throwable event) {
handleShutdown(connectedInfo);
}
};
socketFromClient.readStream().endHandler(endHandler);
socketFromClient.readStream().exceptionHandler(exceptionHandler);
socketToServer.endHandler(endHandler);
socketToServer.exceptionHandler(exceptionHandler);
if (LOG.isTraceEnabled()) {
LOG.trace("Sending out to destination socket: {}", received);
}
socketToServer.write(received);
Pump.createPump(socketToServer, socketFromClient.writeStream()).start();
Pump.createPump(socketFromClient.readStream(), socketToServer).start();
LOG.debug("socketFromClient {} has been connected to socketToServer {}", socketFromClient.remoteAddress(), socketToServer.remoteAddress());
}
}
});
}
use of org.vertx.java.core.Handler in project fabric8 by jboss-fuse.
the class TcpGatewayHandler method handle.
@Override
public void handle(final NetSocket socket) {
NetClient client = null;
List<String> paths = serviceMap.getPaths();
TcpClientRequestFacade requestFacade = new TcpClientRequestFacade(socket);
String path = pathLoadBalancer.choose(paths, requestFacade);
if (path != null) {
List<ServiceDetails> services = serviceMap.getServices(path);
if (!services.isEmpty()) {
ServiceDetails serviceDetails = serviceLoadBalancer.choose(services, requestFacade);
if (serviceDetails != null) {
List<String> urlStrings = serviceDetails.getServices();
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 (Objects.equal(protocol, urlProtocol)) {
Handler<AsyncResult<NetSocket>> handler = new Handler<AsyncResult<NetSocket>>() {
public void handle(final AsyncResult<NetSocket> asyncSocket) {
socket.resume();
NetSocket clientSocket = asyncSocket.result();
Pump.createPump(clientSocket, socket).start();
Pump.createPump(socket, clientSocket).start();
}
};
client = createClient(socket, uri, handler);
break;
}
} catch (MalformedURLException e) {
LOG.warn("Failed to parse URL: " + urlString + ". " + e, e);
} catch (URISyntaxException e) {
LOG.warn("Failed to parse URI: " + urlString + ". " + e, e);
}
}
}
}
}
}
if (client == null) {
// fail to route
LOG.info("No service available for protocol " + protocol + " for paths " + paths);
socket.close();
}
}
use of org.vertx.java.core.Handler in project fabric8 by jboss-fuse.
the class HttpGatewayConnectionTimeoutTest method testConnectionTimeout.
@Test
public void testConnectionTimeout() throws Exception {
startRestEndpoint();
startHttpGateway();
LOG.info("Requesting...");
final FutureHandler<HttpClientResponse> future = new FutureHandler<>();
vertx.createHttpClient().setHost("localhost").setPort(8080).get("/hello/world?wsdl", new Handler<HttpClientResponse>() {
@Override
public void handle(HttpClientResponse event) {
future.handle(event);
}
}).end();
HttpClientResponse response = future.await();
assertEquals(response.statusCode(), 504);
stopHttpGateway();
stopVertx();
}
use of org.vertx.java.core.Handler in project fabric8 by jboss-fuse.
the class HttpGatewayRequestTimeoutTest method testRequestTimeout.
@Test
public void testRequestTimeout() throws Exception {
startRestEndpoint();
startHttpGateway();
LOG.info("Requesting...");
final FutureHandler<HttpClientResponse> future = new FutureHandler<>();
vertx.createHttpClient().setHost("localhost").setPort(8080).get("/hello/world?wsdl", new Handler<HttpClientResponse>() {
@Override
public void handle(HttpClientResponse event) {
future.handle(event);
}
}).end();
HttpClientResponse response = future.await();
assertEquals(response.statusCode(), 504);
stopHttpGateway();
stopVertx();
}
use of org.vertx.java.core.Handler in project fabric8 by jboss-fuse.
the class HttpGatewayTest method testENTESB7600.
@Test
public void testENTESB7600() throws Exception {
// response can not contain CONTENT_LENGTH and TRANSFER_ENCODING see https://tools.ietf.org/html/rfc7230#section-3.3.3
startRestEndpoint();
startHttpGateway();
System.out.println("Requesting...");
final FutureHandler<HttpClientResponse> future = new FutureHandler<>();
vertx.createHttpClient().setHost("localhost").setPort(8080).get("/hello/world?wsdl", new Handler<HttpClientResponse>() {
@Override
public void handle(HttpClientResponse event) {
future.handle(event);
}
}).end();
MultiMap responseHeaders = future.await().headers();
assertTrue((responseHeaders.contains(CONTENT_LENGTH) && !responseHeaders.contains(TRANSFER_ENCODING)) || (!responseHeaders.contains(CONTENT_LENGTH) && responseHeaders.contains(TRANSFER_ENCODING)));
stopHttpGateway();
stopVertx();
}
Aggregations