当前位置: 首页>>代码示例>>Java>>正文


Java Headers.add方法代码示例

本文整理汇总了Java中com.sun.net.httpserver.Headers.add方法的典型用法代码示例。如果您正苦于以下问题:Java Headers.add方法的具体用法?Java Headers.add怎么用?Java Headers.add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.sun.net.httpserver.Headers的用法示例。


在下文中一共展示了Headers.add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: handle

import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
@Override
public void handle(HttpExchange exchange) throws IOException {
    InputStream inputStream = exchange.getRequestBody();
    String query;
    String response;
    try (Scanner scanner = new Scanner(inputStream, StandardCharsets.UTF_8.name())) {
        query = scanner.useDelimiter("\\A").next();
        logger.info("Query: " + query);
        inputStream.close();
        response = queryServer(query, ReturnType.JSON);
    } catch (Exception e) {
        logger.info("ERROR", e);
        response = new Message("ERROR: " + e.getMessage(), true).toJson().toString();
    }

    // Add the header to avoid error:
    // “No 'Access-Control-Allow-Origin' header is present on the requested resource”
    Headers responseHeaders = exchange.getResponseHeaders();
    responseHeaders.add("Access-Control-Allow-Origin", "*");
    exchange.sendResponseHeaders(200, response.length());

    OutputStream outputStream = exchange.getResponseBody();
    outputStream.write(response.getBytes());
    outputStream.close();
}
 
开发者ID:graphflow,项目名称:graphflow,代码行数:26,代码来源:PlanViewerHttpServer.java

示例2: handle

import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
public void handle(HttpExchange exchange) {
	try {
		exchange.getRequestMethod();
		exchange.getRequestBody().close();
		Headers respHeaders = null;
		if(!accessed && (source == null)) { //context hasn't been accessed before
			source = exchange.getRemoteAddress().getHostString();
			accessed = true;
			respHeaders = exchange.getResponseHeaders();
			respHeaders.add("Content-Type","text/plain");
			exchange.sendResponseHeaders(200,path.length());
			OutputStream os = exchange.getResponseBody();
			os.write(path.getBytes());
			os.flush();
			os.close();
			accessList.put(path,source);
		} else {
			respHeaders = exchange.getResponseHeaders();
			exchange.sendResponseHeaders(404,0);
			exchange.getResponseBody().close();
		}
	} catch(Exception e) {
		System.err.println("Unforeseen error occurred!");
		e.printStackTrace();
	}
}
 
开发者ID:Bort-Millipede,项目名称:WLT3Serial,代码行数:27,代码来源:WebServerTestHelper.java

示例3: handle

import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
@Override
public synchronized void handle(HttpExchange t)
        throws IOException {
    byte[] buf = new byte[2048];
    System.out.println("Server: " + t.getRequestURI());
    try (InputStream is = t.getRequestBody()) {
        while (is.read(buf) != -1) ;
    }
    increment();
    if (count() == 1) {
        Headers map = t.getResponseHeaders();
        String redirect = "/redirect/bar.txt";
        map.add("Location", redirect);
        t.sendResponseHeaders(301, -1);
        t.close();
    } else {
        String response = "Hello world";
        t.sendResponseHeaders(200, response.length());
        OutputStream os = t.getResponseBody();
        os.write(response.getBytes(StandardCharsets.ISO_8859_1));
        t.close();
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:24,代码来源:Security.java

示例4: handle

import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
@Override
public synchronized void handle(HttpExchange t)
    throws IOException
{
    byte[] buf = new byte[2048];
    try (InputStream is = t.getRequestBody()) {
        while (is.read(buf) != -1) ;
    }

    Headers responseHeaders = t.getResponseHeaders();

    if (count++ < 1) {
        responseHeaders.add("Location", root + "/foo/" + count);
    } else {
        responseHeaders.add("Location", SmokeTest.midSizedFilename);
    }
    t.sendResponseHeaders(301, -1);
    t.close();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:20,代码来源:SmokeTest.java

示例5: handle

import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
@Override
public synchronized void handle(HttpExchange t)
        throws IOException {
    byte[] buf = new byte[2048];
    try (InputStream is = t.getRequestBody()) {
        while (is.read(buf) != -1) ;
    }

    Headers map = t.getResponseHeaders();

    if (count++ < 1) {
        map.add("Location", root + "/foo/" + count);
    } else {
        map.add("Location", SmokeTest.midSizedFilename);
    }
    t.sendResponseHeaders(301, -1);
    t.close();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:19,代码来源:LightWeightHttpServer.java

示例6: handle

import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
@Override
public void handle(HttpExchange he) throws IOException {
    List<String> l = he.getRequestHeaders().get("X-Bar");
    if (!l.contains("foo1") || !l.contains("foo2")) {
        for (String s : l) {
            System.out.println("HH: " + s);
        }
        he.sendResponseHeaders(500, -1);
        he.close();
        return;
    }
    Headers h = he.getResponseHeaders();
    h.add("X-Foo-Response", "resp1");
    h.add("X-Foo-Response", "resp2");
    he.sendResponseHeaders(200, RESPONSE.length());
    OutputStream os = he.getResponseBody();
    os.write(RESPONSE.getBytes(US_ASCII));
    os.close();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:20,代码来源:HeadersTest1.java

示例7: handle

import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
@Override
public void handle(HttpExchange he) throws IOException {
    String method = he.getRequestMethod();
    InputStream is = he.getRequestBody();
    List<String> l = he.getRequestHeaders().get("X-Bar");
    if (!l.contains("foo1") || !l.contains("foo2")) {
        for (String s : l)
            System.out.println("HH: " + s);
        he.sendResponseHeaders(500, -1);
        he.close();
        return;
    }
    Headers h = he.getResponseHeaders();
    h.add("X-Foo-Response", "resp1");
    h.add("X-Foo-Response", "resp2");
    he.sendResponseHeaders(200, RESPONSE.length());
    OutputStream os = he.getResponseBody();
    os.write(RESPONSE.getBytes(US_ASCII));
    os.close();
}
 
开发者ID:campolake,项目名称:openjdk9,代码行数:21,代码来源:HeadersTest1.java

示例8: write

import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
/**
 * Write the given resource as an http response body.
 */
public boolean write(String filename, HttpExchange t) throws IOException {
	InputStream stream = getInputStream(filename);
	if (stream != null) {
		Headers headers = t.getResponseHeaders();
		if (!headers.containsKey("Content-Type")) {
			String mime = getContentType(filename);
			if (mime != null) {
				headers.add("Content-Type", mime);
			}
		}
		// Note: available() isn't officially guaranteed to return the full
		// stream length but effectively seems to do so in our context.
		t.sendResponseHeaders(200, stream.available());
		dump(stream, t.getResponseBody());
		return true;
	}
	return false;
}
 
开发者ID:DigitalMediaServer,项目名称:DigitalMediaServer,代码行数:22,代码来源:RemoteUtil.java

示例9: handle

import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
public void handle(HttpExchange t) throws IOException {
    URI requestedUri = t.getRequestURI();
    String query = requestedUri.getRawQuery();
    URL ycsb = new URL("http://" + hostYCSB + ":" + portYCSB + "?" + query);
    try {
        InputStream in = ycsb.openStream();
        Headers h = t.getResponseHeaders();
        h.add("Content-Type", "text/html");
        t.sendResponseHeaders(200, 0);
        OutputStream os = t.getResponseBody();
        int i;
        while ((i = in.read()) != -1) {
            os.write(i);
            os.flush();
        }
        os.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
开发者ID:wgolab,项目名称:WatCA,代码行数:21,代码来源:WebServer.java

示例10: getRequestHeaders

import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
@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;
}
 
开发者ID:iMartinezMateu,项目名称:openbravo-pos,代码行数:18,代码来源:JettyHttpExchange.java

示例11: handle

import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
public void handle(HttpExchange t) throws IOException {
	
	{
		Headers h = t.getResponseHeaders();
		String type="text/plain";
		h.add("Content-Type", type);
	}
	
	String response=USAGE;
	if(verbose2){System.out.println("Sending: "+response);}
	
	t.sendResponseHeaders(200, response.length());
	OutputStream os = t.getResponseBody();
	os.write(response.getBytes());
	os.close();
}
 
开发者ID:abiswas-odu,项目名称:Disco,代码行数:17,代码来源:TaxServer.java

示例12: sendResponse

import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
private void sendResponse(HttpExchange httpExchange, Response response) {
    try (OutputStream os = httpExchange.getResponseBody()) {
        byte[] bytes = response.getBody().getBytes();

        Headers headers = httpExchange.getResponseHeaders();
        headers.add(Constants.REQUEST_HEADER_ACCESS_CONTROL,"*");
        httpExchange.sendResponseHeaders(response.getStatusCode(), bytes.length);

        os.write( bytes);
        // there is no need to close stream manually
        // as try-catch with auto-closable is used
        /**
         * {@see http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html}
         */
    } catch (IOException e) {
        logger.error("Could not send response", e);
    }
}
 
开发者ID:exadel-edu,项目名称:lab-materials,代码行数:19,代码来源:ServerHandler.java

示例13: sendResponse

import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
private void sendResponse(HttpExchange httpExchange, Response response) {
    try (OutputStream os = httpExchange.getResponseBody()) {
        byte[] bytes = response.getBody().getBytes();

        Headers headers = httpExchange.getResponseHeaders();
        headers.add(Constants.REQUEST_HEADER_ACCESS_CONTROL_ORIGIN,"*");
        httpExchange.sendResponseHeaders(response.getStatusCode(), bytes.length);

        os.write( bytes);
        // there is no need to close stream manually
        // as try-catch with auto-closable is used
        /**
         * {@see http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html}
         */
    } catch (IOException e) {
        logger.error("Could not send response", e);
    }
}
 
开发者ID:exadel-edu,项目名称:lab-materials,代码行数:19,代码来源:ServerHandler.java

示例14: CORS

import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
private static boolean CORS(HttpExchange httpExchange) throws IOException {
    Headers headers = httpExchange.getResponseHeaders();
    headers.add("Access-Control-Allow-Headers", "Content-Type");
    headers.add("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE");
    headers.add("Access-Control-Allow-Origin", httpExchange.getRequestHeaders().getFirst("origin"));
    headers.add("Access-Control-Max-Age", "3600");

    if (!httpExchange.getRequestMethod().equals("OPTIONS")) return false;
    httpExchange.sendResponseHeaders(200, 0);
    write(httpExchange, "");
    return true;
}
 
开发者ID:ibaca,项目名称:autorest-streaming-example,代码行数:13,代码来源:Sun.java

示例15: handle

import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
@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();
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:11,代码来源:UnmodifiableMaps.java


注:本文中的com.sun.net.httpserver.Headers.add方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。