@Override public void onSuccess(Response<PermitAllocation> result) { BatchedPermitsRequester.this.retries = 0; BatchedPermitsRequester.this.callbackCounter.incrementAndGet(); BatchedPermitsRequester.this.lock.lock(); try { PermitAllocation allocation = result.getEntity(); log.debug("Received permit allocation " + allocation); Long retryDelay = allocation.getMinRetryDelayMillis(GetMode.NULL); if (retryDelay != null) { BatchedPermitsRequester.this.retryStatus.blockRetries(retryDelay, null); } if (allocation.getPermits() > 0) { BatchedPermitsRequester.this.permitBatchContainer.addPermitAllocation(allocation); } BatchedPermitsRequester.this.requestSemaphore.release(); if (allocation.getPermits() > 0) { BatchedPermitsRequester.this.newPermitsAvailable.signalAll(); } } finally { try { this.timerContext.close(); } catch (IOException ioe) { // Do nothing } BatchedPermitsRequester.this.lock.unlock(); } }
@Override public void onSuccess(Response<BatchKVResponse<K, EntityResponse<RT>>> responseToBatch) { final ProtocolVersion version = ProtocolVersionUtil.extractProtocolVersion(responseToBatch.getHeaders()); batch.entries().stream() .forEach(entry -> { try { RestRequestBatchKey rrbk = entry.getKey(); Request request = rrbk.getRequest(); if (request instanceof GetRequest) { successGet((GetRequest) request, responseToBatch, batchGet, entry, version); } else if (request instanceof BatchGetKVRequest) { successBatchGetKV((BatchGetKVRequest) request, responseToBatch, entry, version); } else if (request instanceof BatchGetRequest) { successBatchGet((BatchGetRequest) request, responseToBatch, entry, version); } else if (request instanceof BatchGetEntityRequest) { successBatchGetEntity((BatchGetEntityRequest) request, responseToBatch, entry, version); } else { entry.getValue().getPromise().fail(unsupportedGetRequestType(request)); } } catch (RemoteInvocationException e) { entry.getValue().getPromise().fail(e); } }); }
/** * Create a flow configuration * It differs from {@link FlowConfigClient} in a way that it returns FlowStatusId, * which can be used to find the FlowExecutionId * @param flowConfig FlowConfig to be used to create the flow * @return FlowStatusId * @throws RemoteInvocationException */ public FlowStatusId createFlowConfig(FlowConfig flowConfig) throws RemoteInvocationException { LOG.debug("createFlowConfig with groupName " + flowConfig.getId().getFlowGroup() + " flowName " + flowConfig.getId().getFlowName()); CreateIdRequest<ComplexResourceKey<FlowId, FlowStatusId>, FlowConfig> request = _flowconfigsV2RequestBuilders.create().input(flowConfig).build(); ResponseFuture<IdResponse<ComplexResourceKey<FlowId, FlowStatusId>>> flowConfigResponseFuture = _restClient.get().sendRequest(request); return createFlowStatusId(flowConfigResponseFuture.getResponse().getLocation().toString()); }
@Override public Response<IdEntityResponse<K, V>> decodeResponse(RestResponse restResponse) throws RestLiDecodingException { final Response<IdEntityResponse<K, V>> rawResponse = super.decodeResponse(restResponse); final Map<String, String> modifiableHeaders = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER); modifiableHeaders.putAll(rawResponse.getHeaders()); modifiableHeaders.remove(RestConstants.HEADER_ID); modifiableHeaders.remove(RestConstants.HEADER_RESTLI_ID); return new ResponseImpl<IdEntityResponse<K, V>>(rawResponse.getStatus(), modifiableHeaders, rawResponse.getCookies(), rawResponse.getEntity(), rawResponse.getError()); }
public ResponseImpl(Response<?> origin, T entity) { this(origin.getStatus(), origin.getHeaders(), origin.getCookies()); _entity = entity; }
private void notifyAggregatedCallback(Response<MultiplexedResponseContent> response) { MultiplexedResponse muxResponse = new MultiplexedResponse(response.getStatus(), response.getHeaders()); _aggregatedCallback.onSuccess(muxResponse); } }
private static RestResponse buildIndividualRestResponse(Response<?> envelopeResponse, IndividualResponse individualResponse) throws IOException, MimeTypeParseException { IndividualBody body = individualResponse.getBody(GetMode.NULL); ByteString entity = (body != null) ? DataMapConverter.dataMapToByteString(individualResponse.getHeaders(), body.data()) : ByteString.empty(); return new RestResponseBuilder() .setStatus(individualResponse.getStatus()) .setHeaders(inheritHeaders(individualResponse, envelopeResponse)) .setCookies(CookieUtil.encodeSetCookies(envelopeResponse.getCookies())) .setEntity(entity) .build(); }
@Override public Response<EmptyRecord> decodeResponse(RestResponse restResponse) throws RestLiDecodingException { final Response<EmptyRecord> rawResponse = super.decodeResponse(restResponse); // ResponseImpl will make the headers unmodifiable final Map<String, String> modifiableHeaders = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER); modifiableHeaders.putAll(rawResponse.getHeaders()); // remove ID header to prevent user to access the weakly typed ID modifiableHeaders.remove(RestConstants.HEADER_ID); modifiableHeaders.remove(RestConstants.HEADER_RESTLI_ID); return new ResponseImpl<EmptyRecord>(rawResponse.getStatus(), modifiableHeaders, rawResponse.getCookies(), rawResponse.getEntity(), rawResponse.getError()); }
@Override public void onSuccess(Response<RT> responseToGet) { batch.entries().stream().forEach(entry -> { Request request = entry.getKey().getRequest(); if (request instanceof GetRequest) { entry.getValue().getPromise().done(new ResponseImpl<>(responseToGet, responseToGet.getEntity())); } else { entry.getValue().getPromise().fail(unsupportedGetRequestType(request)); } }); }
@Override public Response<IdResponse<K>> decodeResponse(RestResponse restResponse) throws RestLiDecodingException { final Response<IdResponse<K>> rawResponse = super.decodeResponse(restResponse); // ResponseImpl will make the headers unmodifiable final Map<String, String> modifiableHeaders = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER); modifiableHeaders.putAll(rawResponse.getHeaders()); // remove ID header to prevent user to access the weakly typed ID modifiableHeaders.remove(RestConstants.HEADER_ID); modifiableHeaders.remove(RestConstants.HEADER_RESTLI_ID); return new ResponseImpl<IdResponse<K>>(rawResponse.getStatus(), modifiableHeaders, rawResponse.getCookies(), rawResponse.getEntity(), rawResponse.getError()); }
/** * Create a flow configuration * It differs from {@link FlowConfigClient} in a way that it returns FlowStatusId, * which can be used to find the FlowExecutionId * @param flowConfig FlowConfig to be used to create the flow * @return FlowStatusId * @throws RemoteInvocationException */ public FlowStatusId createFlowConfig(FlowConfig flowConfig) throws RemoteInvocationException { LOG.debug("createFlowConfig with groupName " + flowConfig.getId().getFlowGroup() + " flowName " + flowConfig.getId().getFlowName()); CreateIdRequest<ComplexResourceKey<FlowId, FlowStatusId>, FlowConfig> request = _flowconfigsV2RequestBuilders.create().input(flowConfig).build(); ResponseFuture<IdResponse<ComplexResourceKey<FlowId, FlowStatusId>>> flowConfigResponseFuture = _restClient.get().sendRequest(request); return createFlowStatusId(flowConfigResponseFuture.getResponse().getLocation().toString()); }
private static Map<String, String> inheritHeaders(IndividualResponse individualResponse, Response<?> envelopeResponse) { Map<String, String> envelopeHeaders = HeaderUtil.removeHeaders(envelopeResponse.getHeaders(), HeaderUtil.NONINHERITABLE_RESPONSE_HEADERS); return HeaderUtil.mergeHeaders(envelopeHeaders, individualResponse.getHeaders()); }
private void successBatchGet(BatchGetRequest request, Response<BatchKVResponse<K, EntityResponse<RT>>> responseToBatch, Entry<RestRequestBatchKey, BatchEntry<Response<Object>>> entry, final ProtocolVersion version) { Set<String> ids = (Set<String>) request.getObjectIds().stream() .map(o -> BatchResponse.keyToString(o, version)) .collect(Collectors.toSet()); DataMap dm = filterIdsInBatchResult(responseToBatch.getEntity().data(), ids); BatchResponse br = new BatchResponse<>(dm, request.getResponseDecoder().getEntityClass()); Response rsp = new ResponseImpl(responseToBatch, br); entry.getValue().getPromise().done(rsp); }
/** * Get the latest flow status * @param flowId identifier of flow status to get * @return a {@link FlowStatus} with the flow status * @throws RemoteInvocationException */ public FlowStatus getLatestFlowStatus(FlowId flowId) throws RemoteInvocationException { LOG.debug("getFlowConfig with groupName " + flowId.getFlowGroup() + " flowName " + flowId.getFlowName()); FindRequest<FlowStatus> findRequest = _flowstatusesRequestBuilders.findByLatestFlowStatus().flowIdParam(flowId).build(); Response<CollectionResponse<FlowStatus>> response = _restClient.get().sendRequest(findRequest).getResponse(); List<FlowStatus> flowStatusList = response.getEntity().getElements(); if (flowStatusList.isEmpty()) { return null; } else { Preconditions.checkArgument(flowStatusList.size() == 1); return flowStatusList.get(0); } }
private static <K, RT extends RecordTemplate> Response<RT> unbatchResponse(BatchGetEntityRequest<K, RT> request, Response<BatchKVResponse<K, EntityResponse<RT>>> batchResponse, Object id) throws RemoteInvocationException { final BatchKVResponse<K, EntityResponse<RT>> batchEntity = batchResponse.getEntity(); final ErrorResponse errorResponse = batchEntity.getErrors().get(id); if (errorResponse != null) { throw new RestLiResponseException(errorResponse); } final EntityResponse<RT> entityResponse = batchEntity.getResults().get(id); if (entityResponse != null) { final RT entityResult = entityResponse.getEntity(); if (entityResult != null) { return new ResponseImpl<>(batchResponse, entityResult); } } LOGGER.debug("No result or error for base URI : {}, id: {}. Verify that the batchGet endpoint returns response keys that match batchGet request IDs.", request.getBaseUriTemplate(), id); throw NOT_FOUND_EXCEPTION; }
@Override public void sendRequest(PermitRequest request, Callback<Response<PermitAllocation>> callback) { this.requestList.add(request); PermitAllocation permitAllocation = new PermitAllocation(); permitAllocation.setPermits(request.getPermits()); permitAllocation.setExpiration(Long.MAX_VALUE); Response<PermitAllocation> response = Mockito.mock(Response.class); Mockito.when(response.getEntity()).thenReturn(permitAllocation); callback.onSuccess(response); } }
protected Task<String> toMessage(Task<Response<Greeting>> greeting) { return greeting.map("toMessage", g -> g.getEntity().getMessage()); }
@SuppressWarnings({ "deprecation" }) private void successBatchGetEntity(BatchGetEntityRequest request, Response<BatchKVResponse<K, EntityResponse<RT>>> responseToBatch, Entry<RestRequestBatchKey, BatchEntry<Response<Object>>> entry, final ProtocolVersion version) { Set<String> ids = (Set<String>) request.getObjectIds().stream() .map(o -> BatchResponse.keyToString(o, version)) .collect(Collectors.toSet()); DataMap dm = filterIdsInBatchResult(responseToBatch.getEntity().data(), ids); BatchKVResponse br = new BatchEntityResponse<>(dm, request.getResourceSpec().getKeyType(), request.getResourceSpec().getValueType(), request.getResourceSpec().getKeyParts(), request.getResourceSpec().getComplexKeyType(), version); Response rsp = new ResponseImpl(responseToBatch, br); entry.getValue().getPromise().done(rsp); }
@SuppressWarnings({ "deprecation" }) private void successBatchGetKV(BatchGetKVRequest request, Response<BatchKVResponse<K, EntityResponse<RT>>> responseToBatch, Entry<RestRequestBatchKey, BatchEntry<Response<Object>>> entry, final ProtocolVersion version) { Set<String> ids = (Set<String>) request.getObjectIds().stream() .map(o -> BatchResponse.keyToString(o, version)) .collect(Collectors.toSet()); DataMap dm = filterIdsInBatchResult(responseToBatch.getEntity().data(), ids); BatchKVResponse br = new BatchKVResponse(dm, request.getResourceSpec().getKeyType(), request.getResourceSpec().getValueType(), request.getResourceSpec().getKeyParts(), request.getResourceSpec().getComplexKeyType(), version); Response rsp = new ResponseImpl(responseToBatch, br); entry.getValue().getPromise().done(rsp); }
/** * Get a flow configuration * @param flowId identifier of flow configuration to get * @return a {@link FlowConfig} with the flow configuration * @throws RemoteInvocationException */ public FlowConfig getFlowConfig(FlowId flowId) throws RemoteInvocationException { LOG.debug("getFlowConfig with groupName " + flowId.getFlowGroup() + " flowName " + flowId.getFlowName()); GetRequest<FlowConfig> getRequest = _flowconfigsRequestBuilders.get() .id(new ComplexResourceKey<>(flowId, new EmptyRecord())).build(); Response<FlowConfig> response = _restClient.get().sendRequest(getRequest).getResponse(); return response.getEntity(); }