Search in sources :

Example 66 with RestProcessResult

use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult in project ma-modules-public by infiniteautomation.

the class EventsRestController method acknowledgeEvent.

/**
 * Update an event
 * @param vo
 * @param xid
 * @param builder
 * @param request
 * @return
 */
@ApiOperation(value = "Acknowledge an existing event", notes = "")
@RequestMapping(method = RequestMethod.PUT, consumes = { "application/json" }, produces = { "application/json" }, value = "/acknowledge/{id}")
public ResponseEntity<EventInstanceModel> acknowledgeEvent(@PathVariable Integer id, @RequestBody(required = false) TranslatableMessageModel message, UriComponentsBuilder builder, HttpServletRequest request) {
    RestProcessResult<EventInstanceModel> result = new RestProcessResult<EventInstanceModel>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        TranslatableMessage tlm = null;
        if (message != null)
            tlm = new TranslatableMessage(message.getKey(), message.getArgs().toArray());
        EventInstance event = EventDao.instance.get(id);
        if (event == null) {
            result.addRestMessage(getDoesNotExistMessage());
            return result.createResponseEntity();
        } else if (!Permissions.hasEventTypePermission(user, event.getEventType())) {
            result.addRestMessage(getUnauthorizedMessage());
            return result.createResponseEntity();
        }
        Common.eventManager.acknowledgeEventById(id, System.currentTimeMillis(), user, tlm);
        // if event has a different ack timestamp, user or message it was already acked, we could return a different message
        EventInstanceModel model = new EventInstanceModel(event);
        // Put a link to the updated data in the header?
        URI location = builder.path("/v1/events/{id}").buildAndExpand(id).toUri();
        result.addRestMessage(getResourceUpdatedMessage(location));
        return result.createResponseEntity(model);
    }
    // Not logged in
    return result.createResponseEntity();
}
Also used : EventInstanceModel(com.serotonin.m2m2.web.mvc.rest.v1.model.events.EventInstanceModel) EventInstance(com.serotonin.m2m2.rt.event.EventInstance) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) URI(java.net.URI) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 67 with RestProcessResult

use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult in project ma-modules-public by infiniteautomation.

the class HelpRestController method getHelp.

@ApiOperation(value = "Get Help", notes = "request help via the internal Mango help id", response = HelpModel.class, responseContainer = "Array")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" }, value = "/by-id/{helpId}")
public ResponseEntity<HelpModel> getHelp(@PathVariable String helpId, HttpServletRequest request) {
    RestProcessResult<HelpModel> result = new RestProcessResult<HelpModel>(HttpStatus.OK);
    this.checkUser(request, result);
    if (result.isOk()) {
        HelpModel model = new HelpModel();
        DocumentationItem item = Common.documentationManifest.getItem(helpId);
        if (item == null) {
            result.addRestMessage(this.getDoesNotExistMessage());
        } else {
            model.setId(item.getId());
            model.setTitle(new TranslatableMessage(item.getKey()).translate(Common.getTranslations()));
            // Find the file appropriate for the locale.
            Locale locale = ControllerUtils.getLocale(request);
            File file = Common.documentationManifest.getDocumentationFile(item, locale.getLanguage(), locale.getCountry(), locale.getVariant());
            // Read the content.
            try {
                Reader in = new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8"));
                StringWriter out = new StringWriter();
                StreamUtils.transfer(in, out);
                in.close();
                model.setContent(out.toString());
                List<RelatedHelpItemModel> relatedItems = new ArrayList<RelatedHelpItemModel>();
                for (String relatedId : item.getRelated()) {
                    DocumentationItem relatedItem = Common.documentationManifest.getItem(relatedId);
                    if (relatedItem == null)
                        throw new RuntimeException("Related document '" + relatedId + "' not found");
                    relatedItems.add(new RelatedHelpItemModel(relatedItem.getId(), new TranslatableMessage(relatedItem.getKey()).translate(Common.getTranslations())));
                }
                model.setRelatedList(relatedItems);
                return result.createResponseEntity(model);
            } catch (FileNotFoundException e) {
                result.addRestMessage(HttpStatus.NOT_FOUND, new TranslatableMessage("dox.fileNotFound", file.getPath()));
            } catch (IOException e) {
                result.addRestMessage(HttpStatus.NOT_FOUND, new TranslatableMessage("dox.readError", e.getClass().getName(), e.getMessage()));
            }
        }
    }
    return result.createResponseEntity();
}
Also used : Locale(java.util.Locale) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) RelatedHelpItemModel(com.serotonin.m2m2.web.mvc.rest.v1.model.help.RelatedHelpItemModel) IOException(java.io.IOException) DocumentationItem(com.serotonin.m2m2.util.DocumentationItem) HelpModel(com.serotonin.m2m2.web.mvc.rest.v1.model.help.HelpModel) FileInputStream(java.io.FileInputStream) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) StringWriter(java.io.StringWriter) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) File(java.io.File) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 68 with RestProcessResult

use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult in project ma-modules-public by infiniteautomation.

the class SessionExceptionRestV2Controller method clearLatest.

@ApiOperation(value = "Clear Last Exception for your session", notes = "")
@ApiResponses({ @ApiResponse(code = 401, message = "Unauthorized user access", response = ResponseEntity.class), @ApiResponse(code = 404, message = "No Exception exists", response = ResponseEntity.class), @ApiResponse(code = 500, message = "Error processing request", response = ResponseEntity.class) })
@RequestMapping(method = { RequestMethod.PUT }, value = { "/latest" }, produces = { "application/json" })
public ResponseEntity<Map<String, Exception>> clearLatest(HttpServletRequest request) {
    RestProcessResult<Map<String, Exception>> result = new RestProcessResult<>(HttpStatus.OK);
    // Get latest Session Exception
    HttpSession session = request.getSession(false);
    if (session == null)
        throw new ServerErrorException(new TranslatableMessage("rest.error.noSession"));
    Map<String, Exception> exceptionMap = new HashMap<String, Exception>();
    for (String key : exceptionKeys) {
        exceptionMap.put(key, (Exception) session.getAttribute(key));
        session.removeAttribute(key);
    }
    return result.createResponseEntity(exceptionMap);
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) HashMap(java.util.HashMap) HttpSession(javax.servlet.http.HttpSession) ServerErrorException(com.infiniteautomation.mango.rest.v2.exception.ServerErrorException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) HashMap(java.util.HashMap) Map(java.util.Map) ServerErrorException(com.infiniteautomation.mango.rest.v2.exception.ServerErrorException) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) ApiResponses(com.wordnik.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 69 with RestProcessResult

use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult in project ma-modules-public by infiniteautomation.

the class SessionExceptionRestV2Controller method getLatest.

@ApiOperation(value = "Get Last Exception for your session", notes = "")
@ApiResponses({ @ApiResponse(code = 401, message = "Unauthorized user access", response = ResponseEntity.class), @ApiResponse(code = 404, message = "No Exception exists", response = ResponseEntity.class), @ApiResponse(code = 500, message = "Error processing request", response = ResponseEntity.class) })
@RequestMapping(method = { RequestMethod.GET }, value = { "/latest" }, produces = { "application/json" })
public ResponseEntity<Map<String, Exception>> getLatest(HttpServletRequest request) {
    RestProcessResult<Map<String, Exception>> result = new RestProcessResult<>(HttpStatus.OK);
    // Get latest Session Exception
    HttpSession session = request.getSession(false);
    if (session == null)
        throw new ServerErrorException(new TranslatableMessage("rest.error.noSession"));
    Map<String, Exception> exceptionMap = new HashMap<String, Exception>();
    for (String key : exceptionKeys) {
        exceptionMap.put(key, (Exception) session.getAttribute(key));
    }
    return result.createResponseEntity(exceptionMap);
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) HashMap(java.util.HashMap) HttpSession(javax.servlet.http.HttpSession) ServerErrorException(com.infiniteautomation.mango.rest.v2.exception.ServerErrorException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) HashMap(java.util.HashMap) Map(java.util.Map) ServerErrorException(com.infiniteautomation.mango.rest.v2.exception.ServerErrorException) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) ApiResponses(com.wordnik.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 70 with RestProcessResult

use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult in project ma-core-public by infiniteautomation.

the class AbstractPublisherModel method validate.

/*
	 * (non-Javadoc)
	 * @see com.serotonin.m2m2.web.mvc.rest.v1.model.AbstractRestModel#validate(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)
	 */
public void validate(RestProcessResult<?> result) throws RestValidationFailedException {
    ProcessResult validation = new ProcessResult();
    this.data.validate(validation);
    if (validation.getHasMessages()) {
        result.addValidationMessages(validation);
        throw new RestValidationFailedException(this, result);
    }
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) RestValidationFailedException(com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException)

Aggregations

RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)132 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)125 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)125 User (com.serotonin.m2m2.vo.User)113 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)33 ArrayList (java.util.ArrayList)30 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)29 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)28 List (java.util.List)27 InvalidRQLRestException (com.infiniteautomation.mango.rest.v2.exception.InvalidRQLRestException)23 ASTNode (net.jazdw.rql.parser.ASTNode)23 RestValidationFailedException (com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException)22 URI (java.net.URI)18 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)15 HashMap (java.util.HashMap)14 ApiResponses (com.wordnik.swagger.annotations.ApiResponses)13 ValidationFailedRestException (com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException)11 RTException (com.serotonin.m2m2.rt.RTException)11 DataPointModel (com.serotonin.m2m2.web.mvc.rest.v1.model.DataPointModel)11 QueryDataPageStream (com.serotonin.m2m2.web.mvc.rest.v1.model.QueryDataPageStream)11