class HeaderRequestTokenExtractor implements RequestTokenExtractor {

  @Override
  public String getToken(HttpServletRequest req) {
    if (req.getHeader(HttpHeaders.AUTHORIZATION) == null) {
      return null;
    }
    if (req.getHeader(HttpHeaders.AUTHORIZATION).toLowerCase().startsWith("bearer")) {
      String[] parts = req.getHeader(HttpHeaders.AUTHORIZATION).split(" ");
      if (parts.length != 2) {
        throw new BadRequestException("Invalid authorization header format.");
      }
      return parts[1];
    } else {
      return req.getHeader(HttpHeaders.AUTHORIZATION);
    }
  }

}