當前位置: 首頁>>代碼示例>>Java>>正文


Java Headers.getFirst方法代碼示例

本文整理匯總了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");
}
 
開發者ID:linkedin,項目名稱:pinot,代碼行數:25,代碼來源:FileUploadDownloadClientTest.java

示例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");
}
 
開發者ID:EricssonResearch,項目名稱:trap,代碼行數:17,代碼來源:CORSUtil.java

示例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;
}
 
開發者ID:fhoeben,項目名稱:hsac-fitnesse-fixtures,代碼行數:17,代碼來源:HttpServer.java

示例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();
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:12,代碼來源:SimpleHttpServer.java

示例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;
}
 
開發者ID:gskorupa,項目名稱:Cricket,代碼行數:32,代碼來源:CorsProcessor.java

示例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;
    }
}
 
開發者ID:leonamp,項目名稱:SPFBL,代碼行數:31,代碼來源:ServerHTTP.java

示例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);
    }

}
 
開發者ID:GloriousEggroll,項目名稱:quorrabot,代碼行數:73,代碼來源:HTTPSServer.java

示例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);
}
 
開發者ID:leonamp,項目名稱:SPFBL,代碼行數:6,代碼來源:ServerHTTP.java

示例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;
	}
}
 
開發者ID:cping,項目名稱:RipplePower,代碼行數:56,代碼來源:RpcHandler.java

示例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);
}
 
開發者ID:Netflix,項目名稱:spectator,代碼行數:5,代碼來源:DefaultHttpClientTest.java


注:本文中的com.sun.net.httpserver.Headers.getFirst方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。