use of won.owner.pojo.MessageUriPojo in project webofneeds by researchstudio-sat.
the class RestMessageController method send.
/**
* Prepares and sends the WonMessage received in the body of this post request.
* The message has to be encoded in JSON-LD and use the reserved self message
* URI.
*
* @param message
* @return
*/
@Transactional
@RequestMapping(value = "/send", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST, consumes = "application/ld+json")
public ResponseEntity send(@RequestBody String serializedWonMessage) {
String username = SecurityContextHolder.getContext().getAuthentication().getName();
if (username == null) {
return generateStatusResponse(RestStatusResponse.USER_NOT_SIGNED_IN, Optional.empty());
}
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
WonMessage msg = null;
// decode the message
try {
msg = WonMessageDecoder.decode(Lang.JSONLD, serializedWonMessage);
} catch (Exception e) {
logger.debug("Error parsing message, returning error response", e);
return generateStatusResponse(RestStatusResponse.ERROR_PARSING_MESSAGE, Optional.of(getErrorMessage(e)));
}
// prepare it
try {
AuthenticationThreadLocal.setAuthentication(authentication);
msg = ownerApplicationService.prepareMessage(msg);
} catch (Exception e) {
logger.debug("Error preparing message, returning error response", e);
return generateStatusResponse(RestStatusResponse.ERROR_PREPARING_MESSAGE, Optional.of(getErrorMessage(e)));
} finally {
// be sure to remove the principal from the threadlocal
AuthenticationThreadLocal.remove();
}
// associate all the user's websocket sessions with
// the sender atom so that we can route the response properly
User user = getUser(authentication, msg);
URI atomURI = msg.getSenderAtomURI();
if (user != null && atomURI != null) {
webSocketSessionService.getWebSocketSessions(user).forEach(session -> webSocketSessionService.addMapping(atomURI, session));
this.userAtomService.updateUserAtomAssociation(msg, user);
}
// send it in a separate thread (so we can return our result immediately)
try {
final WonMessage preparedMessage = msg;
if (msg.getMessageType() == WonMessageType.DELETE) {
try {
userAtomService.setAtomDeleted(atomURI);
} catch (Exception e) {
logger.debug("Could not delete atom with uri {} because of {}", atomURI, e);
}
}
executor.submit(() -> {
ownerApplicationService.sendMessage(preparedMessage);
});
} catch (Exception e) {
logger.debug("Error sending message, returning error response", e);
return generateStatusResponse(RestStatusResponse.ERROR_SENDING_MESSAGE_TO_NODE, Optional.of(getErrorMessage(e)));
}
return new ResponseEntity(new MessageUriPojo(msg.getMessageURIRequired().toString()), HttpStatus.OK);
}
Aggregations