001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.kernel.impl.operations;
007
008import static org.fcrepo.kernel.api.utils.RelaxedPropertiesHelper.checkTripleForDisallowed;
009
010import org.fcrepo.config.ServerManagedPropsMode;
011import org.fcrepo.kernel.api.RdfStream;
012import org.fcrepo.kernel.api.Transaction;
013import org.fcrepo.kernel.api.identifiers.FedoraId;
014import org.fcrepo.kernel.api.operations.RdfSourceOperationBuilder;
015import org.fcrepo.kernel.api.rdf.DefaultRdfStream;
016
017import org.apache.jena.rdf.model.Model;
018
019/**
020 * Abstract builder for interacting with an Rdf Source Operation Builder
021 * @author bseeger
022 */
023public abstract class AbstractRdfSourceOperationBuilder extends AbstractRelaxableResourceOperationBuilder
024                                                        implements RdfSourceOperationBuilder {
025
026    /**
027     * Holds the stream of user's triples.
028     */
029    protected RdfStream tripleStream;
030
031    /**
032     * Principal of the user performing the operation
033     */
034    protected String userPrincipal;
035
036    /**
037     * The interaction model of this resource, null in case of update.
038     */
039    protected final String interactionModel;
040
041    protected AbstractRdfSourceOperationBuilder(final Transaction transaction, final FedoraId rescId,
042                                                final String model,
043                                                final ServerManagedPropsMode serverManagedPropsMode) {
044        super(transaction, rescId, serverManagedPropsMode);
045        interactionModel = model;
046    }
047
048    @Override
049    public RdfSourceOperationBuilder userPrincipal(final String userPrincipal) {
050        this.userPrincipal = userPrincipal;
051        return this;
052    }
053
054    @Override
055    public RdfSourceOperationBuilder triples(final RdfStream triples) {
056        if (this.serverManagedPropsMode.equals(ServerManagedPropsMode.RELAXED)) {
057            // Filter out server managed properties, they should only matter to the relaxedProperties method.
058            this.tripleStream = new DefaultRdfStream(triples.topic(), triples.filter(t -> {
059                try {
060                    checkTripleForDisallowed(t);
061                } catch (final Exception e) {
062                    return false;
063                }
064                return true;
065            }));
066        } else {
067            this.tripleStream = triples;
068        }
069        return this;
070    }
071
072    @Override
073    public RdfSourceOperationBuilder relaxedProperties(final Model model) {
074        return (RdfSourceOperationBuilder) super.relaxedProperties(model);
075    }
076}