Search in sources :

Example 51 with HttpExchange

use of com.sun.net.httpserver.HttpExchange in project connect-java-library by urbanairship.

the class StreamConnectionTest method testCloseKillsConsume.

@Test
public void testCloseKillsConsume() throws Exception {
    Answer httpAnswer = new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            HttpExchange exchange = (HttpExchange) invocation.getArguments()[0];
            exchange.sendResponseHeaders(200, 0L);
            // hoping this is enough to force flushing data down the wire
            exchange.getResponseBody().write(randomAlphabetic(10000).getBytes(UTF_8));
            exchange.getResponseBody().write("\n".getBytes(UTF_8));
            exchange.getResponseBody().flush();
            return null;
        }
    };
    doAnswer(httpAnswer).when(serverHandler).handle(Matchers.<HttpExchange>any());
    final CountDownLatch latch = new CountDownLatch(1);
    Answer consumerAnswer = new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            latch.countDown();
            return null;
        }
    };
    doAnswer(consumerAnswer).when(consumer).accept(anyString());
    stream = new StreamConnection(descriptor(), http, connectionRetryStrategy, consumer, url);
    ExecutorService thread = Executors.newSingleThreadExecutor();
    Callable<Boolean> callable = new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            stream.read(Optional.<StartPosition>absent());
            return Boolean.TRUE;
        }
    };
    try {
        Future<Boolean> future = thread.submit(callable);
        // Wait till we get something from the server
        latch.await(1, TimeUnit.MINUTES);
        // Now kill the stream
        stream.close();
        // Consume future should return without issue now
        try {
            future.get(1, TimeUnit.SECONDS);
        } catch (TimeoutException e) {
            fail();
        }
    } finally {
        thread.shutdownNow();
    }
}
Also used : Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpExchange(com.sun.net.httpserver.HttpExchange) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 52 with HttpExchange

use of com.sun.net.httpserver.HttpExchange in project connect-java-library by urbanairship.

the class StreamConnectionTest method testExceptionDuringConsume.

@Test
public void testExceptionDuringConsume() throws Exception {
    Answer httpAnswer = new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            HttpExchange exchange = (HttpExchange) invocation.getArguments()[0];
            exchange.sendResponseHeaders(200, 0L);
            exchange.getResponseBody().write(randomAlphabetic(10).getBytes(UTF_8));
            exchange.getResponseBody().write("\n".getBytes(UTF_8));
            exchange.getResponseBody().flush();
            return null;
        }
    };
    doAnswer(httpAnswer).when(serverHandler).handle(Matchers.<HttpExchange>any());
    doThrow(new RuntimeException("boom")).when(consumer).accept(anyString());
    stream = new StreamConnection(descriptor(), http, connectionRetryStrategy, consumer, url);
    expectedException.expect(RuntimeException.class);
    stream.read(Optional.<StartPosition>absent());
    stream.wait(100);
}
Also used : Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpExchange(com.sun.net.httpserver.HttpExchange) Test(org.junit.Test)

Example 53 with HttpExchange

use of com.sun.net.httpserver.HttpExchange 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 54 with HttpExchange

use of com.sun.net.httpserver.HttpExchange 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 55 with HttpExchange

use of com.sun.net.httpserver.HttpExchange in project crate by crate.

the class AzureHttpHandler method handle.

@Override
public void handle(final HttpExchange exchange) throws IOException {
    final String request = exchange.getRequestMethod() + " " + exchange.getRequestURI().toString();
    if (request.startsWith("GET") || request.startsWith("HEAD") || request.startsWith("DELETE")) {
        int read = exchange.getRequestBody().read();
        assert read == -1 : "Request body should have been empty but saw [" + read + "]";
    }
    try {
        if (Regex.simpleMatch("PUT /" + container + "/*blockid=*", request)) {
            // Put Block (https://docs.microsoft.com/en-us/rest/api/storageservices/put-block)
            final Map<String, String> params = decodeQueryString(exchange.getRequestURI().toString());
            final String blockId = params.get("blockid");
            blobs.put(blockId, Streams.readFully(exchange.getRequestBody()));
            exchange.sendResponseHeaders(RestStatus.CREATED.getStatus(), -1);
        } else if (Regex.simpleMatch("PUT /" + container + "/*comp=blocklist*", request)) {
            // Put Block List (https://docs.microsoft.com/en-us/rest/api/storageservices/put-block-list)
            final String blockList = Streams.copyToString(new InputStreamReader(exchange.getRequestBody(), StandardCharsets.UTF_8));
            final List<String> blockIds = Arrays.stream(blockList.split("<Latest>")).filter(line -> line.contains("</Latest>")).map(line -> line.substring(0, line.indexOf("</Latest>"))).collect(Collectors.toList());
            final ByteArrayOutputStream blob = new ByteArrayOutputStream();
            for (String blockId : blockIds) {
                BytesReference block = blobs.remove(blockId);
                assert block != null;
                block.writeTo(blob);
            }
            blobs.put(exchange.getRequestURI().getPath(), new BytesArray(blob.toByteArray()));
            exchange.sendResponseHeaders(RestStatus.CREATED.getStatus(), -1);
        } else if (Regex.simpleMatch("PUT /" + container + "/*", request)) {
            // PUT Blob (see https://docs.microsoft.com/en-us/rest/api/storageservices/put-blob)
            final String ifNoneMatch = exchange.getRequestHeaders().getFirst("If-None-Match");
            if ("*".equals(ifNoneMatch)) {
                if (blobs.putIfAbsent(exchange.getRequestURI().getPath(), Streams.readFully(exchange.getRequestBody())) != null) {
                    sendError(exchange, RestStatus.CONFLICT);
                    return;
                }
            } else {
                blobs.put(exchange.getRequestURI().getPath(), Streams.readFully(exchange.getRequestBody()));
            }
            exchange.sendResponseHeaders(RestStatus.CREATED.getStatus(), -1);
        } else if (Regex.simpleMatch("HEAD /" + container + "/*", request)) {
            // Get Blob Properties (see https://docs.microsoft.com/en-us/rest/api/storageservices/get-blob-properties)
            final BytesReference blob = blobs.get(exchange.getRequestURI().getPath());
            if (blob == null) {
                sendError(exchange, RestStatus.NOT_FOUND);
                return;
            }
            exchange.getResponseHeaders().add("x-ms-blob-content-length", String.valueOf(blob.length()));
            exchange.getResponseHeaders().add("x-ms-blob-type", "blockblob");
            exchange.sendResponseHeaders(RestStatus.OK.getStatus(), -1);
        } else if (Regex.simpleMatch("GET /" + container + "/*", request)) {
            // GET Object (https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectGET.html)
            final BytesReference blob = blobs.get(exchange.getRequestURI().getPath());
            if (blob == null) {
                sendError(exchange, RestStatus.NOT_FOUND);
                return;
            }
            // see Constants.HeaderConstants.STORAGE_RANGE_HEADER
            final String range = exchange.getRequestHeaders().getFirst("x-ms-range");
            final Matcher matcher = Pattern.compile("^bytes=([0-9]+)-([0-9]+)$").matcher(range);
            if (matcher.matches() == false) {
                throw new AssertionError("Range header does not match expected format: " + range);
            }
            final int start = Integer.parseInt(matcher.group(1));
            final int length = Integer.parseInt(matcher.group(2)) - start + 1;
            exchange.getResponseHeaders().add("Content-Type", "application/octet-stream");
            exchange.getResponseHeaders().add("x-ms-blob-content-length", String.valueOf(length));
            exchange.getResponseHeaders().add("x-ms-blob-type", "blockblob");
            exchange.sendResponseHeaders(RestStatus.OK.getStatus(), length);
            exchange.getResponseBody().write(blob.toBytesRef().bytes, start, length);
        } else if (Regex.simpleMatch("DELETE /" + container + "/*", request)) {
            // Delete Blob (https://docs.microsoft.com/en-us/rest/api/storageservices/delete-blob)
            blobs.entrySet().removeIf(blob -> blob.getKey().startsWith(exchange.getRequestURI().getPath()));
            exchange.sendResponseHeaders(RestStatus.ACCEPTED.getStatus(), -1);
        } else if (Regex.simpleMatch("GET /" + container + "?restype=container&comp=list*", request)) {
            // List Blobs (https://docs.microsoft.com/en-us/rest/api/storageservices/list-blobs)
            final Map<String, String> params = decodeQueryString(exchange.getRequestURI().toString());
            final StringBuilder list = new StringBuilder();
            list.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            list.append("<EnumerationResults>");
            final String prefix = params.get("prefix");
            final Set<String> blobPrefixes = new HashSet<>();
            final String delimiter = params.get("delimiter");
            if (delimiter != null) {
                list.append("<Delimiter>").append(delimiter).append("</Delimiter>");
            }
            list.append("<Blobs>");
            for (Map.Entry<String, BytesReference> blob : blobs.entrySet()) {
                if (prefix != null && blob.getKey().startsWith("/" + container + "/" + prefix) == false) {
                    continue;
                }
                String blobPath = blob.getKey().replace("/" + container + "/", "");
                if (delimiter != null) {
                    int fromIndex = (prefix != null ? prefix.length() : 0);
                    int delimiterPosition = blobPath.indexOf(delimiter, fromIndex);
                    if (delimiterPosition > 0) {
                        blobPrefixes.add(blobPath.substring(0, delimiterPosition) + delimiter);
                        continue;
                    }
                }
                list.append("<Blob><Name>").append(blobPath).append("</Name>");
                list.append("<Properties><Content-Length>").append(blob.getValue().length()).append("</Content-Length>");
                list.append("<BlobType>BlockBlob</BlobType></Properties></Blob>");
            }
            if (blobPrefixes.isEmpty() == false) {
                blobPrefixes.forEach(p -> list.append("<BlobPrefix><Name>").append(p).append("</Name></BlobPrefix>"));
            }
            list.append("</Blobs>");
            list.append("</EnumerationResults>");
            byte[] response = list.toString().getBytes(StandardCharsets.UTF_8);
            exchange.getResponseHeaders().add("Content-Type", "application/xml");
            exchange.sendResponseHeaders(RestStatus.OK.getStatus(), response.length);
            exchange.getResponseBody().write(response);
        } else {
            sendError(exchange, RestStatus.BAD_REQUEST);
        }
    } finally {
        exchange.close();
    }
}
Also used : Arrays(java.util.Arrays) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) IOException(java.io.IOException) HashMap(java.util.HashMap) BytesReference(org.elasticsearch.common.bytes.BytesReference) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) BytesArray(org.elasticsearch.common.bytes.BytesArray) HashSet(java.util.HashSet) Objects(java.util.Objects) List(java.util.List) Matcher(java.util.regex.Matcher) HttpHandler(com.sun.net.httpserver.HttpHandler) RestStatus(org.elasticsearch.rest.RestStatus) Map(java.util.Map) Headers(com.sun.net.httpserver.Headers) QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) Regex(org.elasticsearch.common.regex.Regex) HttpExchange(com.sun.net.httpserver.HttpExchange) Streams(org.elasticsearch.common.io.Streams) Pattern(java.util.regex.Pattern) BytesReference(org.elasticsearch.common.bytes.BytesReference) BytesArray(org.elasticsearch.common.bytes.BytesArray) InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) ByteArrayOutputStream(java.io.ByteArrayOutputStream) List(java.util.List) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

HttpExchange (com.sun.net.httpserver.HttpExchange)56 Test (org.junit.Test)36 IOException (java.io.IOException)25 HttpHandler (com.sun.net.httpserver.HttpHandler)22 InetSocketAddress (java.net.InetSocketAddress)19 OutputStream (java.io.OutputStream)18 Mockito.doAnswer (org.mockito.Mockito.doAnswer)16 InvocationOnMock (org.mockito.invocation.InvocationOnMock)16 Answer (org.mockito.stubbing.Answer)16 CountDownLatch (java.util.concurrent.CountDownLatch)12 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 HttpServer (com.sun.net.httpserver.HttpServer)11 Matchers.anyString (org.mockito.Matchers.anyString)11 Headers (com.sun.net.httpserver.Headers)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 StreamQueryDescriptor (com.urbanairship.connect.client.model.StreamQueryDescriptor)9 InputStream (java.io.InputStream)9 JsonObject (com.google.gson.JsonObject)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4