001/**
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.auth.roles.common;
017
018import static java.util.Collections.singletonMap;
019
020import java.util.Map;
021
022import javax.jcr.RepositoryException;
023import javax.ws.rs.core.UriInfo;
024
025import org.fcrepo.http.commons.api.rdf.UriAwareResourceModelFactory;
026import org.fcrepo.kernel.api.FedoraJcrTypes;
027import org.fcrepo.kernel.api.RdfLexicon;
028import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
029import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
030import org.fcrepo.kernel.api.models.FedoraResource;
031import org.springframework.stereotype.Component;
032
033import com.hp.hpl.jena.rdf.model.Model;
034import com.hp.hpl.jena.rdf.model.ModelFactory;
035import com.hp.hpl.jena.rdf.model.Resource;
036
037/**
038 * Exposes access roles endpoint for any resource.
039 *
040 * @author Gregory Jansen
041 */
042@Component
043public class AccessRolesResources implements UriAwareResourceModelFactory {
044
045    /*
046     * (non-Javadoc)
047     * @see org.fcrepo.http.commons.api.rdf.UriAwareResourceModelFactory#
048     * createModelForResource( org.fcrepo.kernel.api.FedoraResourceImpl,
049     * javax.ws.rs.core.UriInfo, org.fcrepo.kernel.api.rdf.IdentifierTranslator)
050     */
051    @Override
052    public Model createModelForResource(final FedoraResource resource,
053            final UriInfo uriInfo, final IdentifierConverter<Resource, FedoraResource> graphSubjects) {
054        final Model model = ModelFactory.createDefaultModel();
055
056        try {
057            final Resource s = graphSubjects.reverse().convert(resource);
058
059            if (resource.getNode().isNodeType(
060                    FedoraJcrTypes.FEDORA_RESOURCE)) {
061                final Map<String, String> pathMap =
062                        singletonMap("path", resource.getPath().substring(1));
063                final Resource acl = model.createResource(uriInfo.getBaseUriBuilder().path(
064                        AccessRoles.class).buildFromMap(pathMap).toASCIIString());
065                model.add(s, RdfLexicon.HAS_ACCESS_ROLES_SERVICE, acl);
066            }
067        } catch (final RepositoryException e) {
068            throw new RepositoryRuntimeException(e);
069        }
070        return model;
071    }
072}