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);
}
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);
}
}
}
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;
}
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);
// }
}
});
}
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);
// }
}
Aggregations