Example #1
0
 /**
  * Sends an untyped REST request using a callback.
  *
  * @param requestContext context for the request
  * @param uri for resource
  * @param method to perform
  * @param dataMap request body entity
  * @param protocolVersion the version of the Rest.li protocol used to build this request
  * @param requestOptions contains compression force on/off overrides, request content type and
  *     accept types
  * @param callback to call on request completion. In the event of an error, the callback will
  *     receive a {@link com.linkedin.r2.RemoteInvocationException}. If a valid error response was
  *     received from the remote server, the callback will receive a {@link
  *     com.linkedin.r2.message.rest.RestException} containing the error details.
  */
 private void sendRequestImpl(
     RequestContext requestContext,
     URI uri,
     ResourceMethod method,
     DataMap dataMap,
     Map<String, String> headers,
     List<String> cookies,
     String methodName,
     ProtocolVersion protocolVersion,
     RestliRequestOptions requestOptions,
     Callback<RestResponse> callback) {
   try {
     RestRequest request =
         buildRequest(
             uri,
             method,
             dataMap,
             headers,
             cookies,
             protocolVersion,
             requestOptions.getContentType(),
             requestOptions.getAcceptTypes());
     String operation = OperationNameGenerator.generate(method, methodName);
     requestContext.putLocalAttr(R2Constants.OPERATION, operation);
     requestContext.putLocalAttr(
         R2Constants.REQUEST_COMPRESSION_OVERRIDE, requestOptions.getRequestCompressionOverride());
     requestContext.putLocalAttr(
         R2Constants.RESPONSE_COMPRESSION_OVERRIDE,
         requestOptions.getResponseCompressionOverride());
     _client.restRequest(request, requestContext, callback);
   } catch (Exception e) {
     // No need to wrap the exception; RestLiCallbackAdapter.onError() will take care of that
     callback.onError(e);
   }
 }
Example #2
0
 @Override
 public void onRestRequest(
     RestRequest req,
     RequestContext requestContext,
     Map<String, String> wireAttrs,
     NextFilter<RestRequest, RestResponse> nextFilter) {
   trace("onRestRequest", req, wireAttrs, requestContext);
   requestContext.putLocalAttr(REQUEST_URI, req.getURI());
   requestContext.putLocalAttr(REQUEST_METHOD, req.getMethod());
   nextFilter.onRequest(req, requestContext, wireAttrs);
 }
Example #3
0
 private void trace(
     String method,
     Response response,
     Map<String, String> wireAttrs,
     RequestContext requestContext) {
   URI requestUri = (URI) requestContext.getLocalAttr(REQUEST_URI);
   String requestMethod = (String) requestContext.getLocalAttr(REQUEST_METHOD);
   _log.debug(
       buildLogMessage(
           method,
           "response",
           formatResponse(response, requestUri, requestMethod),
           wireAttrs,
           requestContext));
 }
Example #4
0
  private String buildLogMessage(
      String method,
      String type,
      String obj,
      Map<String, String> wireAttrs,
      RequestContext requestContext) {
    String operationName = (String) requestContext.getLocalAttr(OPERATION);

    StringBuilder builder = new StringBuilder();
    builder.append("[").append(method).append("] ");
    builder.append(type).append(": ");
    if (operationName != null) {
      builder.append("(" + operationName + ") ");
    }
    builder.append(obj);

    if (wireAttrs.size() > 0) {
      builder.append(" wire: " + wireAttrs);
    }
    return builder.toString();
  }
Example #5
0
 /**
  * Looks for a target host hint in the RequestContext, returning it if found, or null if no hint
  * is present.
  *
  * @param context RequestContext for the request
  * @return URI for target host hint, or null if no hint is present in the RequestContext
  */
 public static URI getRequestContextTargetHost(RequestContext context) {
   return (URI) context.getLocalAttr(TARGET_HOST_KEY_NAME);
 }
Example #6
0
 /**
  * Inserts a hint in RequestContext instructing D2 to bypass normal hashing behavior and instead
  * route to the specified target host. Clients should obtain the URI for target host from D2,
  * e.g., by calling {@link KeyMapper}.mapKeys().
  *
  * @param context RequestContext for the request which will be made
  * @param targetHost target host's URI to be used as a hint in the RequestContext
  */
 public static void setRequestContextTargetHost(RequestContext context, URI targetHost) {
   context.putLocalAttr(TARGET_HOST_KEY_NAME, targetHost);
 }
Example #7
0
 /**
  * Looks for a target service hint in the RequestContext, returning it if found, or null if no
  * hint is present.
  *
  * @param context RequestContext for the request
  * @return URI for target service hint, or null if no hint is present in the RequestContext
  */
 public static URI getRequestContextTargetService(RequestContext context) {
   return (URI) context.getLocalAttr(TARGET_SERVICE_KEY_NAME);
 }
Example #8
0
 /**
  * Inserts a hint in RequestContext instructing D2 to bypass normal hashing behavior and instead
  * route to the specified target service. This is different than setRequestContextTargetHost
  * because it sets the service context path as well as the host.
  *
  * @param context RequestContext for the request which will be made
  * @param targetService target service's URI to be used as a hint in the RequestContext
  */
 public static void setRequestContextTargetService(RequestContext context, URI targetService) {
   context.putLocalAttr(TARGET_SERVICE_KEY_NAME, targetService);
 }