Java Code Examples for io.vertx.ext.web.RoutingContext#queryParam()

The following examples show how to use io.vertx.ext.web.RoutingContext#queryParam() . 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: CustomTenantResolver.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@Override
public String resolve(RoutingContext context) {
    List<String> tenantId = context.queryParam("tenantId");
    if (!tenantId.isEmpty()) {
        return tenantId.get(0).isEmpty() ? null : tenantId.get(0);
    }

    String path = context.request().path();

    if (path.contains("tenant-logout")) {
        return "tenant-logout";
    }

    if (path.contains("tenant-https")) {
        return "tenant-https";
    }

    return path.contains("callback-after-redirect") || path.contains("callback-before-redirect") ? "tenant-1"
            : path.contains("callback-jwt-after-redirect") || path.contains("callback-jwt-before-redirect") ? "tenant-jwt"
                    : path.contains("callback-jwt-not-used-after-redirect")
                            || path.contains("callback-jwt-not-used-before-redirect")
                                    ? "tenant-jwt-not-used"
                                    : path.contains("/web-app2") ? "tenant-2" : null;
}
 
Example 2
Source File: MetricsHttpService.java    From besu with Apache License 2.0 5 votes vote down vote up
private void metricsRequest(final RoutingContext routingContext) {
  final Set<String> names = new TreeSet<>(routingContext.queryParam("name[]"));
  final HttpServerResponse response = routingContext.response();
  vertx.<String>executeBlocking(
      future -> {
        try {
          final ByteArrayOutputStream metrics = new ByteArrayOutputStream(16 * 1024);
          final OutputStreamWriter osw = new OutputStreamWriter(metrics, StandardCharsets.UTF_8);
          TextFormat.write004(
              osw,
              ((PrometheusMetricsSystem) (metricsSystem))
                  .getRegistry()
                  .filteredMetricFamilySamples(names));
          osw.flush();
          osw.close();
          metrics.flush();
          metrics.close();
          future.complete(metrics.toString(StandardCharsets.UTF_8.name()));
        } catch (final IOException ioe) {
          future.fail(ioe);
        }
      },
      false,
      (res) -> {
        if (response.closed()) {
          // Request for metrics closed before response was generated
          return;
        }
        if (res.failed()) {
          LOG.error("Request for metrics failed", res.cause());
          response.setStatusCode(HttpResponseStatus.INTERNAL_SERVER_ERROR.code()).end();
        } else {
          response.setStatusCode(HttpResponseStatus.OK.code());
          response.putHeader("Content-Type", TextFormat.CONTENT_TYPE_004);
          response.end(res.result());
        }
      });
}
 
Example 3
Source File: RequestUtils.java    From andesite-node with MIT License 5 votes vote down vote up
/**
 * Attempts to find a password in a request.
 *
 * The password will be read from the following locations, in order, with
 * the first one present being returned.
 *
 * <ul>
 * <li>The {@code Authorization} header</li>
 * <li>A websocket protocol which starts with {@code andesite-password:}
 * (the returned password will have this prefix stripped)</li>
 * <li>The {@code password} query param</li>
 * </ul>
 *
 * If the websocket protocol is matched, it's value will be inserted into the
 * {@code Sec-WebSocket-Protocol} header of the response.
 *
 * @param context Context where the password should be located.
 *
 * @return The password located, or null if nothing was found.
 */
@Nullable
@CheckReturnValue
public static String findPassword(@Nonnull RoutingContext context) {
    var authHeader = context.request().getHeader("Authorization");
    if(authHeader != null) {
        return authHeader;
    }
    
    //allow browser access
    //the browser websocket api doesn't support custom headers,
    //so this hack is needed.
    //browsers can use new WebSocket(url, "andesite-password:" + password)
    var wsHeader = context.request().getHeader("Sec-WebSocket-Protocol");
    if(wsHeader != null) {
        var parts = wsHeader.split(",");
        for(var part : parts) {
            if(part.startsWith("andesite-password:")) {
                context.response().putHeader("Sec-WebSocket-Protocol", part);
                return part.substring("andesite-password:".length());
            }
        }
    }
    
    var query = context.queryParam("password");
    
    return query.isEmpty() ? null : query.get(0);
}
 
Example 4
Source File: AmpHandler.java    From prebid-server-java with Apache License 2.0 5 votes vote down vote up
private static String originFrom(RoutingContext context) {
    String origin = null;
    final List<String> ampSourceOrigin = context.queryParam("__amp_source_origin");
    if (CollectionUtils.isNotEmpty(ampSourceOrigin)) {
        origin = ampSourceOrigin.get(0);
    }
    if (origin == null) {
        // Just to be safe
        origin = ObjectUtils.defaultIfNull(context.request().headers().get("Origin"), StringUtils.EMPTY);
    }
    return origin;
}
 
Example 5
Source File: SmallRyeGraphQLExecutionHandler.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private String getQueryParameter(RoutingContext ctx, String parameterName) {
    List<String> all = ctx.queryParam(parameterName);
    if (all != null && !all.isEmpty()) {
        return all.get(0);
    }
    return null;
}
 
Example 6
Source File: OpenApiHandler.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(RoutingContext event) {
    if (event.request().method().equals(HttpMethod.OPTIONS)) {
        addCorsResponseHeaders(event.response());
        event.response().headers().set("Allow", ALLOWED_METHODS);
    } else {
        HttpServerRequest req = event.request();
        HttpServerResponse resp = event.response();
        String accept = req.headers().get("Accept");

        List<String> formatParams = event.queryParam(QUERY_PARAM_FORMAT);
        String formatParam = formatParams.isEmpty() ? null : formatParams.get(0);

        // Default content type is YAML
        Format format = Format.YAML;

        // Check Accept, then query parameter "format" for JSON; else use YAML.
        if ((accept != null && accept.contains(Format.JSON.getMimeType())) ||
                ("JSON".equalsIgnoreCase(formatParam))) {
            format = Format.JSON;
        }

        addCorsResponseHeaders(resp);
        resp.headers().set("Content-Type", format.getMimeType() + ";charset=UTF-8");
        ClassLoader cl = classLoader == null ? Thread.currentThread().getContextClassLoader() : classLoader;
        try (InputStream in = cl.getResourceAsStream(BASE_NAME + format)) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            int r;
            byte[] buf = new byte[1024];
            while ((r = in.read(buf)) > 0) {
                out.write(buf, 0, r);
            }
            resp.end(Buffer.buffer(out.toByteArray()));
        } catch (IOException e) {
            event.fail(e);
        }

    }
}