Java Code Examples for io.vertx.core.MultiMap#names()

The following examples show how to use io.vertx.core.MultiMap#names() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DigestBlob.java    From sfs with Apache License 2.0 6 votes vote down vote up
public DigestBlob(HttpClientResponse httpClientResponse) {
    super(httpClientResponse);
    digests = new HashMap<>();
    BaseEncoding baseEncoding = base64();
    MultiMap headers = httpClientResponse.headers();
    for (String headerName : headers.names()) {
        Matcher matcher = COMPUTED_DIGEST.matcher(headerName);
        if (matcher.find()) {
            String digestName = matcher.group(1);
            Optional<MessageDigestFactory> oMessageDigestFactory = fromValueIfExists(digestName);
            if (oMessageDigestFactory.isPresent()) {
                MessageDigestFactory messageDigestFactory = oMessageDigestFactory.get();
                withDigest(messageDigestFactory, baseEncoding.decode(headers.get(headerName)));
            }
        }
    }
}
 
Example 2
Source File: ValidateParamComputedDigest.java    From sfs with Apache License 2.0 6 votes vote down vote up
@Override
public SfsRequest call(SfsRequest httpServerRequest) {
    MultiMap params = httpServerRequest.params();

    for (String param : params.names()) {
        Matcher matcher = COMPUTED_DIGEST.matcher(param);
        if (matcher.matches()) {
            String digestName = matcher.group(1);
            if (!fromValueIfExists(digestName).isPresent()) {
                JsonObject jsonObject = new JsonObject()
                        .put("message", format("%s is not a supported digest", digestName));

                throw new HttpRequestValidationException(HTTP_BAD_REQUEST, jsonObject);
            }
        }
    }

    return httpServerRequest;
}
 
Example 3
Source File: HttpServerRequestHeaderLogger.java    From sfs with Apache License 2.0 6 votes vote down vote up
@Override
public T call(T httpServerRequest) {
    if (LOGGER.isDebugEnabled()) {
        StringBuilder sb = new StringBuilder();
        sb.append("\r\nHttp Header Dump >>>>>\r\n\r\n");
        String query = httpServerRequest.query();
        sb.append(format("%s %s%s %s\r\n", httpServerRequest.method(), httpServerRequest.path(), query != null ? '?' + query : "", httpServerRequest.version().toString()));
        MultiMap headers = httpServerRequest.headers();
        for (String headerName : headers.names()) {
            List<String> values = headers.getAll(headerName);
            sb.append(format("%s: %s\r\n", headerName, on(',').join(values)));
        }
        sb.append("\r\n");
        sb.append("Http Header Dump <<<<<\r\n");
        LOGGER.debug(sb.toString());
    }
    return httpServerRequest;
}
 
Example 4
Source File: HttpServerRequestHeaderToJsonObject.java    From sfs with Apache License 2.0 6 votes vote down vote up
public static JsonObject call(HttpServerRequest httpServerRequest) {
    JsonObject jsonObject = new JsonObject();

    String query = httpServerRequest.query();
    String requestLine = String.format("%s %s%s %s", httpServerRequest.method(), httpServerRequest.path(), query != null ? '?' + query : "", httpServerRequest.version().toString());
    jsonObject.put("request_line", requestLine);
    MultiMap headers = httpServerRequest.headers();
    JsonArray jsonHeaders = new JsonArray();
    for (String headerName : headers.names()) {
        List<String> values = headers.getAll(headerName);
        JsonObject jsonHeader = new JsonObject();
        jsonHeader.put(headerName, values);
        jsonHeaders.add(jsonHeader);
    }
    jsonObject.put("headers", jsonHeaders);
    return jsonObject;
}
 
Example 5
Source File: HttpClientResponseHeaderLogger.java    From sfs with Apache License 2.0 6 votes vote down vote up
@Override
public HttpClientResponse call(HttpClientResponse httpClientResponse) {
    if (LOGGER.isDebugEnabled()) {
        StringBuilder sb = new StringBuilder();
        sb.append("\r\nHttp Header Dump >>>>>\r\n\r\n");
        sb.append(format("HTTP/1.1 %d %s\r\n", httpClientResponse.statusCode(), httpClientResponse.statusMessage()));
        MultiMap headers = httpClientResponse.headers();
        for (String headerName : headers.names()) {
            List<String> values = headers.getAll(headerName);
            sb.append(format("%s: %s\r\n", headerName, on(',').join(values)));
        }
        sb.append("\r\n");
        sb.append("Http Header Dump <<<<<\r\n");
        LOGGER.debug(sb.toString());
    }
    return httpClientResponse;
}
 
Example 6
Source File: FormBodyProcessorImpl.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Override
public Future<RequestParameter> process(RoutingContext requestContext) {
  try {
    MultiMap multiMap = requestContext.request().formAttributes();
    JsonObject object = new JsonObject();
    for (String key : multiMap.names()) {
      List<String> serialized = multiMap.getAll(key);
      Map.Entry<String, Object> parsed = parseField(key, serialized);
      if (parsed != null) object.put(parsed.getKey(), parsed.getValue());
    }
    return valueValidator.validate(object).recover(err -> Future.failedFuture(
      BodyProcessorException.createValidationError(requestContext.parsedHeaders().contentType().value(), err)
    ));
  } catch (MalformedValueException e) {
    return Future.failedFuture(BodyProcessorException.createParsingError(requestContext.request().getHeader(HttpHeaders.CONTENT_TYPE), e));
  }
}
 
Example 7
Source File: VertxServerRequestToHttpServletRequest.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, String[]> getParameterMap() {
  if (parameterMap == null) {
    Map<String, String[]> paramMap = new HashMap<>();
    MultiMap map = this.vertxRequest.params();
    for (String name : map.names()) {
      List<String> valueList = map.getAll(name);
      paramMap.put(name, map.getAll(name).toArray(new String[valueList.size()]));
    }
    parameterMap = paramMap;
  }

  return parameterMap;
}
 
Example 8
Source File: ProxyService.java    From okapi with Apache License 2.0 5 votes vote down vote up
private static void dumpHeaders(MultiMap headers) {
  for (String name : headers.names()) {
    List<String> values = headers.getAll(name);
    for (String value : values) {
      logger.info("{}: {}", name, value);
    }
  }
}
 
Example 9
Source File: ProxyService.java    From okapi with Apache License 2.0 5 votes vote down vote up
/**
 * Helper to make request headers for the system requests we make. Copies all
 * X- headers over. Adds a tenant, and a token, if we have one.
 */
private static Map<String, String> sysReqHeaders(
    MultiMap headersIn, String tenantId, String authToken,
    ModuleInstance inst, String modPerms) {
  Map<String, String> headersOut = new HashMap<>();
  for (String hdr : headersIn.names()) {
    if (hdr.matches("^X-.*$")) {
      headersOut.put(hdr, headersIn.get(hdr));
    }
  }
  headersOut.put(XOkapiHeaders.TENANT, tenantId);
  logger.debug("Added {} : {}", XOkapiHeaders.TENANT, tenantId);
  if (authToken == null) {
    headersOut.remove(XOkapiHeaders.TOKEN);
  } else {
    headersOut.put(XOkapiHeaders.TOKEN, authToken);
  }
  headersOut.put("Accept", "*/*");
  headersOut.put("Content-Type", "application/json; charset=UTF-8");
  if (modPerms != null) { // We are making an auth call
    RoutingEntry re = inst.getRoutingEntry();
    if (re != null) {
      headersOut.put(XOkapiHeaders.FILTER, re.getPhase());
    }
    if (!modPerms.isEmpty()) {
      headersOut.put(XOkapiHeaders.MODULE_PERMISSIONS, modPerms);
    }
    // Clear the permissions-required header that we inherited from the
    // original request (e.g. to tenant-enable), as we do not have those
    // perms set in the target tenant
    headersOut.remove(XOkapiHeaders.PERMISSIONS_REQUIRED);
    headersOut.remove(XOkapiHeaders.PERMISSIONS_DESIRED);
    logger.debug("Auth call, some tricks with permissions");
  }
  return headersOut;
}
 
Example 10
Source File: XBlob.java    From sfs with Apache License 2.0 5 votes vote down vote up
public XBlob merge(SfsRequest httpServerRequest) {
    MultiMap queryParams = httpServerRequest.params();
    MultiMap headers = httpServerRequest.headers();

    if (queryParams.contains(VOLUME)) {
        volume = tryParse(queryParams.get(VOLUME));
    }
    if (queryParams.contains(POSITION)) {
        position = Longs.tryParse(queryParams.get(POSITION));
    }
    if (queryParams.contains(VERSION)) {
        version = base64().decode(queryParams.get(VERSION));
    }
    if (headers.contains(CONTENT_LENGTH)) {
        length = Longs.tryParse(headers.get(CONTENT_LENGTH));
    }

    for (String queryParam : queryParams.names()) {
        Matcher matcher = COMPUTED_DIGEST.matcher(queryParam);
        if (matcher.matches()) {
            MessageDigestFactory digest = fromValueIfExists(matcher.group(1)).get();
            messageDigests.add(digest);
        }
    }

    return this;
}
 
Example 11
Source File: MailEncoder.java    From vertx-mail-client with Apache License 2.0 5 votes vote down vote up
/**
 * create the headers of the MIME message by combining the headers the user has supplied with the ones necessary for
 * the message
 *
 * @return MultiMap of final headers
 */
private MultiMap createHeaders(MultiMap additionalHeaders) {
  MultiMap headers = MultiMap.caseInsensitiveMultiMap();;

  if (!message.isFixedHeaders()) {
    headers.set("MIME-Version", "1.0");
    headers.set("Message-ID", Utils.generateMessageID(hostname, userAgent));
    headers.set("Date", Utils.generateDate());

    if (message.getSubject() != null) {
      headers.set("Subject", Utils.encodeHeader(message.getSubject(), 9));
    }

    if (message.getFrom() != null) {
      headers.set("From", Utils.encodeHeaderEmail(message.getFrom(), 6));
    }
    if (message.getTo() != null) {
      headers.set("To", Utils.encodeEmailList(message.getTo(), 4));
    }
    if (message.getCc() != null) {
      headers.set("Cc", Utils.encodeEmailList(message.getCc(), 4));
    }

    headers.addAll(additionalHeaders);
  }

  // add the user-supplied headers as last step, this way it is possible
  // to supply a custom Message-ID for example.
  MultiMap headersToSet = message.getHeaders();
  if (headersToSet != null) {
    for (String key : headersToSet.names()) {
      headers.remove(key);
    }
    headers.addAll(headersToSet);
  }

  messageID = headers.get("Message-ID");
  
  return headers;
}
 
Example 12
Source File: Utils.java    From vertx-mail-client with Apache License 2.0 5 votes vote down vote up
public static JsonObject multiMapToJson(final MultiMap headers) {
  JsonObject json = new JsonObject();
  for (String key : headers.names()) {
    json.put(key, headers.getAll(key));
  }
  return json;
}
 
Example 13
Source File: RequestFromVertx.java    From wisdom with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves all headers.
 *
 * @return headers
 */
@Override
public Map<String, List<String>> headers() {
    if (headers != null) {
        return headers;
    }
    headers = new HashMap<>();
    final MultiMap requestHeaders = request.headers();
    Set<String> names = requestHeaders.names();
    for (String name : names) {
        headers.put(name, requestHeaders.getAll(name));
    }
    return headers;
}
 
Example 14
Source File: HystrixDashboardProxyConnectionHandler.java    From standalone-hystrix-dashboard with MIT License 4 votes vote down vote up
/**
 * Extract the url to Proxy.
 *
 * @param requestCtx Context of the current request.
 * @return The url to proxy or null if it wasn't found.
 */
Optional<String> getProxyUrl(RoutingContext requestCtx) {
  final HttpServerRequest serverRequest = requestCtx.request();
  final HttpServerResponse serverResponse = requestCtx.response();

  // origin = metrics stream endpoint
  String origin = serverRequest.getParam("origin");
  if (origin == null || origin.isEmpty()) {
    log.warn("Request without origin");
    serverResponse.setStatusCode(500)
                  .end(Buffer.buffer("Required parameter 'origin' missing. Example: 107.20.175.135:7001".getBytes(StandardCharsets.UTF_8)));
    return Optional.empty();
  }
  origin = origin.trim();

  boolean hasFirstParameter = false;
  StringBuilder url = new StringBuilder();
  // if there is no http, i add
  if (!origin.startsWith("http")) {
    url.append("http://");
  }
  url.append(origin);

  // if contains any query parameter
  if (origin.contains("?")) {
    hasFirstParameter = true;
  }

  // add the request params to the url to proxy, because need to forward Delay and maybe another future param
  MultiMap params = serverRequest.params();
  for (String key : params.names()) {
    if (!"origin".equals(key) && !"authorization".equals(key)) {
      String value = params.get(key);

      if (hasFirstParameter) {
        url.append("&");
      } else {
        url.append("?");
        hasFirstParameter = true;
      }
      url.append(key).append("=").append(value);
    }
  }

  return Optional.of(url.toString());
}