use of com.infiniteautomation.mango.rest.latest.exception.BadRequestException in project ma-modules-public by infiniteautomation.
the class DataPointTagsRestController method doIndividualRequest.
private TagIndividualResponse doIndividualRequest(TagIndividualRequest request, BulkTagAction defaultAction, Map<String, String> defaultBody) {
TagIndividualResponse result = new TagIndividualResponse();
try {
String xid = request.getXid();
if (xid == null) {
throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "xid"));
}
result.setXid(xid);
BulkTagAction action = request.getAction() == null ? defaultAction : request.getAction();
if (action == null) {
throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "action"));
}
result.setAction(action);
Map<String, String> tags = request.getBody() == null ? defaultBody : request.getBody();
switch(action) {
case GET:
result.setBody(this.getTagsForDataPoint(xid));
break;
case SET:
if (tags == null) {
throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "body"));
}
result.setBody(this.setTagsForDataPoint(xid, tags));
break;
case MERGE:
case UPDATE:
if (tags == null) {
throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "body"));
}
result.setBody(this.mergeTagsForDataPoint(xid, tags));
break;
}
} catch (Exception e) {
result.exceptionCaught(e);
}
return result;
}
use of com.infiniteautomation.mango.rest.latest.exception.BadRequestException in project ma-modules-public by infiniteautomation.
the class UserRestController method bulkUserOperation.
@ApiOperation(value = "Bulk get/create/update/delete users", notes = "User must have read/edit permission for the user")
@RequestMapping(method = RequestMethod.POST, value = "/bulk")
public ResponseEntity<TemporaryResource<UserBulkResponse, AbstractRestException>> bulkUserOperation(@RequestBody UserBulkRequest requestBody, HttpServletRequest servletRequest, Authentication authentication, UriComponentsBuilder builder) {
VoAction defaultAction = requestBody.getAction();
UserModel defaultBody = requestBody.getBody();
List<UserIndividualRequest> requests = requestBody.getRequests();
if (requests == null) {
throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "requests"));
} else if (requests.isEmpty()) {
throw new BadRequestException(new TranslatableMessage("rest.error.cantBeEmpty", "requests"));
}
String resourceId = requestBody.getId();
Long expiration = requestBody.getExpiration();
Long timeout = requestBody.getTimeout();
TemporaryResource<UserBulkResponse, AbstractRestException> responseBody = bulkResourceManager.newTemporaryResource(RESOURCE_TYPE_BULK_USER, resourceId, expiration, timeout, (resource) -> {
UserBulkResponse bulkResponse = new UserBulkResponse();
int i = 0;
resource.progressOrSuccess(bulkResponse, i++, requests.size());
for (UserIndividualRequest request : requests) {
UriComponentsBuilder reqBuilder = UriComponentsBuilder.newInstance();
PermissionHolder resourceUser = Common.getUser();
UserIndividualResponse individualResponse = doIndividualRequest(request, defaultAction, defaultBody, resourceUser, servletRequest, authentication, reqBuilder);
bulkResponse.addResponse(individualResponse);
resource.progressOrSuccess(bulkResponse, i++, requests.size());
}
return null;
});
HttpHeaders headers = new HttpHeaders();
headers.setLocation(builder.path("/users/bulk/{id}").buildAndExpand(responseBody.getId()).toUri());
return new ResponseEntity<>(responseBody, headers, HttpStatus.CREATED);
}
use of com.infiniteautomation.mango.rest.latest.exception.BadRequestException in project ma-modules-public by infiniteautomation.
the class AuditEventInstanceModel method unwrapContext.
private JsonObject unwrapContext(String json) {
try {
JsonTypeReader reader = new JsonTypeReader(json);
JsonValue value = reader.read();
return value.toJsonObject();
} catch (Exception e) {
throw new BadRequestException(e);
}
}
use of com.infiniteautomation.mango.rest.latest.exception.BadRequestException in project ma-modules-public by infiniteautomation.
the class AsciiFileDataSourceRestController method validateASCIIString.
@ApiOperation(value = "Validate ASCII", notes = "")
@RequestMapping(method = RequestMethod.POST, value = "/validate-ascii/{xid}", consumes = { "text/plain;charset=UTF-8" })
public List<AsciiFileTestResultModel> validateASCIIString(@RequestBody String ascii, @ApiParam(value = "Valid ASCII data source XID", required = true, allowMultiple = false) @PathVariable String xid, @AuthenticationPrincipal PermissionHolder user) {
DataSourceVO ds = service.get(xid);
if (!(ds instanceof AsciiFileDataSourceVO))
throw new BadRequestException(new TranslatableMessage("validate.incompatibleDataSourceType"));
// Message we will work with
String msg = StringEscapeUtils.unescapeJava(ascii);
// Map to store the values vs the points they are for
List<AsciiFileTestResultModel> results = new ArrayList<>();
DataPointDao dpd = DataPointDao.getInstance();
List<DataPointVO> points = dpd.getDataPoints(ds.getId());
for (final DataPointVO vo : points) {
MatchCallback callback = new MatchCallback() {
@Override
public void onMatch(String pointIdentifier, PointValueTime value) {
AsciiFileTestResultModel result = new AsciiFileTestResultModel();
results.add(result);
result.setSuccess(true);
result.setPointName(vo.getName());
result.setPointXid(vo.getXid());
result.setIdentifier(pointIdentifier);
if (value != null) {
result.setValue(value.getValue().getObjectValue());
result.setTimestamp(new Date(value.getTime()));
}
}
@Override
public void pointPatternMismatch(String message, String pointValueRegex) {
AsciiFileTestResultModel result = new AsciiFileTestResultModel();
results.add(result);
result.setSuccess(false);
result.setPointName(vo.getName());
result.setPointXid(vo.getXid());
result.setError(new TranslatableMessage("dsEdit.file.test.noPointRegexMatch"));
}
@Override
public void messagePatternMismatch(String message, String messageRegex) {
}
@Override
public void pointNotIdentified(String message, String messageRegex, int pointIdentifierIndex) {
AsciiFileTestResultModel result = new AsciiFileTestResultModel();
results.add(result);
result.setSuccess(false);
result.setPointName(vo.getName());
result.setPointXid(vo.getXid());
result.setError(new TranslatableMessage("dsEdit.file.test.noIdentifierFound"));
}
@Override
public void matchGeneralFailure(Exception e) {
AsciiFileTestResultModel result = new AsciiFileTestResultModel();
results.add(result);
result.setSuccess(false);
result.setPointName(vo.getName());
result.setPointXid(vo.getXid());
result.setError(new TranslatableMessage("common.default", e.getMessage()));
}
};
AsciiFilePointLocatorVO locator = vo.getPointLocator();
AsciiFileDataSourceRT.matchPointValueTime(msg, Pattern.compile(locator.getValueRegex()), locator.getPointIdentifier(), locator.getPointIdentifierIndex(), locator.getDataType(), locator.getValueIndex(), locator.getHasTimestamp(), locator.getTimestampIndex(), locator.getTimestampFormat(), callback);
}
return results;
}
Aggregations