io.vertx.core.MultiMap类的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(14.2k)|赞(0)|评价(0)|浏览(264)

本文整理了Java中io.vertx.core.MultiMap类的一些代码示例,展示了MultiMap类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MultiMap类的具体详情如下:
包路径:io.vertx.core.MultiMap
类名称:MultiMap

MultiMap介绍

[英]This class represents a MultiMap of String keys to a List of String values.

It's useful in Vert.x to represent things in Vert.x like HTTP headers and HTTP parameters which allow multiple values for keys.
[中]此类表示字符串键到字符串值列表的多重映射。
它在Vert中很有用。x表示垂直方向的事物。类似于x的HTTP头和HTTP参数,允许密钥具有多个值。

代码示例

代码示例来源:origin: vert-x3/vertx-examples

@Override
public void start() throws Exception {
 Router router = Router.router(vertx);
 allowedMethods.add(HttpMethod.PUT);
 router.route().handler(CorsHandler.create("*").allowedHeaders(allowedHeaders).allowedMethods(allowedMethods));
 router.get("/access-control-with-get").handler(ctx -> {
  HttpServerResponse httpServerResponse = ctx.response();
  httpServerResponse.setChunked(true);
  MultiMap headers = ctx.request().headers();
  for (String key : headers.names()) {
   httpServerResponse.write(key + ": ");
   httpServerResponse.write(headers.get(key));
   httpServerResponse.write("<br>");
 });
 router.post("/access-control-with-post-preflight").handler(ctx -> {
  HttpServerResponse httpServerResponse = ctx.response();
  httpServerResponse.setChunked(true);
  MultiMap headers = ctx.request().headers();
  for (String key : headers.names()) {
   httpServerResponse.write(key + ": ");
   httpServerResponse.write(headers.get(key));
   httpServerResponse.write("<br>");

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testGetShouldReturnAddedEntriesInOrder() {
 MultiMap mmap = newMultiMap();
 mmap.add("header", "value1");
 mmap.add("header", "value2");
 mmap.add("header", "value3");
 assertEquals("value1", mmap.get("header"));
 assertEquals(Arrays.asList("value1", "value2", "value3"), mmap.getAll("header"));
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testHeadCanSetContentLength() {
 server.requestHandler(req -> {
  assertEquals(HttpMethod.HEAD, req.method());
  // Head never contains a body but it can contain a Content-Length header
  // Since headers from HEAD must correspond EXACTLY with corresponding headers for GET
  req.response().headers().set("Content-Length", String.valueOf(41));
  req.response().end();
 });
 server.listen(onSuccess(s -> {
  client.request(HttpMethod.HEAD, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> {
   assertEquals("41", resp.headers().get("Content-Length"));
   resp.endHandler(v -> testComplete());
  })).end();
 }));
 await();
}

代码示例来源:origin: vert-x3/vertx-examples

@Override
public void start() throws Exception {
 vertx.createHttpServer().requestHandler(req -> {
  System.out.println("Got request " + req.uri());
  for (String name : req.headers().names()) {
   System.out.println(name + ": " + req.headers().get(name));
  }
  req.handler(data -> System.out.println("Got data " + data.toString("ISO-8859-1")));
  req.endHandler(v -> {
   // Now send back a response
   req.response().setChunked(true);
   for (int i = 0; i < 10; i++) {
    req.response().write("server-data-chunk-" + i);
   }
   req.response().end();
  });
 }).listen(8282);
}

代码示例来源:origin: vert-x3/vertx-examples

@Override
 public void start() throws Exception {
  vertx.createHttpServer().requestHandler(req -> {
   if (req.uri().equals("/")) {
    // Serve the index page
    req.response().sendFile("index.html");
   } else if (req.uri().startsWith("/form")) {
    req.response().setChunked(true);
    req.setExpectMultipart(true);
    req.endHandler((v) -> {
     for (String attr : req.formAttributes().names()) {
      req.response().write("Got attr " + attr + " : " + req.formAttributes().get(attr) + "\n");
     }
     req.response().end();
    });
   } else {
    req.response().setStatusCode(404).end();
   }
  }).listen(8080);
 }
}

代码示例来源:origin: vert-x3/vertx-web

@Test
public void testPattern6() throws Exception {
 router.route("/blah/:abc/:def/:ghi/blah").handler(rc -> {
  MultiMap params = rc.request().params();
  rc.response().setStatusMessage(params.get("abc") + params.get("def") + params.get("ghi")).end();
 });
 testPattern("/blah/tim/julien/nick/blah", "timjuliennick");
}

代码示例来源:origin: vert-x3/vertx-web

@Test
public void testPreflightAllowedHeaders() throws Exception {
 Set<HttpMethod> allowedMethods = new LinkedHashSet<>(Arrays.asList(HttpMethod.PUT, HttpMethod.DELETE));
 Set<String> allowedHeaders = new LinkedHashSet<>(Arrays.asList("X-wibble", "X-blah"));
 router.route().handler(CorsHandler.create("vertx\\.io").allowedMethods(allowedMethods).allowedHeaders(allowedHeaders));
 router.route().handler(context -> context.response().end());
 testRequest(HttpMethod.OPTIONS, "/", req -> {
  req.headers().add("origin", "vertx.io");
  req.headers().add("access-control-request-method", "PUT,DELETE");
  req.headers().add("access-control-request-headers", allowedHeaders);
 }, resp -> checkHeaders(resp, "vertx.io", "PUT,DELETE", "X-wibble,X-blah", null), 200, "OK", null);
}

代码示例来源:origin: vert-x3/vertx-examples

@Override
public void start() throws Exception {
 Router router = Router.router(vertx);
 router.get("/api/newToken").handler(ctx -> {
  List<String> authorities = new ArrayList<>();
  for (String authority : ctx.request().params().getAll("authority")) {
   authorities.add(authority);
  ctx.response().putHeader("Content-Type", "text/plain");
  ctx.response().end(jwt.generateToken(new JsonObject(), new JWTOptions().setExpiresInSeconds(60).setPermissions
    (authorities)));
 });
 router.route("/api/protected*").handler(JWTAuthHandler.create(jwt));
 router.get("/api/protected").handler(ctx -> {
  ctx.response().putHeader("Content-Type", "text/plain");
  ctx.response().end("this secret is not defcon!");
 });

代码示例来源:origin: eclipse-vertx/vert.x

lastRequestHeaders = MultiMap.caseInsensitiveMultiMap().addAll(request.headers());
if (error != 0) {
 request.response().setStatusCode(error).end("proxy request failed");
} else if (method == HttpMethod.CONNECT) {
 if (!uri.contains(":")) {
  if (ar.succeeded()) {
   HttpClientResponse resp = ar.result();
   for (String name : resp.headers().names()) {
    request.response().putHeader(name, resp.headers().getAll(name));
   resp.bodyHandler(body -> {
    request.response().end(body);
   });
 for (String name : request.headers().names()) {
  if (!name.equals("Proxy-Authorization")) {
   clientRequest.putHeader(name, request.headers().getAll(name));
 clientRequest.end();
} else {
 request.response().setStatusCode(405).end("method not supported");

代码示例来源:origin: eclipse-vertx/vert.x

AtomicInteger reqCount = new AtomicInteger();
server.requestHandler(req -> {
 assertEquals("https", req.scheme());
 assertEquals(HttpMethod.GET, req.method());
 assertEquals("/somepath", req.path());
 assertEquals(2, req.headers().getAll("juu_request").size());
 assertEquals("juu_request_value_1", req.headers().getAll("juu_request").get(0));
 assertEquals("juu_request_value_2", req.headers().getAll("juu_request").get(1));
 resp.putHeader("content-type", "text/plain");
 resp.putHeader("Foo_response", "foo_value");
 resp.putHeader("bar_response", "bar_value");
  assertEquals(2, resp.headers().getAll("juu_response").size());
  assertEquals("juu_value_1", resp.headers().getAll("juu_response").get(0));
  assertEquals("juu_value_2", resp.headers().getAll("juu_response").get(1));
   testComplete();
  });
 })).putHeader("Foo_request", "foo_request_value")
  .putHeader("bar_request", "bar_request_value")

代码示例来源:origin: vert-x3/vertx-examples

@Override
public void start() throws Exception {
 Router router = Router.router(vertx);
 router.get("/api/newToken").handler(ctx -> {
  for (String authority : ctx.request().params().getAll("authority")) {
   authorities.add(authority);
  ctx.response().putHeader("Content-Type", "text/plain");
  ctx.response().end(jwt.generateToken(new JsonObject(), new JWTOptions().setExpiresInSeconds(60).setPermissions
    (authorities)));
 });
 router.route("/api/protected").handler(JWTAuthHandler.create(jwt));
 router.get("/api/protected").handler(ctx -> {
  ctx.response().putHeader("Content-Type", "text/plain");
  ctx.response().end("this secret is not defcon!");
 });

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testContainsValueString() {
 server.requestHandler(req -> {
  assertTrue(req.headers().contains("Foo", "foo", false));
  assertFalse(req.headers().contains("Foo", "fOo", false));
  req.response().putHeader("quux", "quux");
  req.response().end();
 });
 server.listen(onSuccess(server -> {
  HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> {
   assertTrue(resp.headers().contains("Quux", "quux", false));
   assertFalse(resp.headers().contains("Quux", "quUx", false));
   testComplete();
  }));
  req.putHeader("foo", "foo");
  req.end();
 }));
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testHeadButCanSetContentLength() {
 server.requestHandler(req -> {
  assertEquals(HttpMethod.HEAD, req.method());
  // By default HEAD does not have a content-length header but it can contain a content-length header
  // if explicitly set
  req.response().putHeader(HttpHeaders.CONTENT_LENGTH, "41").end();
 });
 server.listen(onSuccess(s -> {
  client.request(HttpMethod.HEAD, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> {
   assertEquals("41", resp.headers().get(HttpHeaders.CONTENT_LENGTH));
   resp.endHandler(v -> testComplete());
  })).end();
 }));
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

private void testHttpProxyRequest2(Function<Handler<AsyncResult<HttpClientResponse>>, HttpClientRequest> reqFact) throws Exception {
 server.requestHandler(req -> {
  req.response().end();
 });
 server.listen(onSuccess(s -> {
  HttpClientRequest req = reqFact.apply(onSuccess(resp -> {
   assertEquals(200, resp.statusCode());
   assertNotNull("request did not go through proxy", proxy.getLastUri());
   assertEquals("Host header doesn't contain target host", "localhost:8080", proxy.getLastRequestHeaders().get("Host"));
   testComplete();
  }));
  req.exceptionHandler(this::fail);
  req.end();
 }));
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

protected MultiMap checkEmptyHttpResponse(HttpMethod method, int sc, MultiMap reqHeaders) throws Exception {
 server.requestHandler(req -> {
  HttpServerResponse resp = req.response();
  resp.setStatusCode(sc);
  resp.headers().addAll(reqHeaders);
  resp.end();
 });
 startServer();
 try {
  CompletableFuture<MultiMap> result = new CompletableFuture<>();
  client.request(method, DEFAULT_HTTP_PORT, DEFAULT_HTTPS_HOST, "/", onSuccess(resp -> {
   Buffer body = Buffer.buffer();
   resp.exceptionHandler(result::completeExceptionally);
   resp.handler(body::appendBuffer);
   resp.endHandler(v -> {
    if (body.length() > 0) {
     result.completeExceptionally(new Exception());
    } else {
     result.complete(resp.headers());
    }
   });
  })).setFollowRedirects(false)
   .exceptionHandler(result::completeExceptionally)
   .end();
  return result.get(20, TimeUnit.SECONDS);
 } finally {
  client.close();
 }
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testSkipEncoding() throws Exception {
 serverWithMaxCompressionLevel.requestHandler(req -> {
  assertNotNull(req.headers().get("Accept-Encoding"));
  req.response()
   .putHeader(HttpHeaders.CONTENT_ENCODING, HttpHeaders.IDENTITY)
   .end(Buffer.buffer(COMPRESS_TEST_STRING).toString(CharsetUtil.UTF_8));
 });
 startServer(serverWithMaxCompressionLevel);
 clientraw.get(DEFAULT_HTTP_PORT + 1, DEFAULT_HTTP_HOST, "some-uri",
  onSuccess(resp -> {
   resp.bodyHandler(responseBuffer -> {
    String responseBody = responseBuffer.toString(CharsetUtil.UTF_8);
    assertEquals(COMPRESS_TEST_STRING, responseBody);
    testComplete();
   });
  })).putHeader(HttpHeaders.ACCEPT_ENCODING, HttpHeaders.DEFLATE_GZIP).end();
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testPutHeadersOnRequest() {
 server.requestHandler(req -> {
  assertEquals("bar", req.headers().get("foo"));
  assertEquals("bar", req.getHeader("foo"));
  req.response().end();
 });
 server.listen(onSuccess(server -> {
  client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> {
   assertEquals(200, resp.statusCode());
   testComplete();
  })).putHeader("foo", "bar").end();
 }));
 await();
}

代码示例来源:origin: vert-x3/vertx-web

@Test
public void testDuplicateParams() throws Exception {
 router.route("/test/:p").handler(RoutingContext::next);
 router.route("/test/:p").handler(RoutingContext::next);
 router.route("/test/:p").handler(routingContext -> {
  assertEquals(1, routingContext.request().params().getAll("p").size());
  assertEquals("abc", routingContext.request().getParam("p"));
  routingContext.response().end();
 });
 testRequest(HttpMethod.GET, "/test/abc", 200, "OK");
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testRequestHeadersWithCharSequence() {
 HashMap<CharSequence, String> headers = new HashMap<>();
 headers.put(HttpHeaders.TEXT_HTML, "text/html");
 headers.put(HttpHeaders.USER_AGENT, "User-Agent");
 headers.put(HttpHeaders.APPLICATION_X_WWW_FORM_URLENCODED, "application/x-www-form-urlencoded");
 server.requestHandler(req -> {
  assertTrue(headers.size() < req.headers().size());
  headers.forEach((k, v) -> assertEquals(v, req.headers().get(k)));
  headers.forEach((k, v) -> assertEquals(v, req.getHeader(k)));
  req.response().end();
 });
 server.listen(onSuccess(server -> {
  HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> testComplete());
  headers.forEach((k, v) -> req.headers().add(k, v));
  req.end();
 }));
 await();
}

代码示例来源:origin: vert-x3/vertx-web

@Test
public void testRegex1() throws Exception {
 router.routeWithRegex("\\/([^\\/]+)\\/([^\\/]+)").handler(rc -> {
  MultiMap params = rc.request().params();
  rc.response().setStatusMessage(params.get("param0") + params.get("param1")).end();
 });
 testPattern("/dog/cat", "dogcat");
}

相关文章