use of com.sun.net.httpserver.Headers in project component-runtime by Talend.
the class HttpClientFactoryImplTest method createTestServer.
private HttpServer createTestServer(int responseStatus) throws IOException {
final HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
server.createContext("/").setHandler(httpExchange -> {
final Headers headers = httpExchange.getRequestHeaders();
final byte[] bytes;
try (final BufferedReader in = new BufferedReader(new InputStreamReader(httpExchange.getRequestBody(), StandardCharsets.UTF_8))) {
bytes = (httpExchange.getRequestMethod() + "@" + headers.keySet().stream().sorted().filter(k -> !asList("Accept", "Host", "User-agent").contains(k)).map(k -> k + "=" + headers.getFirst(k)).collect(joining("/")) + "@" + httpExchange.getRequestURI().toASCIIString() + "@" + in.lines().collect(joining("\n"))).getBytes(StandardCharsets.UTF_8);
}
httpExchange.sendResponseHeaders(responseStatus, bytes.length);
httpExchange.getResponseBody().write(bytes);
httpExchange.close();
});
return server;
}
use of com.sun.net.httpserver.Headers in project component-runtime by Talend.
the class CaptureJUnit4HttpApiTest method doCapture.
@Test
public void doCapture() throws Throwable {
final HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
server.createContext("/").setHandler(httpExchange -> {
final Headers headers = httpExchange.getRequestHeaders();
final byte[] bytes;
try (final BufferedReader in = new BufferedReader(new InputStreamReader(httpExchange.getRequestBody(), StandardCharsets.UTF_8))) {
bytes = (httpExchange.getRequestMethod() + "@" + headers.keySet().stream().sorted().filter(k -> !asList("Accept", "Host", "User-agent").contains(k)).map(k -> k + "=" + headers.getFirst(k)).collect(joining("/")) + "@" + httpExchange.getRequestURI().toASCIIString() + "@" + in.lines().collect(joining("\n"))).getBytes(StandardCharsets.UTF_8);
}
httpExchange.sendResponseHeaders(200, bytes.length);
httpExchange.getResponseBody().write(bytes);
httpExchange.close();
});
server.start();
final Path output = TEMPORARY_FOLDER.getRoot().toPath().toAbsolutePath().resolve("talend/testing/http/" + getClass().getName() + "_doCapture.json");
try {
new JUnit4HttpApiPerMethodConfigurator(API).apply(new Statement() {
@Override
public void evaluate() throws Throwable {
final URL url = new URL("http://localhost:" + server.getAddress().getPort() + "/supertest");
final HttpURLConnection connection = HttpURLConnection.class.cast(url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", API.getPort()))));
connection.setConnectTimeout(30000);
connection.setReadTimeout(20000);
connection.setRequestProperty("Authorization", "should be filtered");
connection.setRequestProperty("ok", "yes");
assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
connection.disconnect();
}
}, Description.createTestDescription(getClass(), "doCapture")).evaluate();
assertTrue(output.toFile().exists());
final String lines = Files.readAllLines(output).stream().collect(joining("\n"));
assertEquals("[\n" + " {\n" + " \"request\":{\n" + " \"headers\":{\n" + " \"content-length\":\"0\",\n" + " \"Accept\":\"text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\",\n" + " \"ok\":\"yes\",\n" + " \"Proxy-Connection\":\"keep-alive\"\n" + " },\n" + " \"method\":\"GET\",\n" + " \"uri\":\"http://localhost:" + server.getAddress().getPort() + "/supertest\"\n" + " },\n" + " \"response\":{\n" + " \"headers\":{\n" + " \"Content-length\":\"105\"\n" + " },\n" + " \"payload\":\"GET@Authorization=should be filtered/Connection=keep-alive/Ok=yes/Proxy-connection=keep-alive@/supertest@\",\n" + " \"status\":200\n" + " }\n" + " }\n" + "]", lines);
} finally {
server.stop(0);
}
}
use of com.sun.net.httpserver.Headers in project component-runtime by Talend.
the class CaptureJUnit4HttpsApiTest method doCapture.
@Test
public void doCapture() throws Throwable {
final SelfSignedCertificate certificate = new SelfSignedCertificate();
final SslContext nettyContext = SslContextBuilder.forServer(certificate.certificate(), certificate.privateKey()).trustManager(InsecureTrustManagerFactory.INSTANCE).sslProvider(SslProvider.JDK).build();
final HttpsServer server = HttpsServer.create(new InetSocketAddress(0), 0);
server.setHttpsConfigurator(new HttpsConfigurator(JdkSslContext.class.cast(nettyContext).context()));
server.createContext("/").setHandler(httpExchange -> {
final Headers headers = httpExchange.getRequestHeaders();
final byte[] bytes;
try (final BufferedReader in = new BufferedReader(new InputStreamReader(httpExchange.getRequestBody(), StandardCharsets.UTF_8))) {
bytes = (httpExchange.getRequestMethod() + "@" + headers.keySet().stream().sorted().filter(k -> !asList("Accept", "Host", "User-agent").contains(k)).map(k -> k + "=" + headers.getFirst(k)).collect(joining("/")) + "@" + httpExchange.getRequestURI().toASCIIString() + "@" + in.lines().collect(joining("\n"))).getBytes(StandardCharsets.UTF_8);
}
httpExchange.sendResponseHeaders(200, bytes.length);
httpExchange.getResponseBody().write(bytes);
httpExchange.close();
});
server.start();
final Path output = TEMPORARY_FOLDER.getRoot().toPath().toAbsolutePath().resolve("talend/testing/http/" + getClass().getName() + "_doCapture.json");
try {
new JUnit4HttpApiPerMethodConfigurator(API).apply(new Statement() {
@Override
public void evaluate() throws Throwable {
final URL url = new URL("https://localhost:" + server.getAddress().getPort() + "/supertest");
final HttpsURLConnection connection = HttpsURLConnection.class.cast(url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", API.getPort()))));
connection.setConnectTimeout(30000);
connection.setReadTimeout(20000);
connection.setHostnameVerifier((h, s) -> true);
connection.setSSLSocketFactory(server.getHttpsConfigurator().getSSLContext().getSocketFactory());
assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
connection.disconnect();
}
}, Description.createTestDescription(getClass(), "doCapture")).evaluate();
assertTrue(output.toFile().exists());
final String lines = Files.readAllLines(output).stream().collect(joining("\n"));
assertEquals("[\n" + " {\n" + " \"request\":{\n" + " \"headers\":{\n" + " \"content-length\":\"0\",\n" + " \"Accept\":\"text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\",\n" + " \"Connection\":\"keep-alive\"\n" + " },\n" + " \"method\":\"GET\",\n" + " \"uri\":\"https://localhost:" + server.getAddress().getPort() + "/supertest\"\n" + " },\n" + " \"response\":{\n" + " \"headers\":{\n" + " \"Content-length\":\"37\"\n" + " },\n" + " \"payload\":\"GET@Connection=keep-alive@/supertest@\",\n" + " \"status\":200\n" + " }\n" + " }\n" + "]", lines);
} finally {
server.stop(0);
}
}
use of com.sun.net.httpserver.Headers in project crate by crate.
the class AzureHttpHandler method sendError.
public static void sendError(final HttpExchange exchange, final RestStatus status) throws IOException {
final Headers headers = exchange.getResponseHeaders();
headers.add("Content-Type", "application/xml");
// see Constants.HeaderConstants.CLIENT_REQUEST_ID_HEADER
final String requestId = exchange.getRequestHeaders().getFirst("x-ms-client-request-id");
if (requestId != null) {
// see Constants.HeaderConstants.STORAGE_RANGE_HEADER
headers.add("x-ms-request-id", requestId);
}
final String errorCode = toAzureErrorCode(status);
// see Constants.HeaderConstants.ERROR_CODE
headers.add("x-ms-error-code", errorCode);
if ("HEAD".equals(exchange.getRequestMethod())) {
exchange.sendResponseHeaders(status.getStatus(), -1L);
} else {
final byte[] response = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Error><Code>" + errorCode + "</Code><Message>" + status + "</Message></Error>").getBytes(StandardCharsets.UTF_8);
exchange.sendResponseHeaders(status.getStatus(), response.length);
exchange.getResponseBody().write(response);
}
}
use of com.sun.net.httpserver.Headers in project eureka by Netflix.
the class SimpleEurekaHttpServer method mapToEurekaHttpRequest.
private EurekaHttpRequest mapToEurekaHttpRequest(HttpExchange httpExchange) {
Headers exchangeHeaders = httpExchange.getRequestHeaders();
Map<String, String> headers = new HashMap<>();
for (String key : exchangeHeaders.keySet()) {
headers.put(key, exchangeHeaders.getFirst(key));
}
return new EurekaHttpRequest(httpExchange.getRequestMethod(), httpExchange.getRequestURI(), headers);
}
Aggregations