@Override protected String featureValueOf(Request actual) { return actual.method(); } };
private String getPath(final Request message) { try { final URI uri = new URI(message.uri()); return uri.getRawPath(); } catch (URISyntaxException e) { LOG.warn("Invalid URI sent {} {} by service {}", message.method(), message.uri(), message.service().orElse("<unknown>"), e); return null; } }
match = applicationRouter.match(request); } catch (InvalidUriException e) { LOG.warn("bad uri {} {} {}", request.method(), request.uri(), BAD_REQUEST, e); ongoingRequest.reply(forStatus(BAD_REQUEST)); return; Collection<String> methods = applicationRouter.getMethodsForValidRules(request); if (methods.isEmpty()) { LOG.warn("not found {} {} {}", request.method(), request.uri(), NOT_FOUND); ongoingRequest.reply(forStatus(NOT_FOUND)); } else { StatusType statusCode; if ("OPTIONS".equals(request.method())) { statusCode = NO_CONTENT; } else { statusCode = METHOD_NOT_ALLOWED; LOG.warn("wrong method {} {} {}", request.method(), request.uri(), statusCode);
@Override public Collection<String> getMethodsForValidRules(final Request message) { // TODO (dano): Ideally match() should return the result so that we don't have to route twice. final String path = getPath(message); final Router.Result<Rule<T>> result = router.result(); router.route(message.method(), path, result); if (result.status() == Router.Status.NOT_FOUND) { return Collections.emptyList(); } return result.allowedMethods(); }
private static Response<ByteString> applyHttpPayloadSemantics( Request request, Response<ByteString> response) { Response<ByteString> result = response; Optional<ByteString> payload = response.payload(); if (setContentLengthForStatus(response.status())) { int payloadSize = payload.isPresent() ? payload.get().size() : 0; result = result.withHeader("Content-Length", String.valueOf(payloadSize)); } if (!setPayloadForMethod(request.method()) || !setPayloadForStatus(response.status())) { result = result.withPayload(null); } return result; }
@Override public Optional<RuleMatch<T>> match(Request message) throws InvalidUriException { final String method = message.method(); final String path = getPath(message); if (method == null) { LOG.warn("Invalid request for {} sent without method by service {}", message.uri(), message.service().orElse("<unknown>")); throw new InvalidUriException(); } if (path == null) { // Problem already logged in detail upstream throw new InvalidUriException(); } final Router.Result<Rule<T>> result = router.result(); router.route(method, path, result); if (!result.isSuccess()) { return Optional.empty(); } final Rule<T> rule = result.target(); final ImmutableMap.Builder<String, String> pathArgs = ImmutableMap.builder(); for (int i = 0; i < result.params(); i++) { pathArgs.put(result.paramName(i), readParameterValue(result, i)); } return Optional.of(new RuleMatch<T>(rule, pathArgs.build())); }
.method(apolloRequest.method(), requestBody.orElse(null)) .url(apolloRequest.uri()) .headers(headersBuilder.build())
@Override public void gatherIncomingCall(OngoingRequest ongoingRequest, Endpoint endpoint) { Request message = ongoingRequest.request(); String fromService = message.service().orElse(null); String method = message.method(); EndpointInfo info = endpoint.info(); String endpointMethodName = info.getJavaMethodName(); CallsGatherer callsGatherer = gatherer.getIncomingCallsGatherer(fromService); EndpointGatherer endpointGatherer = callsGatherer.namedEndpointGatherer(endpointMethodName); endpointGatherer.setUri(info.getUri()); endpointGatherer.addMethod(method); for (String name : message.parameters().keySet()) { endpointGatherer.addQueryParameterName(name); } } }
@Override protected String featureValueOf(Request actual) { return actual.method(); } };
public static <T> Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<T>>> authenticator( RequestAuthenticator authenticator) { return h -> rc -> { final Optional<GoogleIdToken> idToken = auth(rc, authenticator).user(); if (!"GET".equals(rc.request().method()) && !idToken.isPresent()) { return completedFuture( Response.forStatus(Status.UNAUTHORIZED.withReasonPhrase("Unauthorized access"))); } return h.invoke(rc); }; } }
public static <T> Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<T>>> httpLogger( Logger log, RequestAuthenticator authenticator) { return innerHandler -> requestContext -> { final Request request = requestContext.request(); log.info("{}{} {} by {} with headers {} parameters {} and payload {}", "GET".equals(request.method()) ? "" : "[AUDIT] ", request.method(), request.uri(), // TODO: pass in auth context instead of authenticating twice auth(requestContext, authenticator).user().map(idToken -> idToken.getPayload() .getEmail()) .orElse("anonymous"), hideSensitiveHeaders(request.headers()), request.parameters(), request.payload().map(ByteString::utf8).orElse("") .replaceAll("\n", " ")); return innerHandler.invoke(requestContext); }; }
public static <T> Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<T>>> tracer( Tracer tracer, String service) { return innerHandler -> requestContext -> { final Request request = requestContext.request(); final URI uri = URI.create(request.uri()); final Span span = tracer.spanBuilder(service + '/' + uri.getPath()).startSpan(); span.putAttribute("method", stringAttributeValue(request.method())); span.putAttribute("uri", stringAttributeValue(request.uri())); final CompletionStage<Response<T>> response; try { response = innerHandler.invoke(requestContext); } catch (Exception e) { span.setStatus(UNKNOWN); span.end(); Throwables.throwIfUnchecked(e); throw new RuntimeException(e); } response.whenComplete((r, ex) -> { if (ex != null) { span.setStatus(UNKNOWN); } span.end(); }); return response; }; }