Search in sources :

Example 21 with DecodeException

use of io.vertx.core.json.DecodeException in project mod-inventory-storage by folio-org.

the class LoanTypeTest method send.

private JsonObject send(URL url, HttpMethod method, String content, int expectedStatusCode) {
    CompletableFuture<Response> future = new CompletableFuture<>();
    Handler<HttpClientResponse> handler = ResponseHandler.any(future);
    send(url, method, content, handler);
    Response response;
    try {
        response = future.get(5, TimeUnit.SECONDS);
    } catch (InterruptedException | ExecutionException | TimeoutException e) {
        throw new IllegalStateException(e);
    }
    assertThat(url + " - " + method + " - " + content + ":" + response.getBody(), response.getStatusCode(), is(expectedStatusCode));
    try {
        return response.getJson();
    } catch (DecodeException e) {
        // No body at all or not in JSON format.
        return null;
    }
}
Also used : Response(org.folio.rest.support.Response) HttpClientResponse(io.vertx.core.http.HttpClientResponse) CompletableFuture(java.util.concurrent.CompletableFuture) HttpClientResponse(io.vertx.core.http.HttpClientResponse) ExecutionException(java.util.concurrent.ExecutionException) DecodeException(io.vertx.core.json.DecodeException) TimeoutException(java.util.concurrent.TimeoutException)

Example 22 with DecodeException

use of io.vertx.core.json.DecodeException in project vert.x by eclipse.

the class BufferTest method testToJsonObject.

@Test
public void testToJsonObject() throws Exception {
    JsonObject obj = new JsonObject();
    obj.put("wibble", "wibble_value");
    obj.put("foo", 5);
    obj.put("bar", true);
    Buffer buff = Buffer.buffer(obj.encode());
    assertEquals(obj, buff.toJsonObject());
    buff = Buffer.buffer(TestUtils.randomAlphaString(10));
    try {
        buff.toJsonObject();
        fail();
    } catch (DecodeException ignore) {
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ByteBuffer(java.nio.ByteBuffer) JsonObject(io.vertx.core.json.JsonObject) DecodeException(io.vertx.core.json.DecodeException) Test(org.junit.Test)

Example 23 with DecodeException

use of io.vertx.core.json.DecodeException in project vert.x by eclipse.

the class RunCommand method getConfiguration.

protected JsonObject getConfiguration() {
    JsonObject conf;
    if (config != null) {
        try (Scanner scanner = new Scanner(new File(config)).useDelimiter("\\A")) {
            String sconf = scanner.next();
            try {
                conf = new JsonObject(sconf);
            } catch (DecodeException e) {
                log.error("Configuration file " + sconf + " does not contain a valid JSON object");
                return null;
            }
        } catch (FileNotFoundException e) {
            try {
                conf = new JsonObject(config);
            } catch (DecodeException e2) {
                // The configuration is not printed for security purpose, it can contain sensitive data.
                log.error("The -conf option does not point to an existing file or is not a valid JSON object");
                e2.printStackTrace();
                return null;
            }
        }
    } else {
        conf = null;
    }
    return conf;
}
Also used : Scanner(java.util.Scanner) FileNotFoundException(java.io.FileNotFoundException) JsonObject(io.vertx.core.json.JsonObject) File(java.io.File) DecodeException(io.vertx.core.json.DecodeException)

Example 24 with DecodeException

use of io.vertx.core.json.DecodeException in project hono by eclipse.

the class RegistrationTestSupport method send.

private CompletableFuture<RegistrationResult> send(final String deviceId, final String action, final Integer expectedStatus) {
    try {
        final String correlationId = UUID.randomUUID().toString();
        final Message message = session.createMessage();
        message.setStringProperty(MessageHelper.APP_PROPERTY_DEVICE_ID, deviceId);
        message.setJMSType(action);
        message.setJMSReplyTo(reply);
        message.setJMSCorrelationID(correlationId);
        LOGGER.debug("adding response handler for request [correlation ID: {}]", correlationId);
        final CompletableFuture<RegistrationResult> result = c.add(correlationId, response -> {
            final Integer status = getIntProperty(response, MessageHelper.APP_PROPERTY_STATUS);
            LOGGER.debug("received response [type: {}, status: {}] for request [correlation ID: {}]", response.getClass().getName(), status, correlationId);
            int httpStatus = status;
            if (status == null || httpStatus <= 0) {
                throw new IllegalStateException("Response to " + getMessageID(response) + " contained no valid status: " + httpStatus);
            }
            if (expectedStatus != null && expectedStatus != httpStatus) {
                throw new IllegalStateException("returned status " + httpStatus);
            }
            try {
                if (response.isBodyAssignableTo(String.class)) {
                    String body = response.getBody(String.class);
                    if (body != null) {
                        LOGGER.debug("extracting response body");
                        return RegistrationResult.from(httpStatus, new JsonObject(body));
                    }
                }
            } catch (JMSException | DecodeException e) {
                LOGGER.debug("cannot extract body from response", e);
            }
            return RegistrationResult.from(httpStatus);
        });
        producer.send(message);
        return result;
    } catch (final JMSException jmsException) {
        throw new IllegalStateException("Failed to send message.", jmsException);
    }
}
Also used : Message(javax.jms.Message) JsonObject(io.vertx.core.json.JsonObject) JMSException(javax.jms.JMSException) RegistrationResult(org.eclipse.hono.util.RegistrationResult) DecodeException(io.vertx.core.json.DecodeException)

Example 25 with DecodeException

use of io.vertx.core.json.DecodeException in project hono by eclipse.

the class FileBasedCredentialsService method addAll.

private Future<Void> addAll(final Buffer credentials) {
    Future<Void> result = Future.future();
    try {
        int credentialsCount = 0;
        final JsonArray allObjects = credentials.toJsonArray();
        log.debug("trying to load credentials for {} tenants", allObjects.size());
        for (Object obj : allObjects) {
            if (JsonObject.class.isInstance(obj)) {
                credentialsCount += addCredentialsForTenant((JsonObject) obj);
            }
        }
        log.info("successfully loaded {} credentials from file [{}]", credentialsCount, getConfig().getFilename());
        result.complete();
    } catch (DecodeException e) {
        log.warn("cannot read malformed JSON from credentials file [{}]", getConfig().getFilename());
        result.fail(e);
    }
    return result;
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) DecodeException(io.vertx.core.json.DecodeException)

Aggregations

DecodeException (io.vertx.core.json.DecodeException)42 JsonObject (io.vertx.core.json.JsonObject)17 Failure (org.folio.okapi.common.Failure)13 Test (org.junit.Test)8 File (java.io.File)6 DeploymentDescriptor (org.folio.okapi.bean.DeploymentDescriptor)6 TenantModuleDescriptor (org.folio.okapi.bean.TenantModuleDescriptor)6 JsonArray (io.vertx.core.json.JsonArray)4 ArrayList (java.util.ArrayList)4 ModuleDescriptor (org.folio.okapi.bean.ModuleDescriptor)4 Buffer (io.vertx.rxjava.core.buffer.Buffer)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Tuple2 (io.vavr.Tuple2)2 Vertx (io.vertx.core.Vertx)2 Buffer (io.vertx.core.buffer.Buffer)2 FileNotFoundException (java.io.FileNotFoundException)2 ByteBuffer (java.nio.ByteBuffer)2 Scanner (java.util.Scanner)2 AmqpClient (io.enmasse.iot.transport.AmqpClient)1