Search in sources :

Example 36 with Connection

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

the class SendMessageFromOwnerProcessor 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 37 with Connection

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

the class CoordinatorFacetImpl method connectFromOwner.

@Override
public void connectFromOwner(final Connection con, final Model content, final WonMessage wonMessage) throws NoSuchNeedException, IllegalMessageForNeedStateException, ConnectionAlreadyExistsException {
    logger.debug("Coordinator: 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;
// try {
// final ListenableFuture<URI> remoteConnectionURI = needProtocolNeedService.connect(con.getRemoteNeedURI(),
// con.getNeedURI(), connectionForRunnable.getConnectionURI(), remoteFacetModel, wonMessage);
// this.executorService.execute(new Runnable(){
// @Override
// public void run() {
// try{
// if (logger.isDebugEnabled()) {
// logger.debug("saving remote connection URI");
// }
// dataService.updateRemoteConnectionURI(con, remoteConnectionURI.get());
// } catch (Exception e) {
// logger.warn("Error saving connection {}. Stacktrace follows", con);
// logger.warn("Error saving connection ", e);
// }
// }
// });
// } catch (WonProtocolException 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 {
// //        Connection c = closeConnectionLocally(connectionForRunnable, content);
// //          // ToDo (FS): should probably not be the same wonMessage!?
// //        needFacingConnectionCommunicationService.close(c.getConnectionURI(), content, wonMessage);
// //      } catch (NoSuchConnectionException e1) {
// //        logger.warn("caught NoSuchConnectionException:", e1);
// //      } catch (IllegalMessageForConnectionStateException e1) {
// //        logger.warn("caught IllegalMessageForConnectionStateException:", e1);
// //      }
// }
// catch (InterruptedException e) {
// e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
// } catch (ExecutionException e) {
// e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
// } catch (Exception e) {
// logger.debug("caught Exception", e);
// }
}
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 38 with Connection

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

the class ParticipantFacetImpl method connectFromNeed.

public void connectFromNeed(final Connection con, final Model content, final WonMessage wonMessage) throws NoSuchNeedException, IllegalMessageForNeedStateException, ConnectionAlreadyExistsException {
    final Connection connectionForRunnable = con;
    logger.debug("Participant: ConnectFromNeed");
    executorService.execute(new Runnable() {

        @Override
        public void run() {
        // TODO: use new system
        // ownerProtocolOwnerService.connect(
        // con.getNeedURI(), con.getRemoteNeedURI(),
        // connectionForRunnable.getConnectionURI(), content, wonMessage);
        }
    });
}
Also used : Connection(won.protocol.model.Connection)

Example 39 with Connection

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

the class CloseMessageFromNodeProcessor 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 = dataService.nextConnectionState(connectionURIFromWonMessage, ConnectionEventType.PARTNER_CLOSE);
}
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 40 with Connection

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

the class CloseMessageFromOwnerProcessor method process.

public void process(final Exchange exchange) throws Exception {
    Message message = exchange.getIn();
    WonMessage wonMessage = (WonMessage) message.getHeader(WonCamelConstants.MESSAGE_HEADER);
    logger.debug("CLOSE received from the owner side for connection {}", wonMessage.getSenderURI());
    Connection con = connectionRepository.findOneByConnectionURIForUpdate(wonMessage.getSenderURI());
    ConnectionState originalState = con.getState();
    con = dataService.nextConnectionState(con, ConnectionEventType.OWNER_CLOSE);
    // if the connection was in suggested state, don't send a close message to the remote need
    if (originalState != ConnectionState.SUGGESTED) {
        // prepare the message to pass to the remote node
        // create the message to send to the remote node
        URI remoteMessageURI = wonNodeInformationService.generateEventURI(wonMessage.getReceiverNodeURI());
        OutboundMessageCreator outboundMessageCreator = new OutboundMessageCreator(remoteMessageURI);
        // put it into the 'outbound message' header (so the persister doesn't pick up the wrong one).
        message.setHeader(WonCamelConstants.OUTBOUND_MESSAGE_FACTORY_HEADER, outboundMessageCreator);
        // set the sender uri in the envelope TODO: TwoMsgs: do not set sender here
        wonMessage.addMessageProperty(WONMSG.SENDER_PROPERTY, con.getConnectionURI());
        // 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 from the header
    }
}
Also used : Message(org.apache.camel.Message) WonMessage(won.protocol.message.WonMessage) WonMessage(won.protocol.message.WonMessage) Connection(won.protocol.model.Connection) ConnectionState(won.protocol.model.ConnectionState) 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