Exemple #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);
   }
 }
Exemple #2
0
 /**
  * Sends a multiplexed request. Responses are provided to individual requests' callbacks. After
  * all responses are received the given aggregated callback is invoked.
  *
  * <p>The request is sent using the protocol version 2.0.
  *
  * @param multiplexedRequest the multiplexed request to send.
  * @param callback the aggregated response callback.
  */
 public void sendRequest(
     MultiplexedRequest multiplexedRequest, Callback<MultiplexedResponse> callback) {
   MultiplexedCallback muxCallback =
       new MultiplexedCallback(multiplexedRequest.getCallbacks(), callback);
   try {
     RestRequest restRequest = buildMultiplexedRequest(multiplexedRequest);
     RequestContext requestContext = new RequestContext();
     _client.restRequest(restRequest, requestContext, muxCallback);
   } catch (Exception e) {
     muxCallback.onError(e);
   }
 }
  @SuppressWarnings({"unchecked", "rawtypes"})
  private <T extends Request> RestRequest clientGeneratedRequest(
      Class<T> requestClass,
      ResourceMethod method,
      DataMap entityBody,
      RestClient.ContentType contentType,
      List<RestClient.AcceptType> acceptTypes)
      throws URISyntaxException {
    // massive setup...
    Client mockClient = EasyMock.createMock(Client.class);

    @SuppressWarnings({"rawtypes"})
    Request<?> mockRequest = EasyMock.createMock(requestClass);
    RecordTemplate mockRecordTemplate = EasyMock.createMock(RecordTemplate.class);
    @SuppressWarnings({"rawtypes"})
    RestResponseDecoder mockResponseDecoder = EasyMock.createMock(RestResponseDecoder.class);

    setCommonExpectations(mockRequest, method, mockRecordTemplate, mockResponseDecoder);

    if (method == ResourceMethod.BATCH_PARTIAL_UPDATE || method == ResourceMethod.BATCH_UPDATE) {
      buildInputForBatchPathAndUpdate(mockRequest);
    } else {
      EasyMock.expect(mockRequest.getInputRecord()).andReturn(mockRecordTemplate).times(2);
      EasyMock.expect(mockRequest.getResourceSpec()).andReturn(new ResourceSpecImpl()).once();
    }

    if (method == ResourceMethod.GET) {
      EasyMock.expect(((GetRequest) mockRequest).getObjectId()).andReturn(null).once();
      EasyMock.expect(((GetRequest) mockRequest).getResourceSpec())
          .andReturn(new ResourceSpecImpl())
          .once();
      EasyMock.expect(mockRequest.getMethodName()).andReturn(null);
    } else if (method == ResourceMethod.BATCH_GET) {
      EasyMock.expect(mockRequest.getMethodName()).andReturn(null);
    } else if (method == ResourceMethod.ACTION) {
      EasyMock.expect(((ActionRequest) mockRequest).getId()).andReturn(null);
      EasyMock.expect(mockRequest.getMethodName()).andReturn("testAction");
    } else if (method == ResourceMethod.FINDER) {
      EasyMock.expect(((FindRequest) mockRequest).getAssocKey()).andReturn(new CompoundKey());
      EasyMock.expect(mockRequest.getMethodName()).andReturn("testFinder");
    } else if (method == ResourceMethod.GET_ALL) {
      EasyMock.expect(((GetAllRequest) mockRequest).getAssocKey()).andReturn(new CompoundKey());
      EasyMock.expect(mockRequest.getMethodName()).andReturn(null);
    } else if (method == ResourceMethod.UPDATE) {
      EasyMock.expect(((UpdateRequest) mockRequest).getResourceSpec())
          .andReturn(new ResourceSpecImpl())
          .once();
      EasyMock.expect(((UpdateRequest) mockRequest).getId()).andReturn(null);
      EasyMock.expect(mockRequest.getMethodName()).andReturn(null);
    } else if (method == ResourceMethod.PARTIAL_UPDATE) {
      EasyMock.expect(mockRequest.getResourceSpec()).andReturn(new ResourceSpecImpl()).times(2);
      EasyMock.expect(((PartialUpdateRequest) mockRequest).getId()).andReturn(null);
      EasyMock.expect(mockRequest.getMethodName()).andReturn(null);
    } else if (method == ResourceMethod.DELETE) {
      EasyMock.expect(((DeleteRequest) mockRequest).getResourceSpec())
          .andReturn(new ResourceSpecImpl())
          .once();
      EasyMock.expect(((DeleteRequest) mockRequest).getId()).andReturn(null);
      EasyMock.expect(mockRequest.getMethodName()).andReturn(null);
    } else {
      EasyMock.expect(mockRequest.getMethodName()).andReturn(null);
    }

    EasyMock.expect(mockRecordTemplate.data()).andReturn(entityBody).once();

    Capture<RestRequest> restRequestCapture = new Capture<RestRequest>();

    EasyMock.expect(mockClient.getMetadata(new URI(HOST + SERVICE_NAME)))
        .andReturn(Collections.<String, Object>emptyMap())
        .once();

    mockClient.restRequest(
        EasyMock.capture(restRequestCapture),
        (RequestContext) EasyMock.anyObject(),
        (Callback<RestResponse>) EasyMock.anyObject());
    EasyMock.expectLastCall().once();

    EasyMock.replay(mockClient, mockRequest, mockRecordTemplate);

    // do work!
    RestClient restClient;
    if (acceptTypes == null) {
      restClient = new RestClient(mockClient, HOST);
    } else if (contentType == null) {
      restClient = new RestClient(mockClient, HOST, acceptTypes);
    } else {
      restClient = new RestClient(mockClient, HOST, contentType, acceptTypes);
    }

    restClient.sendRequest(mockRequest);

    return restRequestCapture.getValue();
  }