Search in sources :

Example 1 with MessageUriPojo

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);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) MessageUriPojo(won.owner.pojo.MessageUriPojo) User(won.owner.model.User) Authentication(org.springframework.security.core.Authentication) WonMessage(won.protocol.message.WonMessage) URI(java.net.URI) WonMessageSenderException(won.protocol.message.sender.exception.WonMessageSenderException) WonMessageProcessingException(won.protocol.exception.WonMessageProcessingException) Transactional(javax.transaction.Transactional) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

URI (java.net.URI)1 Transactional (javax.transaction.Transactional)1 ResponseEntity (org.springframework.http.ResponseEntity)1 Authentication (org.springframework.security.core.Authentication)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 User (won.owner.model.User)1 MessageUriPojo (won.owner.pojo.MessageUriPojo)1 WonMessageProcessingException (won.protocol.exception.WonMessageProcessingException)1 WonMessage (won.protocol.message.WonMessage)1 WonMessageSenderException (won.protocol.message.sender.exception.WonMessageSenderException)1