use of io.vertx.core.json.Json in project mod-inventory-storage by folio-org.
the class LocationsTest method send.
// /////////////////////////// helpers
private static void send(URL url, HttpMethod method, String content, String contentType, Handler<HttpClientResponse> handler) {
HttpClient client = StorageTestSuite.getVertx().createHttpClient();
HttpClientRequest request;
if (content == null) {
content = "";
}
Buffer buffer = Buffer.buffer(content);
if (method == HttpMethod.POST) {
request = client.postAbs(url.toString());
} else if (method == HttpMethod.DELETE) {
request = client.deleteAbs(url.toString());
} else if (method == HttpMethod.GET) {
request = client.getAbs(url.toString());
} else {
request = client.putAbs(url.toString());
}
request.exceptionHandler(error -> {
Assert.fail(error.getLocalizedMessage());
}).handler(handler);
request.putHeader("Authorization", "test_tenant");
request.putHeader("x-okapi-tenant", "test_tenant");
request.putHeader("Accept", "application/json,text/plain");
request.putHeader("Content-type", contentType);
request.end(buffer);
}
use of io.vertx.core.json.Json in project raml-module-builder by folio-org.
the class DemoRamlRestTest method testStream.
private void testStream(TestContext context, boolean chunked) {
int chunkSize = 1024;
int numberChunks = 50;
Async async = context.async();
vertx.createHttpClient().request(HttpMethod.POST, port, "localhost", "/rmbtests/testStream").onComplete(context.asyncAssertSuccess(request -> {
request.response().onComplete(context.asyncAssertSuccess(response -> {
assertThat(response.statusCode(), is(200));
response.body(context.asyncAssertSuccess(body -> {
assertThat(body.toJsonObject().getBoolean("complete"), is(true));
async.complete();
}));
}));
if (chunked) {
request.setChunked(true);
} else {
request.putHeader("Content-Length", Integer.toString(chunkSize * numberChunks));
}
request.putHeader("Accept", "application/json,text/plain");
request.putHeader("Content-type", "application/octet-stream");
request.putHeader("x-okapi-tenant", TENANT);
String chunk = "X".repeat(chunkSize);
for (int i = 0; i < numberChunks; i++) {
request.write(chunk);
}
request.end(context.asyncAssertSuccess());
}));
}
use of io.vertx.core.json.Json in project hono by eclipse.
the class ProtonBasedCredentialsClient method get.
@Override
public Future<CredentialsObject> get(final String tenantId, final String type, final String authId, final JsonObject clientContext, final SpanContext spanContext) {
Objects.requireNonNull(tenantId);
Objects.requireNonNull(type);
Objects.requireNonNull(authId);
Objects.requireNonNull(clientContext);
final int clientContextHashCode;
if (clientContext.isEmpty()) {
clientContextHashCode = clientContext.hashCode();
} else {
// "normalize" JSON so that binary valued properties always
// contain the value's Base64 encoding instead of the raw byte array
// and thus always result in the same hash code
clientContextHashCode = new JsonObject(clientContext.encode()).hashCode();
}
final AnnotatedCacheKey<CacheKey> responseCacheKey = new AnnotatedCacheKey<>(new CacheKey(tenantId, type, authId, clientContextHashCode));
final Span span = newChildSpan(spanContext, "get Credentials");
span.setTag(MessageHelper.APP_PROPERTY_TENANT_ID, tenantId);
span.setTag(TAG_CREDENTIALS_TYPE, type);
span.setTag(TAG_AUTH_ID, authId);
final Future<CredentialsResult<CredentialsObject>> resultTracker = getResponseFromCache(responseCacheKey, span).recover(cacheMiss -> getOrCreateClient(tenantId).compose(client -> {
final JsonObject specification = CredentialsConstants.getSearchCriteria(type, authId).mergeIn(clientContext);
if (LOG.isTraceEnabled()) {
LOG.trace("getting credentials using spec:{}{}", System.lineSeparator(), specification.encodePrettily());
}
return client.createAndSendRequest(CredentialsConstants.CredentialsAction.get.toString(), null, specification.toBuffer(), RequestResponseApiConstants.CONTENT_TYPE_APPLICATION_JSON, this::getRequestResponseResult, span);
}).map(credentialsResult -> {
addResultToCache(responseCacheKey, credentialsResult);
return credentialsResult;
}));
return mapResultAndFinishSpan(resultTracker, result -> {
switch(result.getStatus()) {
case HttpURLConnection.HTTP_OK:
case HttpURLConnection.HTTP_CREATED:
return result.getPayload();
case HttpURLConnection.HTTP_NOT_FOUND:
throw new ClientErrorException(result.getStatus(), "no such credentials");
default:
throw StatusCodeMapper.from(result);
}
}, span);
}
use of io.vertx.core.json.Json in project gravitee-gateway by gravitee-io.
the class ApisHandler method handle.
@Override
public void handle(RoutingContext ctx) {
HttpServerResponse response = ctx.response();
response.setStatusCode(HttpStatusCode.OK_200);
response.putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
response.setChunked(true);
try {
Collection<ListApiEntity> apis = apiManager.apis().stream().map(api -> {
ListApiEntity entity = new ListApiEntity();
entity.setId(api.getId());
entity.setName(api.getName());
entity.setVersion(api.getVersion());
return entity;
}).collect(Collectors.toList());
Json.prettyMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
response.write(Json.prettyMapper.writeValueAsString(apis));
} catch (JsonProcessingException jpe) {
response.setStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR_500);
LOGGER.error("Unable to transform data object to JSON", jpe);
}
response.end();
}
Aggregations