Search in sources :

Example 1 with Command

use of com.google.refine.commands.Command in project OpenRefine by OpenRefine.

the class PreviewWikibaseSchemaCommand method doPost.

/**
 * This command uses POST but is left CSRF-unprotected since it does not
 * incur a side effect or state change in the backend.
 * The reason why it uses POST is to make sure large schemas and engines
 * can be passed as parameters.
 */
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        Project project = getProject(request);
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Type", "application/json");
        String schemaJson = request.getParameter("schema");
        WikibaseSchema schema = null;
        if (schemaJson != null) {
            try {
                schema = WikibaseSchema.reconstruct(schemaJson);
            } catch (IOException e) {
                respondError(response, "Wikibase schema could not be parsed. Error message: " + e.getMessage());
                return;
            }
        } else {
            schema = (WikibaseSchema) project.overlayModels.get("wikibaseSchema");
        }
        if (schema == null) {
            respondError(response, "No Wikibase schema provided.");
            return;
        }
        Manifest manifest = null;
        String manifestJson = request.getParameter("manifest");
        if (manifestJson != null) {
            try {
                manifest = ManifestParser.parse(manifestJson);
            } catch (ManifestException e) {
                respondError(response, "Wikibase manifest could not be parsed. Error message: " + e.getMessage());
                return;
            }
        }
        if (manifest == null) {
            respondError(response, "No Wikibase manifest provided.");
            return;
        }
        QAWarningStore warningStore = new QAWarningStore();
        // Evaluate project
        Engine engine = getEngine(request, project);
        List<TermedStatementEntityEdit> editBatch = schema.evaluate(project, engine, warningStore);
        // Inspect the edits and generate warnings
        EditInspector inspector = new EditInspector(warningStore, manifest);
        inspector.inspect(editBatch, schema);
        // Dump the first 10 edits, scheduled with the default scheduler
        WikibaseAPIUpdateScheduler scheduler = new WikibaseAPIUpdateScheduler();
        List<TermedStatementEntityEdit> nonNullEdits = scheduler.schedule(editBatch).stream().filter(e -> !e.isNull()).collect(Collectors.toList());
        List<TermedStatementEntityEdit> firstEdits = nonNullEdits.stream().limit(10).collect(Collectors.toList());
        PreviewResults previewResults = new PreviewResults(warningStore.getWarnings(), warningStore.getMaxSeverity(), warningStore.getNbWarnings(), nonNullEdits.size(), firstEdits);
        respondJSON(response, previewResults);
    } catch (Exception e) {
        respondException(response, e);
    }
}
Also used : Project(com.google.refine.model.Project) ServletException(javax.servlet.ServletException) ManifestException(org.openrefine.wikidata.manifests.ManifestException) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) EditInspector(org.openrefine.wikidata.qa.EditInspector) Collectors(java.util.stream.Collectors) ManifestParser(org.openrefine.wikidata.manifests.ManifestParser) List(java.util.List) HttpServletRequest(javax.servlet.http.HttpServletRequest) WikibaseSchema(org.openrefine.wikidata.schema.WikibaseSchema) WikibaseAPIUpdateScheduler(org.openrefine.wikidata.updates.scheduler.WikibaseAPIUpdateScheduler) CommandUtilities.respondError(org.openrefine.wikidata.commands.CommandUtilities.respondError) QAWarningStore(org.openrefine.wikidata.qa.QAWarningStore) Command(com.google.refine.commands.Command) Engine(com.google.refine.browsing.Engine) Manifest(org.openrefine.wikidata.manifests.Manifest) IOException(java.io.IOException) Manifest(org.openrefine.wikidata.manifests.Manifest) ManifestException(org.openrefine.wikidata.manifests.ManifestException) ServletException(javax.servlet.ServletException) ManifestException(org.openrefine.wikidata.manifests.ManifestException) IOException(java.io.IOException) Project(com.google.refine.model.Project) EditInspector(org.openrefine.wikidata.qa.EditInspector) WikibaseAPIUpdateScheduler(org.openrefine.wikidata.updates.scheduler.WikibaseAPIUpdateScheduler) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) WikibaseSchema(org.openrefine.wikidata.schema.WikibaseSchema) QAWarningStore(org.openrefine.wikidata.qa.QAWarningStore) Engine(com.google.refine.browsing.Engine)

Example 2 with Command

use of com.google.refine.commands.Command in project polymap4-core by Polymap4.

the class RefineServiceImpl method post.

// public Object get( Class<? extends Command> commandClazz ) {
// try {
// Command command = command( commandClazz );
// RefineResponse response = createResponse();
// command.doGet( createRequest( null ), response );
// return response.result();
// }
// catch (Exception e) {
// throw new RuntimeException( e );
// }
// }
// 
// 
// public Object post( Class<? extends Command> commandClazz ) {
// try {
// Command command = command( commandClazz );
// RefineResponse response = createResponse();
// command.doPost( createRequest( null ), response );
// return response.result();
// }
// catch (Exception e) {
// throw new RuntimeException( e );
// }
// }
public Object post(Class<? extends Command> commandClazz, Map<String, String> params) {
    try {
        Command command = command(commandClazz);
        RefineResponse response = createResponse();
        command.doPost(createRequest(params), response);
        return response.result();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ImportingControllerCommand(com.google.refine.commands.importing.ImportingControllerCommand) Command(com.google.refine.commands.Command)

Example 3 with Command

use of com.google.refine.commands.Command in project polymap4-core by Polymap4.

the class RefineServiceImpl method command.

private Command command(Class<? extends Command> commandClazz) throws InstantiationException, IllegalAccessException {
    Command command = commandInstances.get(commandClazz);
    if (command == null) {
        command = commandClazz.newInstance();
        commandInstances.put(commandClazz, command);
    }
    return command;
}
Also used : ImportingControllerCommand(com.google.refine.commands.importing.ImportingControllerCommand) Command(com.google.refine.commands.Command)

Example 4 with Command

use of com.google.refine.commands.Command in project OpenRefine by OpenRefine.

the class RefineServlet method service.

@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if (request.getPathInfo().startsWith("/command/")) {
        String commandKey = getCommandKey(request);
        Command command = commands.get(commandKey);
        if (command != null) {
            if (request.getMethod().equals("GET")) {
                if (!logger.isTraceEnabled() && command.logRequests()) {
                    logger.info("GET {}", request.getPathInfo());
                }
                logger.trace("> GET {}", commandKey);
                command.doGet(request, response);
                logger.trace("< GET {}", commandKey);
            } else if (request.getMethod().equals("POST")) {
                if (!logger.isTraceEnabled() && command.logRequests()) {
                    logger.info("POST {}", request.getPathInfo());
                }
                logger.trace("> POST {}", commandKey);
                command.doPost(request, response);
                logger.trace("< POST {}", commandKey);
            } else if (request.getMethod().equals("PUT")) {
                if (!logger.isTraceEnabled() && command.logRequests()) {
                    logger.info("PUT {}", request.getPathInfo());
                }
                logger.trace("> PUT {}", commandKey);
                command.doPut(request, response);
                logger.trace("< PUT {}", commandKey);
            } else if (request.getMethod().equals("DELETE")) {
                if (!logger.isTraceEnabled() && command.logRequests()) {
                    logger.info("DELETE {}", request.getPathInfo());
                }
                logger.trace("> DELETE {}", commandKey);
                command.doDelete(request, response);
                logger.trace("< DELETE {}", commandKey);
            } else {
                response.sendError(HttpStatus.SC_METHOD_NOT_ALLOWED);
            }
        } else {
            response.sendError(HttpStatus.SC_NOT_FOUND);
        }
    } else {
        super.service(request, response);
    }
}
Also used : Command(com.google.refine.commands.Command)

Aggregations

Command (com.google.refine.commands.Command)4 ImportingControllerCommand (com.google.refine.commands.importing.ImportingControllerCommand)2 Engine (com.google.refine.browsing.Engine)1 Project (com.google.refine.model.Project)1 IOException (java.io.IOException)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 CommandUtilities.respondError (org.openrefine.wikidata.commands.CommandUtilities.respondError)1 Manifest (org.openrefine.wikidata.manifests.Manifest)1 ManifestException (org.openrefine.wikidata.manifests.ManifestException)1 ManifestParser (org.openrefine.wikidata.manifests.ManifestParser)1 EditInspector (org.openrefine.wikidata.qa.EditInspector)1 QAWarningStore (org.openrefine.wikidata.qa.QAWarningStore)1 WikibaseSchema (org.openrefine.wikidata.schema.WikibaseSchema)1 TermedStatementEntityEdit (org.openrefine.wikidata.updates.TermedStatementEntityEdit)1 WikibaseAPIUpdateScheduler (org.openrefine.wikidata.updates.scheduler.WikibaseAPIUpdateScheduler)1