spark.Request.headers()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(12.0k)|赞(0)|评价(0)|浏览(180)

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

Request.headers介绍

[英]Returns all headers
[中]

代码示例

代码示例来源:origin: perwendel/spark

@Override
public String headers(String header) {
  return delegate.headers(header);
}

代码示例来源:origin: perwendel/spark

@Override
public Set<String> headers() {
  return delegate.headers();
}

代码示例来源:origin: gocd/gocd

default String preconditionHeader(Request request, String header) {
  //possibly move to a filter/middleware?
  String etag = request.headers(header);
  if (etag == null) {
    return null;
  }
  // workaround for how jetty's gzip handler modifies the etag
  return etag.replaceAll("^\"(.*)\"$", "$1").replaceAll("(.*)(--(gzip|deflate))", "$1");
}

代码示例来源:origin: gocd/gocd

protected boolean isJsonContentType(Request request) {
  String mime = request.headers("Content-Type");
  if (isBlank(mime)) {
    return false;
  }
  try {
    MimeType mimeType = MimeType.valueOf(mime);
    return "application".equals(mimeType.getType()) && "json".equals(mimeType.getSubtype());
  } catch (InvalidMimeTypeException e) {
    return false;
  }
}

代码示例来源:origin: gocd/gocd

protected void verifyContentType(Request request, Response response) throws IOException {
  if (!UPDATE_HTTP_METHODS.contains(request.requestMethod().toUpperCase())) {
    return;
  }
  boolean requestHasBody = request.contentLength() >= 1 || request.raw().getInputStream().available() >= 1 || "chunked".equalsIgnoreCase(request.headers("Transfer-Encoding"));
  if (requestHasBody) {
    if (!isJsonContentType(request)) {
      throw haltBecauseJsonContentTypeExpected();
    }
  } else if (request.headers().stream().noneMatch(headerName -> headerName.toLowerCase().equals("x-gocd-confirm"))) {
    throw haltBecauseConfirmHeaderMissing();
  }
}

代码示例来源:origin: bwssytems/ha-bridge

public User getAuthenticatedUser(Request request) {
  User theUser = request.session().attribute(USER_SESSION_ID);
  if(theUser == null) {
    String authHeader = request.headers("Authorization");
    if(authHeader != null) {
      byte[] authData;

代码示例来源:origin: bwssytems/ha-bridge

response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";

代码示例来源:origin: bwssytems/ha-bridge

response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.type("application/json");
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.type("application/json");
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html");
return "";
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.type("application/json");
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.type("application/json");
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.type("application/json");
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.type("application/json");
response.status(HttpStatus.SC_OK);

代码示例来源:origin: bwssytems/ha-bridge

response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.status(HttpStatus.SC_CREATED);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "POST");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "PUT");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "POST");

代码示例来源:origin: cdelmas/microservices-comparison

private String readToken(Request request) {
    String authorization = request.headers("Authorization");
    return authorization.substring(authorization.indexOf(' ') + 1);
  }
}

代码示例来源:origin: ViDA-NYU/ache

private String injectAuthorizationMetaTag(Request request, String indexHtml) {
  String authHeader = request.headers("Authorization");
  if (authHeader == null || authHeader.isEmpty()) {
    return indexHtml;
  }
  String meta = String.format("<meta name=\"authorization\" content=\"%s\">", authHeader);
  return indexHtml.replaceFirst("<head>", "<head>" + meta);
}

代码示例来源:origin: simplesteph/ec2-masterclass-sampleapp

private static String details(Request request, Response response) throws UnknownHostException {
  StringBuilder sb = new StringBuilder();
  String myHostname = InetAddress.getLocalHost().getHostName();
  sb.append("Hello World By: ").append(myHostname).append("<br/>");
  String sourceIP = request.ip();
  sb.append("Receive Request From: ").append(sourceIP).append("<br/>");
  sb.append("Request Headers are: ").append("<br/>");
  request.headers().forEach(header -> sb.append(header).append(": ").append(request.headers(header)).append("<br/>"));
  return sb.toString();
}

代码示例来源:origin: apache/james-project

@Override
  public void define(Service service) {
    service.options("/*", (request, response) -> {

      Optional.ofNullable(request.headers("Access-Control-Request-Headers"))
        .ifPresent(header -> response.header("Access-Control-Allow-Headers", header));

      Optional.ofNullable(request.headers("Access-Control-Request-Method"))
        .ifPresent(header -> response.header("Access-Control-Allow-Methods", header));

      return "";
    });
  }
}

代码示例来源:origin: dessalines/torrenttunes-client

public static void allowAllHeaders(Request req, Response res) {
  String origin = req.headers("Origin");
  res.header("Access-Control-Allow-Credentials", "true");
  res.header("Access-Control-Allow-Origin", origin);
  res.header("Content-Encoding", "gzip");
}

代码示例来源:origin: ViDA-NYU/ache

@Override
public void handle(final Request request, final Response response) {
  if (!isAuthenticated(request.headers("Authorization"))) {
    response.header("WWW-Authenticate", "Basic");
    server.halt(UNAUTHORIZED_STATUS_CODE);
  }
}

代码示例来源:origin: org.apache.james/james-server-webadmin-core

@Test
  public void handleShouldAcceptValidJwt() throws Exception {
    Request request = mock(Request.class);
    when(request.headers(JwtFilter.AUTHORIZATION_HEADER_NAME)).thenReturn("Bearer value");
    when(jwtTokenVerifier.verify("value")).thenReturn(true);
    when(jwtTokenVerifier.hasAttribute("admin", true, "value")).thenReturn(true);

    jwtFilter.handle(request, mock(Response.class));
  }
}

代码示例来源:origin: apache/james-project

@Test
public void handleShouldRejectRequestWithBearersHeaders() throws Exception {
  Request request = mock(Request.class);
  when(request.headers(JwtFilter.AUTHORIZATION_HEADER_NAME)).thenReturn("Invalid value");
  expectedException.expect(HaltException.class);
  expectedException.expect(STATUS_CODE_MATCHER_401);
  jwtFilter.handle(request, mock(Response.class));
}

代码示例来源:origin: apache/james-project

@Test
public void handleShouldRejectRequestWithHeaders() throws Exception {
  Request request = mock(Request.class);
  when(request.headers()).thenReturn(ImmutableSet.of());
  expectedException.expect(HaltException.class);
  expectedException.expect(STATUS_CODE_MATCHER_401);
  jwtFilter.handle(request, mock(Response.class));
}

代码示例来源:origin: apache/james-project

@Test
public void handleShouldRejectRequestWithoutAdminClaim() throws Exception {
  Request request = mock(Request.class);
  when(request.headers(JwtFilter.AUTHORIZATION_HEADER_NAME)).thenReturn("Bearer value");
  when(jwtTokenVerifier.verify("value")).thenReturn(true);
  when(jwtTokenVerifier.hasAttribute("admin", true, "value")).thenReturn(false);
  expectedException.expect(HaltException.class);
  expectedException.expect(STATUS_CODE_MATCHER_401);
  jwtFilter.handle(request, mock(Response.class));
}

代码示例来源:origin: org.apache.james/james-server-webadmin-core

@Test
public void handleShouldRejectRequestWithInvalidBearerHeaders() throws Exception {
  Request request = mock(Request.class);
  when(request.headers(JwtFilter.AUTHORIZATION_HEADER_NAME)).thenReturn("Bearer value");
  when(jwtTokenVerifier.verify("value")).thenReturn(false);
  expectedException.expect(HaltException.class);
  expectedException.expect(STATUS_CODE_MATCHER_401);
  jwtFilter.handle(request, mock(Response.class));
}

相关文章