Search in sources :

Example 26 with HttpHandler

use of com.sun.net.httpserver.HttpHandler in project metron by apache.

the class MockDGAModel method start.

public static void start(int port) throws IOException {
    // Create an HTTP server listening at port
    URI uri = UriBuilder.fromUri("http://localhost/").port(port).build();
    server = HttpServer.create(new InetSocketAddress(uri.getPort()), 0);
    HttpHandler handler = RuntimeDelegate.getInstance().createEndpoint(new ApplicationConfig(), HttpHandler.class);
    server.createContext(uri.getPath(), handler);
    server.start();
}
Also used : HttpHandler(com.sun.net.httpserver.HttpHandler) InetSocketAddress(java.net.InetSocketAddress) URI(java.net.URI)

Example 27 with HttpHandler

use of com.sun.net.httpserver.HttpHandler in project azure-tools-for-java by Microsoft.

the class JobViewDummyHttpServer method initlize.

public static synchronized void initlize() {
    if (isEnabled) {
        return;
    }
    try {
        server = HttpServer.create(new InetSocketAddress(PORT), 10);
        server.createContext("/clusters/", new HttpHandler() {

            @Override
            public void handle(final HttpExchange httpExchange) throws IOException {
                requestDetail = RequestDetail.getRequestDetail(httpExchange.getRequestURI());
                if (requestDetail == null) {
                    return;
                }
                IClusterDetail clusterDetail = requestDetail.getClusterDetail();
                final String queryUrl = requestDetail.getQueryUrl();
                if (requestDetail.getApiType() == HttpRequestType.YarnHistory) {
                    TaskExecutor.submit(new YarnHistoryTask(clusterDetail, queryUrl, new HttpFutureCallback(httpExchange) {

                        @Override
                        public void onSuccess(String str) {
                            httpExchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
                            try {
                                // work around of get job result
                                // TODO: get job result by REST API
                                Document doc = Jsoup.parse(str);
                                Elements contentElements = doc.getElementsByClass("content");
                                if (contentElements.size() == 1) {
                                    Elements elements = contentElements.get(0).getElementsByTag("pre");
                                    if (elements.size() == 1) {
                                        str = elements.get(0).html();
                                    }
                                }
                                httpExchange.sendResponseHeaders(200, str.length());
                                OutputStream stream = httpExchange.getResponseBody();
                                stream.write(str.getBytes());
                                stream.close();
                            } catch (IOException e) {
                                int a = 1;
                            // LOGGER.error("Get job history error", e);
                            }
                        }
                    }));
                } else if (requestDetail.getApiType() == HttpRequestType.LivyBatchesRest) {
                    TaskExecutor.submit(new LivyTask(clusterDetail, queryUrl, new HttpFutureCallback(httpExchange) {

                        @Override
                        public void onSuccess(String str) {
                            httpExchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
                            try {
                                String applicationId = requestDetail.getProperty("applicationId");
                                if (applicationId != null) {
                                    str = JobUtils.getJobInformation(str, applicationId);
                                }
                                httpExchange.sendResponseHeaders(200, str.length());
                                OutputStream stream = httpExchange.getResponseBody();
                                stream.write(str.getBytes());
                                stream.close();
                            } catch (IOException e) {
                            // LOGGER.error("Get job history error", e);
                            }
                        }
                    }));
                } else if (requestDetail.getApiType() == HttpRequestType.MultiTask) {
                    TaskExecutor.submit(new MultiRestTask(clusterDetail, requestDetail.getQueryUrls(), new MultiHttpFutureCallback(httpExchange) {

                        public void onSuccess(List<String> strs) {
                            httpExchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
                            try {
                                String str = tasksDetailsConvert(strs);
                                httpExchange.sendResponseHeaders(200, str.length());
                                OutputStream stream = httpExchange.getResponseBody();
                                stream.write(str.getBytes());
                                stream.close();
                            } catch (IOException e) {
                            // LOGGER.error("Get job history error", e);
                            }
                        }
                    }));
                } else {
                    TaskExecutor.submit(new RestTask(clusterDetail, queryUrl, new HttpFutureCallback(httpExchange) {

                        @Override
                        public void onSuccess(@NotNull String str) {
                            httpExchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
                            try {
                                httpExchange.sendResponseHeaders(200, str.length());
                                OutputStream stream = httpExchange.getResponseBody();
                                stream.write(str.getBytes());
                                stream.close();
                            } catch (IOException e) {
                            // LOGGER.error("Get job history error", e);
                            }
                        }
                    }));
                }
            }
        });
        executorService = Executors.newFixedThreadPool(NO_OF_THREADS);
        server.setExecutor(executorService);
        server.start();
        isEnabled = true;
    } catch (IOException e) {
    // LOGGER.error("Get job history error", e);
    // DefaultLoader.getUIHelper().showError(e.getClass().getName(), e.getMessage());
    }
}
Also used : HttpHandler(com.sun.net.httpserver.HttpHandler) InetSocketAddress(java.net.InetSocketAddress) OutputStream(java.io.OutputStream) HttpExchange(com.sun.net.httpserver.HttpExchange) IOException(java.io.IOException) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) IClusterDetail(com.microsoft.azure.hdinsight.sdk.cluster.IClusterDetail) MultiHttpFutureCallback(com.microsoft.azure.hdinsight.common.MultiHttpFutureCallback) MultiHttpFutureCallback(com.microsoft.azure.hdinsight.common.MultiHttpFutureCallback) HttpFutureCallback(com.microsoft.azure.hdinsight.common.HttpFutureCallback)

Example 28 with HttpHandler

use of com.sun.net.httpserver.HttpHandler in project ceylon by eclipse.

the class CeylonDocToolTests method externalLinksToRemoteRepoWithoutModuleNamePattern.

@Test
public void externalLinksToRemoteRepoWithoutModuleNamePattern() throws Exception {
    Assume.assumeTrue(CompilerTests.allowNetworkTests());
    HttpServer stubServer = HttpServer.create(new InetSocketAddress(0), 1);
    stubServer.createContext("/repo", new HttpHandler() {

        @Override
        public void handle(HttpExchange httpExchange) throws IOException {
            if (httpExchange.getRequestMethod().equals("HEAD")) {
                httpExchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, -1);
            } else {
                httpExchange.sendResponseHeaders(HttpURLConnection.HTTP_NOT_IMPLEMENTED, -1);
            }
            httpExchange.close();
        }
    });
    stubServer.start();
    try {
        String repoUrl = "http://localhost:" + stubServer.getAddress().getPort() + "/repo";
        externalLinks(repoUrl, "file://not-existing-dir", "https://not-existing-url", repoUrl);
    } finally {
        stubServer.stop(0);
    }
}
Also used : HttpHandler(com.sun.net.httpserver.HttpHandler) InetSocketAddress(java.net.InetSocketAddress) HttpServer(com.sun.net.httpserver.HttpServer) HttpExchange(com.sun.net.httpserver.HttpExchange) IOException(java.io.IOException) Test(org.junit.Test)

Example 29 with HttpHandler

use of com.sun.net.httpserver.HttpHandler in project ceylon-compiler by ceylon.

the class CeylonDocToolTests method externalLinksToRemoteRepoWithoutModuleNamePattern.

@Test
public void externalLinksToRemoteRepoWithoutModuleNamePattern() throws Exception {
    HttpServer stubServer = HttpServer.create(new InetSocketAddress(0), 1);
    stubServer.createContext("/repo", new HttpHandler() {

        @Override
        public void handle(HttpExchange httpExchange) throws IOException {
            if (httpExchange.getRequestMethod().equals("HEAD")) {
                httpExchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, -1);
            } else {
                httpExchange.sendResponseHeaders(HttpURLConnection.HTTP_NOT_IMPLEMENTED, -1);
            }
            httpExchange.close();
        }
    });
    stubServer.start();
    try {
        String repoUrl = "http://localhost:" + stubServer.getAddress().getPort() + "/repo";
        externalLinks(repoUrl, "file://not-existing-dir", "https://not-existing-url", repoUrl);
    } finally {
        stubServer.stop(0);
    }
}
Also used : HttpHandler(com.sun.net.httpserver.HttpHandler) InetSocketAddress(java.net.InetSocketAddress) HttpServer(com.sun.net.httpserver.HttpServer) HttpExchange(com.sun.net.httpserver.HttpExchange) IOException(java.io.IOException) Test(org.junit.Test)

Example 30 with HttpHandler

use of com.sun.net.httpserver.HttpHandler in project spf4j by zolyfarkas.

the class TcpServerTest method createHttpServer.

@BeforeClass
public static void createHttpServer() throws IOException {
    server = HttpServer.create(new InetSocketAddress(TEST_PORT), 0);
    server.createContext("/", new HttpHandler() {

        @Override
        public void handle(final HttpExchange he) throws IOException {
            Headers respHeaders = he.getResponseHeaders();
            respHeaders.add("testheader", "testValue");
            he.sendResponseHeaders(200, 0);
            OutputStream responseBody = he.getResponseBody();
            responseBody.write("Some Body".getBytes(StandardCharsets.UTF_8));
            responseBody.close();
        }
    });
    server.start();
}
Also used : HttpHandler(com.sun.net.httpserver.HttpHandler) InetSocketAddress(java.net.InetSocketAddress) Headers(com.sun.net.httpserver.Headers) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) HttpExchange(com.sun.net.httpserver.HttpExchange) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Aggregations

HttpHandler (com.sun.net.httpserver.HttpHandler)31 InetSocketAddress (java.net.InetSocketAddress)25 HttpExchange (com.sun.net.httpserver.HttpExchange)21 IOException (java.io.IOException)17 HttpServer (com.sun.net.httpserver.HttpServer)12 OutputStream (java.io.OutputStream)12 Test (org.junit.Test)5 Headers (com.sun.net.httpserver.Headers)3 HttpContext (com.sun.net.httpserver.HttpContext)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 BasicAuthenticator (com.sun.net.httpserver.BasicAuthenticator)2 InputStream (java.io.InputStream)2 URI (java.net.URI)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 ExecutorService (java.util.concurrent.ExecutorService)2 UnsupportedTypeException (co.cask.cdap.api.data.schema.UnsupportedTypeException)1 CloseableIterator (co.cask.cdap.api.dataset.lib.CloseableIterator)1