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();
}
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();
}
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);
}
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);
}
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);
}
}
Aggregations