use of won.protocol.exception.WonMessageBuilderException in project webofneeds by researchstudio-sat.
the class WonMessageBuilder method build.
/**
* Builds a WonMessage by adding data to the specified dataset.
* The dataset may be null or empty.
* @param dataset
* @return
* @throws WonMessageBuilderException
*/
public WonMessage build(final Dataset dataset) throws WonMessageBuilderException {
if (dataset == null) {
throw new IllegalArgumentException("specified dataset must not be null. If a new dataset is to be created for the message, build() should be called.");
}
if (messageURI == null) {
throw new WonMessageBuilderException("No messageURI specified");
}
Model envelopeGraph = ModelFactory.createDefaultModel();
DefaultPrefixUtils.setDefaultPrefixes(envelopeGraph);
// create a new envelope graph uri and add the envelope graph to the dataset
// ... and make sure that the graph URI will be new by also checking inside the wrapped message
String envelopeGraphURI = RdfUtils.createNewGraphURI(messageURI.toString(), ENVELOPE_URI_SUFFIX, 4, graphUri -> {
if (dataset.containsNamedModel(graphUri))
return false;
if (wrappedMessage == null)
return true;
if (wrappedMessage.getEnvelopeGraphURIs().contains(graphUri))
return false;
if (wrappedMessage.getContentGraphURIs().contains(graphUri))
return false;
return true;
}).toString();
dataset.addNamedModel(envelopeGraphURI, envelopeGraph);
// message URI
Resource messageEventResource = envelopeGraph.createResource(messageURI.toString(), this.wonMessageDirection.getResource());
// the [envelopeGraphURI] rdf:type msg:EnvelopeGraph makes it easy to select graphs by type
Resource envelopeGraphResource = envelopeGraph.createResource(envelopeGraphURI, WONMSG.ENVELOPE_GRAPH);
envelopeGraphResource.addProperty(RDFG.SUBGRAPH_OF, messageEventResource);
addWrappedOrForwardedMessage(dataset, envelopeGraph, envelopeGraphResource, messageURI);
// make sure the envelope type has been set
if (this.wonMessageDirection == null) {
throw new IllegalStateException("envelopeType must be set!");
}
if (wonMessageType != null) {
messageEventResource.addProperty(WONMSG.HAS_MESSAGE_TYPE_PROPERTY, wonMessageType.getResource());
}
messageEventResource.addLiteral(WONMSG.PROTOCOL_VERSION, envelopeGraph.createTypedLiteral("1.0"));
// add sender
if (senderURI != null)
messageEventResource.addProperty(WONMSG.SENDER_PROPERTY, envelopeGraph.createResource(senderURI.toString()));
if (senderNeedURI != null)
messageEventResource.addProperty(WONMSG.SENDER_NEED_PROPERTY, envelopeGraph.createResource(senderNeedURI.toString()));
if (senderNodeURI != null)
messageEventResource.addProperty(WONMSG.SENDER_NODE_PROPERTY, envelopeGraph.createResource(senderNodeURI.toString()));
// add receiver
if (receiverURI != null)
messageEventResource.addProperty(WONMSG.RECEIVER_PROPERTY, envelopeGraph.createResource(receiverURI.toString()));
if (receiverNeedURI != null)
messageEventResource.addProperty(WONMSG.RECEIVER_NEED_PROPERTY, envelopeGraph.createResource(receiverNeedURI.toString()));
if (receiverNodeURI != null)
messageEventResource.addProperty(WONMSG.RECEIVER_NODE_PROPERTY, envelopeGraph.createResource(receiverNodeURI.toString()));
// add refersTo
for (URI refersToURI : refersToURIs) {
messageEventResource.addProperty(WONMSG.REFERS_TO_PROPERTY, envelopeGraph.createResource(refersToURI.toString()));
}
if (isResponseToMessageURI != null) {
if (wonMessageType != WonMessageType.SUCCESS_RESPONSE && wonMessageType != WonMessageType.FAILURE_RESPONSE) {
throw new IllegalArgumentException("isResponseToMessageURI can only be used for SUCCESS_RESPONSE and " + "FAILURE_RESPONSE types");
}
if (isResponseToMessageType == null) {
throw new IllegalArgumentException("response messages must specify the type of message they are a response to" + ". Use setIsResponseToMessageType(type)");
}
messageEventResource.addProperty(WONMSG.IS_RESPONSE_TO, envelopeGraph.createResource(isResponseToMessageURI.toString()));
messageEventResource.addProperty(WONMSG.IS_RESPONSE_TO_MESSAGE_TYPE, this.isResponseToMessageType.getResource());
if (isRemoteResponseToMessageURI != null) {
messageEventResource.addProperty(WONMSG.IS_REMOTE_RESPONSE_TO, envelopeGraph.createResource(isRemoteResponseToMessageURI.toString()));
}
}
if (correspondingRemoteMessageURI != null) {
messageEventResource.addProperty(WONMSG.HAS_CORRESPONDING_REMOTE_MESSAGE, envelopeGraph.createResource(correspondingRemoteMessageURI.toString()));
}
if (forwardedMessageURI != null) {
messageEventResource.addProperty(WONMSG.HAS_FORWARDED_MESSAGE, envelopeGraph.createResource(forwardedMessageURI.toString()));
}
if (sentTimestamp != null) {
messageEventResource.addProperty(WONMSG.HAS_SENT_TIMESTAMP, envelopeGraph.createTypedLiteral(this.sentTimestamp));
}
if (receivedTimestamp != null) {
messageEventResource.addProperty(WONMSG.HAS_RECEIVED_TIMESTAMP, envelopeGraph.createTypedLiteral(this.receivedTimestamp));
}
for (URI contentURI : contentMap.keySet()) {
String contentUriString = contentURI.toString();
dataset.addNamedModel(contentUriString, contentMap.get(contentURI));
messageEventResource.addProperty(WONMSG.HAS_CONTENT_PROPERTY, messageEventResource.getModel().createResource(contentUriString));
// add the [content-graph] rdfg:subGraphOf [message-uri] triple to the envelope
envelopeGraph.createStatement(envelopeGraph.getResource(contentURI.toString()), RDFG.SUBGRAPH_OF, messageEventResource);
Model signatureGraph = signatureMap.get(contentURI);
if (signatureGraph != null) {
throw new UnsupportedOperationException("signatures are not supported yet");
/* in principle, this should work, but its untested:
uniqueContentUri = RdfUtils.createNewGraphURI(contentURI.toString(), SIGNATURE_URI_SUFFIX, 5,
dataset).toString();
//the signature refers to the name of the other graph. We changed that name
//so we have to replace the resource referencing it, too:
signatureGraph = RdfUtils.replaceResource(signatureGraph.getResource(contentURI.toString()),
signatureGraph.getResource(uniqueContentUri));
dataset.addNamedModel(uniqueContentUri, signatureGraph);
*/
}
// now replace the content URIs
}
return new WonMessage(dataset);
}
Aggregations