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

The following are Jave code examples for showing how to use getResponseHeaders() of thecom.sun.net.httpserver.HttpExchange class. You can vote up the examples you like. Your votes will be used in our system to get more good examples. 

+ Save this method
Example 1
Project: fourinone-master View Source Vote up 6 votes
public void handle(HttpExchange exchange) throws IOException
{
    /*InputStream is = t.getRequestBody();
    String response = "response ok";
    t.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.length());
    OutputStream os = t.getResponseBody();
    os.write(response.getBytes());
    os.close();
    */
    Headers responseHeaders = exchange.getResponseHeaders();
	responseHeaders.set("Content-Type", "text/html");
	String response = ConfigContext.getRequest(ConfigContext.getProp("RSPE404"));
	exchange.sendResponseHeaders(HttpURLConnection.HTTP_NOT_FOUND, response.length());
	OutputStream os = exchange.getResponseBody();
	os.write(response.getBytes());
	os.close();
}
 
Example 2
Project: victims-lib-java-master View Source Vote up 6 votes
public void handle(HttpExchange exchange) {
    try {
        Headers headers = exchange.getResponseHeaders();
        headers.add("Content-Type", "application/json");

        exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK,
                json.length);
        OutputStream os = exchange.getResponseBody();
        os.write(json);
        os.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 3
Project: Fourinone-master View Source Vote up 6 votes
public void handle(HttpExchange exchange) throws IOException
{
    /*InputStream is = t.getRequestBody();
    String response = "response ok";
    t.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.length());
    OutputStream os = t.getResponseBody();
    os.write(response.getBytes());
    os.close();
    */
    Headers responseHeaders = exchange.getResponseHeaders();
	responseHeaders.set("Content-Type", "text/html");
	String response = ConfigContext.getRequest(ConfigContext.getProp("RSPE404"));
	exchange.sendResponseHeaders(HttpURLConnection.HTTP_NOT_FOUND, response.length());
	OutputStream os = exchange.getResponseBody();
	os.write(response.getBytes());
	os.close();
}
 
Example 4
Project: mock-httpserver-master View Source Vote up 6 votes
private void sendHeaders(HttpExchange httpExchange, Response response) throws IOException {
    int length = response.getContent().getLength();
    int statusCode = response.getStatus().getStatusCode();

    Headers responseHeaders = httpExchange.getResponseHeaders();
    Header headers = response.getHeader();
    for (String headerField : headers.getHeaderFields()) {
        for (Header.Parameter value : headers.getHeaderParameter(headerField)) {
            responseHeaders.add(headerField, value.toString());
        }
    }
    addHeaderIfSet(responseHeaders, Header.Fields.CONTENT_ENCODING, response.getContent().getEncoding());
    addHeaderIfSet(responseHeaders, Header.Fields.CONTENT_LANGUAGE, response.getContent().getLanguage());
    addHeaderIfSet(responseHeaders, Header.Fields.CONTENT_MD5, response.getContent().getMd5());
    addHeaderIfSet(responseHeaders, Header.Fields.CONTENT_TYPE, response.getContent().getComposedContentType());
    addHeaderIfSet(responseHeaders, Header.Fields.CONTENT_RANGE, response.getContent().getRange());
    httpExchange.sendResponseHeaders(statusCode, max(0, length));
}
 
Example 5
Project: product-esb-master View Source Vote up 6 votes
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(200, response.length());
			OutputStream os = t.getResponseBody();
			os.write(response.getBytes());
			os.close();
		}
 
Example 6
Project: werval-master View Source Vote up 6 votes
private void writeOutcome( Outcome outcome, HttpExchange exchange )
    throws IOException
{
    // Headers
    Headers headers = exchange.getResponseHeaders();
    headers.putAll( outcome.responseHeader().headers().allValues() );

    // Body
    InputStream bodyStream = null;
    int chunkSize = HTTP_BUF_SIZE;
    final long responseLength;
    if( outcome instanceof ChunkedInputOutcome )
    {
        ChunkedInputOutcome chunked = (ChunkedInputOutcome) outcome;
        bodyStream = chunked.inputStream();
        chunkSize = chunked.chunkSize();
        responseLength = 0;
    }
    else if( outcome instanceof InputStreamOutcome )
    {
        InputStreamOutcome input = (InputStreamOutcome) outcome;
        bodyStream = input.bodyInputStream();
        responseLength = input.contentLength();
    }
    else if( outcome instanceof SimpleOutcome )
    {
        SimpleOutcome simple = (SimpleOutcome) outcome;
        bodyStream = simple.body().asStream();
        responseLength = 0;
    }
    else
    {
        responseLength = -1;
    }

    // Status and Content-Length
    exchange.sendResponseHeaders( outcome.responseHeader().status().code(), responseLength );

    // Body if any
    if( bodyStream != null )
    {
        InputStreams.transferTo( bodyStream, exchange.getResponseBody(), chunkSize );
    }
}
 
Example 7
Project: restrepo-master View Source Vote up 6 votes
public void handle(HttpExchange exchange) throws IOException
{
    assertEquals("HTTP/1.1", exchange.getProtocol());
    assertEquals("POST", exchange.getRequestMethod());
    Headers headers = exchange.getRequestHeaders();
    assertTrue (headers.containsKey("foo"));
    String val = headers.getFirst("foo");
  
    assertEquals ("fooValue", val);
    assertNotNull (exchange.getHttpContext());
    assertEquals ("/foo", exchange.getHttpContext().getPath());
    assertTrue (this == exchange.getHttpContext().getHandler());
    assertTrue (exchange.getHttpContext().getAttributes().containsKey("fooAttribute"));
    assertEquals("fooValue", (String)exchange.getHttpContext().getAttributes().get("fooAttribute"));
   
    assertEquals ("Was Here", (String)exchange.getAttribute("fooFilter"));
    assertEquals ("Was Also Here", (String)exchange.getAttribute("barFilter"));
    
    assertNotNull(exchange.getPrincipal());
    assertEquals("humpty", exchange.getPrincipal().getName());
    
    String response = "Hello World!";
    InputStream is = exchange.getRequestBody();
    String body = IO.toString(is);
    assertEquals(0, body.length());
    
    Headers responseHeaders = exchange.getResponseHeaders();
    responseHeaders.add("bar", "barValue");
    exchange.sendResponseHeaders(200, response.length());
    OutputStream os = exchange.getResponseBody();
    os.write(response.getBytes());
    os.close();
}
 
Example 8
Project: openjdk-master View Source Vote up 6 votes
@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 9
Project: niolex-common-utils-master View Source Vote up 6 votes
public void handle(HttpExchange t) throws IOException {
    String response = "?????????????????????????????";
    Headers h = t.getResponseHeaders();
    h.add("Content-Type", " text/html;charset=utf-8");
    byte[] data = response.getBytes(StringUtil.UTF_8);
    t.sendResponseHeaders(200, data.length);
    OutputStream os = t.getResponseBody();
    os.write(data);
    os.close();
}
 
发布了145 篇原创文章 · 获赞 17 · 访问量 23万+

猜你喜欢

转载自blog.csdn.net/guichenglin/article/details/52807885