Search in sources :

Example 1 with INDEX

use of com.codingchili.Model.FileParser.INDEX in project parser-excel-elasticsearch by codingchili.

the class Website method setRouterAPI.

/**
 * Adds the upload route to the given router
 *
 * @param router the upload route is added to the given router
 */
private void setRouterAPI(Router router) {
    router.route("/api/upload").handler(context -> {
        Iterator<FileUpload> iterator = context.fileUploads().iterator();
        if (iterator.hasNext()) {
            MultiMap params = context.request().params();
            logger.info("Receiving uploaded file with request id " + params.get(UPLOAD_ID));
            FileUpload upload = context.fileUploads().iterator().next();
            vertx.fileSystem().readFile(upload.uploadedFileName(), file -> {
                parse(file.result(), params, upload.fileName(), Future.<Integer>future().setHandler(result -> {
                    if (result.succeeded()) {
                        String index = context.request().params().get(INDEX);
                        logger.info(String.format("Imported file '%s' successfully into '%s'.", upload.fileName(), index));
                        context.put(INDEX, index);
                        context.put(FILE, upload.fileName());
                        context.put(IMPORTED, result.result());
                        context.reroute(DONE);
                    } else {
                        context.put(MESSAGE, traceToText(result.cause()));
                        logger.log(Level.SEVERE, String.format("Failed to parse file '%s'.", upload.fileName()), result.cause());
                        context.reroute(ERROR);
                    }
                }));
            });
        } else {
            context.put(MESSAGE, NO_FILE_WAS_UPLOADED);
            context.reroute(ERROR);
        }
    });
}
Also used : VERSION(com.codingchili.ApplicationLauncher.VERSION) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) MultiMap(io.vertx.core.MultiMap) Router(io.vertx.ext.web.Router) BodyHandler(io.vertx.ext.web.handler.BodyHandler) Context(io.vertx.core.Context) Level(java.util.logging.Level) Configuration(com.codingchili.Model.Configuration) ElasticWriter(com.codingchili.Model.ElasticWriter) JadeTemplateEngine(io.vertx.ext.web.templ.JadeTemplateEngine) JsonObject(io.vertx.core.json.JsonObject) PrintWriter(java.io.PrintWriter) Iterator(java.util.Iterator) StaticHandler(io.vertx.ext.web.handler.StaticHandler) StringWriter(java.io.StringWriter) FileParser(com.codingchili.Model.FileParser) Vertx(io.vertx.core.Vertx) ParserException(com.codingchili.Model.ParserException) Logger(java.util.logging.Logger) Future(io.vertx.core.Future) INDEXING_ELASTICSEARCH(com.codingchili.Model.Configuration.INDEXING_ELASTICSEARCH) FileUpload(io.vertx.ext.web.FileUpload) TemplateHandler(io.vertx.ext.web.handler.TemplateHandler) INDEX(com.codingchili.Model.FileParser.INDEX) Buffer(io.vertx.core.buffer.Buffer) AbstractVerticle(io.vertx.core.AbstractVerticle) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) MultiMap(io.vertx.core.MultiMap) FileUpload(io.vertx.ext.web.FileUpload)

Aggregations

VERSION (com.codingchili.ApplicationLauncher.VERSION)1 Configuration (com.codingchili.Model.Configuration)1 INDEXING_ELASTICSEARCH (com.codingchili.Model.Configuration.INDEXING_ELASTICSEARCH)1 ElasticWriter (com.codingchili.Model.ElasticWriter)1 FileParser (com.codingchili.Model.FileParser)1 INDEX (com.codingchili.Model.FileParser.INDEX)1 ParserException (com.codingchili.Model.ParserException)1 AbstractVerticle (io.vertx.core.AbstractVerticle)1 Context (io.vertx.core.Context)1 Future (io.vertx.core.Future)1 MultiMap (io.vertx.core.MultiMap)1 Vertx (io.vertx.core.Vertx)1 Buffer (io.vertx.core.buffer.Buffer)1 DeliveryOptions (io.vertx.core.eventbus.DeliveryOptions)1 MessageConsumer (io.vertx.core.eventbus.MessageConsumer)1 JsonObject (io.vertx.core.json.JsonObject)1 FileUpload (io.vertx.ext.web.FileUpload)1 Router (io.vertx.ext.web.Router)1 BodyHandler (io.vertx.ext.web.handler.BodyHandler)1 StaticHandler (io.vertx.ext.web.handler.StaticHandler)1