private static <T> Response<T> ensureResponse(T t) { if (t instanceof Response) { //noinspection unchecked return (Response<T>) t; } else { return Response.forPayload(t); } } }
/** * Middleware that serializes the result of the inner handler using the supplied * {@link ObjectWriter}, and sets the Content-Type header to application/json. */ public static <T> Middleware<AsyncHandler<T>, AsyncHandler<Response<ByteString>>> jsonSerialize(ObjectWriter objectWriter) { return handler -> requestContext -> handler.invoke(requestContext) .thenApply(result -> Response .forPayload(serialize(objectWriter, result)) .withHeader(CONTENT_TYPE, JSON)); }
/** * Async middleware for POJO. * * @param templateName The template name, respective to the "resources" folder * @param <T> The Type of the parameters * @return the middlware */ public static <T> Middleware<AsyncHandler<T>, AsyncHandler<Response<ByteString>>> htmlSerialize( final String templateName) { return handler -> requestContext -> handler.invoke(requestContext) .thenApply(result -> Response .forPayload(serialize(templateName, result)) .withHeader(CONTENT_TYPE, HTML)); }
private Response<Collection<Workflow>> workflows() { try { return Response.forPayload(storage.workflows().values()); } catch (IOException e) { return Response.forStatus(Status.INTERNAL_SERVER_ERROR.withReasonPhrase( "Failed to get workflows")); } }
private Response<TriggerRequest> triggerAndWait(TriggerRequest triggerRequest, Workflow workflow, Instant instant) { final TriggerParameters parameters = triggerRequest.triggerParameters().orElse(TriggerParameters.zero()); final String triggerId = randomGenerator.generateUniqueId(AD_HOC_CLI_TRIGGER_PREFIX); final CompletionStage<Void> triggered = triggerListener.event( workflow, Trigger.adhoc(triggerId), instant, parameters); // TODO: return future instead of blocking try { triggered.toCompletableFuture().get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException(e); } catch (ExecutionException e) { return handleException(e); } // todo: change payload to a struct returning the triggerId as well so the user can refer to it return Response.forPayload(triggerRequest); }
private Response<List<Workflow>> workflows(String componentId) { try { return Response.forPayload(storage.workflows(componentId)); } catch (IOException e) { return Response.forStatus(Status.INTERNAL_SERVER_ERROR.withReasonPhrase( "Failed to get workflows of component " + componentId)); } }
private Response<BackfillPayload> getBackfill(RequestContext rc, String id) { final boolean includeStatuses = rc.request().parameter("status").orElse("true").equals("true"); final Optional<Backfill> backfillOpt; try { backfillOpt = storage.backfill(id); } catch (IOException e) { final String message = String.format("Couldn't read backfill %s. ", id); log.warn(message, e); return Response.forStatus(Status.INTERNAL_SERVER_ERROR.withReasonPhrase("Error in internal storage")); } if (!backfillOpt.isPresent()) { return Response.forStatus(Status.NOT_FOUND); } final Backfill backfill = backfillOpt.get(); if (includeStatuses) { final List<RunStateData> statuses = retrieveBackfillStatuses(backfill); return Response.forPayload(BackfillPayload.create( backfill, Optional.of(RunStateDataPayload.create(statuses)))); } else { return Response.forPayload(BackfillPayload.create(backfill, Optional.empty())); } }
private Response<WorkflowState> state(String componentId, String id) { try { return Response.forPayload(storage.workflowState(WorkflowId.create(componentId, id))); } catch (IOException e) { return Response .forStatus(Status.INTERNAL_SERVER_ERROR.withReasonPhrase("Couldn't fetch state.")); } }
return Response.forPayload(workflow);
Status.INTERNAL_SERVER_ERROR.withReasonPhrase("Couldn't fetch execution info.")); return Response.forPayload(data);
private Response<WorkflowInstanceExecutionData> instance( String componentId, String id, String instanceId) { final WorkflowId workflowId = WorkflowId.create(componentId, id); final WorkflowInstance workflowInstance = WorkflowInstance.create(workflowId, instanceId); try { final WorkflowInstanceExecutionData workflowInstanceExecutionData = storage.executionData(workflowInstance); return Response.forPayload(workflowInstanceExecutionData); } catch (ResourceNotFoundException e) { return Response.forStatus(Status.NOT_FOUND.withReasonPhrase(e.getMessage())); } catch (IOException e) { return Response.forStatus( Status.INTERNAL_SERVER_ERROR.withReasonPhrase("Couldn't fetch execution info.")); } }
return Response.forPayload(backfill);
private Response<TestServiceAccountUsageAuthorizationResponse> testServiceAccountUsageAuthorization( TestServiceAccountUsageAuthorizationRequest request) { final ServiceAccountUsageAuthorizationResult result = accountUsageAuthorizer.checkServiceAccountUsageAuthorization(request.serviceAccount(), request.principal()); result.errorResponse().ifPresent(e -> { throw new ResponseException(e); }); final TestServiceAccountUsageAuthorizationResponse response = new TestServiceAccountUsageAuthorizationResponseBuilder() .authorized(result.authorized()) .serviceAccount(request.serviceAccount()) .principal(request.principal()) .message(result.message()) .build(); return Response.forPayload(response); }