Search in sources :

Example 21 with BadRequestException

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;
}
Also used : VoAction(com.infiniteautomation.mango.rest.latest.bulk.VoAction) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) AbstractRestException(com.infiniteautomation.mango.rest.latest.exception.AbstractRestException)

Example 22 with BadRequestException

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);
    }
}
Also used : JsonException(com.serotonin.json.JsonException) UnknownHostException(java.net.UnknownHostException) ValidationFailedRestException(com.infiniteautomation.mango.rest.latest.exception.ValidationFailedRestException) JsonValue(com.serotonin.json.type.JsonValue) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) JsonObject(com.serotonin.json.type.JsonObject) JsonString(com.serotonin.json.type.JsonString) IOException(java.io.IOException) ModuleModel(com.infiniteautomation.mango.rest.latest.model.modules.ModuleModel) CoreModuleModel(com.infiniteautomation.mango.rest.latest.model.modules.CoreModuleModel) JsonArray(com.serotonin.json.type.JsonArray) ModuleUpgradeModel(com.infiniteautomation.mango.rest.latest.model.modules.ModuleUpgradeModel) SocketTimeoutException(java.net.SocketTimeoutException) ModuleUpgradesModel(com.infiniteautomation.mango.rest.latest.model.modules.ModuleUpgradesModel) Iterator(java.util.Iterator) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) ArrayList(java.util.ArrayList) List(java.util.List) HttpException(org.apache.http.HttpException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) JsonString(com.serotonin.json.type.JsonString) ServerErrorException(com.infiniteautomation.mango.rest.latest.exception.ServerErrorException) Module(com.serotonin.m2m2.module.Module) CoreModule(com.serotonin.m2m2.module.ModuleRegistry.CoreModule) InvalidModule(com.infiniteautomation.mango.rest.latest.model.modules.UpgradeUploadResult.InvalidModule) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 23 with BadRequestException

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);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) VoAction(com.infiniteautomation.mango.rest.latest.bulk.VoAction) DataPointModel(com.infiniteautomation.mango.rest.latest.model.dataPoint.DataPointModel) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) ResponseEntity(org.springframework.http.ResponseEntity) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) AbstractRestException(com.infiniteautomation.mango.rest.latest.exception.AbstractRestException) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 24 with BadRequestException

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);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) AbstractRestException(com.infiniteautomation.mango.rest.latest.exception.AbstractRestException) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 25 with BadRequestException

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;
}
Also used : BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

BadRequestException (com.infiniteautomation.mango.rest.latest.exception.BadRequestException)29 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)26 ApiOperation (io.swagger.annotations.ApiOperation)15 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)15 AbstractRestException (com.infiniteautomation.mango.rest.latest.exception.AbstractRestException)10 VoAction (com.infiniteautomation.mango.rest.latest.bulk.VoAction)8 ArrayList (java.util.ArrayList)7 ResponseEntity (org.springframework.http.ResponseEntity)7 HttpHeaders (org.springframework.http.HttpHeaders)6 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)5 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)5 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 DataPointModel (com.infiniteautomation.mango.rest.latest.model.dataPoint.DataPointModel)4 NotFoundException (com.infiniteautomation.mango.util.exception.NotFoundException)4 DataSourceVO (com.serotonin.m2m2.vo.dataSource.DataSourceVO)4 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)4 NotFoundRestException (com.infiniteautomation.mango.rest.latest.exception.NotFoundRestException)3