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);
}
}
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);
}
}
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;
}
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);
}
}
Aggregations