本文整理汇总了Java中com.sun.net.httpserver.Headers类的典型用法代码示例。如果您正苦于以下问题:Java Headers类的具体用法?Java Headers怎么用?Java Headers使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Headers类属于com.sun.net.httpserver包,在下文中一共展示了Headers类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: noTriple
import com.sun.net.httpserver.Headers; //导入依赖的package包/类
private void noTriple(HttpExchange he) {
try {
Headers responseHeaders = he.getResponseHeaders();
FileHandler ext = new FileHandler();
File tmp;
responseHeaders.set(TYPE, PNG);
ext.getFile(NOTRIPLE);
tmp = new File(PAGESDIR + NOTRIPLE);
he.sendResponseHeaders(200, tmp.length());
try (OutputStream os = he.getResponseBody()) {
Files.copy(tmp.toPath(), os);
tmp.delete();
}
} catch (IOException ex) {
Logger.getLogger(CompareHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
示例2: handle
import com.sun.net.httpserver.Headers; //导入依赖的package包/类
@Override
public void handle(HttpExchange httpExchange) throws IOException {
StringBuilder body = new StringBuilder();
try (InputStreamReader reader = new InputStreamReader(httpExchange.getRequestBody(), Consts.UTF_8)) {
char[] buffer = new char[256];
int read;
while ((read = reader.read(buffer)) != -1) {
body.append(buffer, 0, read);
}
}
Headers requestHeaders = httpExchange.getRequestHeaders();
Headers responseHeaders = httpExchange.getResponseHeaders();
for (Map.Entry<String, List<String>> header : requestHeaders.entrySet()) {
responseHeaders.put(header.getKey(), header.getValue());
}
httpExchange.getRequestBody().close();
httpExchange.sendResponseHeaders(statusCode, body.length() == 0 ? -1 : body.length());
if (body.length() > 0) {
try (OutputStream out = httpExchange.getResponseBody()) {
out.write(body.toString().getBytes(Consts.UTF_8));
}
}
httpExchange.close();
}
示例3: handle
import com.sun.net.httpserver.Headers; //导入依赖的package包/类
@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();
}
示例4: setCors
import com.sun.net.httpserver.Headers; //导入依赖的package包/类
public static void setCors(Headers requestHeaders, Headers responseHeaders)
{
String origin = requestHeaders.getFirst("Origin");
if (origin == null)
origin = "null";
responseHeaders.set("Allow", "GET,PUT,POST,DELETE,OPTIONS");
responseHeaders.set("Access-Control-Allow-Origin", origin);
responseHeaders.set("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS");
responseHeaders.set("Access-Control-Allow-Headers", "Content-Type");
responseHeaders.set("Access-Control-Request-Methods", "GET,PUT,POST,DELETE,OPTIONS");
responseHeaders.set("Access-Control-Request-Headers", "Content-Type");
responseHeaders.set("Access-Control-Max-Age", "3600");
}
示例5: 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();
}
示例6: parseMetrics
import com.sun.net.httpserver.Headers; //导入依赖的package包/类
@Test
public void parseMetrics() throws IOException {
final Counter counter = Counter
.build("testcounter", "testdescription")
.labelNames("one", "two")
.register();
counter.labels("1", "2").inc();
final HttpExchange mockExchange = mock(HttpExchange.class);
final OutputStream outputStream = new ByteArrayOutputStream();
when(mockExchange.getResponseBody()).thenReturn(outputStream);
final Headers headers = new Headers();
when(mockExchange.getResponseHeaders()).thenReturn(headers);
new PrometheusWebConsole().getServlet().handle(mockExchange);
assertEquals(TextFormat.CONTENT_TYPE_004, headers.get("Content-Type").get(0));
final String response = outputStream.toString();
assertTrue(response.contains("# HELP testcounter testdescription"));
assertTrue(response.contains("# TYPE testcounter counter"));
assertTrue(response.contains("one=\"1\""));
assertTrue(response.contains("two=\"2\""));
}
示例7: handle
import com.sun.net.httpserver.Headers; //导入依赖的package包/类
@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();
}
示例8: 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();
}
}
示例9: 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();
}
}
示例10: 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();
}
示例11: 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();
}
示例12: 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();
}
示例13: 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();
}
示例14: 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;
}
示例15: 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();
}
}