Search in sources :

Example 46 with Headers

use of com.sun.net.httpserver.Headers in project cloudstack by apache.

the class ConsoleProxyAjaxHandler method handleClientUpdate.

private void handleClientUpdate(HttpExchange t, ConsoleProxyClient viewer) throws IOException {
    String response = viewer.onAjaxClientUpdate();
    Headers hds = t.getResponseHeaders();
    hds.set("Content-Type", "text/javascript");
    t.sendResponseHeaders(200, response.length());
    OutputStream os = t.getResponseBody();
    try {
        os.write(response.getBytes());
    } finally {
        os.close();
    }
}
Also used : Headers(com.sun.net.httpserver.Headers) OutputStream(java.io.OutputStream)

Example 47 with Headers

use of com.sun.net.httpserver.Headers in project cloudstack by apache.

the class ConsoleProxyAjaxHandler method sendResponse.

private void sendResponse(HttpExchange t, String contentType, String response) throws IOException {
    Headers hds = t.getResponseHeaders();
    hds.set("Content-Type", contentType);
    t.sendResponseHeaders(200, response.length());
    OutputStream os = t.getResponseBody();
    try {
        os.write(response.getBytes());
    } finally {
        os.close();
    }
}
Also used : Headers(com.sun.net.httpserver.Headers) OutputStream(java.io.OutputStream)

Example 48 with Headers

use of com.sun.net.httpserver.Headers in project cloudstack by apache.

the class ConsoleProxyAjaxHandler method handleClientStart.

private void handleClientStart(HttpExchange t, ConsoleProxyClient viewer, String title, String guest) throws IOException {
    List<String> languages = t.getRequestHeaders().get("Accept-Language");
    String response = viewer.onAjaxClientStart(title, languages, guest);
    Headers hds = t.getResponseHeaders();
    hds.set("Content-Type", "text/html");
    hds.set("Cache-Control", "no-cache");
    hds.set("Cache-Control", "no-store");
    t.sendResponseHeaders(200, response.length());
    OutputStream os = t.getResponseBody();
    try {
        os.write(response.getBytes());
    } finally {
        os.close();
    }
}
Also used : Headers(com.sun.net.httpserver.Headers) OutputStream(java.io.OutputStream)

Example 49 with Headers

use of com.sun.net.httpserver.Headers in project component-runtime by Talend.

the class HttpClientFactoryImplTest method requestWithJSON.

@Test
void requestWithJSON() throws IOException {
    final HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
    server.createContext("/").setHandler(httpExchange -> {
        final Headers headers = httpExchange.getResponseHeaders();
        headers.set("content-type", "application/json;charset=UTF-8");
        final byte[] bytes;
        try (final BufferedReader in = new BufferedReader(new InputStreamReader(httpExchange.getRequestBody(), StandardCharsets.UTF_8))) {
            bytes = in.lines().collect(joining("\n")).getBytes(StandardCharsets.UTF_8);
        }
        httpExchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, bytes.length);
        httpExchange.getResponseBody().write(bytes);
        httpExchange.close();
    });
    try {
        server.start();
        final ResponseJson client = newDefaultFactory().create(ResponseJson.class, null);
        client.base("http://localhost:" + server.getAddress().getPort() + "/api");
        final Response<Sample> result = client.main("application/json", new Sample(singletonList(new Foo("testJSON"))));
        assertEquals(1, result.body().getFoos().size());
        assertEquals("testJSON", result.body().getFoos().iterator().next().getName());
        assertEquals(HttpURLConnection.HTTP_OK, result.status());
        assertEquals("30", result.headers().get("content-length").iterator().next());
    } finally {
        server.stop(0);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) Headers(com.sun.net.httpserver.Headers) HttpServer(com.sun.net.httpserver.HttpServer) BufferedReader(java.io.BufferedReader) Test(org.junit.jupiter.api.Test)

Example 50 with Headers

use of com.sun.net.httpserver.Headers in project component-runtime by Talend.

the class HttpClientFactoryImplTest method requestWithXML.

@Test
void requestWithXML() throws IOException {
    final HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
    server.createContext("/").setHandler(httpExchange -> {
        final Headers headers = httpExchange.getResponseHeaders();
        headers.set("content-type", "application/xml;charset=UTF-8");
        final byte[] bytes;
        try (final BufferedReader in = new BufferedReader(new InputStreamReader(httpExchange.getRequestBody(), StandardCharsets.UTF_8))) {
            bytes = in.lines().collect(joining("\n")).getBytes(StandardCharsets.UTF_8);
        }
        httpExchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, bytes.length);
        httpExchange.getResponseBody().write(bytes);
        httpExchange.close();
    });
    try {
        server.start();
        final ResponseXml client = newDefaultFactory().create(ResponseXml.class, null);
        client.base("http://localhost:" + server.getAddress().getPort() + "/api");
        final Response<XmlRecord> result = client.main("application/xml", new XmlRecord("xml content"));
        assertEquals("xml content", result.body().getValue());
        assertEquals(HttpURLConnection.HTTP_OK, result.status());
        assertEquals("104", result.headers().get("content-length").iterator().next());
    } finally {
        server.stop(0);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) Headers(com.sun.net.httpserver.Headers) HttpServer(com.sun.net.httpserver.HttpServer) BufferedReader(java.io.BufferedReader) Test(org.junit.jupiter.api.Test)

Aggregations

Headers (com.sun.net.httpserver.Headers)56 OutputStream (java.io.OutputStream)23 IOException (java.io.IOException)17 InetSocketAddress (java.net.InetSocketAddress)12 InputStream (java.io.InputStream)10 HttpExchange (com.sun.net.httpserver.HttpExchange)9 Test (org.junit.Test)9 Test (org.junit.jupiter.api.Test)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 HttpServer (com.sun.net.httpserver.HttpServer)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 BufferedImage (java.awt.image.BufferedImage)5 BufferedReader (java.io.BufferedReader)5 InputStreamReader (java.io.InputStreamReader)5 Path (java.nio.file.Path)5 HashMap (java.util.HashMap)5 BeforeClass (org.junit.BeforeClass)5 Graphics2D (java.awt.Graphics2D)4 Image (java.awt.Image)4