Search in sources :

Example 56 with WonMessage

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

the class ConnectMessageFromNodeProcessor method process.

public void process(final Exchange exchange) throws Exception {
    Message message = exchange.getIn();
    WonMessage wonMessage = (WonMessage) message.getHeader(WonCamelConstants.MESSAGE_HEADER);
    // a need wants to connect.
    // get the required data from the message and create a connection
    URI needUri = wonMessage.getReceiverNeedURI();
    URI wonNodeUriFromWonMessage = wonMessage.getReceiverNodeURI();
    URI remoteNeedUri = wonMessage.getSenderNeedURI();
    URI remoteConnectionUri = wonMessage.getSenderURI();
    URI facetURI = WonRdfUtils.FacetUtils.getFacet(wonMessage);
    // if the uri is known already, we can load the connection!
    URI connectionURI = wonMessage.getReceiverURI();
    if (remoteConnectionUri == null)
        throw new MissingMessagePropertyException(URI.create(WONMSG.SENDER_PROPERTY.getURI().toString()));
    Connection con = null;
    if (connectionURI != null) {
        con = connectionRepository.findOneByConnectionURIForUpdate(connectionURI);
        if (con == null)
            throw new NoSuchConnectionException(connectionURI);
    } else {
        con = connectionRepository.findOneByNeedURIAndRemoteNeedURIAndTypeURIForUpdate(needUri, remoteNeedUri, facetURI);
    }
    if (con == null) {
        // create Connection in Database
        URI connectionUri = wonNodeInformationService.generateConnectionURI(wonNodeUriFromWonMessage);
        con = dataService.createConnection(connectionUri, needUri, remoteNeedUri, remoteConnectionUri, facetURI, ConnectionState.REQUEST_RECEIVED, ConnectionEventType.PARTNER_OPEN);
    }
    con.setRemoteConnectionURI(remoteConnectionUri);
    con.setState(con.getState().transit(ConnectionEventType.PARTNER_OPEN));
    connectionRepository.save(con);
    // set the receiver to the newly generated connection uri
    wonMessage.addMessageProperty(WONMSG.RECEIVER_PROPERTY, con.getConnectionURI());
}
Also used : Message(org.apache.camel.Message) WonMessage(won.protocol.message.WonMessage) MissingMessagePropertyException(won.protocol.message.processor.exception.MissingMessagePropertyException) NoSuchConnectionException(won.protocol.exception.NoSuchConnectionException) WonMessage(won.protocol.message.WonMessage) Connection(won.protocol.model.Connection) URI(java.net.URI)

Example 57 with WonMessage

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

the class CreateNeedMessageProcessor method storeNeed.

private Need storeNeed(final WonMessage wonMessage) {
    Dataset needContent = wonMessage.getMessageContent();
    List<WonMessage.AttachmentHolder> attachmentHolders = wonMessage.getAttachments();
    // remove attachment and its signature from the needContent
    removeAttachmentsFromNeedContent(needContent, attachmentHolders);
    URI needURI = getNeedURIFromWonMessage(needContent);
    if (!needURI.equals(wonMessage.getSenderNeedURI()))
        throw new IllegalArgumentException("receiverNeedURI and NeedURI of the content are not equal");
    Need need = new Need();
    need.setState(NeedState.ACTIVE);
    need.setNeedURI(needURI);
    // ToDo (FS) check if the WON node URI corresponds with the WON node (maybe earlier in the message layer)
    NeedEventContainer needEventContainer = needEventContainerRepository.findOneByParentUri(needURI);
    if (needEventContainer == null) {
        needEventContainer = new NeedEventContainer(need, need.getNeedURI());
    } else {
        throw new UriAlreadyInUseException("Found a NeedEventContainer for the need we're about to create (" + needURI + ") - aborting");
    }
    need.setWonNodeURI(wonMessage.getReceiverNodeURI());
    ConnectionContainer connectionContainer = new ConnectionContainer(need);
    need.setConnectionContainer(connectionContainer);
    need.setEventContainer(needEventContainer);
    // store the need content
    DatasetHolder datasetHolder = new DatasetHolder(needURI, needContent);
    // store attachments
    List<DatasetHolder> attachments = new ArrayList<>(attachmentHolders.size());
    for (WonMessage.AttachmentHolder attachmentHolder : attachmentHolders) {
        datasetHolder = new DatasetHolder(attachmentHolder.getDestinationUri(), attachmentHolder.getAttachmentDataset());
        attachments.add(datasetHolder);
    }
    // add everything to the need model class and save it
    need.setDatatsetHolder(datasetHolder);
    need.setAttachmentDatasetHolders(attachments);
    need = needRepository.save(need);
    connectionContainerRepository.save(connectionContainer);
    NeedModelWrapper needModelWrapper = new NeedModelWrapper(needContent);
    Collection<String> facets = needModelWrapper.getFacetUris();
    if (facets.size() == 0)
        throw new IllegalArgumentException("at least one property won:hasFacet required ");
    for (String facetUri : facets) {
        // TODO: check if there is a implementation for the facet on the node
        Facet f = new Facet();
        f.setNeedURI(needURI);
        f.setTypeURI(URI.create(facetUri));
        facetRepository.save(f);
    }
    return need;
}
Also used : Dataset(org.apache.jena.query.Dataset) ArrayList(java.util.ArrayList) NeedModelWrapper(won.protocol.util.NeedModelWrapper) URI(java.net.URI) UriAlreadyInUseException(won.protocol.message.processor.exception.UriAlreadyInUseException) WonMessage(won.protocol.message.WonMessage)

Example 58 with WonMessage

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

the class DeactivateNeedMessageFromOwnerReactionProcessor method process.

public void process(final Exchange exchange) throws Exception {
    WonMessage wonMessage = (WonMessage) exchange.getIn().getHeader(WonCamelConstants.MESSAGE_HEADER);
    URI receiverNeedURI = wonMessage.getReceiverNeedURI();
    logger.debug("DEACTIVATING need. needURI:{}", receiverNeedURI);
    if (receiverNeedURI == null)
        throw new WonMessageProcessingException("receiverNeedURI is not set");
    Need need = DataAccessUtils.loadNeed(needRepository, receiverNeedURI);
    matcherProtocolMatcherClient.needDeactivated(need.getNeedURI(), wonMessage);
    // close all connections
    Collection<Connection> conns = connectionRepository.getConnectionsByNeedURIAndNotInStateForUpdate(need.getNeedURI(), ConnectionState.CLOSED);
    for (Connection con : conns) {
        closeConnection(need, con);
    }
}
Also used : WonMessageProcessingException(won.protocol.message.processor.exception.WonMessageProcessingException) Need(won.protocol.model.Need) WonMessage(won.protocol.message.WonMessage) Connection(won.protocol.model.Connection) URI(java.net.URI)

Example 59 with WonMessage

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

the class DeactivateNeedMessageProcessor method process.

public void process(final Exchange exchange) throws Exception {
    WonMessage wonMessage = (WonMessage) exchange.getIn().getHeader(WonCamelConstants.MESSAGE_HEADER);
    URI receiverNeedURI = wonMessage.getReceiverNeedURI();
    logger.debug("DEACTIVATING need. needURI:{}", receiverNeedURI);
    if (receiverNeedURI == null)
        throw new WonMessageProcessingException("receiverNeedURI is not set");
    Need need = DataAccessUtils.loadNeed(needRepository, receiverNeedURI);
    need.getEventContainer().getEvents().add(messageEventRepository.findOneByMessageURIforUpdate(wonMessage.getMessageURI()));
    need.setState(NeedState.INACTIVE);
    need = needRepository.save(need);
}
Also used : WonMessageProcessingException(won.protocol.message.processor.exception.WonMessageProcessingException) Need(won.protocol.model.Need) WonMessage(won.protocol.message.WonMessage) URI(java.net.URI)

Example 60 with WonMessage

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

the class FailureResponseFromSystemToNodeProcessor method process.

@Override
public void process(Exchange exchange) throws Exception {
    WonMessage wonMessage = (WonMessage) exchange.getIn().getHeader(WonCamelConstants.MESSAGE_HEADER);
    // prepare the message to pass to the remote node
    URI remoteMessageUri = wonNodeInformationService.generateEventURI(wonMessage.getReceiverNodeURI());
    // add the information about the corresponding message to the local one
    wonMessage.addMessageProperty(WONMSG.HAS_CORRESPONDING_REMOTE_MESSAGE, remoteMessageUri);
    // the persister will pick it up later
    // put the factory into the outbound message factory header. It will be used to generate the outbound message
    // after the wonMessage has been processed and saved, to make sure that the outbound message contains
    // all the data that we also store locally
    OutboundMessageFactory outboundMessageFactory = new OutboundMessageFactory(remoteMessageUri);
    exchange.getIn().setHeader(WonCamelConstants.OUTBOUND_MESSAGE_FACTORY_HEADER, outboundMessageFactory);
}
Also used : WonMessage(won.protocol.message.WonMessage) URI(java.net.URI)

Aggregations

WonMessage (won.protocol.message.WonMessage)89 URI (java.net.URI)47 Connection (won.protocol.model.Connection)19 Test (org.junit.Test)18 Message (org.apache.camel.Message)17 Dataset (org.apache.jena.query.Dataset)17 Exchange (org.apache.camel.Exchange)13 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)13 DefaultExchange (org.apache.camel.impl.DefaultExchange)13 WonMessageProcessingException (won.protocol.message.processor.exception.WonMessageProcessingException)11 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)10 MissingMessagePropertyException (won.protocol.message.processor.exception.MissingMessagePropertyException)9 Event (won.bot.framework.eventbot.event.Event)8 FailureResponseEvent (won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent)8 EventListener (won.bot.framework.eventbot.listener.EventListener)7 Need (won.protocol.model.Need)7 WonNodeInformationService (won.protocol.service.WonNodeInformationService)7 Model (org.apache.jena.rdf.model.Model)5 WonURI (won.bot.framework.eventbot.action.impl.mail.model.WonURI)5 DefaultNeedModelWrapper (won.protocol.util.DefaultNeedModelWrapper)4