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();
}
}
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();
}
}
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();
}
}
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);
}
}
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);
}
}
Aggregations