Search in sources :

Example 16 with BadRequestException

use of com.infiniteautomation.mango.rest.latest.exception.BadRequestException in project ma-modules-public by infiniteautomation.

the class EventTypesRestController method getEventTypes.

/**
 */
private List<EventTypeVOModel<?, ?, ?>> getEventTypes(String typeName, PermissionHolder user) throws NotFoundException {
    // track if the type was a default type
    List<EventTypeVOModel<?, ?, ?>> types = new ArrayList<>();
    boolean found = false;
    switch(typeName) {
        case EventTypeNames.DATA_POINT:
        case EventTypeNames.DATA_SOURCE:
        case EventTypeNames.PUBLISHER:
            throw new BadRequestException();
        case EventTypeNames.SYSTEM:
            found = true;
            for (SystemEventTypeDefinition def : ModuleRegistry.getDefinitions(SystemEventTypeDefinition.class)) {
                EventTypeVO type = SystemEventType.getEventType(def.getTypeName());
                if (type.getEventType().hasPermission(user, permissionService)) {
                    SystemEventTypeModel model = modelMapper.map(type.getEventType(), SystemEventTypeModel.class, user);
                    types.add(new EventTypeVOModel<>(model, type.getDescription(), type.getAlarmLevel(), true, def.supportsReferenceId1(), def.supportsReferenceId2()));
                }
            }
            break;
        case EventTypeNames.AUDIT:
            found = true;
            for (EventTypeVO vo : AuditEventType.getRegisteredEventTypes()) {
                AuditEventType aet = (AuditEventType) vo.getEventType();
                if (aet.hasPermission(user, permissionService)) {
                    AuditEventTypeModel aetm = new AuditEventTypeModel(aet);
                    EventTypeVOModel<?, ?, ?> audit = new EventTypeVOModel<>(aetm, vo.getDescription(), vo.getAlarmLevel(), true, false, false);
                    types.add(audit);
                }
            }
            break;
    }
    if (!found) {
        // Module defined
        for (EventTypeDefinition def : ModuleRegistry.getDefinitions(EventTypeDefinition.class)) {
            if (StringUtils.equals(typeName, def.getTypeName())) {
                found = true;
                for (EventTypeVO type : def.generatePossibleEventTypesWithSubtype(user, permissionService)) {
                    EventType eventType = type.getEventType();
                    AbstractEventTypeModel<?, ?, ?> model = modelMapper.map(eventType, AbstractEventTypeModel.class, user);
                    types.add(new EventTypeVOModel<>(model, type.getDescription(), type.getAlarmLevel(), def.supportsSubType(), def.supportsReferenceId1(), def.supportsReferenceId2()));
                }
                break;
            }
        }
    }
    if (!found)
        throw new NotFoundException();
    return types;
}
Also used : AuditEventType(com.serotonin.m2m2.rt.event.type.AuditEventType) SystemEventType(com.serotonin.m2m2.rt.event.type.SystemEventType) DataPointEventType(com.serotonin.m2m2.rt.event.type.DataPointEventType) PublisherEventType(com.serotonin.m2m2.rt.event.type.PublisherEventType) AuditEventType(com.serotonin.m2m2.rt.event.type.AuditEventType) DataSourceEventType(com.serotonin.m2m2.rt.event.type.DataSourceEventType) EventType(com.serotonin.m2m2.rt.event.type.EventType) ArrayList(java.util.ArrayList) SystemEventTypeDefinition(com.serotonin.m2m2.module.SystemEventTypeDefinition) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) EventTypeVO(com.serotonin.m2m2.vo.event.EventTypeVO) SystemEventTypeDefinition(com.serotonin.m2m2.module.SystemEventTypeDefinition) EventTypeDefinition(com.serotonin.m2m2.module.EventTypeDefinition) AuditEventTypeModel(com.infiniteautomation.mango.rest.latest.model.event.AuditEventTypeModel) SystemEventTypeModel(com.infiniteautomation.mango.rest.latest.model.event.SystemEventTypeModel) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) EventTypeVOModel(com.infiniteautomation.mango.rest.latest.model.event.EventTypeVOModel)

Example 17 with BadRequestException

use of com.infiniteautomation.mango.rest.latest.exception.BadRequestException in project ma-modules-public by infiniteautomation.

the class EventTypesRestController method getEventTypesForSubtypeAndReferenceId1.

/**
 * Generate a list of all event types generalized by sub-type and referenceId1
 */
private List<EventTypeVOModel<?, ?, ?>> getEventTypesForSubtypeAndReferenceId1(String typeName, String subtype, Integer referenceId1, PermissionHolder user) throws NotFoundException {
    // track if the type was a default type
    List<EventTypeVOModel<?, ?, ?>> types = new ArrayList<>();
    boolean found = false;
    switch(typeName) {
        case EventTypeNames.DATA_POINT:
            // There is no subtype for data points
            if (subtype != null)
                throw new BadRequestException();
            DataPointVO dp = this.dataPointService.get(referenceId1);
            for (AbstractPointEventDetectorVO vo : eventDetectorDao.getWithSource(dp.getId(), dp)) {
                AbstractPointEventDetectorModel<?> edm = modelMapper.map(vo, AbstractPointEventDetectorModel.class, user);
                EventTypeVO type = vo.getEventType();
                DataPointEventType eventType = (DataPointEventType) type.getEventType();
                DataPointEventTypeModel model = new DataPointEventTypeModel(eventType, modelMapper.map(dp, DataPointModel.class, user), edm);
                types.add(new EventTypeVOModel<DataPointEventType, DataPointModel, AbstractPointEventDetectorModel<?>>(model, type.getDescription(), type.getAlarmLevel(), false, true, true));
            }
            found = true;
            break;
        case EventTypeNames.DATA_SOURCE:
            // There is no subtype for data sources
            if (subtype != null)
                throw new BadRequestException();
            DataSourceVO ds = dataSourceDao.get(referenceId1);
            if (ds == null)
                throw new NotFoundException();
            permissionService.ensurePermission(user, ds.getReadPermission());
            AbstractDataSourceModel<?> dsModel = modelMapper.map(ds, AbstractDataSourceModel.class, user);
            for (EventTypeVO type : ds.getEventTypes()) {
                DataSourceEventType eventType = (DataSourceEventType) type.getEventType();
                DataSourceEventTypeModel model = new DataSourceEventTypeModel(eventType, dsModel);
                types.add(new EventTypeVOModel<DataSourceEventType, AbstractDataSourceModel<?>, String>(model, type.getDescription(), type.getAlarmLevel(), false, true, true));
            }
            found = true;
            break;
        case EventTypeNames.PUBLISHER:
            // There is no subtype for publishers
            if (subtype != null)
                throw new BadRequestException();
            // There are no permissions for publishers
            if (!permissionService.hasAdminRole(user))
                throw new PermissionException(new TranslatableMessage("permission.exception.doesNotHaveRequiredPermission", user), user);
            PublisherVO pub = publisherDao.get(referenceId1);
            if (pub == null)
                throw new NotFoundException();
            for (EventTypeVO type : pub.getEventTypes()) {
                PublisherEventType eventType = (PublisherEventType) type.getEventType();
                AbstractPublisherModel<?, ?> publisherModel = modelMapper.map(pub, AbstractPublisherModel.class, user);
                PublisherEventTypeModel model = new PublisherEventTypeModel(eventType, publisherModel);
                types.add(new EventTypeVOModel<PublisherEventType, AbstractPublisherModel<?, ?>, String>(model, type.getDescription(), type.getAlarmLevel(), false, true, true));
            }
            found = true;
            break;
        case EventTypeNames.SYSTEM:
            // System
            for (SystemEventTypeDefinition def : ModuleRegistry.getDefinitions(SystemEventTypeDefinition.class)) {
                if (!StringUtils.equals(def.getTypeName(), subtype))
                    continue;
                found = true;
                for (EventTypeVO type : def.generatePossibleEventTypesWithReferenceId2(user, subtype, referenceId1)) {
                    SystemEventType eventType = (SystemEventType) type.getEventType();
                    SystemEventTypeModel model = modelMapper.map(eventType, SystemEventTypeModel.class, user);
                    types.add(new EventTypeVOModel<>(model, type.getDescription(), type.getAlarmLevel(), true, def.supportsReferenceId1(), def.supportsReferenceId2()));
                }
                break;
            }
            break;
        case EventTypeNames.AUDIT:
            // Audit does not yet support reference id 2
            throw new BadRequestException();
    }
    if (!found) {
        // Module defined
        for (EventTypeDefinition def : ModuleRegistry.getDefinitions(EventTypeDefinition.class)) {
            if (StringUtils.equals(typeName, def.getTypeName())) {
                found = true;
                for (EventTypeVO type : def.generatePossibleEventTypesWithReferenceId2(user, subtype, referenceId1, permissionService)) {
                    EventType eventType = type.getEventType();
                    if (!StringUtils.equals(eventType.getEventSubtype(), subtype))
                        continue;
                    if (!eventType.hasPermission(user, permissionService))
                        continue;
                    AbstractEventTypeModel<?, ?, ?> model = modelMapper.map(eventType, AbstractEventTypeModel.class, user);
                    types.add(new EventTypeVOModel<>(model, type.getDescription(), type.getAlarmLevel(), def.supportsSubType(), def.supportsReferenceId1(), def.supportsReferenceId2()));
                }
                break;
            }
        }
    }
    if (!found)
        throw new NotFoundException();
    return types;
}
Also used : AbstractPointEventDetectorModel(com.infiniteautomation.mango.rest.latest.model.event.detectors.AbstractPointEventDetectorModel) DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) AbstractDataSourceModel(com.infiniteautomation.mango.rest.latest.model.datasource.AbstractDataSourceModel) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) SystemEventType(com.serotonin.m2m2.rt.event.type.SystemEventType) SystemEventType(com.serotonin.m2m2.rt.event.type.SystemEventType) DataPointEventType(com.serotonin.m2m2.rt.event.type.DataPointEventType) PublisherEventType(com.serotonin.m2m2.rt.event.type.PublisherEventType) AuditEventType(com.serotonin.m2m2.rt.event.type.AuditEventType) DataSourceEventType(com.serotonin.m2m2.rt.event.type.DataSourceEventType) EventType(com.serotonin.m2m2.rt.event.type.EventType) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) PublisherEventTypeModel(com.infiniteautomation.mango.rest.latest.model.event.PublisherEventTypeModel) ArrayList(java.util.ArrayList) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) AbstractPublisherModel(com.infiniteautomation.mango.rest.latest.model.publisher.AbstractPublisherModel) SystemEventTypeDefinition(com.serotonin.m2m2.module.SystemEventTypeDefinition) EventTypeVO(com.serotonin.m2m2.vo.event.EventTypeVO) SystemEventTypeDefinition(com.serotonin.m2m2.module.SystemEventTypeDefinition) EventTypeDefinition(com.serotonin.m2m2.module.EventTypeDefinition) PublisherEventType(com.serotonin.m2m2.rt.event.type.PublisherEventType) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) PublisherVO(com.serotonin.m2m2.vo.publish.PublisherVO) DataPointEventType(com.serotonin.m2m2.rt.event.type.DataPointEventType) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataSourceEventType(com.serotonin.m2m2.rt.event.type.DataSourceEventType) DataPointModel(com.infiniteautomation.mango.rest.latest.model.dataPoint.DataPointModel) DataSourceEventTypeModel(com.infiniteautomation.mango.rest.latest.model.event.DataSourceEventTypeModel) DataPointEventTypeModel(com.infiniteautomation.mango.rest.latest.model.event.DataPointEventTypeModel) SystemEventTypeModel(com.infiniteautomation.mango.rest.latest.model.event.SystemEventTypeModel) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) EventTypeVOModel(com.infiniteautomation.mango.rest.latest.model.event.EventTypeVOModel)

Example 18 with BadRequestException

use of com.infiniteautomation.mango.rest.latest.exception.BadRequestException in project ma-modules-public by infiniteautomation.

the class JsonDataRestController method replaceNode.

JsonNode replaceNode(final JsonNode existingData, final String[] dataPath, final JsonNode newData) {
    if (dataPath.length == 0) {
        return newData;
    }
    String[] parentPath = Arrays.copyOfRange(dataPath, 0, dataPath.length - 1);
    String fieldName = dataPath[dataPath.length - 1];
    JsonNode parent = getNode(existingData, parentPath);
    if (parent.isObject()) {
        ObjectNode parentObject = (ObjectNode) parent;
        parentObject.set(fieldName, newData);
    } else if (parent.isArray()) {
        ArrayNode parentArray = (ArrayNode) parent;
        int index = toArrayIndex(fieldName);
        parentArray.set(index, newData);
    } else {
        throw new BadRequestException(new TranslatableMessage("rest.error.cantSetFieldOfNodeType", parent.getNodeType()));
    }
    return existingData;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 19 with BadRequestException

use of com.infiniteautomation.mango.rest.latest.exception.BadRequestException in project ma-modules-public by infiniteautomation.

the class JsonDataRestController method deleteNode.

boolean deleteNode(final JsonNode existingData, final String[] dataPath) {
    if (dataPath.length == 0)
        throw new IllegalArgumentException();
    String[] parentPath = Arrays.copyOfRange(dataPath, 0, dataPath.length - 1);
    String fieldName = dataPath[dataPath.length - 1];
    JsonNode parent = getNode(existingData, parentPath);
    JsonNode deletedValue = null;
    if (parent.isObject()) {
        ObjectNode parentObject = (ObjectNode) parent;
        deletedValue = parentObject.remove(fieldName);
    } else if (parent.isArray()) {
        ArrayNode parentArray = (ArrayNode) parent;
        int index = toArrayIndex(fieldName);
        deletedValue = parentArray.remove(index);
    } else {
        throw new BadRequestException(new TranslatableMessage("rest.error.cantDeleteFieldOfNodeType", parent.getNodeType()));
    }
    return deletedValue != null;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 20 with BadRequestException

use of com.infiniteautomation.mango.rest.latest.exception.BadRequestException in project ma-modules-public by infiniteautomation.

the class JsonEmportController method uploadConfigurationFile.

@PreAuthorize("isAdmin()")
@ApiOperation(value = "Upload 1 configuration json file", notes = "Files should only contain the json object to be imported")
@RequestMapping(method = RequestMethod.POST, value = "/upload-file", consumes = { "multipart/form-data", "multipart/form-data;boundary=-----SWAG_BOUND" })
public ResponseEntity<ImportStatusProvider> uploadConfigurationFile(@RequestPart(required = true) MultipartFile file, UriComponentsBuilder builder, HttpServletRequest request, @ApiParam(value = "timeout for Status Resource to Expire, defaults to 5 minutes", required = false, allowMultiple = false) @RequestParam(value = "timeout", required = false) Long timeout, @AuthenticationPrincipal PermissionHolder user) throws IOException, JsonException {
    if (!file.isEmpty()) {
        JsonReader jr = new JsonReader(Common.JSON_CONTEXT, new String(file.getBytes()));
        JsonObject jo = jr.read(JsonObject.class);
        String resourceId = importStatusResources.generateResourceId();
        ImportStatusProvider statusProvider = new ImportStatusProvider(importStatusResources, resourceId, websocket, timeout, user, jo);
        // Setup the Temporary Resource
        this.importStatusResources.put(resourceId, statusProvider);
        URI location = builder.path("/json-emport/import/{id}").buildAndExpand(resourceId).toUri();
        return getResourceCreated(statusProvider, location);
    } else {
        throw new BadRequestException(new TranslatableMessage("rest.error.noFileProvided"));
    }
}
Also used : JsonReader(com.serotonin.json.JsonReader) JsonObject(com.serotonin.json.type.JsonObject) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) URI(java.net.URI) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) 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