Search in sources :

Example 26 with Server

use of io.fabric8.insight.metrics.model.Server in project fabric8 by jboss-fuse.

the class ExampleCamelClusterTest method testRegistryEntries.

@Test
public void testRegistryEntries() throws Exception {
    System.out.println(executeCommand("fabric:create -n --wait-for-provisioning root"));
    // System.out.println(executeCommand("shell:info"));
    // System.out.println(executeCommand("fabric:info"));
    // System.out.println(executeCommand("fabric:profile-list"));
    ServiceProxy<FabricService> fabricProxy = ServiceProxy.createServiceProxy(bundleContext, FabricService.class);
    try {
        FabricService fabricService = fabricProxy.getService();
        CuratorFramework curator = fabricService.adapt(CuratorFramework.class);
        Set<ContainerProxy> containers = ContainerBuilder.create(fabricProxy, 3).withName("fabric-camel").assertProvisioningResult().build();
        try {
            // We will use the first container as a client and the rest as servers.
            LinkedList<Container> containerList = new LinkedList<Container>(containers);
            Container client = containerList.removeLast();
            LinkedList<Container> servers = new LinkedList<Container>(containerList);
            for (Container c : servers) {
                Profile p1 = c.getVersion().getRequiredProfile("feature-camel");
                Profile p2 = c.getVersion().getRequiredProfile("example-camel-cluster-cluster.server");
                c.setProfiles(new Profile[] { p1, p2 });
            }
            Provision.provisioningSuccess(servers, PROVISION_TIMEOUT);
            Profile p1 = client.getVersion().getRequiredProfile("feature-camel");
            Profile p2 = client.getVersion().getRequiredProfile("example-camel-cluster-cluster.client");
            client.setProfiles(new Profile[] { p1, p2 });
            Provision.provisioningSuccess(Arrays.asList(new Container[] { client }), PROVISION_TIMEOUT);
            System.out.println(executeCommand("fabric:container-list"));
            System.out.println(executeCommand("fabric:profile-display --overlay fabric-camel-server"));
            // Check that the entries have been properly propagated.
            Assert.assertNotNull(exists(curator, "/fabric/registry/camel/endpoints"));
            Assert.assertEquals(1, getChildren(curator, "/fabric/registry/camel/endpoints").size());
            Assert.assertTrue(Provision.waitForCondition(Arrays.asList(client), new ContainerCondition() {

                @Override
                public Boolean checkConditionOnContainer(final Container c) {
                    return getCompletedExchangesCount(c) > 0;
                }
            }, 60000L));
            // We want to kill all but one server, so we take out the first and keep it to the end.
            Container lastActiveServerContainer = servers.removeLast();
            for (Container c : servers) {
                try {
                    c.destroy(true);
                } catch (Exception ex) {
                // ignore.
                }
                // Get the number of exchanges completed before we kill the server.
                final int completed = getCompletedExchangesCount(client);
                // Ensure that we still have messages flowing
                Assert.assertTrue(Provision.waitForCondition(Arrays.asList(client), new ContainerCondition() {

                    @Override
                    public Boolean checkConditionOnContainer(final Container c) {
                        return getCompletedExchangesCount(c) > completed + 3;
                    }
                }, 60000L));
            }
            System.out.println(new AnsiString(executeCommand("fabric:container-connect -u admin -p admin " + client.getId() + " camel:route-info fabric-client")).getPlain().toString());
        } finally {
            ContainerBuilder.destroy(containers);
        }
    } finally {
        fabricProxy.close();
    }
}
Also used : AnsiString(org.fusesource.jansi.AnsiString) LinkedList(java.util.LinkedList) Profile(io.fabric8.api.Profile) ContainerCondition(io.fabric8.itests.paxexam.support.ContainerCondition) CuratorFramework(org.apache.curator.framework.CuratorFramework) Container(io.fabric8.api.Container) FabricService(io.fabric8.api.FabricService) ContainerProxy(io.fabric8.itests.paxexam.support.ContainerProxy) Test(org.junit.Test)

Example 27 with Server

use of io.fabric8.insight.metrics.model.Server in project fabric8 by jboss-fuse.

the class ProxyServlet method executeProxyRequest.

/**
 * Executes the {@link HttpMethod} passed in and sends the proxy response
 * back to the client via the given {@link javax.servlet.http.HttpServletResponse}
 *
 * @param proxyDetails
 * @param httpMethodProxyRequest An object representing the proxy request to be made
 * @param httpServletResponse    An object by which we can send the proxied
 *                               response back to the client
 * @throws java.io.IOException            Can be thrown by the {@link HttpClient}.executeMethod
 * @throws javax.servlet.ServletException Can be thrown to indicate that another error has occurred
 */
private void executeProxyRequest(ProxyDetails proxyDetails, HttpMethod httpMethodProxyRequest, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
    httpMethodProxyRequest.setDoAuthentication(false);
    httpMethodProxyRequest.setFollowRedirects(false);
    // Create a default HttpClient
    HttpClient httpClient = proxyDetails.createHttpClient(httpMethodProxyRequest);
    // Execute the request
    int intProxyResponseCode = httpClient.executeMethod(httpMethodProxyRequest);
    // Hooray for open source software
    if (intProxyResponseCode >= HttpServletResponse.SC_MULTIPLE_CHOICES && /* 300 */
    intProxyResponseCode < HttpServletResponse.SC_NOT_MODIFIED) /* 304 */
    {
        String stringStatusCode = Integer.toString(intProxyResponseCode);
        String stringLocation = httpMethodProxyRequest.getResponseHeader(STRING_LOCATION_HEADER).getValue();
        if (stringLocation == null) {
            throw new ServletException("Received status code: " + stringStatusCode + " but no " + STRING_LOCATION_HEADER + " header was found in the response");
        }
        // Modify the redirect to go to this proxy servlet rather that the proxied host
        String stringMyHostName = httpServletRequest.getServerName();
        if (httpServletRequest.getServerPort() != 80) {
            stringMyHostName += ":" + httpServletRequest.getServerPort();
        }
        stringMyHostName += httpServletRequest.getContextPath();
        httpServletResponse.sendRedirect(stringLocation.replace(proxyDetails.getProxyHostAndPort() + proxyDetails.getProxyPath(), stringMyHostName));
        return;
    } else if (intProxyResponseCode == HttpServletResponse.SC_NOT_MODIFIED) {
        // 304 needs special handling.  See:
        // http://www.ics.uci.edu/pub/ietf/http/rfc1945.html#Code304
        // We get a 304 whenever passed an 'If-Modified-Since'
        // header and the data on disk has not changed; server
        // responds w/ a 304 saying I'm not going to send the
        // body because the file has not changed.
        httpServletResponse.setIntHeader(STRING_CONTENT_LENGTH_HEADER_NAME, 0);
        httpServletResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        return;
    }
    // Pass the response code back to the client
    httpServletResponse.setStatus(intProxyResponseCode);
    // Pass response headers back to the client
    Header[] headerArrayResponse = httpMethodProxyRequest.getResponseHeaders();
    for (Header header : headerArrayResponse) {
        if (!ProxySupport.isHopByHopHeader(header.getName())) {
            if (ProxySupport.isSetCookieHeader(header)) {
                HttpProxyRule proxyRule = proxyDetails.getProxyRule();
                String setCookie = ProxySupport.replaceCookieAttributes(header.getValue(), proxyRule.getCookiePath(), proxyRule.getCookieDomain());
                httpServletResponse.setHeader(header.getName(), setCookie);
            } else {
                httpServletResponse.setHeader(header.getName(), header.getValue());
            }
        }
    }
    // check if we got data, that is either the Content-Length > 0
    // or the response code != 204
    int code = httpMethodProxyRequest.getStatusCode();
    boolean noData = code == HttpStatus.SC_NO_CONTENT;
    if (!noData) {
        String length = httpServletRequest.getHeader(STRING_CONTENT_LENGTH_HEADER_NAME);
        if (length != null && "0".equals(length.trim())) {
            noData = true;
        }
    }
    LOG.trace("Response has data? {}", !noData);
    if (!noData) {
        // Send the content to the client
        InputStream inputStreamProxyResponse = httpMethodProxyRequest.getResponseBodyAsStream();
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStreamProxyResponse);
        OutputStream outputStreamClientResponse = httpServletResponse.getOutputStream();
        int intNextByte;
        while ((intNextByte = bufferedInputStream.read()) != -1) {
            outputStreamClientResponse.write(intNextByte);
        }
    }
}
Also used : ServletException(javax.servlet.ServletException) Header(org.apache.commons.httpclient.Header) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) HttpClient(org.apache.commons.httpclient.HttpClient) OutputStream(java.io.OutputStream) HttpProxyRule(io.fabric8.gateway.model.HttpProxyRule)

Example 28 with Server

use of io.fabric8.insight.metrics.model.Server in project fabric8 by jboss-fuse.

the class ExampleCxfProfileLongTest method testExample.

@Test
public void testExample() throws Exception {
    System.out.println("creating the cxf-server container.");
    ServiceProxy<FabricService> fabricProxy = ServiceProxy.createServiceProxy(bundleContext, FabricService.class);
    try {
        Set<ContainerProxy> containers = ContainerBuilder.create(fabricProxy).withName("child").withProfiles("example-cxf-cxf.server").assertProvisioningResult().build();
        try {
            assertTrue("We should create the cxf-server container.", containers.size() == 1);
            System.out.println("created the cxf-server container.");
            // install bundle of CXF
            Thread.sleep(2000);
            System.out.println(executeCommand("fabric:cluster-list"));
            // install bundle of CXF
            Thread.sleep(2000);
            // calling the client here
            System.out.println("install the cxf client demo in root container");
            // This test need to take sometime to download the cxf feature related bundles
            System.out.println(executeCommand("features:install fabric-cxf", 600000, false));
            String projectVersion = System.getProperty("fabricitest.version");
            // install bundle of CXF demo client
            System.out.println(executeCommand("osgi:install -s mvn:io.fabric8.examples/fabric-cxf-demo-common/" + projectVersion));
            System.out.println(executeCommand("osgi:install -s mvn:io.fabric8.examples/fabric-cxf-demo-client/" + projectVersion));
            System.out.println(executeCommand("osgi:list"));
            System.out.println("invoking the web service");
            Hello proxy = ServiceLocator.awaitService(bundleContext, Hello.class);
            assertNotNull(proxy);
            String result1 = proxy.sayHello();
            String result2 = proxy.sayHello();
            assertNotSame("We should get the two different result", result1, result2);
        } finally {
            ContainerBuilder.destroy(containers);
        }
    } finally {
        fabricProxy.close();
    }
}
Also used : Hello(io.fabric8.demo.cxf.Hello) FabricService(io.fabric8.api.FabricService) ContainerProxy(io.fabric8.itests.paxexam.support.ContainerProxy) Test(org.junit.Test)

Example 29 with Server

use of io.fabric8.insight.metrics.model.Server in project fabric8 by jboss-fuse.

the class FabricHTTPGateway method updateConfiguration.

private void updateConfiguration(Map<String, ?> configuration) throws Exception {
    configurer.configure(configuration, this);
    Vertx vertx = getVertx();
    handler = new HttpGatewayHandler(vertx, this);
    handler.setAddMissingTrailingSlashes(addMissingTrailingSlashes);
    handler.setConnectionTimeout(connectionTimeout);
    handler.setRequestTimeout(requestTimeout);
    websocketHandler.setPathPrefix(websocketGatewayPrefix);
    server = new HttpGatewayServer(vertx, handler, enableWebSocketGateway ? websocketHandler : null, port);
    server.init();
}
Also used : HttpGatewayServer(io.fabric8.gateway.handlers.http.HttpGatewayServer) HttpGatewayHandler(io.fabric8.gateway.handlers.http.HttpGatewayHandler) Vertx(org.vertx.java.core.Vertx)

Example 30 with Server

use of io.fabric8.insight.metrics.model.Server in project fabric8 by jboss-fuse.

the class CreateChildContainerTest method testCreateChildContainerWithCustomZKServerPort.

@Test
public void testCreateChildContainerWithCustomZKServerPort() throws Exception {
    System.err.println(CommandSupport.executeCommand("fabric:create --force --clean -n --wait-for-provisioning --zookeeper-server-port 2345"));
    System.err.println(CommandSupport.executeCommand("fabric:profile-create --parent default p1"));
    System.err.println(CommandSupport.executeCommand("fabric:profile-edit --feature fabric-zookeeper-commands p1"));
    BundleContext moduleContext = ServiceLocator.getSystemContext();
    ServiceProxy<FabricService> fabricProxy = ServiceProxy.createServiceProxy(moduleContext, FabricService.class);
    try {
        FabricService fabricService = fabricProxy.getService();
        Set<Container> containers = ContainerBuilder.child(1).withName("smoke_child_b").withProfiles("p1").build(fabricService);
        ProvisionSupport.provisioningSuccess(containers, ProvisionSupport.PROVISION_TIMEOUT);
        try {
            Container child = containers.iterator().next();
            String ensembleUrl = CommandSupport.executeCommand("fabric:container-connect -u admin -p admin " + child.getId() + " zk:get /fabric/configs/ensemble/url");
            Assert.assertTrue("Child should use custom ZK server port, but was: " + ensembleUrl, ensembleUrl.contains("${zk:root/ip}:2345"));
        } finally {
            ContainerBuilder.stop(fabricService, containers);
        }
    } finally {
        fabricProxy.close();
    }
}
Also used : Container(io.fabric8.api.Container) FabricService(io.fabric8.api.FabricService) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)20 Test (org.junit.Test)19 IOException (java.io.IOException)18 File (java.io.File)9 Map (java.util.Map)9 ClientInvokerImpl (io.fabric8.dosgi.tcp.ClientInvokerImpl)8 ServerInvokerImpl (io.fabric8.dosgi.tcp.ServerInvokerImpl)8 ArrayList (java.util.ArrayList)8 Profile (io.fabric8.api.Profile)7 ServerInvoker (io.fabric8.dosgi.io.ServerInvoker)7 InvocationHandler (java.lang.reflect.InvocationHandler)7 DispatchQueue (org.fusesource.hawtdispatch.DispatchQueue)7 Container (io.fabric8.api.Container)6 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)6 Version (io.fabric8.api.Version)5 CuratorFramework (org.apache.curator.framework.CuratorFramework)5 FabricException (io.fabric8.api.FabricException)4 RuntimeProperties (io.fabric8.api.RuntimeProperties)4 InputStream (java.io.InputStream)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3