use of org.structr.rest.exception.SystemException in project structr by structr.
the class MaintenanceResource method doPost.
@Override
public RestMethodResult doPost(Map<String, Object> propertySet) throws FrameworkException {
if ((securityContext != null) && isSuperUser()) {
if (this.taskOrCommand != null) {
try {
final App app = StructrApp.getInstance(securityContext);
if (Task.class.isAssignableFrom(taskOrCommand)) {
Task task = (Task) taskOrCommand.newInstance();
app.processTasks(task);
} else if (MaintenanceCommand.class.isAssignableFrom(taskOrCommand)) {
MaintenanceCommand cmd = (MaintenanceCommand) StructrApp.getInstance(securityContext).command(taskOrCommand);
// flush caches if required
if (cmd.requiresFlushingOfCaches()) {
app.command(FlushCachesCommand.class).execute(Collections.EMPTY_MAP);
}
// create enclosing transaction if required
if (cmd.requiresEnclosingTransaction()) {
try (final Tx tx = app.tx()) {
cmd.execute(propertySet);
tx.success();
}
} else {
cmd.execute(propertySet);
}
final RestMethodResult result = new RestMethodResult(HttpServletResponse.SC_OK);
cmd.getCustomHeaders().forEach((final String headerName, final String headerValue) -> {
result.addHeader(headerName, headerValue);
});
cmd.getCustomHeaders().clear();
return result;
} else {
return new RestMethodResult(HttpServletResponse.SC_NOT_FOUND);
}
// return 200 OK
return new RestMethodResult(HttpServletResponse.SC_OK);
} catch (InstantiationException iex) {
throw new SystemException(iex.getMessage());
} catch (IllegalAccessException iaex) {
throw new SystemException(iaex.getMessage());
}
} else {
if (taskOrCommandName != null) {
throw new NotFoundException("No such task or command: " + this.taskOrCommandName);
} else {
throw new IllegalPathException("Maintenance resource needs parameter");
}
}
} else {
throw new NotAllowedException("Use of the maintenance endpoint is restricted to admin users");
}
}
Aggregations