Search in sources :

Example 11 with Headers

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);
    }
}
Also used : Headers(com.sun.net.httpserver.Headers) OutputStream(java.io.OutputStream) IOException(java.io.IOException)

Example 12 with Headers

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;
}
Also used : Headers(com.sun.net.httpserver.Headers)

Example 13 with 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();
}
Also used : Path(java.nio.file.Path) HttpsConfigurator(com.sun.net.httpserver.HttpsConfigurator) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) InetSocketAddress(java.net.InetSocketAddress) Headers(com.sun.net.httpserver.Headers) OutputStream(java.io.OutputStream) SSLContext(javax.net.ssl.SSLContext) StringWriter(java.io.StringWriter) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) BeforeClass(org.junit.BeforeClass)

Example 14 with Headers

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);
}
Also used : HashMap(java.util.HashMap) Headers(com.sun.net.httpserver.Headers)

Example 15 with 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();
}
Also used : Headers(com.sun.net.httpserver.Headers) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpExchange(com.sun.net.httpserver.HttpExchange) IOException(java.io.IOException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Headers (com.sun.net.httpserver.Headers)23 OutputStream (java.io.OutputStream)12 Test (org.junit.Test)6 IOException (java.io.IOException)5 InetSocketAddress (java.net.InetSocketAddress)4 HttpHeaders (javax.ws.rs.core.HttpHeaders)4 HttpExchange (com.sun.net.httpserver.HttpExchange)3 BufferedImage (java.awt.image.BufferedImage)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Path (java.nio.file.Path)3 BeforeClass (org.junit.BeforeClass)3 HttpsConfigurator (com.sun.net.httpserver.HttpsConfigurator)2 Graphics2D (java.awt.Graphics2D)2 Image (java.awt.Image)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 StringWriter (java.io.StringWriter)2 HashMap (java.util.HashMap)2 SSLContext (javax.net.ssl.SSLContext)2 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)2