use of com.sun.net.httpserver.Headers in project quorrabot by GloriousEggroll.
the class HTTPSServer method sendData.
private void sendData(String contentType, byte[] data, HttpExchange exchange) {
Headers outHeaders = exchange.getResponseHeaders();
outHeaders.set("Content-Type", contentType);
try {
exchange.sendResponseHeaders(200, data.length);
OutputStream outputStream = exchange.getResponseBody();
outputStream.write(data);
outputStream.close();
} catch (IOException ex) {
sendHTMLError(500, "Server Error", exchange);
com.gmt2001.Console.err.println("HTTPS Server: sendData()" + ex.getMessage());
com.gmt2001.Console.err.logStackTrace(ex);
}
}
use of com.sun.net.httpserver.Headers in project jetty.project by eclipse.
the class JettyHttpExchangeDelegate method getRequestHeaders.
@Override
public Headers getRequestHeaders() {
Headers headers = new Headers();
Enumeration<?> en = _req.getHeaderNames();
while (en.hasMoreElements()) {
String name = (String) en.nextElement();
Enumeration<?> en2 = _req.getHeaders(name);
while (en2.hasMoreElements()) {
String value = (String) en2.nextElement();
headers.add(name, value);
}
}
return headers;
}
use of com.sun.net.httpserver.Headers in project elasticsearch by elastic.
the class AzureDiscoveryClusterFormationTests method startHttpd.
/**
* Creates mock EC2 endpoint providing the list of started nodes to the DescribeInstances API call
*/
@BeforeClass
public static void startHttpd() throws Exception {
logDir = createTempDir();
SSLContext sslContext = getSSLContext();
httpsServer = MockHttpServer.createHttps(new InetSocketAddress(InetAddress.getLoopbackAddress().getHostAddress(), 0), 0);
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
httpsServer.createContext("/subscription/services/hostedservices/myservice", (s) -> {
Headers headers = s.getResponseHeaders();
headers.add("Content-Type", "text/xml; charset=UTF-8");
XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newFactory();
xmlOutputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
StringWriter out = new StringWriter();
XMLStreamWriter sw;
try {
sw = xmlOutputFactory.createXMLStreamWriter(out);
sw.writeStartDocument();
String namespace = "http://schemas.microsoft.com/windowsazure";
sw.setDefaultNamespace(namespace);
sw.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX, "HostedService", namespace);
{
sw.writeStartElement("Deployments");
{
Path[] files = FileSystemUtils.files(logDir);
for (int i = 0; i < files.length; i++) {
Path resolve = files[i].resolve("transport.ports");
if (Files.exists(resolve)) {
List<String> addresses = Files.readAllLines(resolve);
Collections.shuffle(addresses, random());
String address = addresses.get(0);
int indexOfLastColon = address.lastIndexOf(':');
String host = address.substring(0, indexOfLastColon);
String port = address.substring(indexOfLastColon + 1);
sw.writeStartElement("Deployment");
{
sw.writeStartElement("Name");
sw.writeCharacters("mydeployment");
sw.writeEndElement();
sw.writeStartElement("DeploymentSlot");
sw.writeCharacters(DeploymentSlot.Production.name());
sw.writeEndElement();
sw.writeStartElement("Status");
sw.writeCharacters(DeploymentStatus.Running.name());
sw.writeEndElement();
sw.writeStartElement("RoleInstanceList");
{
sw.writeStartElement("RoleInstance");
{
sw.writeStartElement("RoleName");
sw.writeCharacters(UUID.randomUUID().toString());
sw.writeEndElement();
sw.writeStartElement("IpAddress");
sw.writeCharacters(host);
sw.writeEndElement();
sw.writeStartElement("InstanceEndpoints");
{
sw.writeStartElement("InstanceEndpoint");
{
sw.writeStartElement("Name");
sw.writeCharacters("myendpoint");
sw.writeEndElement();
sw.writeStartElement("Vip");
sw.writeCharacters(host);
sw.writeEndElement();
sw.writeStartElement("PublicPort");
sw.writeCharacters(port);
sw.writeEndElement();
}
sw.writeEndElement();
}
sw.writeEndElement();
}
sw.writeEndElement();
}
sw.writeEndElement();
}
sw.writeEndElement();
}
}
}
sw.writeEndElement();
}
sw.writeEndElement();
sw.writeEndDocument();
sw.flush();
final byte[] responseAsBytes = out.toString().getBytes(StandardCharsets.UTF_8);
s.sendResponseHeaders(200, responseAsBytes.length);
OutputStream responseBody = s.getResponseBody();
responseBody.write(responseAsBytes);
responseBody.close();
} catch (XMLStreamException e) {
Loggers.getLogger(AzureDiscoveryClusterFormationTests.class).error("Failed serializing XML", e);
throw new RuntimeException(e);
}
});
httpsServer.start();
}
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);
}
use of com.sun.net.httpserver.Headers in project heron by twitter.
the class NetworkUtilsTest method testReadHttpRequestBodyFail.
@Test
public void testReadHttpRequestBodyFail() throws Exception {
HttpExchange exchange = Mockito.mock(HttpExchange.class);
Headers headers = Mockito.mock(Headers.class);
Mockito.doReturn(headers).when(exchange).getRequestHeaders();
Mockito.doReturn("-1").when(headers).getFirst(Matchers.anyString());
Assert.assertArrayEquals(new byte[0], NetworkUtils.readHttpRequestBody(exchange));
Mockito.doReturn("10").when(headers).getFirst(Matchers.anyString());
InputStream inputStream = Mockito.mock(InputStream.class);
Mockito.doReturn(inputStream).when(exchange).getRequestBody();
Mockito.doThrow(new IOException("Designed IO exception for testing")).when(inputStream).read(Matchers.any(byte[].class), Matchers.anyInt(), Matchers.anyInt());
Assert.assertArrayEquals(new byte[0], NetworkUtils.readHttpRequestBody(exchange));
Mockito.verify(inputStream, Mockito.atLeastOnce()).close();
}
Aggregations