Search in sources :

Example 1 with MappedServices

use of io.fabric8.gateway.handlers.http.MappedServices in project fabric8 by jboss-fuse.

the class ExtendedBurnIn method startHttpGateway.

public HttpGatewayServer startHttpGateway() {
    if (restEndpointServer != null) {
        LoadBalancer loadBalancer = new RoundRobinLoadBalancer();
        ServiceDTO serviceDetails = new ServiceDTO();
        serviceDetails.setContainer("local");
        serviceDetails.setVersion("1");
        mappedServices.put("/hello/world", new MappedServices("http://localhost:8181", serviceDetails, loadBalancer, false));
    }
    DetectingGatewayWebSocketHandler websocketHandler = new DetectingGatewayWebSocketHandler();
    HttpGatewayHandler handler = new HttpGatewayHandler(vertx, new HttpGateway() {

        @Override
        public void addMappingRuleConfiguration(HttpMappingRule mappingRule) {
        }

        @Override
        public void removeMappingRuleConfiguration(HttpMappingRule mappingRule) {
        }

        @Override
        public Map<String, MappedServices> getMappedServices() {
            return mappedServices;
        }

        @Override
        public boolean isEnableIndex() {
            return true;
        }

        @Override
        public InetSocketAddress getLocalAddress() {
            return new InetSocketAddress("0.0.0.0", 8080);
        }

        @Override
        public void addCallDetailRecord(CallDetailRecord cdr) {
        }
    });
    websocketHandler.setPathPrefix("");
    httpGatewayServer = new HttpGatewayServer(vertx, handler, websocketHandler, 8080);
    httpGatewayServer.setHost("localhost");
    httpGatewayServer.init();
    return httpGatewayServer;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) RoundRobinLoadBalancer(io.fabric8.gateway.loadbalancer.RoundRobinLoadBalancer) LoadBalancer(io.fabric8.gateway.loadbalancer.LoadBalancer) RoundRobinLoadBalancer(io.fabric8.gateway.loadbalancer.RoundRobinLoadBalancer) DetectingGatewayWebSocketHandler(io.fabric8.gateway.handlers.detecting.DetectingGatewayWebSocketHandler)

Example 2 with MappedServices

use of io.fabric8.gateway.handlers.http.MappedServices in project fabric8 by jboss-fuse.

the class FabricHTTPGateway method getMappedServices.

@Override
public Map<String, MappedServices> getMappedServices() {
    assertValid();
    Map<String, MappedServices> answer = new HashMap<String, MappedServices>();
    for (HttpMappingRule mappingRuleConfiguration : mappingRuleConfigurations) {
        mappingRuleConfiguration.appendMappedServices(answer);
    }
    return answer;
}
Also used : HashMap(java.util.HashMap) HttpMappingRule(io.fabric8.gateway.handlers.http.HttpMappingRule) MappedServices(io.fabric8.gateway.handlers.http.MappedServices)

Example 3 with MappedServices

use of io.fabric8.gateway.handlers.http.MappedServices in project fabric8 by jboss-fuse.

the class HttpMappingRuleBase method updateMappingRules.

/**
 * Given a path being added or removed, update the services.
 *
 * @param remove        whether to remove (if true) or add (if false) this mapping
 * @param path          the path that this mapping is bound
 * @param services      the HTTP URLs of the services to map to
 * @param defaultParams the default parameters to use in the URI templates such as for version and container
 * @param serviceDetails
 */
public void updateMappingRules(boolean remove, String path, List<String> services, Map<String, String> defaultParams, ServiceDetails serviceDetails) {
    SimplePathTemplate pathTemplate = getUriTemplate();
    if (pathTemplate != null) {
        boolean versionSpecificUri = pathTemplate.getParameterNames().contains("version");
        String versionId = defaultParams.get("version");
        if (!remove && Strings.isNotBlank(versionId) && !versionSpecificUri && gatewayVersion != null) {
            // lets ignore this mapping if the version does not match
            if (!gatewayVersion.equals(versionId)) {
                remove = true;
            }
        }
        Map<String, String> params = new HashMap<String, String>();
        if (defaultParams != null) {
            params.putAll(defaultParams);
        }
        params.put("servicePath", path);
        if (!versionSpecificUri && Strings.isNotBlank(this.enabledVersion)) {
            if (!serviceDetails.getVersion().equals(this.enabledVersion)) {
                remove = true;
            }
        }
        for (String service : services) {
            populateUrlParams(params, service);
            String fullPath = pathTemplate.bindByNameNonStrict(params);
            if (remove) {
                MappedServices rule = mappingRules.get(fullPath);
                if (rule != null) {
                    List<String> serviceUrls = rule.getServiceUrls();
                    serviceUrls.remove(service);
                    if (serviceUrls.isEmpty()) {
                        mappingRules.remove(fullPath);
                    }
                }
            } else {
                MappedServices mappedServices = new MappedServices(service, serviceDetails, loadBalancer, reverseHeaders);
                MappedServices oldRule = mappingRules.put(fullPath, mappedServices);
                if (oldRule != null) {
                    mappedServices.getServiceUrls().addAll(oldRule.getServiceUrls());
                }
            }
        }
    }
    fireMappingRulesChanged();
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) MappedServices(io.fabric8.gateway.handlers.http.MappedServices) SimplePathTemplate(io.fabric8.zookeeper.internal.SimplePathTemplate)

Example 4 with MappedServices

use of io.fabric8.gateway.handlers.http.MappedServices in project fabric8 by jboss-fuse.

the class HttpGatewayHandler method handle.

@Override
public void handle(HttpServerRequest request) {
    long callStart = System.nanoTime();
    LOG.debug("Proxying request: {} {}", request.method(), request.uri());
    // lets map the request URI to map to the service URI and then the renaming URI
    // using mapping rules...
    Map<String, MappedServices> mappingRules = httpGateway.getMappedServices();
    try {
        if (isMappingIndexRequest(request)) {
            // lets return the JSON of all the results
            doReturnIndex(request, mappingRules);
        } else {
            doRouteRequest(mappingRules, request);
        }
        CallDetailRecord cdr = new CallDetailRecord(System.nanoTime() - callStart, null);
        httpGateway.addCallDetailRecord(cdr);
    } catch (Throwable e) {
        LOG.error("Caught: " + e, e);
        CallDetailRecord cdr = new CallDetailRecord(System.nanoTime() - callStart, new Date() + ":" + e.getMessage());
        httpGateway.addCallDetailRecord(cdr);
        request.response().setStatusCode(404);
        StringWriter buffer = new StringWriter();
        e.printStackTrace(new PrintWriter(buffer));
        request.response().setStatusMessage("Error: " + e + "\nStack Trace: " + buffer);
        request.response().close();
    }
}
Also used : StringWriter(java.io.StringWriter) Date(java.util.Date) CallDetailRecord(io.fabric8.gateway.CallDetailRecord) PrintWriter(java.io.PrintWriter)

Example 5 with MappedServices

use of io.fabric8.gateway.handlers.http.MappedServices in project fabric8 by jboss-fuse.

the class HttpGatewayConnectionTimeoutTest method startHttpGateway.

@Override
public HttpGatewayServer startHttpGateway() {
    if (restEndpointServer != null) {
        LoadBalancer loadBalancer = new RoundRobinLoadBalancer();
        ServiceDTO serviceDetails = new ServiceDTO();
        serviceDetails.setContainer("local");
        serviceDetails.setVersion("1");
        // XXX: pick a non routable address to simulate connection refused (in this case 10.0.0.0 )
        mappedServices.put("/hello/world", new MappedServices("http://10.0.0.0:8181", serviceDetails, loadBalancer, false));
    }
    HttpGatewayHandler handler = new HttpGatewayHandler(vertx, new HttpGateway() {

        @Override
        public void addMappingRuleConfiguration(HttpMappingRule mappingRule) {
        }

        @Override
        public void removeMappingRuleConfiguration(HttpMappingRule mappingRule) {
        }

        @Override
        public Map<String, MappedServices> getMappedServices() {
            return mappedServices;
        }

        @Override
        public boolean isEnableIndex() {
            return true;
        }

        @Override
        public InetSocketAddress getLocalAddress() {
            return new InetSocketAddress("0.0.0.0", 8080);
        }

        @Override
        public void addCallDetailRecord(CallDetailRecord cdr) {
        }
    });
    handler.setConnectionTimeout(1000);
    httpGatewayServer = new HttpGatewayServer(vertx, handler, null, 8080);
    httpGatewayServer.setHost("localhost");
    httpGatewayServer.init();
    return httpGatewayServer;
}
Also used : HttpGatewayServer(io.fabric8.gateway.handlers.http.HttpGatewayServer) HttpGatewayHandler(io.fabric8.gateway.handlers.http.HttpGatewayHandler) HttpMappingRule(io.fabric8.gateway.handlers.http.HttpMappingRule) InetSocketAddress(java.net.InetSocketAddress) MappedServices(io.fabric8.gateway.handlers.http.MappedServices) RoundRobinLoadBalancer(io.fabric8.gateway.loadbalancer.RoundRobinLoadBalancer) LoadBalancer(io.fabric8.gateway.loadbalancer.LoadBalancer) RoundRobinLoadBalancer(io.fabric8.gateway.loadbalancer.RoundRobinLoadBalancer) Map(java.util.Map) HttpGateway(io.fabric8.gateway.handlers.http.HttpGateway)

Aggregations

MappedServices (io.fabric8.gateway.handlers.http.MappedServices)7 HttpMappingRule (io.fabric8.gateway.handlers.http.HttpMappingRule)4 LoadBalancer (io.fabric8.gateway.loadbalancer.LoadBalancer)4 RoundRobinLoadBalancer (io.fabric8.gateway.loadbalancer.RoundRobinLoadBalancer)4 InetSocketAddress (java.net.InetSocketAddress)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 HttpGateway (io.fabric8.gateway.handlers.http.HttpGateway)3 HttpGatewayHandler (io.fabric8.gateway.handlers.http.HttpGatewayHandler)3 HttpGatewayServer (io.fabric8.gateway.handlers.http.HttpGatewayServer)3 CallDetailRecord (io.fabric8.gateway.CallDetailRecord)1 DetectingGatewayWebSocketHandler (io.fabric8.gateway.handlers.detecting.DetectingGatewayWebSocketHandler)1 SimplePathTemplate (io.fabric8.zookeeper.internal.SimplePathTemplate)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Date (java.util.Date)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 MultiMap (org.vertx.java.core.MultiMap)1