本文整理匯總了Java中com.sun.net.httpserver.Headers.get方法的典型用法代碼示例。如果您正苦於以下問題:Java Headers.get方法的具體用法?Java Headers.get怎麽用?Java Headers.get使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.sun.net.httpserver.Headers
的用法示例。
在下文中一共展示了Headers.get方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: 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();
}
示例2: handle
import com.sun.net.httpserver.Headers; //導入方法依賴的package包/類
public void handle(HttpExchange exchange) throws IOException
{
String requestMethod = exchange.getRequestMethod();
if(requestMethod.equalsIgnoreCase("GET"))
{
Headers responseHeaders = exchange.getResponseHeaders();
responseHeaders.set("Content-Type", "text/plain");
exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, 0);
OutputStream responseBody = exchange.getResponseBody();
Headers requestHeaders = exchange.getRequestHeaders();
Set<String> keySet = requestHeaders.keySet();
Iterator<String> iter = keySet.iterator();
while (iter.hasNext()){
String key = iter.next();
List values = requestHeaders.get(key);
String s = key + " = " + values.toString() + "\n";
responseBody.write(s.getBytes());
}
responseBody.close();
}
}
示例3: getCookie
import com.sun.net.httpserver.Headers; //導入方法依賴的package包/類
private String getCookie(HttpExchange r, String name) {
Headers headers = r.getRequestHeaders();
if (headers != null) {
List<String> cookies = headers.get("Cookie");
if (cookies != null) {
for (String cookieString : cookies) {
String[] tokens = cookieString.split("\\s*;\\s*");
for (String token : tokens) {
if (token.startsWith(name) && token.charAt(name.length()) == '=') {
return token.substring(name.length() + 1);
}
}
}
}
}
return null;
}
示例4: validateZeroOrOneInstanceOfRequestHeader
import com.sun.net.httpserver.Headers; //導入方法依賴的package包/類
@Nullable @NonNls
public static String validateZeroOrOneInstanceOfRequestHeader(@NotNull final Headers requestHeaders, @NotNull @RequestHeader final String requestHeader) throws BadRequestException
{
@Nullable final List<String> strings = requestHeaders.get(requestHeader);
if (strings == null)
{
return null;
}
final int size = strings.size();
if (size == 1)
{
return strings.get(0);
}
if (size == 0)
{
return null;
}
throw new BadRequestException(format(ENGLISH, "More than one value for Request header %1$s", requestHeader));
}
開發者ID:health-and-care-developer-network,項目名稱:health-and-care-developer-network,代碼行數:20,代碼來源:RequestHeadersHelper.java
示例5: parseRange
import com.sun.net.httpserver.Headers; //導入方法依賴的package包/類
public static Range.Byte parseRange(Headers hdr, long len) {
if (hdr == null) {
return nullRange(len);
}
List<String> r = hdr.get("Range");
if (r == null) { // no range
return nullRange(len);
}
// assume only one
String range = r.get(0);
String[] tmp = range.split("=")[1].split("-");
long start = Long.parseLong(tmp[0]);
long end = tmp.length == 1 ? len : Long.parseLong(tmp[1]);
return new Range.Byte(start, end);
}
示例6: getHeader
import com.sun.net.httpserver.Headers; //導入方法依賴的package包/類
static String getHeader(HttpExchange exchange, String header) {
Headers h = exchange.getRequestHeaders();
List<String> values = h.get(header);
if (values == null || values.size() == 0)
return null;
return values.get(0);
}
示例7: handle
import com.sun.net.httpserver.Headers; //導入方法依賴的package包/類
@Override
public void handle(HttpExchange exchange) throws IOException {
switch (exchange.getRequestMethod()) {
// POST requests.
case "POST":
DataInputStream in = new DataInputStream(exchange.getRequestBody());
String[] params = in.readUTF().split(",");
orders.add(new Order(params[0], params[1], new BigDecimal(params[2]), new BigDecimal(params[3]), params[4]));
exchange.sendResponseHeaders(200, 0);
// GET requests.
case "GET":
orders.get(orders.indexOf(new Order("BTC", "", BigDecimal.ZERO, BigDecimal.ZERO, "")));
textArea.appendText(exchange.getRequestURI().getQuery() + "\n");
Headers responseHeaders = exchange.getResponseHeaders();
responseHeaders.set("Content-Type", "text/plain");
exchange.sendResponseHeaders(200, 0);
OutputStream responseBody = exchange.getResponseBody();
Headers requestHeaders = exchange.getRequestHeaders();
Set<String> keySet = requestHeaders.keySet();
Iterator<String> iter = keySet.iterator();
while (iter.hasNext()) {
String key = iter.next();
List<String> values = requestHeaders.get(key);
String s = key + " = " + values.toString() + "\n";
responseBody.write(s.getBytes());
}
responseBody.close();
// Unknown requests.
default:
exchange.sendResponseHeaders(400, 0);
}
}
示例8: handle
import com.sun.net.httpserver.Headers; //導入方法依賴的package包/類
@Override
public void handle(HttpExchange exchange) throws IOException
{
String requestMethod = exchange.getRequestMethod();
if (requestMethod.equalsIgnoreCase("GET"))
{
Headers responseHeaders = exchange.getResponseHeaders();
responseHeaders.set("Content-Type", "text/plain");
exchange.sendResponseHeaders(200, 0);
OutputStream responseBody = exchange.getResponseBody();
Headers requestHeaders = exchange.getRequestHeaders();
Set<String> keySet = requestHeaders.keySet();
Iterator<String> iter = keySet.iterator();
while (iter.hasNext()) {
String key = iter.next();
List<String> values = requestHeaders.get(key);
String s = key + " = " + values.toString() + "\n";
responseBody.write(s.getBytes());
}
responseBody.close();
if (sharedQueue != null)
{
String query = exchange.getRequestURI().getQuery() ;
try
{
sharedQueue.put(queryToParameterMap(query));
}
catch (InterruptedException e)
{
logger.error("Error occurred in the server handler", e);
}
}
}
}
示例9: handle
import com.sun.net.httpserver.Headers; //導入方法依賴的package包/類
@Override
public void handle(HttpExchange t) throws IOException {
if (t.getRequestMethod().equals("POST")) {
Headers headers = t.getRequestHeaders();
String boundary = "boundary=";
for (String s : headers.get("Content-type")) {
int i = s.indexOf(boundary);
if (i > -1) {
boundary = s.substring(i + boundary.length());
break;
}
}
String id = writeConfigFile(t.getRequestBody(), boundary);
jobs.put(Long.parseLong(id), 0);
String response = id + "\n";
t.sendResponseHeaders(200, response.length());
logger.info("New Job: " + id);
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
AConfigurationReader reader = new XMLConfigurationReader(STORAGE_DIR_PATH + CONFIG_FILE_PREFIX + id + ".xml");
Configuration config = reader.read();
jobs.put(Long.parseLong(id), 1);
ResultMappings mappings = Controller.getMapping(config);
String outputFormat = config.getOutputFormat();
ISerializer output = SerializerFactory.createSerializer(outputFormat);
output.setPrefixes(config.getPrefixes());
File tempDir = new File(STORAGE_DIR_PATH + id + "/");
File lockDir = new File(tempDir + LOCK_DIR_PATH);
if (!lockDir.exists()) {
lockDir.mkdirs();
}
File _verificationFile = new File(lockDir + "/" + config.getVerificationFile());
File _acceptanceFile = new File(lockDir + "/" + config.getAcceptanceFile());
File verificationFile = new File(tempDir + "/" + config.getVerificationFile());
File acceptanceFile = new File(tempDir + "/" + config.getAcceptanceFile());
output.writeToFile(mappings.getVerificationMapping(), config.getVerificationRelation(),
_verificationFile.getAbsolutePath());
output.writeToFile(mappings.getAcceptanceMapping(), config.getAcceptanceRelation(),
_acceptanceFile.getAbsolutePath());
_verificationFile.renameTo(verificationFile);
_acceptanceFile.renameTo(acceptanceFile);
lockDir.delete();
jobs.put(Long.parseLong(id), 2);
} else {
// we only accept POST requests here, anything else gets code "405 - Method Not Allowed"
t.sendResponseHeaders(405 ,-1);
logger.info("Bad request: HTTP VERB must be POST for " + t.getRequestURI());
}
}
示例10: handle
import com.sun.net.httpserver.Headers; //導入方法依賴的package包/類
@Override
public void handle(HttpExchange arg0) throws IOException {
// Retrieve request headers
Headers reqHeaders = arg0.getRequestHeaders();
Iterator<String> iterator = reqHeaders.keySet().iterator();
System.out.println();
while(iterator.hasNext()) {
String key = iterator.next();
List value = reqHeaders.get(key);
System.out.printf("%s:", key);
for(int i = 0; i < value.size(); i++) {
System.out.printf(" %s", value.get(i));
}
System.out.print("\n");
}
InputStreamReader isr = new InputStreamReader(arg0.getRequestBody(), "utf-8");
BufferedReader streamReader = new BufferedReader(isr);
StringBuilder responseStrBuilder = new StringBuilder();
String inputStr;
while((inputStr = streamReader.readLine()) != null) {
responseStrBuilder.append(inputStr);
}
System.out.println("Request received by Customer point updating server.");
String membershipID = "";
int point = 0;
try {
JSONObject object = new JSONObject(responseStrBuilder.toString());
point = object.getInt("updatedPoint");
membershipID = object.get("membershipID").toString();
c = this.store.getMember(membershipID);
c.setRewardsPoint(point);
//c = new Customer(firstName, lastName, customerPhoneNumber, this.store);
} catch(JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(c != null) {
System.out.println("Request received. Customer found with ID:" + c.getMemberShipNumber() + " and point" + store.getMember(membershipID).getRewardsPoint());
Gson gson = new GsonBuilder().create();
String customer = gson.toJson(c);
arg0.sendResponseHeaders(200, customer.length());
OutputStream os = arg0.getResponseBody();
os.write(customer.getBytes());
os.close();
} else {
System.out.println("Request received. But no customer point is updated");
}
}
示例11: handle
import com.sun.net.httpserver.Headers; //導入方法依賴的package包/類
@Override
public void handle(HttpExchange arg0) throws IOException {
// Retrieve request headers
Headers reqHeaders = arg0.getRequestHeaders();
Iterator<String> iterator = reqHeaders.keySet().iterator();
System.out.println();
while(iterator.hasNext()) {
String key = iterator.next();
List value = reqHeaders.get(key);
System.out.printf("%s:", key);
for(int i = 0; i < value.size(); i++) {
System.out.printf(" %s", value.get(i));
}
System.out.print("\n");
}
InputStreamReader isr = new InputStreamReader(arg0.getRequestBody(), "utf-8");
BufferedReader streamReader = new BufferedReader(isr);
StringBuilder responseStrBuilder = new StringBuilder();
String inputStr;
while((inputStr = streamReader.readLine()) != null) {
responseStrBuilder.append(inputStr);
}
Customer c = null;
System.out.println("Request received by Customer creating server.");
try {
JSONObject object = new JSONObject(responseStrBuilder.toString());
String firstName = object.get("firstName").toString();
String lastName = object.get("lastName").toString();
String customerPhoneNumber = object.get("customerPhoneNumber").toString();
c = this.store.addNewMember(firstName, lastName, customerPhoneNumber);
//c = new Customer(firstName, lastName, customerPhoneNumber, this.store);
} catch(JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(c != null) {
System.out.println("Request received. Customer is created with ID:" + c.getMemberShipNumber());
Gson gson = new GsonBuilder().create();
String customer = gson.toJson(c);
arg0.sendResponseHeaders(200, customer.length());
OutputStream os = arg0.getResponseBody();
os.write(customer.getBytes());
os.close();
} else {
System.out.println("Request received. But no customer is created");
}
}