本文整理汇总了Java中com.sun.net.httpserver.Headers.getFirst方法的典型用法代码示例。如果您正苦于以下问题:Java Headers.getFirst方法的具体用法?Java Headers.getFirst怎么用?Java Headers.getFirst使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.net.httpserver.Headers
的用法示例。
在下文中一共展示了Headers.getFirst方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: handle
import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
@Override
public void handle(HttpExchange httpExchange) throws IOException {
Headers requestHeaders = httpExchange.getRequestHeaders();
String uploadTypeStr = requestHeaders.getFirst(FileUploadDownloadClient.CustomHeaders.UPLOAD_TYPE);
FileUploadType uploadType = FileUploadType.valueOf(uploadTypeStr);
String downloadUri = null;
if (uploadType == FileUploadType.JSON) {
InputStream bodyStream = httpExchange.getRequestBody();
try {
JSONObject jsonObject = new JSONObject(IOUtils.toString(bodyStream, "UTF-8"));
downloadUri = (String) jsonObject.get(CommonConstants.Segment.Offline.DOWNLOAD_URL);
} catch (JSONException e) {
throw new RuntimeException(e);
}
Assert.assertEquals(downloadUri, TEST_URI);
} else if (uploadType == FileUploadType.URI) {
downloadUri = requestHeaders.getFirst(FileUploadDownloadClient.CustomHeaders.DOWNLOAD_URI);
} else {
Assert.fail();
}
Assert.assertEquals(downloadUri, TEST_URI);
sendResponse(httpExchange, HttpStatus.SC_OK, "OK");
}
示例2: 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");
}
示例3: getCharSet
import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
protected Charset getCharSet(Headers heHeaders) {
Charset charset = UTF8;
String contentTypeHeader = heHeaders.getFirst(HTTP.CONTENT_TYPE);
if (contentTypeHeader != null) {
try {
ContentType contentType = ContentType.parse(contentTypeHeader);
Charset contentTypeCharset = contentType.getCharset();
if (contentTypeCharset != null) {
charset = contentTypeCharset;
}
} catch (ParseException | UnsupportedCharsetException e) {
// ignore, use default charset UTF8
}
}
return charset;
}
示例4: validateNoContentLength
import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
private static void validateNoContentLength(final Headers requestHeaders) throws BadRequestException
{
@NonNls @Nullable final String contentLength = requestHeaders.getFirst(ContentLengthHeaderName);
final boolean hasContent = contentLength != null && !"0".equals(contentLength);
if (hasContent)
{
throw new BadRequestException("HEAD and GET should not have a non-zero Content-Length header");
}
}
开发者ID:health-and-care-developer-network,项目名称:health-and-care-developer-network,代码行数:10,代码来源:AbstractHeadOrGetRegisterableMethodEndpoint.java
示例5: moved
import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
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();
}
示例6: getResponseHeaders
import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
public static Headers getResponseHeaders(Headers responseHeaders, Headers requestHeaders, ArrayList corsConfig) {
String origin = requestHeaders.getFirst("Origin");
if(origin==null){
origin = requestHeaders.getFirst("Referer"); //TODO: this is workaround - see HttpAdapter 188
}
if(origin==null){
}
// boolean withCredentials = "true".equals(requestHeaders.getFirst("Access-Control-Allow-Credentials"));
HttpHeader h;
//if (!withCredentials) {
// for (int i = 0; i < corsConfig.size(); i++) {
// h = (HttpHeader) corsConfig.get(i);
// responseHeaders.set(h.name, h.value);
// }
//}else{
for (int i = 0; i < corsConfig.size(); i++) {
h = (HttpHeader) corsConfig.get(i);
if("Access-Control-Allow-Origin".equals(h.name)){
if("*".equals(h.value) && origin!=null){
responseHeaders.set(h.name, origin);
}else{
responseHeaders.set(h.name, h.value);
}
}else{
responseHeaders.set(h.name, h.value);
}
}
//}
return responseHeaders;
}
示例7: getUser
import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
private static User getUser(HttpExchange exchange) {
Headers headers = exchange.getRequestHeaders();
String cookies = headers.getFirst("Cookie");
if (cookies == null) {
return null;
} else {
StringTokenizer tokenizer = new StringTokenizer(cookies, ";");
while (tokenizer.hasMoreTokens()) {
try {
String cookie = tokenizer.nextToken().trim();
if (cookie.startsWith("login=")) {
int index = cookie.indexOf('=');
String registry = Server.decrypt(cookie.substring(index + 1).trim());
StringTokenizer tokenizer2 = new StringTokenizer(registry, " ");
Date date = Server.parseTicketDate(tokenizer2.nextToken());
if (System.currentTimeMillis() - date.getTime() < 604800000) {
String email = tokenizer2.nextToken();
InetAddress ticketAddress = InetAddress.getByName(tokenizer2.nextToken());
if (exchange.getRemoteAddress().getAddress().equals(ticketAddress)) {
return User.get(email);
}
}
}
} catch (Exception ex) {
// Nada deve ser feito.
}
}
return null;
}
}
示例8: handle
import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
public void handle(HttpExchange httpExchange) throws IOException {
HttpsExchange exchange = (HttpsExchange) httpExchange;
Boolean hasPassword = false;
String myPassword = "";
String myHdrUser = "";
String myHdrMessage = "";
String[] uriQueryList = null;
// Get the path and query string from the URI
URI uriData = exchange.getRequestURI();
String uriPath = uriData.getPath();
String uriQuery = uriData.getQuery();
if (uriQuery != null) {
uriQueryList = uriQuery.split("&");
}
// Get the headers
Headers headers = exchange.getRequestHeaders();
// Get the Request Method (GET/PUT)
String requestMethod = exchange.getRequestMethod();
// Get any data from the body, although, we just discard it, this is required
InputStream inputStream = exchange.getRequestBody();
while (inputStream.read() != -1) {
inputStream.skip(0x10000);
}
inputStream.close();
if (headers.containsKey("password")) {
myPassword = headers.getFirst("password");
if (myPassword.equals(serverPassword) || myPassword.equals("oauth:" + serverPassword)) {
hasPassword = true;
}
}
if (headers.containsKey("webauth")) {
myPassword = headers.getFirst("webauth");
if (myPassword.equals(serverWebAuth)) {
hasPassword = true;
}
}
if (headers.containsKey("user")) {
myHdrUser = headers.getFirst("user");
}
if (headers.containsKey("message")) {
myHdrMessage = headers.getFirst("message");
}
if (requestMethod.equals("GET")) {
if (uriPath.contains("..")) {
sendHTMLError(403, "Invalid URL", exchange);
} else if (uriPath.startsWith("/inistore")) {
handleIniStore(uriPath, exchange, hasPassword);
} else if (uriPath.startsWith("/dbquery")) {
handleDBQuery(uriPath, uriQueryList, exchange, hasPassword);
} else if (uriPath.startsWith("/addons") || uriPath.startsWith("/logs")) {
handleFile(uriPath, exchange, hasPassword, true);
} else if (uriPath.equals("/playlist")) {
handleFile("/web/playlist/index.html", exchange, hasPassword, false);
} else if (uriPath.equals("/")) {
handleFile("/web/index.html", exchange, hasPassword, false);
} else {
handleFile("/web" + uriPath, exchange, hasPassword, false);
}
}
if (requestMethod.equals("PUT")) {
handlePutRequest(myHdrUser, myHdrMessage, exchange, hasPassword);
}
}
示例9: getLocale
import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
private static Locale getLocale(HttpExchange exchange) {
Headers headers = exchange.getRequestHeaders();
String acceptLanguage = headers.getFirst("Accept-Language");
return getLocale(acceptLanguage);
}
示例10: handle
import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
/**
* Handle an HTTP request
*
* @param exchange
* HTTP exchange
* @throws IOException
* Error detected while handling the request
*/
@Override
public void handle(HttpExchange exchange) throws IOException {
try {
int responseCode;
String responseBody;
//
// Get the HTTP request
//
InetSocketAddress requestAddress = exchange.getRemoteAddress();
String requestMethod = exchange.getRequestMethod();
Headers requestHeaders = exchange.getRequestHeaders();
String contentType = requestHeaders.getFirst("Content-Type");
Headers responseHeaders = exchange.getResponseHeaders();
BTCLoader.debug(String.format("%s request received from %s", requestMethod, requestAddress.getAddress()));
if (!rpcAllowIp.contains(requestAddress.getAddress())) {
responseCode = HttpURLConnection.HTTP_UNAUTHORIZED;
responseBody = "Your IP address is not authorized to access this server";
responseHeaders.set("Content-Type", "text/plain");
} else if (!exchange.getRequestMethod().equals("POST")) {
responseCode = HttpURLConnection.HTTP_BAD_METHOD;
responseBody = String.format("%s requests are not supported", exchange.getRequestMethod());
responseHeaders.set("Content-Type", "text/plain");
} else if (contentType == null || !contentType.equals("application/json-rpc")) {
responseCode = HttpURLConnection.HTTP_BAD_REQUEST;
responseBody = "Content type must be application/json-rpc";
responseHeaders.set("Content-Type", "text/plain");
} else {
responseBody = processRequest(exchange);
responseCode = HttpURLConnection.HTTP_OK;
responseHeaders.set("Content-Type", "application/json-rpc");
}
//
// Return the HTTP response
//
responseHeaders.set("Cache-Control", "no-cache, no-store, must-revalidate, private");
responseHeaders.set("Server", "JavaBitcoin");
byte[] responseBytes = responseBody.getBytes("UTF-8");
exchange.sendResponseHeaders(responseCode, responseBytes.length);
try (OutputStream out = exchange.getResponseBody()) {
out.write(responseBytes);
}
BTCLoader.debug(String.format("RPC request from %s completed", requestAddress.getAddress()));
} catch (IOException exc) {
BTCLoader.error("Unable to process RPC request", exc);
throw exc;
}
}
示例11: getInt
import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
private static int getInt(Headers headers, String k, int dflt) {
String v = headers.getFirst(k);
return (v == null) ? dflt : Integer.parseInt(v);
}