use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-core-public by infiniteautomation.
the class MangoTestBase method createMockDataSource.
protected MockDataSourceVO createMockDataSource(String name, String xid, boolean enabled, MangoPermission readPermission, MangoPermission editPermission) {
DataSourceService service = Common.getBean(DataSourceService.class);
MockDataSourceVO vo = (MockDataSourceVO) ModuleRegistry.getDataSourceDefinition(MockDataSourceDefinition.TYPE_NAME).baseCreateDataSourceVO();
vo.setXid(name);
vo.setName(xid);
vo.setEnabled(enabled);
vo.setReadPermission(readPermission);
vo.setEditPermission(editPermission);
try {
return (MockDataSourceVO) service.insert(vo);
} catch (ValidationException e) {
fail(e.getValidationErrorMessage(Common.getTranslations()));
return null;
}
}
use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-core-public by infiniteautomation.
the class MangoJavaScriptService method ensureValid.
/**
* Ensure the script is valid
*/
public void ensureValid(MangoJavaScript vo) throws ValidationException {
PermissionHolder user = Common.getUser();
permissionService.ensurePermission(user, dataSourcePermissionDefinition.getPermission());
ProcessResult result = validate(vo);
if (!result.isValid())
throw new ValidationException(result);
}
use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-core-public by infiniteautomation.
the class MangoJavaScriptService method executeScript.
/**
* The preferred way to execute a script
*/
public MangoJavaScriptResult executeScript(MangoJavaScript vo, ScriptPointValueSetter setter) throws ValidationException, PermissionException {
PermissionHolder user = Common.getUser();
ensureValid(vo);
MangoJavaScriptResult result = new MangoJavaScriptResult();
final Writer scriptOut;
final PrintWriter scriptWriter;
if (vo.isReturnLogOutput()) {
scriptOut = new StringWriter();
scriptWriter = new PrintWriter(scriptOut);
} else {
NullWriter writer = new NullWriter();
scriptWriter = new NullPrintWriter(writer);
scriptOut = writer;
}
try {
try (ScriptLogExtender scriptLog = new ScriptLogExtender("scriptTest-" + user.getPermissionHolderName(), vo.getLogLevel(), scriptWriter, vo.getLog(), vo.isCloseLog())) {
CompiledMangoJavaScript script = new CompiledMangoJavaScript(vo, setter, scriptLog, result, this, pointValueDao, pointValueCache);
script.compile(vo.getScript(), vo.isWrapInFunction());
script.initialize(vo.getContext());
long time = Common.timer.currentTimeMillis();
runAs.runAsCallable(script.getPermissionHolder(), () -> {
if (vo.getResultDataType() != null) {
script.execute(time, time, vo.getResultDataType());
} else {
script.execute(time, time);
}
return null;
});
}
} catch (ScriptError e) {
// The script exception should be clean as both compile() and execute() clean it
result.addError(new MangoJavaScriptError(e.getTranslatableMessage(), e.getLineNumber(), e.getColumnNumber()));
} catch (ResultTypeException | DataPointStateException e) {
result.addError(new MangoJavaScriptError(e.getTranslatableMessage()));
} catch (Exception e) {
result.addError(new MangoJavaScriptError(e.getMessage()));
} finally {
if (vo.isReturnLogOutput())
result.setScriptOutput(scriptOut.toString());
}
return result;
}
use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-modules-public by infiniteautomation.
the class EmailVerificationController method publicRegisterUser.
/**
* CAUTION: This method is public!
* However the token's signature is cryptographically verified.
*/
@ApiOperation(value = "Registers a new user if the token's signature can be verified", notes = "The new user is created disabled and must be approved by an administrator.")
@RequestMapping(method = RequestMethod.POST, value = "/public/register")
@AnonymousAccess
public ResponseEntity<UserModel> publicRegisterUser(@RequestBody PublicRegistrationRequest body) {
body.ensureValid();
User newUser = body.getUser().toVO();
try {
User created = emailVerificationService.publicRegisterNewUser(body.getToken(), newUser);
return new ResponseEntity<>(new UserModel(created), HttpStatus.OK);
} catch (ExpiredJwtException | UnsupportedJwtException | MalformedJwtException | IllegalArgumentException | SignatureException | MissingClaimException | IncorrectClaimException e) {
throw new BadRequestException(new TranslatableMessage("rest.error.invalidEmailVerificationToken"), e);
} catch (ValidationException e) {
e.getValidationResult().prefixContextKey("user");
throw e;
}
}
use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-modules-public by infiniteautomation.
the class EventsRestController method queryForEventsBySourceType.
@ApiOperation(value = "Find Events for a set of sources found by the supplied sourceType RQL query, then query for events with these sources using eventsRql", response = EventQueryResult.class)
@RequestMapping(method = RequestMethod.POST, value = "/query/events-by-source-type")
public StreamedArrayWithTotal queryForEventsBySourceType(@RequestBody EventQueryBySourceType body, @AuthenticationPrincipal PermissionHolder user) {
ASTNode rql = RQLUtils.parseRQLtoAST(body.getSourceRql());
ASTNode query = null;
List<Object> args = new ArrayList<>();
args.add("typeRef1");
// Query for the sources
switch(body.getSourceType()) {
case "DATA_POINT":
dataPointService.customizedQuery(rql, (vo) -> args.add(Integer.toString(vo.getId())));
if (args.size() > 1) {
query = new ASTNode("in", args);
query = RQLUtils.addAndRestriction(query, new ASTNode("eq", "typeName", EventTypeNames.DATA_POINT));
}
break;
case "DATA_SOURCE":
dataSourceService.customizedQuery(rql, (vo) -> args.add(Integer.toString(vo.getId())));
if (args.size() > 1) {
query = new ASTNode("in", args);
query = RQLUtils.addAndRestriction(query, new ASTNode("eq", "typeName", EventTypeNames.DATA_SOURCE));
}
break;
default:
ProcessResult result = new ProcessResult();
result.addContextualMessage("sourceType", "validate.invalidValue");
throw new ValidationException(result);
}
// Second query the events
if (query != null) {
// Apply the events query
ASTNode eventQuery = RQLUtils.parseRQLtoAST(body.getEventsRql());
query = RQLUtils.addAndRestriction(query, eventQuery);
return doQuery(query, user);
} else {
return new StreamedArrayWithTotal() {
@Override
public StreamedArray getItems() {
return null;
}
@Override
public int getTotal() {
return 0;
}
};
}
}
Aggregations