use of io.javalin.http.Context in project emfcloud-modelserver by eclipse-emfcloud.
the class DefaultModelControllerTest method getAllXmiFormat.
@Test
public void getAllXmiFormat() throws EncodingException, IOException {
final AtomicReference<JsonNode> response = new AtomicReference<>();
final EClass brewingUnit = EcoreFactory.eINSTANCE.createEClass();
Answer<Void> answer = invocation -> {
response.set(invocation.getArgument(0));
return null;
};
doAnswer(answer).when(context).json(any(JsonNode.class));
final LinkedHashMap<String, List<String>> queryParams = new LinkedHashMap<>();
queryParams.put(ModelServerPathParametersV1.FORMAT, Collections.singletonList(ModelServerPathParametersV1.FORMAT_XMI));
when(context.queryParamMap()).thenReturn(queryParams);
final Map<URI, EObject> allModels = Collections.singletonMap(URI.createURI("test"), brewingUnit);
when(modelRepository.getAllModels()).thenReturn(allModels);
modelController.getAll(context);
assertThat(response.get().get(JsonResponseMember.DATA), is(equalTo(Json.object(Json.prop("test", new XmiCodec().encode(brewingUnit))))));
}
use of io.javalin.http.Context in project teku by ConsenSys.
the class GetBlockHeaders method handle.
@OpenApi(path = ROUTE, method = HttpMethod.GET, summary = "Get block headers", tags = { TAG_BEACON }, description = "Retrieves block headers matching given query. By default it will fetch current head slot blocks.", queryParams = { @OpenApiParam(name = SLOT), @OpenApiParam(name = PARENT_ROOT, description = "Not currently supported.") }, responses = { @OpenApiResponse(status = RES_OK, content = @OpenApiContent(from = GetBlockHeadersResponse.class)), @OpenApiResponse(status = RES_BAD_REQUEST), @OpenApiResponse(status = RES_INTERNAL_ERROR) })
@Override
public void handle(@NotNull final Context ctx) throws Exception {
final Map<String, List<String>> queryParameters = ctx.queryParamMap();
final Optional<Bytes32> parentRoot = SingleQueryParameterUtils.getParameterValueAsBytes32IfPresent(queryParameters, PARENT_ROOT);
final Optional<UInt64> slot = SingleQueryParameterUtils.getParameterValueAsUInt64IfPresent(queryParameters, SLOT);
try {
ctx.future(chainDataProvider.getBlockHeaders(parentRoot, slot).thenApplyChecked(jsonProvider::objectToJSON).exceptionallyCompose(error -> handleError(ctx, error)));
} catch (final IllegalArgumentException e) {
ctx.status(SC_BAD_REQUEST);
ctx.json(jsonProvider.objectToJSON(new BadRequest(e.getMessage())));
}
}
use of io.javalin.http.Context in project teku by ConsenSys.
the class PostBlock method handle.
@OpenApi(path = ROUTE, method = HttpMethod.POST, summary = "Publish a signed block", tags = { TAG_BEACON, TAG_VALIDATOR_REQUIRED }, requestBody = @OpenApiRequestBody(content = { @OpenApiContent(from = SignedBlock.class) }), description = "Submit a signed beacon block to the beacon node to be imported." + " The beacon node performs the required validation.", responses = { @OpenApiResponse(status = RES_OK, description = "Block has been successfully broadcast, validated and imported."), @OpenApiResponse(status = RES_ACCEPTED, description = "Block has been successfully broadcast, but failed validation and has not been imported."), @OpenApiResponse(status = RES_BAD_REQUEST, description = "Unable to parse request body."), @OpenApiResponse(status = RES_INTERNAL_ERROR, description = "Beacon node experienced an internal error."), @OpenApiResponse(status = RES_SERVICE_UNAVAILABLE, description = "Beacon node is currently syncing.") })
@Override
public void handle(final Context ctx) throws Exception {
try {
if (syncDataProvider.isSyncing()) {
ctx.status(SC_SERVICE_UNAVAILABLE);
ctx.json(BadRequest.serviceUnavailable(jsonProvider));
return;
}
final SignedBeaconBlock signedBeaconBlock = validatorDataProvider.parseBlock(jsonProvider, ctx.body());
ctx.future(validatorDataProvider.submitSignedBlock(signedBeaconBlock).thenApplyChecked(validatorBlockResult -> handleResponseContext(ctx, validatorBlockResult)));
} catch (final JsonProcessingException ex) {
ctx.status(SC_BAD_REQUEST);
ctx.json(BadRequest.badRequest(jsonProvider, ex.getMessage()));
} catch (final Exception ex) {
LOG.error("Failed to post block due to internal error", ex);
ctx.status(SC_INTERNAL_SERVER_ERROR);
ctx.json(BadRequest.internalError(jsonProvider, ex.getMessage()));
}
}
use of io.javalin.http.Context in project teku by ConsenSys.
the class GetAggregateAttestation method handle.
@OpenApi(path = ROUTE, method = HttpMethod.GET, summary = "Get aggregated attestations", description = "Aggregates all attestations matching given attestation data root and slot.", tags = { TAG_VALIDATOR, TAG_VALIDATOR_REQUIRED }, queryParams = { @OpenApiParam(name = ATTESTATION_DATA_ROOT, description = "`String` HashTreeRoot of AttestationData that validator wants aggregated.", required = true), @OpenApiParam(name = SLOT, description = "`uint64` Non-finalized slot for which to create the aggregation.", required = true) }, responses = { @OpenApiResponse(status = RES_OK, content = @OpenApiContent(from = GetAggregatedAttestationResponse.class), description = "Returns aggregated `Attestation` object with same `AttestationData` root."), @OpenApiResponse(status = RES_BAD_REQUEST, description = "Invalid parameter supplied"), @OpenApiResponse(status = RES_NOT_FOUND, description = "No matching attestations were found"), @OpenApiResponse(status = RES_FORBIDDEN, description = "Beacon node was not assigned to aggregate on that subnet"), @OpenApiResponse(status = RES_INTERNAL_ERROR, description = "Beacon node internal error.") })
@Override
public void handle(Context ctx) throws Exception {
try {
final Map<String, List<String>> parameters = ctx.queryParamMap();
if (parameters.size() < 2) {
throw new IllegalArgumentException(String.format("Please specify both %s and %s", ATTESTATION_DATA_ROOT, SLOT));
}
Bytes32 beacon_block_root = getParameterValueAsBytes32(parameters, ATTESTATION_DATA_ROOT);
final UInt64 slot = getParameterValueAsUInt64(parameters, SLOT);
ctx.future(provider.createAggregate(slot, beacon_block_root).thenApplyChecked(optionalAttestation -> serializeResult(ctx, optionalAttestation)).exceptionallyCompose(error -> handleError(ctx, error)));
} catch (final IllegalArgumentException e) {
ctx.json(jsonProvider.objectToJSON(new BadRequest(e.getMessage())));
ctx.status(SC_BAD_REQUEST);
}
}
use of io.javalin.http.Context in project cineast by vitrivr.
the class FindSegmentsByIdGetHandler method doGet.
@Override
public MediaSegmentQueryResult doGet(Context ctx) {
final Map<String, String> parameters = ctx.pathParamMap();
final String segmentId = parameters.get(ID_NAME);
final MediaSegmentReader sl = new MediaSegmentReader(Config.sharedConfig().getDatabase().getSelectorSupplier().get());
final List<MediaSegmentDescriptor> list = sl.lookUpSegment(segmentId).map(s -> {
final List<MediaSegmentDescriptor> segments = new ArrayList<>(1);
segments.add(s);
return segments;
}).orElse(new ArrayList<>(0));
sl.close();
return new MediaSegmentQueryResult("", list);
}
Aggregations