Search in sources :

Example 21 with Connection

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

the class FacetExtractingCamelProcessor method process.

@Override
public void process(Exchange exchange) throws Exception {
    URI facetType = null;
    // first, try to extract the facet from the message
    WonMessage wonMessage = (WonMessage) exchange.getIn().getHeader(WonCamelConstants.MESSAGE_HEADER);
    facetType = WonRdfUtils.FacetUtils.getFacet(wonMessage);
    if (facetType == null) {
        // not found.. look into the database. For that, we need to know the connection in question:
        URI conUri = (URI) exchange.getIn().getHeader(WonCamelConstants.CONNECTION_URI_HEADER);
        if (conUri == null)
            throw new MissingMessagePropertyException(URI.create(WONMSG.RECEIVER_PROPERTY.getURI().toString()));
        Connection con = connectionRepository.findOneByConnectionURI(conUri);
        facetType = con.getTypeURI();
    }
    if (facetType == null) {
        throw new WonMessageProcessingException(String.format("Failed to determine connection " + "facet for message %s", wonMessage.getMessageURI()));
    }
    exchange.getIn().setHeader(WonCamelConstants.FACET_TYPE_HEADER, facetType);
}
Also used : WonMessageProcessingException(won.protocol.message.processor.exception.WonMessageProcessingException) MissingMessagePropertyException(won.protocol.message.processor.exception.MissingMessagePropertyException) WonMessage(won.protocol.message.WonMessage) Connection(won.protocol.model.Connection) URI(java.net.URI)

Example 22 with Connection

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

the class SendMessageFromNodeGroupFacetImpl method process.

@Override
public void process(final Exchange exchange) throws Exception {
    final WonMessage wonMessage = (WonMessage) exchange.getIn().getHeader(WonCamelConstants.MESSAGE_HEADER);
    // whatever happens, this message is not sent to the owner:
    exchange.getIn().setHeader(WonCamelConstants.SUPPRESS_MESSAGE_TO_OWNER, Boolean.TRUE);
    if (messageEventRepository.existEarlierMessageWithSameInnermostMessageURIAndReceiverNeedURI(wonMessage.getMessageURI())) {
        if (logger.isDebugEnabled()) {
            URI innermostMessageURI = wonMessage.getInnermostMessageURI();
            URI groupUri = wonMessage.getReceiverNeedURI();
            logger.debug("suppressing message {} " + "as its innermost message is {} which has already " + "been processed by group {}", new Object[] { wonMessage.getMessageURI(), innermostMessageURI, groupUri });
        }
        return;
    }
    final Connection conOfIncomingMessage = connectionRepository.findByConnectionURI(wonMessage.getReceiverURI()).get(0);
    final List<Connection> consInGroup = connectionRepository.findByNeedURIAndStateAndTypeURI(conOfIncomingMessage.getNeedURI(), ConnectionState.CONNECTED, FacetType.GroupFacet.getURI());
    if (consInGroup == null || consInGroup.size() < 2)
        return;
    if (logger.isDebugEnabled()) {
        logger.debug("processing message {} received from need {} in group {} - preparing to send it to {} group members (text message: '{}'}", new Object[] { wonMessage.getMessageURI(), wonMessage.getSenderNeedURI(), wonMessage.getReceiverNeedURI(), consInGroup.size() - 1, WonRdfUtils.MessageUtils.getTextMessage(wonMessage) });
    }
    for (final Connection conToSendTo : consInGroup) {
        try {
            if (!conToSendTo.equals(conOfIncomingMessage)) {
                if (messageEventRepository.isReceivedSameInnermostMessageFromSender(wonMessage.getMessageURI(), conToSendTo.getRemoteNeedURI())) {
                    if (logger.isDebugEnabled()) {
                        URI innermostMessageURI = wonMessage.getInnermostMessageURI();
                        URI groupUri = wonMessage.getReceiverNeedURI();
                        logger.debug("suppressing forward of message {} to {} in group {}" + "as its innermost message is {} which has already " + "been received from that need", new Object[] { wonMessage.getMessageURI(), conToSendTo.getRemoteNeedURI(), groupUri, innermostMessageURI });
                    }
                    continue;
                }
                if (logger.isDebugEnabled()) {
                    logger.debug("forwarding message {} received from need {} in group {} to group member {}", new Object[] { wonMessage.getMessageURI(), wonMessage.getSenderNeedURI(), wonMessage.getReceiverNeedURI(), conToSendTo.getRemoteNeedURI() });
                }
                URI forwardedMessageURI = wonNodeInformationService.generateEventURI(wonMessage.getReceiverNodeURI());
                URI remoteWonNodeUri = WonLinkedDataUtils.getWonNodeURIForNeedOrConnectionURI(conOfIncomingMessage.getRemoteConnectionURI(), linkedDataSource);
                WonMessage newWonMessage = WonMessageBuilder.forwardReceivedNodeToNodeMessageAsNodeToNodeMessage(forwardedMessageURI, wonMessage, conToSendTo.getConnectionURI(), conToSendTo.getNeedURI(), wonMessage.getReceiverNodeURI(), conToSendTo.getRemoteConnectionURI(), conToSendTo.getRemoteNeedURI(), remoteWonNodeUri);
                sendSystemMessage(newWonMessage);
            }
        } catch (Exception e) {
            logger.warn("caught Exception:", e);
        }
    }
}
Also used : WonMessage(won.protocol.message.WonMessage) Connection(won.protocol.model.Connection) URI(java.net.URI)

Example 23 with Connection

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

the class LinkedDataServiceImpl method listConnectionEventURIs.

@Override
@Transactional
public Dataset listConnectionEventURIs(final URI connectionUri, boolean deep) throws NoSuchConnectionException {
    Model model = ModelFactory.createDefaultModel();
    setNsPrefixes(model);
    Connection connection = needInformationService.readConnection(connectionUri);
    Resource eventContainer = model.createResource(connection.getConnectionURI().toString() + "/events", WON.EVENT_CONTAINER);
    // add the events with the new format (only the URI, no content)
    List<MessageEventPlaceholder> connectionEvents = messageEventRepository.findByParentURI(connectionUri);
    Dataset eventsContainerDataset = newDatasetWithNamedModel(createDataGraphUriFromResource(eventContainer), model);
    addBaseUriAndDefaultPrefixes(eventsContainerDataset);
    for (MessageEventPlaceholder event : connectionEvents) {
        model.add(model.createStatement(eventContainer, RDFS.member, model.getResource(event.getMessageURI().toString())));
        if (deep) {
            Dataset eventDataset = event.getDatasetHolder().getDataset();
            RdfUtils.addDatasetToDataset(eventsContainerDataset, eventDataset);
        }
    }
    return eventsContainerDataset;
}
Also used : Dataset(org.apache.jena.query.Dataset) Model(org.apache.jena.rdf.model.Model) Connection(won.protocol.model.Connection) Resource(org.apache.jena.rdf.model.Resource) MessageEventPlaceholder(won.protocol.model.MessageEventPlaceholder) Transactional(org.springframework.transaction.annotation.Transactional)

Example 24 with Connection

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

the class AbstractBAFacet method connectFromOwner.

/**
 * This function is invoked when an owner sends an open message to the won node and usually executes registered facet specific code.
 * The connection is identified by the connection object con. A rdf graph can be sent along with the request.
 *
 * @param con the connection object
 * @param content a rdf graph describing properties of the event. The null releative URI ('<>') inside that graph,
 *                as well as the base URI of the graph will be attached to the resource identifying the event.
 * @throws NoSuchConnectionException if connectionURI does not refer to an existing connection
 * @throws IllegalMessageForConnectionStateException if the message is not allowed in the current state of the connection
 */
@Override
public void connectFromOwner(final Connection con, final Model content, final WonMessage wonMessage) throws NoSuchNeedException, IllegalMessageForNeedStateException, ConnectionAlreadyExistsException {
    final Model remoteFacetModel = changeHasRemoteFacetToHasFacet(content);
    final Connection connectionForRunnable = con;
    // send to need
    executorService.execute(new Runnable() {

        @Override
        public void run() {
        // try {
        // ListenableFuture<URI> remoteConnectionURI = needProtocolNeedService.connect(
        // con.getRemoteNeedURI(),
        // con.getNeedURI(),
        // connectionForRunnable.getConnectionURI(),
        // remoteFacetModel,
        // wonMessage);
        // dataService.updateRemoteConnectionURI(con, remoteConnectionURI.get());
        // 
        // } 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).
        // logger.warn("could not connectFromOwner, sending close back. Exception was: ",e);
        // //          try {
        // //            needFacingConnectionCommunicationService.close(
        // //                    connectionForRunnable.getConnectionURI(),
        // //                    content,
        // //                    wonMessage);
        // //          } catch (Exception e1) {
        // //            logger.warn("caught Exception sending close back from connectFromOwner::", e1);
        // //          }
        // } catch (Exception e) {
        // logger.warn("caught Exception in connectFromOwner: ",e);
        // }
        }
    });
}
Also used : Model(org.apache.jena.rdf.model.Model) Connection(won.protocol.model.Connection)

Example 25 with Connection

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

the class AbstractFacet method connectFromOwner.

/**
 * This function is invoked when an owner sends an open message to the won node and usually executes registered facet specific code.
 * The connection is identified by the connection object con. A rdf graph can be sent along with the request.
 *
 * @param con the connection object
 * @param content a rdf graph describing properties of the event. The null releative URI ('<>') inside that graph,
 *                as well as the base URI of the graph will be attached to the resource identifying the event.
 * @throws NoSuchConnectionException if connectionURI does not refer to an existing connection
 * @throws IllegalMessageForConnectionStateException if the message is not allowed in the current state of the connection
 */
@Override
public void connectFromOwner(final Connection con, final Model content, WonMessage wonMessage) throws NoSuchNeedException, IllegalMessageForNeedStateException, ConnectionAlreadyExistsException {
    Model remoteFacetModel = null;
    remoteFacetModel = changeHasRemoteFacetToHasFacet(content);
    final Connection connectionForRunnable = con;
// send to need
// 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).
// logger.warn("could not connectFromOwner, sending close back. Exception was: ",e);
// try {
// 
// // this WonMessage is not valid (the sender part) since it should be send from the remote WON node
// // but it should be replaced with a response message anyway
// WonMessageBuilder builder = new WonMessageBuilder();
// WonMessage closeWonMessage = builder
// .setMessageURI(wonNodeInformationService.generateEventURI(
// wonMessage.getSenderNodeURI())) //not sure if this is correct
// .setWonMessageType(WonMessageType.CLOSE)
// .setSenderURI(wonMessage.getSenderURI())
// .setSenderNeedURI(wonMessage.getSenderNeedURI())
// .setSenderNodeURI(wonMessage.getSenderNodeURI())
// .setReceiverURI(wonMessage.getSenderURI())
// .setReceiverNeedURI(wonMessage.getSenderNeedURI())
// .setReceiverNodeURI(wonMessage.getSenderNodeURI())
// .setWonMessageDirection(WonMessageDirection.FROM_EXTERNAL)
// .build();
// 
// //        needFacingConnectionCommunicationService.close(
// //                connectionForRunnable.getConnectionURI(), content, closeWonMessage);
// } catch (Exception e1) {
// logger.warn("caught Exception sending close back from connectFromOwner::", e1);
// }
// } catch (Exception e) {
// logger.warn("caught Exception in connectFromOwner: ",e);
// }
}
Also used : Model(org.apache.jena.rdf.model.Model) Connection(won.protocol.model.Connection)

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