Search in sources :

Example 6 with HttpExchange

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

the class GitHttpRepository method expectCloneSomething.

public void expectCloneSomething() {
    server.expect(server.get(backingRepo.getName() + "/info/refs", getRefsAction()));
    server.expect(server.post(backingRepo.getName() + "/git-upload-pack", new ErroringAction<HttpExchange>() {

        @Override
        protected void doExecute(HttpExchange httpExchange) throws Exception {
            httpExchange.getResponseHeaders().add("content-type", "application/x-git-upload-pack-result");
            httpExchange.sendResponseHeaders(200, 0);
            ProcessBuilder builder = new ProcessBuilder();
            final Process process = builder.command("git", "upload-pack", "--stateless-rpc", backingRepo.getWorkTree().getAbsolutePath()).redirectErrorStream(true).start();
            InputStream instream = new GZIPInputStream(httpExchange.getRequestBody());
            ByteArrayOutputStream requestContent = new ByteArrayOutputStream();
            IOUtils.copy(instream, requestContent);
            byte[] bytes = requestContent.toByteArray();
            process.getOutputStream().write(bytes);
            process.getOutputStream().flush();
            IOUtils.copy(process.getInputStream(), httpExchange.getResponseBody());
            int result = process.waitFor();
            if (result != 0) {
                throw new RuntimeException("Failed to run git upload-pack");
            }
        }
    }));
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ErroringAction(org.gradle.internal.ErroringAction) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) HttpExchange(com.sun.net.httpserver.HttpExchange) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 7 with HttpExchange

use of com.sun.net.httpserver.HttpExchange in project metrics by dropwizard.

the class InstrumentedHttpClientsTest method registersExpectedExceptionMetrics.

@Test
public void registersExpectedExceptionMetrics() throws Exception {
    HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0);
    final HttpGet get = new HttpGet("http://localhost:" + httpServer.getAddress().getPort() + "/");
    final String requestMetricName = "request";
    final String exceptionMetricName = "exception";
    httpServer.createContext("/", HttpExchange::close);
    httpServer.start();
    when(metricNameStrategy.getNameFor(any(), any(HttpRequest.class))).thenReturn(requestMetricName);
    when(metricNameStrategy.getNameFor(any(), any(Exception.class))).thenReturn(exceptionMetricName);
    try {
        client.execute(get);
        fail();
    } catch (NoHttpResponseException expected) {
        assertThat(metricRegistry.getMeters()).containsKey("exception");
    } finally {
        httpServer.stop(0);
    }
}
Also used : HttpRequest(org.apache.http.HttpRequest) NoHttpResponseException(org.apache.http.NoHttpResponseException) InetSocketAddress(java.net.InetSocketAddress) HttpGet(org.apache.http.client.methods.HttpGet) HttpServer(com.sun.net.httpserver.HttpServer) HttpExchange(com.sun.net.httpserver.HttpExchange) NoHttpResponseException(org.apache.http.NoHttpResponseException) Test(org.junit.Test)

Example 8 with HttpExchange

use of com.sun.net.httpserver.HttpExchange in project tomee by apache.

the class HttpConnectionTest method init.

@Before
public void init() throws Exception {
    server = HttpServer.create(new InetSocketAddress(0), 5);
    server.createContext("/e", new HttpHandler() {

        @Override
        public void handle(final HttpExchange exchange) throws IOException {
            exchange.getResponseHeaders().set("Content-Type", "text/plain");
            exchange.sendResponseHeaders(200, 0);
            final OutputStream responseBody = exchange.getResponseBody();
            responseBody.write("secure page".getBytes());
            final String query = exchange.getRequestURI().getQuery();
            if (query != null) {
                responseBody.write(query.getBytes());
            }
            final String authorization = exchange.getRequestHeaders().getFirst("Authorization");
            if (authorization != null) {
                responseBody.write(authorization.getBytes("UTF-8"));
            }
            final String authorization2 = exchange.getRequestHeaders().getFirst("AltAuthorization");
            if (authorization2 != null) {
                responseBody.write(("alt" + authorization2).getBytes("UTF-8"));
            }
            responseBody.close();
        }
    });
    server.start();
}
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) Before(org.junit.Before)

Example 9 with HttpExchange

use of com.sun.net.httpserver.HttpExchange in project pinot by linkedin.

the class ServerTableSizeReaderTest method createHandler.

private HttpHandler createHandler(final int status, final TableSizeInfo tableSize, final int sleepTimeMs) {
    return new HttpHandler() {

        @Override
        public void handle(HttpExchange httpExchange) throws IOException {
            if (sleepTimeMs > 0) {
                try {
                    Thread.sleep(sleepTimeMs);
                } catch (InterruptedException e) {
                    LOGGER.info("Handler interrupted during sleep");
                }
            }
            String json = new ObjectMapper().writeValueAsString(tableSize);
            httpExchange.sendResponseHeaders(status, json.length());
            OutputStream responseBody = httpExchange.getResponseBody();
            responseBody.write(json.getBytes());
            responseBody.close();
        }
    };
}
Also used : HttpHandler(com.sun.net.httpserver.HttpHandler) OutputStream(java.io.OutputStream) HttpExchange(com.sun.net.httpserver.HttpExchange) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 10 with HttpExchange

use of com.sun.net.httpserver.HttpExchange in project pinot by linkedin.

the class TableSizeReaderTest method createHandler.

private HttpHandler createHandler(final int status, final List<SegmentSizeInfo> segmentSizes, final int sleepTimeMs) {
    return new HttpHandler() {

        @Override
        public void handle(HttpExchange httpExchange) throws IOException {
            if (sleepTimeMs > 0) {
                try {
                    Thread.sleep(sleepTimeMs);
                } catch (InterruptedException e) {
                    LOGGER.info("Handler interrupted during sleep");
                }
            }
            TableSizeInfo tableInfo = new TableSizeInfo("myTable", 0);
            tableInfo.segments = segmentSizes;
            for (SegmentSizeInfo segmentSize : segmentSizes) {
                tableInfo.diskSizeInBytes += segmentSize.diskSizeInBytes;
            }
            String json = new ObjectMapper().writeValueAsString(tableInfo);
            httpExchange.sendResponseHeaders(status, json.length());
            OutputStream responseBody = httpExchange.getResponseBody();
            responseBody.write(json.getBytes());
            responseBody.close();
        }
    };
}
Also used : HttpHandler(com.sun.net.httpserver.HttpHandler) OutputStream(java.io.OutputStream) HttpExchange(com.sun.net.httpserver.HttpExchange) SegmentSizeInfo(com.linkedin.pinot.common.restlet.resources.SegmentSizeInfo) TableSizeInfo(com.linkedin.pinot.common.restlet.resources.TableSizeInfo) Matchers.anyString(org.mockito.Matchers.anyString) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

HttpExchange (com.sun.net.httpserver.HttpExchange)19 HttpHandler (com.sun.net.httpserver.HttpHandler)12 IOException (java.io.IOException)10 InetSocketAddress (java.net.InetSocketAddress)10 OutputStream (java.io.OutputStream)9 HttpServer (com.sun.net.httpserver.HttpServer)7 Test (org.junit.Test)6 InputStream (java.io.InputStream)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 Headers (com.sun.net.httpserver.Headers)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 HttpContext (com.sun.net.httpserver.HttpContext)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ExecutorService (java.util.concurrent.ExecutorService)2 ErroringAction (org.gradle.internal.ErroringAction)2 SegmentSizeInfo (com.linkedin.pinot.common.restlet.resources.SegmentSizeInfo)1 TableSizeInfo (com.linkedin.pinot.common.restlet.resources.TableSizeInfo)1 HttpFutureCallback (com.microsoft.azure.hdinsight.common.HttpFutureCallback)1 MultiHttpFutureCallback (com.microsoft.azure.hdinsight.common.MultiHttpFutureCallback)1