Search in sources :

Example 16 with WonMessageType

use of won.protocol.message.WonMessageType in project webofneeds by researchstudio-sat.

the class ConnectionStateChangeBuilderCamelProcessor method process.

@Override
public void process(Exchange exchange) throws Exception {
    ConnectionStateChangeBuilder stateChangeBuilder = new ConnectionStateChangeBuilder();
    WonMessage wonMessage = getMessageRequired(exchange);
    WonMessageType type = wonMessage.getMessageTypeRequired();
    if (type.isResponseMessage()) {
        type = wonMessage.getRespondingToMessageType();
    }
    if (type.isConnectionSpecificMessage()) {
        Optional<Connection> con = getConnection(exchange, connectionService);
        if (con.isPresent()) {
            stateChangeBuilder.oldState(con.get().getState());
        }
    }
    // put the state change builder in the header
    exchange.getIn().setHeader(WonCamelConstants.CONNECTION_STATE_CHANGE_BUILDER_HEADER, stateChangeBuilder);
}
Also used : WonMessage(won.protocol.message.WonMessage) WonMessageType(won.protocol.message.WonMessageType) Connection(won.protocol.model.Connection)

Example 17 with WonMessageType

use of won.protocol.message.WonMessageType in project webofneeds by researchstudio-sat.

the class LockMessageParentWonMessageProcessor method lockParent.

private void lockParent(WonMessage message, WonMessageDirection direction) {
    // try to lock the message's connection first
    // * if the message is connection-specific
    // * and one exists already
    WonMessageType type = message.getMessageTypeRequired().isResponseMessage() ? message.getRespondingToMessageTypeRequired() : message.getMessageTypeRequired();
    if (type.isConnectionSpecificMessage()) {
        logger.debug("Attempting to lock connection for message {} {}", message.getMessageURI(), direction);
        Optional<URI> conURI = messageService.getConnectionofMessage(message, direction);
        if (conURI.isPresent()) {
            logger.debug("Locking connection {} for message {} {}", new Object[] { conURI.get(), message.getMessageURI(), direction });
            Optional<Connection> con = connectionRepository.findOneByConnectionURIForUpdate(conURI.get());
            if (con.isPresent()) {
                entityManager.refresh(con.get());
                logger.debug("Locked connection {} for message {} {}", new Object[] { conURI.get(), message.getMessageURI(), direction });
                logger.debug("connection: {}", con.get());
                return;
            } else {
                logger.debug("Did not lock connection {} for message {} {}", new Object[] { conURI.get(), message.getMessageURI(), direction });
            }
        } else {
            logger.debug("Did not find connection to lock for message {} {}", message.getMessageURI(), direction);
        }
    } else {
        logger.debug("Not attempting to lock connection for message {} {}", message.getMessageURI(), direction);
    }
    logger.debug("Attempting to lock atom for message {}", message.getMessageURI());
    // no connection found to lock or wanting to lock the atom too
    Optional<URI> atomURI = messageService.getAtomOfMessage(message, direction);
    if (atomURI.isPresent()) {
        Optional<Atom> atom = atomRepository.findOneByAtomURIForUpdate(atomURI.get());
        if (atom.isPresent()) {
            entityManager.refresh(atom.get());
            logger.debug("Locked atom {} for message {} {}", new Object[] { atom.get().getAtomURI(), message.getMessageURI(), direction });
        } else {
            logger.debug("Did not find atom {} to lock for message {} {}", new Object[] { atomURI.get(), message.getMessageURI(), direction });
        }
    } else {
        logger.debug("Did not find atom to lock for message {} {}", message.getMessageURI(), direction);
    }
// it's possible that we did not lock anything because
// * the message is neither atom- nor conneciton-specific
// * the message is a CREATE message - the atom doesn't exist yet
}
Also used : WonMessageType(won.protocol.message.WonMessageType) Connection(won.protocol.model.Connection) URI(java.net.URI) Atom(won.protocol.model.Atom)

Example 18 with WonMessageType

use of won.protocol.message.WonMessageType in project webofneeds by researchstudio-sat.

the class WonCamelHelper method getMessageType.

// // type
public static Optional<WonMessageType> getMessageType(Exchange exchange) {
    Objects.requireNonNull(exchange);
    URI uri = (URI) exchange.getIn().getHeader(WonCamelConstants.MESSAGE_TYPE_HEADER);
    if (uri == null) {
        return Optional.empty();
    }
    WonMessageType type = WonMessageType.getWonMessageType(uri);
    return Optional.ofNullable(type);
}
Also used : WonMessageType(won.protocol.message.WonMessageType) URI(java.net.URI)

Example 19 with WonMessageType

use of won.protocol.message.WonMessageType in project webofneeds by researchstudio-sat.

the class LinkedDataCacheInvalidator method process.

@Override
public WonMessage process(final WonMessage message) throws WonMessageProcessingException {
    WonMessageType type = message.getMessageType();
    if (type == WonMessageType.SUCCESS_RESPONSE) {
        type = message.getRespondingToMessageType();
    }
    URI webId = message.getRecipientAtomURI();
    if (type.isConnectionSpecificMessage()) {
        Optional<URI> connectionURI = WonLinkedDataUtils.getConnectionURIForIncomingMessage(message, linkedDataSource);
        if (connectionURI.isPresent()) {
            // message was created
            try {
                logger.debug("invalidating events list for atom " + message.getRecipientAtomURI() + " for connection " + connectionURI.get());
                URI messageContainerUri = WonRelativeUriHelper.createMessageContainerURIForConnection(connectionURI.get());
                invalidate(messageContainerUri, webId);
                if (type.causesConnectionStateChange()) {
                    invalidate(connectionURI.get(), webId);
                }
            } catch (Exception e) {
                logger.info("Error occurred while trying to invalidate cache for {}: {}", message.getRecipientAtomURI(), e.getMessage());
            }
        }
    }
    if (type.causesNewConnection()) {
        // the list of connections of the receiver atom should be invalidated, since
        // these type
        // of messages mean that the new connection has been created recently
        logger.debug("invalidating connections list for atom " + message.getRecipientAtomURI());
        try {
            URI connectionsListUri = WonRelativeUriHelper.createConnectionContainerURIForAtom(message.getRecipientAtomURI());
            invalidate(connectionsListUri, webId);
        } catch (Exception e) {
            logger.info("Error occurred while trying to invalidate cache for {}: {}", message.getRecipientAtomURI(), e.getMessage());
        }
    }
    if (type.causesAtomStateChange()) {
        invalidate(message.getRecipientAtomURI(), webId);
    }
    return message;
}
Also used : WonMessageType(won.protocol.message.WonMessageType) URI(java.net.URI) WonMessageProcessingException(won.protocol.exception.WonMessageProcessingException)

Example 20 with WonMessageType

use of won.protocol.message.WonMessageType in project webofneeds by researchstudio-sat.

the class OwnerCallbackAdapter method process.

@Override
public WonMessage process(final WonMessage message) throws WonMessageProcessingException {
    assert adaptee != null : "adaptee is not set";
    logger.debug("processing message {} and calling appropriate method on adaptee", message.getMessageURI());
    executor.execute(() -> {
        // if we get only one message, (case succ2), we don't have an option anyway
        // if we get all 3, the interesting one is the head (case: message from remote)
        // if we get a message plus a response, it's the local response and we want to
        // process that one.
        WonMessage focalMessage = message.getFocalMessage();
        WonMessageType messageType = focalMessage.getMessageType();
        switch(messageType) {
            case ATOM_HINT_MESSAGE:
                adaptee.onAtomHintFromMatcher(focalMessage);
                break;
            case SOCKET_HINT_MESSAGE:
                adaptee.onSocketHintFromMatcher(focalMessage);
                break;
            case CONNECT:
                adaptee.onConnectFromOtherAtom(makeConnection(message), focalMessage);
                break;
            case CONNECTION_MESSAGE:
                adaptee.onMessageFromOtherAtom(makeConnection(message), focalMessage);
                break;
            case CLOSE:
                adaptee.onCloseFromOtherAtom(makeConnection(message), focalMessage);
                break;
            case SUCCESS_RESPONSE:
                adaptee.onSuccessResponse(focalMessage.getRespondingToMessageURI(), focalMessage, Optional.ofNullable(makeConnection(message)));
                break;
            case FAILURE_RESPONSE:
                adaptee.onFailureResponse(focalMessage.getRespondingToMessageURI(), focalMessage, Optional.ofNullable(makeConnection(message)));
                break;
            case CREATE_ATOM:
                logger.debug("Handling CREATE_ATOM for message {}", focalMessage);
                break;
            case DELETE:
                logger.debug("Handling DELETE for message {}", focalMessage);
                break;
            case REPLACE:
                logger.debug("Handling REPLACE for message {}", focalMessage);
                break;
            case DEACTIVATE:
                logger.debug("Handling DEACTIVATE for message {}", focalMessage);
                break;
            default:
                logger.info("could not find callback method for wonMessage of type {}", focalMessage);
                if (logger.isDebugEnabled()) {
                    logger.debug("message: {}", RdfUtils.writeDatasetToString(message.getCompleteDataset(), Lang.TRIG));
                }
        }
    });
    // return the message for further processing
    return message;
}
Also used : WonMessage(won.protocol.message.WonMessage) WonMessageType(won.protocol.message.WonMessageType)

Aggregations

WonMessageType (won.protocol.message.WonMessageType)20 URI (java.net.URI)12 WonMessage (won.protocol.message.WonMessage)9 Dataset (org.apache.jena.query.Dataset)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 NoSuchConnectionException (won.protocol.exception.NoSuchConnectionException)6 URISyntaxException (java.net.URISyntaxException)4 ParseException (java.text.ParseException)3 HttpHeaders (org.springframework.http.HttpHeaders)3 ResponseEntity (org.springframework.http.ResponseEntity)3 AtomInformationService (won.node.service.persistence.AtomInformationService)3 Connection (won.protocol.model.Connection)3 StopWatch (org.springframework.util.StopWatch)2 NoSuchAtomException (won.protocol.exception.NoSuchAtomException)2 WonMessageProcessingException (won.protocol.exception.WonMessageProcessingException)2 ConnectionState (won.protocol.model.ConnectionState)2 NeedInformationService (won.protocol.service.NeedInformationService)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NoSuchProviderException (java.security.NoSuchProviderException)1