001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.kernel.impl.services;
019
020import org.apache.jena.rdf.model.Model;
021import org.fcrepo.kernel.api.RdfStream;
022import org.fcrepo.kernel.api.Transaction;
023import org.fcrepo.kernel.api.exception.PathNotFoundException;
024import org.fcrepo.kernel.api.exception.PathNotFoundRuntimeException;
025import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
026import org.fcrepo.kernel.api.identifiers.FedoraId;
027import org.fcrepo.kernel.api.models.ResourceFactory;
028import org.fcrepo.kernel.api.models.WebacAcl;
029import org.fcrepo.kernel.api.operations.RdfSourceOperation;
030import org.fcrepo.kernel.api.operations.RdfSourceOperationFactory;
031import org.fcrepo.kernel.api.services.WebacAclService;
032import org.fcrepo.kernel.impl.models.WebacAclImpl;
033import org.fcrepo.persistence.api.PersistentStorageSession;
034import org.fcrepo.persistence.api.PersistentStorageSessionManager;
035import org.fcrepo.persistence.api.exceptions.PersistentStorageException;
036import org.springframework.stereotype.Component;
037
038import javax.inject.Inject;
039
040import static org.fcrepo.kernel.api.RdfLexicon.FEDORA_WEBAC_ACL_URI;
041import static org.fcrepo.kernel.api.rdf.DefaultRdfStream.fromModel;
042
043/**
044 * Implementation of {@link WebacAclService}
045 *
046 * @author dbernstein
047 */
048@Component
049public class WebacAclServiceImpl extends AbstractService implements WebacAclService {
050
051    @Inject
052    private PersistentStorageSessionManager psManager;
053
054    @Inject
055    private ResourceFactory resourceFactory;
056
057    @Inject
058    private RdfSourceOperationFactory rdfSourceOperationFactory;
059
060    @Override
061    public WebacAcl find(final Transaction transaction, final FedoraId fedoraId) {
062        try {
063            return resourceFactory.getResource(transaction, fedoraId, WebacAclImpl.class);
064        } catch (final PathNotFoundException exc) {
065            throw new PathNotFoundRuntimeException(exc.getMessage(), exc);
066        }
067    }
068
069    @Override
070    public void create(final Transaction transaction, final FedoraId fedoraId, final String userPrincipal,
071                                 final Model model) {
072        final PersistentStorageSession pSession = this.psManager.getSession(transaction.getId());
073
074        ensureValidACLAuthorization(model);
075
076        final RdfStream stream = fromModel(model.getResource(fedoraId.getFullId()).asNode(), model);
077
078        final RdfSourceOperation createOp = rdfSourceOperationFactory
079                .createBuilder(fedoraId, FEDORA_WEBAC_ACL_URI, fedoraPropsConfig.getServerManagedPropsMode())
080                .parentId(fedoraId.asBaseId())
081                .triples(stream)
082                .relaxedProperties(model)
083                .userPrincipal(userPrincipal)
084                .build();
085
086        lockArchivalGroupResourceFromParent(transaction, pSession, fedoraId.asBaseId());
087        transaction.lockResource(fedoraId);
088
089        try {
090            pSession.persist(createOp);
091            recordEvent(transaction.getId(), fedoraId, createOp);
092        } catch (final PersistentStorageException exc) {
093            throw new RepositoryRuntimeException(String.format("failed to create resource %s", fedoraId), exc);
094        }
095    }
096
097}