Package com.sun.net.httpserver

Examples of com.sun.net.httpserver.Headers


        }
        return sb.toString();
    }  
   
    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());
View Full Code Here


   
    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());
View Full Code Here

    }
   
    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());
View Full Code Here

            long lastModified = f.lastModified();
            String ifModifiedSince = t.getRequestHeaders().getFirst("If-Modified-Since");
            if (ifModifiedSince != null) {
                long d = Date.parse(ifModifiedSince);
                if (d + 1000 >= lastModified) {
                    Headers hds = t.getResponseHeaders();
                    hds.set("Content-Type", contentType);
                    t.sendResponseHeaders(304, -1);
                   
                    if(s_logger.isInfoEnabled())
                        s_logger.info("Sent 304 file has not been " +
                                "modified since " + ifModifiedSince);
                    return;
                }
            }
           
            long length = f.length();
            Headers hds = t.getResponseHeaders();
            hds.set("Content-Type", contentType);
            hds.set("Last-Modified", new Date(lastModified).toGMTString());
            t.sendResponseHeaders(200, length);
            responseFileContent(t, f);
           
            if(s_logger.isInfoEnabled())
                s_logger.info("Sent file " + path + " with content type " + contentType);
View Full Code Here

           
            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());
View Full Code Here

            // 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();
        }
View Full Code Here

        // Simple GET method
        expect(exchange.getRequestMethod()).andReturn("GET");


        Headers header = new Headers();
        ByteArrayOutputStream out = prepareResponse(handler, exchange, header);

        handler.handle(exchange);


        assertEquals(header.getFirst("content-type"),"text/javascript; charset=utf-8");
        String result = out.toString("utf-8");
        assertTrue(result.endsWith("});"));
        assertTrue(result.startsWith("data({"));
    }
View Full Code Here

        HttpExchange exchange = prepareExchange("http://localhost:8080/jolokia?callback=data");

        // Simple GET method
        expect(exchange.getRequestMethod()).andReturn("POST");

        Headers reqHeaders = new Headers();
        reqHeaders.add("Content-Type","text/plain; charset=UTF-8");
        expect(exchange.getRequestHeaders()).andReturn(reqHeaders);
        String req = "{\"timestamp\":1287914327,\"status\":200," +
                "\"request\":{\"mbean\":\"java.lang:type=Memory\",\"attribute\":\"HeapMemoryUsage\",\"type\":\"read\"}," +
                "\"value\":{\"max\":\"129957888\",\"committed\":\"85000192\",\"init\":\"0\",\"used\":\"6813824\"}}";
        byte[] buf = req.getBytes("utf-8");
        InputStream is = new ByteArrayInputStream(buf);
        expect(exchange.getRequestBody()).andReturn(is);
        Headers header = new Headers();
        ByteArrayOutputStream out = prepareResponse(handler, exchange, header);

        handler.handle(exchange);

        assertEquals(header.getFirst("content-type"),"text/javascript; charset=utf-8");
        String result = out.toString("utf-8");
        assertTrue(result.endsWith("});"));
        assertTrue(result.startsWith("data({"));
    }
View Full Code Here

        return requestHandler.handleGetRequest(parsedUri.getUri().toString(),parsedUri.getPathInfo(), parsedUri.getParameterMap());
    }

    private JSONAware executePostRequest(HttpExchange pExchange, ParsedUri pUri) throws MalformedObjectNameException, IOException {
        String encoding = null;
        Headers headers = pExchange.getRequestHeaders();
        String cType =  headers.getFirst("Content-Type");
        if (cType != null) {
            Matcher matcher = contentTypePattern.matcher(cType);
            if (matcher.matches()) {
                encoding = matcher.group(1);
            }
View Full Code Here

    private void sendResponse(HttpExchange pExchange, ParsedUri pParsedUri, String pJson) throws IOException {
        OutputStream out = null;
        String callback = pParsedUri.getParameter(ConfigKey.CALLBACK.getKeyValue());
        try {
            Headers headers = pExchange.getResponseHeaders();
            headers.set("Content-Type",(callback == null ? "text/plain" : "text/javascript") + "; charset=utf-8");
            String content = callback == null ? pJson : callback + "(" + pJson + ");";
            byte[] response = content.getBytes();
            pExchange.sendResponseHeaders(200,response.length);
            out = pExchange.getResponseBody();
            out.write(response);
View Full Code Here

TOP

Related Classes of com.sun.net.httpserver.Headers

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.