Search in sources :

Example 1 with Handler

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());
            }
        }
    });
}
Also used : NetSocket(org.vertx.java.core.net.NetSocket) NetClient(org.vertx.java.core.net.NetClient) Handler(org.vertx.java.core.Handler) AsyncResult(org.vertx.java.core.AsyncResult)

Example 2 with Handler

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();
    }
}
Also used : NetSocket(org.vertx.java.core.net.NetSocket) MalformedURLException(java.net.MalformedURLException) Handler(org.vertx.java.core.Handler) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) NetClient(org.vertx.java.core.net.NetClient) ServiceDetails(io.fabric8.gateway.ServiceDetails) AsyncResult(org.vertx.java.core.AsyncResult)

Example 3 with Handler

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();
}
Also used : HttpClientResponse(org.vertx.java.core.http.HttpClientResponse) HttpGatewayHandler(io.fabric8.gateway.handlers.http.HttpGatewayHandler) FutureHandler(io.fabric8.gateway.handlers.detecting.FutureHandler) Handler(org.vertx.java.core.Handler) FutureHandler(io.fabric8.gateway.handlers.detecting.FutureHandler) Test(org.junit.Test)

Example 4 with Handler

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();
}
Also used : HttpClientResponse(org.vertx.java.core.http.HttpClientResponse) HttpGatewayHandler(io.fabric8.gateway.handlers.http.HttpGatewayHandler) Handler(org.vertx.java.core.Handler) FutureHandler(io.fabric8.gateway.handlers.detecting.FutureHandler) FutureHandler(io.fabric8.gateway.handlers.detecting.FutureHandler) Test(org.junit.Test)

Example 5 with Handler

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();
}
Also used : MultiMap(org.vertx.java.core.MultiMap) HttpClientResponse(org.vertx.java.core.http.HttpClientResponse) HttpGatewayHandler(io.fabric8.gateway.handlers.http.HttpGatewayHandler) FutureHandler(io.fabric8.gateway.handlers.detecting.FutureHandler) Handler(org.vertx.java.core.Handler) FutureHandler(io.fabric8.gateway.handlers.detecting.FutureHandler) Test(org.junit.Test)

Aggregations

Handler (org.vertx.java.core.Handler)12 JsonObject (org.vertx.java.core.json.JsonObject)6 JsonArray (org.vertx.java.core.json.JsonArray)5 Either (fr.wseduc.webutils.Either)4 HttpClientResponse (org.vertx.java.core.http.HttpClientResponse)4 SecuredAction (fr.wseduc.security.SecuredAction)3 FutureHandler (io.fabric8.gateway.handlers.detecting.FutureHandler)3 HttpGatewayHandler (io.fabric8.gateway.handlers.http.HttpGatewayHandler)3 UserInfos (org.entcore.common.user.UserInfos)3 Test (org.junit.Test)3 MongoUpdateBuilder (fr.wseduc.mongodb.MongoUpdateBuilder)2 Post (fr.wseduc.rs.Post)2 MalformedURLException (java.net.MalformedURLException)2 MongoDBBuilder (org.entcore.common.aggregation.filters.dbbuilders.MongoDBBuilder)2 AsyncResult (org.vertx.java.core.AsyncResult)2 NetClient (org.vertx.java.core.net.NetClient)2 NetSocket (org.vertx.java.core.net.NetSocket)2 ApiDoc (fr.wseduc.rs.ApiDoc)1 Get (fr.wseduc.rs.Get)1 ServiceDetails (io.fabric8.gateway.ServiceDetails)1