Java Code Examples for com.linecorp.armeria.server.ServiceRequestContext#query()

The following examples show how to use com.linecorp.armeria.server.ServiceRequestContext#query() . 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: QueryRequestConverter.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
private static String getPath(ServiceRequestContext ctx) {
    // check the path param first
    final String path = ctx.pathParam("path");
    if (!isNullOrEmpty(path)) {
        return path;
    }

    // then check HTTP query
    final String query = ctx.query();
    if (query != null) {
        final List<String> params = new QueryStringDecoder(query, false).parameters().get("path");
        if (params != null) {
            return params.get(0);
        }
    }
    // return empty string if there's no path
    return "";
}
 
Example 2
Source File: HttpsOnlyService.java    From curiostack with MIT License 6 votes vote down vote up
@Override
public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception {
  String xForwardedProto =
      Ascii.toLowerCase(Strings.nullToEmpty(req.headers().get(X_FORWARDED_PROTO)));
  if (xForwardedProto.isEmpty() || xForwardedProto.equals("https")) {
    ctx.addAdditionalResponseHeader(STRICT_TRANSPORT_SECURITY, "max-age=31536000; preload");
    ctx.addAdditionalResponseHeader(HttpHeaderNames.X_FRAME_OPTIONS, "DENY");
    config
        .getAdditionalResponseHeaders()
        .forEach(
            (key, value) ->
                ctx.addAdditionalResponseHeader(HttpHeaderNames.of(key), (String) value));
    return delegate().serve(ctx, req);
  }
  StringBuilder redirectUrl =
      new StringBuilder("https://" + req.headers().authority() + ctx.path());
  if (ctx.query() != null) {
    redirectUrl.append('?').append(ctx.query());
  }
  return HttpResponse.of(
      HttpResponse.of(
          ResponseHeaders.of(
              HttpStatus.MOVED_PERMANENTLY, HttpHeaderNames.LOCATION, redirectUrl.toString())));
}
 
Example 3
Source File: QueryRequestConverter.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
@Nullable
private static Iterable<String> getJsonPaths(ServiceRequestContext ctx) {
    final String query = ctx.query();
    if (query != null) {
        final List<String> jsonPaths = new QueryStringDecoder(query, false).parameters().get(
                "jsonpath");
        if (jsonPaths != null) {
            return ImmutableList.copyOf(jsonPaths);
        }
    }
    return null;
}
 
Example 4
Source File: TomcatService.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Nullable
private Request convertRequest(ServiceRequestContext ctx, AggregatedHttpRequest req) throws Throwable {
    final String mappedPath = ctx.mappedPath();
    final Request coyoteReq = new Request();

    coyoteReq.scheme().setString(req.scheme());

    // Set the remote host/address.
    final InetSocketAddress remoteAddr = ctx.remoteAddress();
    coyoteReq.remoteAddr().setString(remoteAddr.getAddress().getHostAddress());
    coyoteReq.remoteHost().setString(remoteAddr.getHostString());
    coyoteReq.setRemotePort(remoteAddr.getPort());

    // Set the local host/address.
    final InetSocketAddress localAddr = ctx.localAddress();
    coyoteReq.localAddr().setString(localAddr.getAddress().getHostAddress());
    coyoteReq.localName().setString(hostName());
    coyoteReq.setLocalPort(localAddr.getPort());

    final String hostHeader = req.authority();
    final int colonPos = hostHeader.indexOf(':');
    if (colonPos < 0) {
        coyoteReq.serverName().setString(hostHeader);
    } else {
        coyoteReq.serverName().setString(hostHeader.substring(0, colonPos));
        try {
            final int port = Integer.parseInt(hostHeader.substring(colonPos + 1));
            coyoteReq.setServerPort(port);
        } catch (NumberFormatException e) {
            // Invalid port number
            return null;
        }
    }

    // Set the method.
    final HttpMethod method = req.method();
    coyoteReq.method().setString(method.name());

    // Set the request URI.
    final byte[] uriBytes = mappedPath.getBytes(StandardCharsets.US_ASCII);
    coyoteReq.requestURI().setBytes(uriBytes, 0, uriBytes.length);

    // Set the query string if any.
    if (ctx.query() != null) {
        coyoteReq.queryString().setString(ctx.query());
    }

    // Set the headers.
    final MimeHeaders cHeaders = coyoteReq.getMimeHeaders();
    convertHeaders(req.headers(), cHeaders);
    convertHeaders(req.trailers(), cHeaders);

    // Set the content.
    final HttpData content = req.content();
    coyoteReq.setInputBuffer((InputBuffer) INPUT_BUFFER_CONSTRUCTOR.invoke(content));

    return coyoteReq;
}