Java Code Examples for com.sun.net.httpserver.HttpExchange#getResponseHeaders()

The following examples show how to use com.sun.net.httpserver.HttpExchange#getResponseHeaders() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Deadlock.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void handle (HttpExchange t)
    throws IOException
{
    InputStream is = t.getRequestBody();
    Headers map = t.getRequestHeaders();
    Headers rmap = t.getResponseHeaders();
    while (is.read() != -1);
    is.close();
    t.sendResponseHeaders(200, -1);
    HttpPrincipal p = t.getPrincipal();
    if (!p.getUsername().equals("fred")) {
        error = true;
    }
    if (!p.getRealm().equals("[email protected]")) {
        error = true;
    }
    t.close();
}
 
Example 2
Source File: BPMNRestTaskJsonMappingTest.java    From product-ei with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(HttpExchange httpExchange) throws IOException {
    Headers headers = httpExchange.getResponseHeaders();
    headers.add("Content-Type", "application/json");

    //response payload
    String response = "{\"user\":null, \"id\":123}";

    httpExchange.sendResponseHeaders(200, response.length());
    OutputStream responseStream = httpExchange.getResponseBody();
    try {
        responseStream.write(response.getBytes(Charset.defaultCharset()));
    } finally {
        responseStream.close();
    }
}
 
Example 3
Source File: Deadlock.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void handle (HttpExchange t)
    throws IOException
{
    InputStream is = t.getRequestBody();
    Headers map = t.getRequestHeaders();
    Headers rmap = t.getResponseHeaders();
    while (is.read() != -1);
    is.close();
    t.sendResponseHeaders(200, -1);
    HttpPrincipal p = t.getPrincipal();
    if (!p.getUsername().equals("fred")) {
        error = true;
    }
    if (!p.getRealm().equals("[email protected]")) {
        error = true;
    }
    t.close();
}
 
Example 4
Source File: DiagnosticsServer.java    From txtUML with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void handle(HttpExchange exchange) throws IOException {
	// Set required headers
	Headers headers = exchange.getResponseHeaders();
	headers.add("Content-type", "application/json");
	headers.add("Access-Control-Allow-Origin", "*");

	// Build the payload
	String response = registry.entrySet().stream()
		.map(DiagnosticsServer::registryEntryToJson)
		.collect(Collectors.joining(",", "[", "]"));

	// Write response
	exchange.sendResponseHeaders(200, response.length());
	OutputStream os = exchange.getResponseBody();
	os.write(response.getBytes());
	os.close();
}
 
Example 5
Source File: ConsoleProxyAjaxHandler.java    From cosmic with Apache License 2.0 6 votes vote down vote up
private void handleClientStart(final HttpExchange t, final ConsoleProxyClient viewer, final String title, final String guest) throws IOException {
    final List<String> languages = t.getRequestHeaders().get("Accept-Language");
    final String response = viewer.onAjaxClientStart(title, languages, guest);

    final Headers hds = t.getResponseHeaders();
    hds.set("Content-Type", "text/html");
    hds.set("Cache-Control", "no-cache");
    hds.set("Cache-Control", "no-store");
    hds.set("Strict-Transport-Security", "max-age=31536000; includeSubDomains");
    t.sendResponseHeaders(200, response.length());

    final OutputStream os = t.getResponseBody();
    try {
        os.write(response.getBytes());
    } finally {
        os.close();
    }
}
 
Example 6
Source File: ConsoleProxyCmdHandler.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public void doHandle(HttpExchange t) throws Exception {
    String path = t.getRequestURI().getPath();
    int i = path.indexOf("/", 1);
    String cmd = path.substring(i + 1);
    s_logger.info("Get CMD request for " + cmd);
    if (cmd.equals("getstatus")) {
        ConsoleProxyClientStatsCollector statsCollector = ConsoleProxy.getStatsCollector();

        Headers hds = t.getResponseHeaders();
        hds.set("Content-Type", "text/plain");
        t.sendResponseHeaders(200, 0);
        OutputStreamWriter os = new OutputStreamWriter(t.getResponseBody(),"UTF-8");
        statsCollector.getStatsReport(os);
        os.close();
    }
}
 
Example 7
Source File: Deadlock.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void handle (HttpExchange t)
    throws IOException
{
    InputStream is = t.getRequestBody();
    Headers map = t.getRequestHeaders();
    Headers rmap = t.getResponseHeaders();
    while (is.read() != -1);
    is.close();
    t.sendResponseHeaders(200, -1);
    HttpPrincipal p = t.getPrincipal();
    if (!p.getUsername().equals("fred")) {
        error = true;
    }
    if (!p.getRealm().equals("[email protected]")) {
        error = true;
    }
    t.close();
}
 
Example 8
Source File: ConsoleProxyAjaxHandler.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 9
Source File: DiagnosticsServer.java    From txtUML with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void handle(HttpExchange exchange) throws IOException {
	// Set required headers
	Headers headers = exchange.getResponseHeaders();
	headers.add("Content-type", "application/json");
	headers.add("Access-Control-Allow-Origin", "*");

	String body = "";
	String response = "";

	if ("POST".equals(exchange.getRequestMethod())) {
		// Get request data
		try (InputStreamReader reader = new InputStreamReader(exchange.getRequestBody())) {
			body = new BufferedReader(reader).lines().collect(Collectors.joining("\n"));
		}

		// Get and set animation delay
		String[] animationDelayArray = body.split("=");
		int animationDelay = Integer.parseInt(animationDelayArray[1]);
		setAnimationDelay(animationDelay);
	} else {
		// Build the payload
		response = "{\"animationDelay\":\"" + diagnosticsPlugin.getAnimationDelay() + "\"}";
	}

	// Write response
	exchange.sendResponseHeaders(200, response.length());
	OutputStream os = exchange.getResponseBody();
	os.write(response.getBytes());
	os.close();
}
 
Example 10
Source File: UnmodifiableMaps.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void handle(HttpExchange t) throws IOException {
    Headers respHeaders = t.getResponseHeaders();
    // ensure some response headers, over the usual ones
    respHeaders.add("RespHdr1", "Value1");
    respHeaders.add("RespHdr2", "Value2");
    respHeaders.add("RespHdr3", "Value3");
    t.sendResponseHeaders(200, -1);
    t.close();
}
 
Example 11
Source File: ConsoleProxyAjaxHandler.java    From cosmic with Apache License 2.0 5 votes vote down vote up
private void handleClientUpdate(final HttpExchange t, final ConsoleProxyClient viewer) throws IOException {
    final String response = viewer.onAjaxClientUpdate();

    final Headers hds = t.getResponseHeaders();
    hds.set("Content-Type", "text/javascript");
    hds.set("Strict-Transport-Security", "max-age=31536000; includeSubDomains");
    t.sendResponseHeaders(200, response.length());

    final OutputStream os = t.getResponseBody();
    try {
        os.write(response.getBytes());
    } finally {
        os.close();
    }
}
 
Example 12
Source File: ConsoleProxyAjaxHandler.java    From cosmic with Apache License 2.0 5 votes vote down vote up
private void sendResponse(final HttpExchange t, final String contentType, final String response) throws IOException {
    final Headers hds = t.getResponseHeaders();
    hds.set("Content-Type", contentType);
    hds.set("Strict-Transport-Security", "max-age=31536000; includeSubDomains");

    t.sendResponseHeaders(200, response.length());
    final OutputStream os = t.getResponseBody();
    try {
        os.write(response.getBytes());
    } finally {
        os.close();
    }
}
 
Example 13
Source File: HttpOnly.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void handle(HttpExchange t) throws IOException {
    Headers reqHeaders = t.getRequestHeaders();

    // some small sanity check
    List<String> cookies = reqHeaders.get("Cookie");
    for (String cookie : cookies) {
        if (!cookie.contains("JSESSIONID")
            || !cookie.contains("WILE_E_COYOTE"))
            t.sendResponseHeaders(400, -1);
    }

    // return some cookies so we can check getHeaderField(s)
    Headers respHeaders = t.getResponseHeaders();
    List<String> values = new ArrayList<>();
    values.add("ID=JOEBLOGGS; version=1; Path=" + URI_PATH);
    values.add("NEW_JSESSIONID=" + (SESSION_ID+1) + "; version=1; Path="
               + URI_PATH +"; HttpOnly");
    values.add("NEW_CUSTOMER=WILE_E_COYOTE2; version=1; Path=" + URI_PATH);
    respHeaders.put("Set-Cookie", values);
    values = new ArrayList<>();
    values.add("COOKIE2_CUSTOMER=WILE_E_COYOTE2; version=1; Path="
               + URI_PATH);
    respHeaders.put("Set-Cookie2", values);
    values.add("COOKIE2_JSESSIONID=" + (SESSION_ID+100)
               + "; version=1; Path=" + URI_PATH +"; HttpOnly");
    respHeaders.put("Set-Cookie2", values);

    t.sendResponseHeaders(200, -1);
    t.close();
}
 
Example 14
Source File: UnmodifiableMaps.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void handle(HttpExchange t) throws IOException {
    Headers respHeaders = t.getResponseHeaders();
    // ensure some response headers, over the usual ones
    respHeaders.add("RespHdr1", "Value1");
    respHeaders.add("RespHdr2", "Value2");
    respHeaders.add("RespHdr3", "Value3");
    t.sendResponseHeaders(200, -1);
    t.close();
}
 
Example 15
Source File: Basic.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void handle(HttpExchange t) throws IOException {
    InputStream is = t.getRequestBody();
    Headers map = t.getRequestHeaders();
    Headers rmap = t.getResponseHeaders();
    URI uri = t.getRequestURI();

    debug("Server: received request for " + uri);
    String path = uri.getPath();
    if (path.endsWith("a.jar"))
        aDotJar++;
    else if (path.endsWith("b.jar"))
        bDotJar++;
    else if (path.endsWith("c.jar"))
        cDotJar++;
    else
        System.out.println("Unexpected resource request" + path);

    while (is.read() != -1);
    is.close();

    File file = new File(docsDir, path);
    if (!file.exists())
        throw new RuntimeException("Error: request for " + file);
    long clen = file.length();
    t.sendResponseHeaders (200, clen);
    OutputStream os = t.getResponseBody();
    FileInputStream fis = new FileInputStream(file);
    try {
        byte[] buf = new byte [16 * 1024];
        int len;
        while ((len=fis.read(buf)) != -1) {
            os.write (buf, 0, len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    fis.close();
    os.close();
}
 
Example 16
Source File: SimpleHttpServer.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
void moved(HttpExchange t) throws IOException {
    Headers req = t.getRequestHeaders();
    Headers map = t.getResponseHeaders();
    URI uri = t.getRequestURI();
    String host = req.getFirst("Host");
    String location = "http://" + host + uri.getPath() + "/";
    map.set("Content-Type", "text/html");
    map.set("Location", location);
    t.sendResponseHeaders(301, -1);
    t.close();
}
 
Example 17
Source File: Basic.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void handle(HttpExchange t) throws IOException {
    InputStream is = t.getRequestBody();
    Headers map = t.getRequestHeaders();
    Headers rmap = t.getResponseHeaders();
    URI uri = t.getRequestURI();

    debug("Server: received request for " + uri);
    String path = uri.getPath();
    if (path.endsWith("a.jar"))
        aDotJar++;
    else if (path.endsWith("b.jar"))
        bDotJar++;
    else if (path.endsWith("c.jar"))
        cDotJar++;
    else
        System.out.println("Unexpected resource request" + path);

    while (is.read() != -1);
    is.close();

    File file = new File(docsDir, path);
    if (!file.exists())
        throw new RuntimeException("Error: request for " + file);
    long clen = file.length();
    t.sendResponseHeaders (200, clen);
    OutputStream os = t.getResponseBody();
    FileInputStream fis = new FileInputStream(file);
    try {
        byte[] buf = new byte [16 * 1024];
        int len;
        while ((len=fis.read(buf)) != -1) {
            os.write (buf, 0, len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    fis.close();
    os.close();
}
 
Example 18
Source File: ContentTypeTestCase.java    From product-ei with Apache License 2.0 5 votes vote down vote up
public void handle(HttpExchange t) throws IOException {

            Headers h = t.getResponseHeaders();
            h.add("Content-Type", contentType);
            String response = "This Content type test case";
            t.sendResponseHeaders(HTTP_STATUS_OK, response.length());
            OutputStream os = t.getResponseBody();
            os.write(response.getBytes());
            os.close();
        }
 
Example 19
Source File: Basic.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void handle(HttpExchange t) throws IOException {
    InputStream is = t.getRequestBody();
    Headers map = t.getRequestHeaders();
    Headers rmap = t.getResponseHeaders();
    URI uri = t.getRequestURI();

    debug("Server: received request for " + uri);
    String path = uri.getPath();
    if (path.endsWith("a.jar"))
        aDotJar++;
    else if (path.endsWith("b.jar"))
        bDotJar++;
    else if (path.endsWith("c.jar"))
        cDotJar++;
    else
        System.out.println("Unexpected resource request" + path);

    while (is.read() != -1);
    is.close();

    File file = new File(docsDir, path);
    if (!file.exists())
        throw new RuntimeException("Error: request for " + file);
    long clen = file.length();
    t.sendResponseHeaders (200, clen);
    OutputStream os = t.getResponseBody();
    FileInputStream fis = new FileInputStream(file);
    try {
        byte[] buf = new byte [16 * 1024];
        int len;
        while ((len=fis.read(buf)) != -1) {
            os.write (buf, 0, len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    fis.close();
    os.close();
}
 
Example 20
Source File: AuthHttpServer.java    From swift-explorer with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(HttpExchange exchange) throws IOException 
{
	String requestMethod = exchange.getRequestMethod();
	if (requestMethod.equalsIgnoreCase("GET")) 
	{
		Headers responseHeaders = exchange.getResponseHeaders();
		responseHeaders.set("Content-Type", "text/plain");
		exchange.sendResponseHeaders(200, 0);
		
		OutputStream responseBody = exchange.getResponseBody();
		Headers requestHeaders = exchange.getRequestHeaders();
		Set<String> keySet = requestHeaders.keySet();
		Iterator<String> iter = keySet.iterator();
		while (iter.hasNext()) {
			String key = iter.next();
			List<String> values = requestHeaders.get(key);
			String s = key + " = " + values.toString() + "\n";
			responseBody.write(s.getBytes());		
		}
		responseBody.close();
			
		if (sharedQueue != null)
		{
			String query = exchange.getRequestURI().getQuery() ;
			try 
			{
				sharedQueue.put(queryToParameterMap(query));
			} 
			catch (InterruptedException e) 
			{
				logger.error("Error occurred in the server handler", e);
			}
		}
	}
}