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