Search in sources :

Example 11 with Connection

use of won.protocol.model.Connection in project webofneeds by researchstudio-sat.

the class OpenMessageFromNodeProcessor method process.

public void process(final Exchange exchange) throws Exception {
    Message message = exchange.getIn();
    WonMessage wonMessage = (WonMessage) message.getHeader(WonCamelConstants.MESSAGE_HEADER);
    URI connectionURIFromWonMessage = wonMessage.getReceiverURI();
    Connection con = null;
    if (connectionURIFromWonMessage == null) {
        // the opener didn't know about the connection
        // this happens, for example, when both parties get a hint. Both create a connection, but they don't know
        // about each other.
        // That's why we first try to find a connection with the same needs and facet:
        // let's extract the facet, we'll need it multiple times here
        URI facet = WonRdfUtils.FacetUtils.getFacet(wonMessage);
        con = connectionRepository.findOneByNeedURIAndRemoteNeedURIAndTypeURIForUpdate(wonMessage.getReceiverNeedURI(), wonMessage.getSenderNeedURI(), facet);
        if (con == null) {
            // ok, we really do not know about the connection. create it.
            URI connectionUri = wonNodeInformationService.generateConnectionURI(wonMessage.getReceiverNodeURI());
            con = dataService.createConnection(connectionUri, wonMessage.getReceiverNeedURI(), wonMessage.getSenderNeedURI(), wonMessage.getSenderURI(), facet, ConnectionState.REQUEST_RECEIVED, ConnectionEventType.PARTNER_OPEN);
        }
    } else {
        // the opener knew about the connection. just load it.
        con = connectionRepository.findOneByConnectionURIForUpdate(connectionURIFromWonMessage);
    }
    // now perform checks
    if (con == null)
        throw new IllegalStateException("connection must not be null");
    if (con.getRemoteNeedURI() == null)
        throw new IllegalStateException("remote need uri must not be null");
    if (!con.getRemoteNeedURI().equals(wonMessage.getSenderNeedURI()))
        throw new IllegalStateException("the remote need uri of the connection must be equal to the sender need uri of the message");
    if (wonMessage.getSenderURI() == null)
        throw new IllegalStateException("the sender uri must not be null");
    // it is possible that we didn't store the reference to the remote conneciton yet. Now we can do it.
    if (con.getRemoteConnectionURI() == null) {
        // Set it from the message (it's the sender of the message)
        con.setRemoteConnectionURI(wonMessage.getSenderURI());
    }
    if (!con.getRemoteConnectionURI().equals(wonMessage.getSenderURI()))
        throw new IllegalStateException("the sender uri of the message must be equal to the remote connection uri");
    con.setState(con.getState().transit(ConnectionEventType.PARTNER_OPEN));
    connectionRepository.save(con);
    // set the receiver to the local connection uri
    wonMessage.addMessageProperty(WONMSG.RECEIVER_PROPERTY, con.getConnectionURI());
}
Also used : Message(org.apache.camel.Message) WonMessage(won.protocol.message.WonMessage) WonMessage(won.protocol.message.WonMessage) Connection(won.protocol.model.Connection) URI(java.net.URI)

Example 12 with Connection

use of won.protocol.model.Connection in project webofneeds by researchstudio-sat.

the class SendMessageFromSystemProcessor method process.

public void process(final Exchange exchange) throws Exception {
    Message message = exchange.getIn();
    WonMessage wonMessage = (WonMessage) message.getHeader(WonCamelConstants.MESSAGE_HEADER);
    URI connectionUri = wonMessage.getSenderURI();
    if (connectionUri == null) {
        throw new MissingMessagePropertyException(URI.create(WONMSG.SENDER_PROPERTY.toString()));
    }
    Connection con = connectionRepository.findOneByConnectionURIForUpdate(connectionUri);
    if (con.getState() != ConnectionState.CONNECTED) {
        throw new IllegalMessageForConnectionStateException(connectionUri, "CONNECTION_MESSAGE", con.getState());
    }
    URI remoteMessageUri = wonNodeInformationService.generateEventURI(wonMessage.getReceiverNodeURI());
    if (wonMessage.getReceiverURI() == null) {
        // set the sender uri in the envelope TODO: TwoMsgs: do not set sender here
        wonMessage.addMessageProperty(WONMSG.RECEIVER_PROPERTY, con.getRemoteConnectionURI());
    }
    // add the information about the remote message to the locally stored 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, con);
    exchange.getIn().setHeader(WonCamelConstants.OUTBOUND_MESSAGE_FACTORY_HEADER, outboundMessageFactory);
}
Also used : Message(org.apache.camel.Message) WonMessage(won.protocol.message.WonMessage) MissingMessagePropertyException(won.protocol.message.processor.exception.MissingMessagePropertyException) WonMessage(won.protocol.message.WonMessage) Connection(won.protocol.model.Connection) IllegalMessageForConnectionStateException(won.protocol.exception.IllegalMessageForConnectionStateException) URI(java.net.URI)

Example 13 with Connection

use of won.protocol.model.Connection in project webofneeds by researchstudio-sat.

the class CoordinatorFacetImpl method abortTransaction.

public void abortTransaction(Connection con, Model content) {
    List<Connection> cons = connectionRepository.findByNeedURI(con.getNeedURI());
    try {
        for (Connection c : cons) {
            if (c.getState() != ConnectionState.CLOSED) {
                Model myContent = ModelFactory.createDefaultModel();
                myContent.setNsPrefix("", "no:uri");
                Resource res = myContent.createResource("no:uri");
                if (c.getState() == ConnectionState.CONNECTED || c.getState() == ConnectionState.REQUEST_SENT) {
                    if (res == null) {
                        logger.debug("no default prexif specified in model, could not obtain additional content, using ABORTED message");
                    }
                    res.removeAll(WON_TX.COORDINATION_MESSAGE);
                    res.addProperty(WON_TX.COORDINATION_MESSAGE, WON_TX.COORDINATION_MESSAGE_ABORT);
                }
            // todo: use new system
            // closeConnectionLocally(c, content);
            // needFacingConnectionClient.close(c, myContent, null);  //Abort sent to participant
            }
        }
    // }  catch (WonProtocolException e) {
    // logger.warn("caught WonProtocolException:", e);
    } catch (Exception e) {
        logger.debug("caught Exception", e);
    }
}
Also used : Connection(won.protocol.model.Connection) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource)

Example 14 with Connection

use of won.protocol.model.Connection in project webofneeds by researchstudio-sat.

the class ParticipantFacetImpl method connectFromOwner.

@Override
public void connectFromOwner(final Connection con, final Model content, final WonMessage wonMessage) throws NoSuchNeedException, IllegalMessageForNeedStateException, ConnectionAlreadyExistsException {
    logger.debug("Participant: ConntectFromOwner");
    Resource baseRes = content.getResource(content.getNsPrefixURI(""));
    StmtIterator stmtIterator = baseRes.listProperties(WON.HAS_REMOTE_FACET);
    if (!stmtIterator.hasNext())
        throw new IllegalArgumentException("at least one RDF node must be of type won:hasRemoteFacet");
    // TODO: This should just remove RemoteFacet from content and replace the value of Facet with the one from RemoteFacet
    final Model remoteFacetModel = ModelFactory.createDefaultModel();
    remoteFacetModel.setNsPrefix("", "no:uri");
    baseRes = remoteFacetModel.createResource(remoteFacetModel.getNsPrefixURI(""));
    Resource remoteFacetResource = stmtIterator.next().getObject().asResource();
    baseRes.addProperty(WON.HAS_FACET, remoteFacetModel.createResource(remoteFacetResource.getURI()));
    final Connection connectionForRunnable = con;
    // send to need
    executorService.execute(new Runnable() {

        @Override
        public void run() {
            try {
            // TODO: use new system
            // Future<URI> remoteConnectionURI = needProtocolNeedService.connect(
            // con.getRemoteNeedURI(),con.getNeedURI(),
            // connectionForRunnable.getConnectionURI(), remoteFacetModel, wonMessage);
            // dataService.updateRemoteConnectionURI(con, remoteConnectionURI.get());
            } catch (Exception e) {
            // we can't connect the connection. we send a close back to the owner
            // TODO should we introduce a new protocol method connectionFailed (because it's not an owner deny but some protocol-level error)?
            // For now, we call the close method as if it had been called from the remote side
            // TODO: even with this workaround, it would be good to send a content along with the close (so we can explain what happened).
            // try {
            // needFacingConnectionCommunicationService.close(
            // connectionForRunnable.getConnectionURI(), content, wonMessage);
            // } catch (NoSuchConnectionException e1) {
            // logger.warn("caught NoSuchConnectionException:", e1);
            // } catch (IllegalMessageForConnectionStateException e1) {
            // logger.warn("caught IllegalMessageForConnectionStateException:", e1);
            // }
            }
        }
    });
}
Also used : StmtIterator(org.apache.jena.rdf.model.StmtIterator) Resource(org.apache.jena.rdf.model.Resource) Model(org.apache.jena.rdf.model.Model) Connection(won.protocol.model.Connection)

Example 15 with Connection

use of won.protocol.model.Connection in project webofneeds by researchstudio-sat.

the class OwnerProtocolCommunicationServiceImpl method getWonNodeUriWithConnectionUri.

@Override
public synchronized URI getWonNodeUriWithConnectionUri(URI connectionUri) throws NoSuchConnectionException {
    // TODO: make this more efficient
    Connection con = DataAccessUtils.loadConnection(connectionRepository, connectionUri);
    URI needURI = con.getNeedURI();
    Need need = needRepository.findByNeedURI(needURI).get(0);
    return need.getWonNodeURI();
}
Also used : Need(won.protocol.model.Need) Connection(won.protocol.model.Connection) URI(java.net.URI)

Aggregations

Connection (won.protocol.model.Connection)50 URI (java.net.URI)30 WonMessage (won.protocol.message.WonMessage)20 Message (org.apache.camel.Message)12 Model (org.apache.jena.rdf.model.Model)12 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)9 Dataset (org.apache.jena.query.Dataset)6 WonURI (won.bot.framework.eventbot.action.impl.mail.model.WonURI)6 Resource (org.apache.jena.rdf.model.Resource)5 ConnectionMessageCommandEvent (won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent)5 MissingMessagePropertyException (won.protocol.message.processor.exception.MissingMessagePropertyException)5 BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)4 EventBus (won.bot.framework.eventbot.bus.EventBus)4 Event (won.bot.framework.eventbot.event.Event)4 CloseCommandEvent (won.bot.framework.eventbot.event.impl.command.close.CloseCommandEvent)4 EventListener (won.bot.framework.eventbot.listener.EventListener)4 WonRdfUtils (won.protocol.util.WonRdfUtils)4 Message (org.telegram.telegrambots.api.objects.Message)3 TelegramBotContextWrapper (won.bot.framework.bot.context.TelegramBotContextWrapper)3 BaseNeedAndConnectionSpecificEvent (won.bot.framework.eventbot.event.BaseNeedAndConnectionSpecificEvent)3