Search in sources :

Example 1 with WonMessageBuilderException

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);
}
Also used : WonMessageBuilderException(won.protocol.exception.WonMessageBuilderException) CheapInsecureRandomString(won.protocol.util.CheapInsecureRandomString) WON(won.protocol.vocabulary.WON) Iterator(java.util.Iterator) Date(java.util.Date) Set(java.util.Set) HashMap(java.util.HashMap) DefaultPrefixUtils(won.protocol.util.DefaultPrefixUtils) WONMSG(won.protocol.vocabulary.WONMSG) WonRdfUtils(won.protocol.util.WonRdfUtils) HashSet(java.util.HashSet) Model(org.apache.jena.rdf.model.Model) RdfUtils(won.protocol.util.RdfUtils) DatasetFactory(org.apache.jena.query.DatasetFactory) Resource(org.apache.jena.rdf.model.Resource) Map(java.util.Map) URI(java.net.URI) ModelFactory(org.apache.jena.rdf.model.ModelFactory) RDFG(won.protocol.vocabulary.RDFG) Dataset(org.apache.jena.query.Dataset) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) CheapInsecureRandomString(won.protocol.util.CheapInsecureRandomString) URI(java.net.URI) WonMessageBuilderException(won.protocol.exception.WonMessageBuilderException)

Aggregations

URI (java.net.URI)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Set (java.util.Set)1 Dataset (org.apache.jena.query.Dataset)1 DatasetFactory (org.apache.jena.query.DatasetFactory)1 Model (org.apache.jena.rdf.model.Model)1 ModelFactory (org.apache.jena.rdf.model.ModelFactory)1 Resource (org.apache.jena.rdf.model.Resource)1 WonMessageBuilderException (won.protocol.exception.WonMessageBuilderException)1 CheapInsecureRandomString (won.protocol.util.CheapInsecureRandomString)1 DefaultPrefixUtils (won.protocol.util.DefaultPrefixUtils)1 RdfUtils (won.protocol.util.RdfUtils)1 WonRdfUtils (won.protocol.util.WonRdfUtils)1 RDFG (won.protocol.vocabulary.RDFG)1 WON (won.protocol.vocabulary.WON)1 WONMSG (won.protocol.vocabulary.WONMSG)1