use of com.infiniteautomation.mango.rest.latest.exception.BadRequestException in project ma-modules-public by infiniteautomation.
the class PublishedPointsRestController method doIndividualRequest.
private PublishedPointIndividualResponse doIndividualRequest(PublishedPointIndividualRequest request, VoAction defaultAction, AbstractPublishedPointModel<?> defaultBody, PermissionHolder user, UriComponentsBuilder builder) {
PublishedPointIndividualResponse result = new PublishedPointIndividualResponse();
try {
String xid = request.getXid();
result.setXid(xid);
VoAction action = request.getAction() == null ? defaultAction : request.getAction();
if (action == null) {
throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "action"));
}
result.setAction(action);
AbstractPublishedPointModel<?> body = request.getBody() == null ? defaultBody : request.getBody();
switch(action) {
case GET:
if (xid == null) {
throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "xid"));
}
result.setBody(this.get(xid, user));
break;
case CREATE:
if (body == null) {
throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "body"));
}
result.setBody(body);
result.setBody(this.create(body, user, builder).getBody());
break;
case UPDATE:
if (xid == null) {
throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "xid"));
}
if (body == null) {
throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "body"));
}
result.setBody(body);
result.setBody(this.update(xid, body, user, builder).getBody());
break;
case DELETE:
if (xid == null) {
throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "xid"));
}
result.setBody(this.delete(xid, user, builder));
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 ModulesRestController method getUpgrades.
@ApiOperation(value = "Get Available Upgrades", notes = "Check the store for Upgrades")
@RequestMapping(method = RequestMethod.GET, value = "/upgrades-available")
public ModuleUpgradesModel getUpgrades(@AuthenticationPrincipal PermissionHolder user) {
permissionService.ensureAdminRole(user);
// Do the check
try {
JsonValue jsonResponse = null;
// This is handled oddly in the ModulesDwr so check it here for validation
String baseUrl = Common.envProps.getString("store.url");
if (!StringUtils.isEmpty(baseUrl)) {
jsonResponse = service.getAvailableUpgrades();
} else {
ProcessResult result = new ProcessResult();
result.addGenericMessage("modules.versionCheck.storeNotSet");
throw new ValidationFailedRestException(result);
}
if (jsonResponse == null) {
// Indicates that the store url is not set, which we check for above so this really means the response was a null JSON value
throw new BadRequestException(new TranslatableMessage("modules.versionCheck.storeResponseEmpty"));
} else if (jsonResponse instanceof JsonString) {
throw new ServerErrorException(new TranslatableMessage("common.default", jsonResponse.toString()));
} else {
List<ModuleUpgradeModel> upgrades = new ArrayList<>();
List<ModuleUpgradeModel> newInstalls = new ArrayList<>();
List<ModuleModel> unavailableModules = new ArrayList<>();
ModuleUpgradesModel model = new ModuleUpgradesModel(upgrades, newInstalls);
JsonObject root = jsonResponse.toJsonObject();
JsonValue jsonUpgrades = root.get("upgrades");
JsonArray jsonUpgradesArray = jsonUpgrades.toJsonArray();
Iterator<JsonValue> it = jsonUpgradesArray.iterator();
while (it.hasNext()) {
JsonValue v = it.next();
if (v.getJsonValue("name") == null) {
it.remove();
continue;
}
String name = v.getJsonValue("name").toString();
Module module = ModuleRegistry.getModule(name);
if (module == null) {
it.remove();
continue;
}
upgrades.add(new ModuleUpgradeModel(module, v));
}
JsonValue jsonInstalls = root.get("newInstalls");
JsonArray jsonInstallsArray = jsonInstalls.toJsonArray();
for (JsonValue v : jsonInstallsArray) {
newInstalls.add(new ModuleUpgradeModel(v));
}
// Extract any unavailable modules
if (root.containsKey("unavailableModules")) {
JsonValue jsonUnavailableModules = root.get("unavailableModules");
JsonArray jsonUnavailableModulesArray = jsonUnavailableModules.toJsonArray();
List<Module> modules = ModuleRegistry.getModules();
for (JsonValue v : jsonUnavailableModulesArray) {
Module unavailable = null;
String moduleName = v.toString();
for (Module module : modules) {
if (StringUtils.equals(module.getName(), moduleName)) {
unavailable = module;
break;
}
}
if (unavailable == null) {
// Didn't find it? Store must be wrong?
LOG.warn("Store reported unavailable module " + moduleName + " but it isn't installed.");
} else {
unavailableModules.add(new ModuleModel(unavailable));
}
}
}
model.setUnavailableModules(unavailableModules);
return model;
}
} catch (SocketTimeoutException e) {
throw new ServerErrorException(new TranslatableMessage("rest.error.requestTimeout", Common.envProps.getString("store.url")));
} catch (UnknownHostException e) {
throw new ServerErrorException(new TranslatableMessage("rest.error.unknownHost", Common.envProps.getString("store.url")));
} catch (IOException | JsonException | HttpException e) {
throw new ServerErrorException(e);
}
}
use of com.infiniteautomation.mango.rest.latest.exception.BadRequestException in project ma-modules-public by infiniteautomation.
the class DataPointRestController method bulkDataPointOperation.
@ApiOperation(value = "Bulk get/create/update/delete data points", notes = "User must have read/edit permission for the data point")
@RequestMapping(method = RequestMethod.POST, value = "/bulk")
public ResponseEntity<TemporaryResource<DataPointBulkResponse, AbstractRestException>> bulkDataPointOperation(@RequestBody DataPointBulkRequest requestBody, UriComponentsBuilder builder) {
VoAction defaultAction = requestBody.getAction();
DataPointModel defaultBody = requestBody.getBody();
List<DataPointIndividualRequest> 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<DataPointBulkResponse, AbstractRestException> responseBody = bulkResourceManager.newTemporaryResource(RESOURCE_TYPE_BULK_DATA_POINT, resourceId, expiration, timeout, (resource) -> {
DataPointBulkResponse bulkResponse = new DataPointBulkResponse();
int i = 0;
resource.progressOrSuccess(bulkResponse, i++, requests.size());
PermissionHolder resourceUser = Common.getUser();
for (DataPointIndividualRequest request : requests) {
UriComponentsBuilder reqBuilder = UriComponentsBuilder.newInstance();
DataPointIndividualResponse individualResponse = doIndividualRequest(request, defaultAction, defaultBody, resourceUser, reqBuilder);
bulkResponse.addResponse(individualResponse);
resource.progressOrSuccess(bulkResponse, i++, requests.size());
}
return null;
});
HttpHeaders headers = new HttpHeaders();
headers.setLocation(builder.path("/data-points/bulk/{id}").buildAndExpand(responseBody.getId()).toUri());
return new ResponseEntity<TemporaryResource<DataPointBulkResponse, AbstractRestException>>(responseBody, headers, HttpStatus.CREATED);
}
use of com.infiniteautomation.mango.rest.latest.exception.BadRequestException in project ma-modules-public by infiniteautomation.
the class DataPointTagsRestController method bulkDataPointTagOperation.
@ApiOperation(value = "Bulk get/set/add data point tags for a list of XIDs", notes = "User must have read/edit permission for the data point")
@RequestMapping(method = RequestMethod.POST, value = "/bulk")
public ResponseEntity<TemporaryResource<TagBulkResponse, AbstractRestException>> bulkDataPointTagOperation(@RequestBody TagBulkRequest requestBody, UriComponentsBuilder builder) {
BulkTagAction defaultAction = requestBody.getAction();
Map<String, String> defaultBody = requestBody.getBody();
List<TagIndividualRequest> requests = requestBody.getRequests();
if (requests == null) {
throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "requests"));
}
String resourceId = requestBody.getId();
Long expiration = requestBody.getExpiration();
Long timeout = requestBody.getTimeout();
TemporaryResource<TagBulkResponse, AbstractRestException> responseBody = bulkResourceManager.newTemporaryResource(RESOURCE_TYPE_BULK_DATA_POINT_TAGS, resourceId, expiration, timeout, (resource) -> {
TagBulkResponse bulkResponse = new TagBulkResponse();
int i = 0;
resource.progress(bulkResponse, i++, requests.size());
for (TagIndividualRequest request : requests) {
TagIndividualResponse individualResponse = doIndividualRequest(request, defaultAction, defaultBody);
bulkResponse.addResponse(individualResponse);
resource.progressOrSuccess(bulkResponse, i++, requests.size());
}
return null;
});
HttpHeaders headers = new HttpHeaders();
headers.setLocation(builder.path("/data-point-tags/bulk/{id}").buildAndExpand(responseBody.getId()).toUri());
return new ResponseEntity<TemporaryResource<TagBulkResponse, AbstractRestException>>(responseBody, headers, HttpStatus.CREATED);
}
use of com.infiniteautomation.mango.rest.latest.exception.BadRequestException in project ma-modules-public by infiniteautomation.
the class DataPointTagsRestController method bulkDataPointTagOperationSync.
@ApiOperation(value = "Synchronously bulk get/set/add data point tags for a list of XIDs", notes = "User must have read/edit permission for the data point")
@RequestMapping(method = RequestMethod.POST, value = "/bulk-sync")
public TagBulkResponse bulkDataPointTagOperationSync(@RequestBody TagBulkRequest requestBody) {
BulkTagAction defaultAction = requestBody.getAction();
Map<String, String> defaultBody = requestBody.getBody();
List<TagIndividualRequest> requests = requestBody.getRequests();
if (requests == null) {
throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "requests"));
}
TagBulkResponse response = new TagBulkResponse();
for (TagIndividualRequest request : requests) {
TagIndividualResponse individualResponse = doIndividualRequest(request, defaultAction, defaultBody);
response.addResponse(individualResponse);
}
return response;
}
Aggregations