Search in sources :

Example 16 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 17 with Headers

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

the class ConsoleProxyThumbnailHandler method handle.

@Override
@SuppressWarnings("access")
public void handle(HttpExchange t) throws IOException {
    try {
        Thread.currentThread().setName("JPG Thread " + Thread.currentThread().getId() + " " + t.getRemoteAddress());
        if (s_logger.isDebugEnabled())
            s_logger.debug("ScreenHandler " + t.getRequestURI());
        long startTick = System.currentTimeMillis();
        doHandle(t);
        if (s_logger.isDebugEnabled())
            s_logger.debug(t.getRequestURI() + "Process time " + (System.currentTimeMillis() - startTick) + " ms");
    } catch (IllegalArgumentException e) {
        String response = "Bad query string";
        s_logger.error(response + ", request URI : " + t.getRequestURI());
        t.sendResponseHeaders(200, response.length());
        OutputStream os = t.getResponseBody();
        os.write(response.getBytes());
        os.close();
    } catch (OutOfMemoryError e) {
        s_logger.error("Unrecoverable OutOfMemory Error, exit and let it be re-launched");
        System.exit(1);
    } catch (Throwable e) {
        s_logger.error("Unexpected exception while handing thumbnail request, ", e);
        String queries = t.getRequestURI().getQuery();
        Map<String, String> queryMap = getQueryMap(queries);
        int width = 0;
        int height = 0;
        String ws = queryMap.get("w");
        String hs = queryMap.get("h");
        try {
            width = Integer.parseInt(ws);
            height = Integer.parseInt(hs);
        } catch (NumberFormatException ex) {
            s_logger.debug("Cannot parse width: " + ws + " or height: " + hs, ex);
        }
        width = Math.min(width, 800);
        height = Math.min(height, 600);
        BufferedImage img = generateTextImage(width, height, "Cannot Connect");
        ByteArrayOutputStream bos = new ByteArrayOutputStream(8196);
        javax.imageio.ImageIO.write(img, "jpg", bos);
        byte[] bs = bos.toByteArray();
        Headers hds = t.getResponseHeaders();
        hds.set("Content-Type", "image/jpeg");
        hds.set("Cache-Control", "no-cache");
        hds.set("Cache-Control", "no-store");
        t.sendResponseHeaders(200, bs.length);
        OutputStream os = t.getResponseBody();
        os.write(bs);
        os.close();
        s_logger.error("Cannot get console, sent error JPG response for " + t.getRequestURI());
        return;
    } finally {
        t.close();
    }
}
Also used : Headers(com.sun.net.httpserver.Headers) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(java.util.HashMap) Map(java.util.Map) BufferedImage(java.awt.image.BufferedImage)

Example 18 with Headers

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

the class ConsoleProxyThumbnailHandler method doHandle.

private void doHandle(HttpExchange t) throws Exception, IllegalArgumentException {
    String queries = t.getRequestURI().getQuery();
    Map<String, String> queryMap = getQueryMap(queries);
    int width = 0;
    int height = 0;
    int port = 0;
    String ws = queryMap.get("w");
    String hs = queryMap.get("h");
    String host = queryMap.get("host");
    String portStr = queryMap.get("port");
    String sid = queryMap.get("sid");
    String tag = queryMap.get("tag");
    String ticket = queryMap.get("ticket");
    String console_url = queryMap.get("consoleurl");
    String console_host_session = queryMap.get("sessionref");
    if (tag == null)
        tag = "";
    if (ws == null || hs == null || host == null || portStr == null || sid == null) {
        throw new IllegalArgumentException();
    }
    try {
        width = Integer.parseInt(ws);
        height = Integer.parseInt(hs);
        port = Integer.parseInt(portStr);
    } catch (NumberFormatException e) {
        throw new IllegalArgumentException(e);
    }
    ConsoleProxyClientParam param = new ConsoleProxyClientParam();
    param.setClientHostAddress(host);
    param.setClientHostPort(port);
    param.setClientHostPassword(sid);
    param.setClientTag(tag);
    param.setTicket(ticket);
    param.setClientTunnelUrl(console_url);
    param.setClientTunnelSession(console_host_session);
    ConsoleProxyClient viewer = ConsoleProxy.getVncViewer(param);
    if (!viewer.isHostConnected()) {
        // use generated image instead of static
        BufferedImage img = generateTextImage(width, height, "Connecting");
        ByteArrayOutputStream bos = new ByteArrayOutputStream(8196);
        javax.imageio.ImageIO.write(img, "jpg", bos);
        byte[] bs = bos.toByteArray();
        Headers hds = t.getResponseHeaders();
        hds.set("Content-Type", "image/jpeg");
        hds.set("Cache-Control", "no-cache");
        hds.set("Cache-Control", "no-store");
        t.sendResponseHeaders(200, bs.length);
        OutputStream os = t.getResponseBody();
        os.write(bs);
        os.close();
        if (s_logger.isInfoEnabled())
            s_logger.info("Console not ready, sent dummy JPG response");
        return;
    }
    {
        Image scaledImage = viewer.getClientScaledImage(width, height);
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D bufImageGraphics = bufferedImage.createGraphics();
        bufImageGraphics.drawImage(scaledImage, 0, 0, null);
        ByteArrayOutputStream bos = new ByteArrayOutputStream(8196);
        javax.imageio.ImageIO.write(bufferedImage, "jpg", bos);
        byte[] bs = bos.toByteArray();
        Headers hds = t.getResponseHeaders();
        hds.set("Content-Type", "image/jpeg");
        hds.set("Cache-Control", "no-cache");
        hds.set("Cache-Control", "no-store");
        t.sendResponseHeaders(200, bs.length);
        OutputStream os = t.getResponseBody();
        os.write(bs);
        os.close();
    }
}
Also used : Headers(com.sun.net.httpserver.Headers) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 19 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 20 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)

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