Search in sources :

Example 6 with DecodeException

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

the class RegistrationHttpEndpoint method doRegisterDeviceJson.

private void doRegisterDeviceJson(final RoutingContext ctx) {
    try {
        JsonObject payload = null;
        if (ctx.getBody().length() > 0) {
            payload = ctx.getBodyAsJson();
        }
        registerDevice(ctx, payload);
    } catch (final DecodeException e) {
        HttpUtils.badRequest(ctx, "body does not contain a valid JSON object");
    }
}
Also used : JsonObject(io.vertx.core.json.JsonObject) DecodeException(io.vertx.core.json.DecodeException)

Example 7 with DecodeException

use of io.vertx.core.json.DecodeException in project rulesservice by genny-project.

the class RulesLoader method processFileRealms.

private static List<Tuple3<String, String, String>> processFileRealms(final String realm, String inputFileStr) {
    File file = new File(inputFileStr);
    String fileName = inputFileStr.replaceFirst(".*/(\\w+).*", "$1");
    String fileNameExt = inputFileStr.replaceFirst(".*/\\w+\\.(.*)", "$1");
    List<Tuple3<String, String, String>> rules = new ArrayList<Tuple3<String, String, String>>();
    if (!file.isFile()) {
        // DIRECTORY
        if (!fileName.startsWith("XX")) {
            String localRealm = realm;
            if (fileName.startsWith("prj_") || fileName.startsWith("PRJ_")) {
                // extract realm name
                localRealm = fileName.substring("prj_".length()).toLowerCase();
            }
            final List<String> filesList = Vertx.currentContext().owner().fileSystem().readDirBlocking(inputFileStr);
            for (final String dirFileStr : filesList) {
                // use
                List<Tuple3<String, String, String>> childRules = processFileRealms(localRealm, dirFileStr);
                // directory
                // name
                // as
                // rulegroup
                rules.addAll(childRules);
            }
        }
        return rules;
    } else {
        Buffer buf = Vertx.currentContext().owner().fileSystem().readFileBlocking(inputFileStr);
        try {
            if ((!fileName.startsWith("XX")) && (fileNameExt.equalsIgnoreCase("drl"))) {
                // ignore files that start
                // with XX
                final String ruleText = buf.toString();
                Tuple3<String, String, String> rule = (Tuple.of(realm, fileName + "." + fileNameExt, ruleText));
                System.out.println(realm + " Loading in Rule:" + rule._1 + " of " + inputFileStr);
                rules.add(rule);
            } else if ((!fileName.startsWith("XX")) && (fileNameExt.equalsIgnoreCase("bpmn"))) {
                // ignore files
                // that start
                // with XX
                final String bpmnText = buf.toString();
                Tuple3<String, String, String> bpmn = (Tuple.of(realm, fileName + "." + fileNameExt, bpmnText));
                System.out.println(realm + " Loading in BPMN:" + bpmn._1 + " of " + inputFileStr);
                rules.add(bpmn);
            } else if ((!fileName.startsWith("XX")) && (fileNameExt.equalsIgnoreCase("xls"))) {
                // ignore files that
                // start with XX
                final String xlsText = buf.toString();
                Tuple3<String, String, String> xls = (Tuple.of(realm, fileName + "." + fileNameExt, xlsText));
                System.out.println(realm + " Loading in XLS:" + xls._1 + " of " + inputFileStr);
                rules.add(xls);
            }
            return rules;
        } catch (final DecodeException dE) {
        }
    }
    return null;
}
Also used : Buffer(io.vertx.rxjava.core.buffer.Buffer) Tuple3(io.vavr.Tuple3) ArrayList(java.util.ArrayList) File(java.io.File) DecodeException(io.vertx.core.json.DecodeException)

Example 8 with DecodeException

use of io.vertx.core.json.DecodeException in project raml-module-builder by folio-org.

the class SchemaDereferencer method dereferencedSchema.

/**
 * Return a schema with all $ref dereferenced.
 *
 * @param inputPath  path of the schema file, may be relative to the current directory
 * @param dereferenceStack  stack of the files chain for file
 * @return dereferenced RAML
 * @throws IOException  when any $ref file cannot be read
 * @throws IllegalStateException  when the $ref chain has a loop
 * @throws DecodeException  when the $ref file is not a JSON
 */
protected JsonObject dereferencedSchema(Path inputPath, Deque<Path> dereferenceStack) throws IOException {
    Path path = inputPath.normalize().toAbsolutePath();
    if (dereferenced.containsKey(path)) {
        return dereferenced.get(path);
    }
    boolean loop = dereferenceStack.contains(path);
    dereferenceStack.push(path);
    if (loop) {
        throw new IllegalStateException("$ref chain has a loop: " + allPaths(dereferenceStack));
    }
    String schemaString;
    try (InputStream reader = new FileInputStream(path.toFile())) {
        schemaString = IoUtil.toStringUtf8(reader);
    }
    JsonObject schema;
    if (schemaString.indexOf('{') == -1) {
        // schemaString contains the filename to open
        Path newInputPath = inputPath.resolveSibling(Paths.get(schemaString)).normalize();
        schema = dereferencedSchema(newInputPath, dereferenceStack);
    } else {
        try {
            schema = new JsonObject(schemaString);
        } catch (DecodeException e) {
            throw new DecodeException(allPaths(dereferenceStack), e);
        }
        dereference(schema, inputPath, dereferenceStack);
    }
    dereferenceStack.pop();
    dereferenced.put(path, schema);
    return schema;
}
Also used : Path(java.nio.file.Path) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JsonObject(io.vertx.core.json.JsonObject) DecodeException(io.vertx.core.json.DecodeException) FileInputStream(java.io.FileInputStream)

Example 9 with DecodeException

use of io.vertx.core.json.DecodeException in project vertx-web by vert-x3.

the class EventBusBridgeImpl method handleSocketData.

private void handleSocketData(SockJSSocket sock, Buffer data, Map<String, MessageConsumer> registrations) {
    JsonObject msg;
    try {
        msg = new JsonObject(data.toString());
    } catch (DecodeException e) {
        replyError(sock, "invalid_json");
        return;
    }
    String type = msg.getString("type");
    if (type == null) {
        replyError(sock, "missing_type");
        return;
    }
    if (type.equals("ping")) {
        internalHandlePing(sock);
    } else {
        String address = msg.getString("address");
        if (address == null) {
            replyError(sock, "missing_address");
            return;
        }
        switch(type) {
            case "send":
                internalHandleSendOrPub(sock, true, msg);
                break;
            case "publish":
                internalHandleSendOrPub(sock, false, msg);
                break;
            case "register":
                internalHandleRegister(sock, msg, registrations);
                break;
            case "unregister":
                internalHandleUnregister(sock, msg, registrations);
                break;
            default:
                log.error("Invalid type in incoming message: " + type);
                replyError(sock, "invalid_type");
        }
    }
}
Also used : JsonObject(io.vertx.core.json.JsonObject) DecodeException(io.vertx.core.json.DecodeException)

Example 10 with DecodeException

use of io.vertx.core.json.DecodeException in project okapi by folio-org.

the class InternalModule method pullModules.

private void pullModules(String body, Handler<ExtendedAsyncResult<String>> fut) {
    try {
        final PullDescriptor pmd = Json.decodeValue(body, PullDescriptor.class);
        pullManager.pull(pmd, res -> {
            if (res.failed()) {
                fut.handle(new Failure<>(res.getType(), res.cause()));
                return;
            }
            fut.handle(new Success<>(Json.encodePrettily(res.result())));
        });
    } catch (DecodeException ex) {
        fut.handle(new Failure<>(USER, ex));
    }
}
Also used : PullDescriptor(org.folio.okapi.bean.PullDescriptor) DecodeException(io.vertx.core.json.DecodeException) Failure(org.folio.okapi.common.Failure)

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