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.kernel.impl;
017
018import static org.fcrepo.kernel.impl.utils.FedoraTypesUtils.isNonRdfSourceDescription;
019import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
020import static org.slf4j.LoggerFactory.getLogger;
021
022import org.fcrepo.kernel.exception.RepositoryRuntimeException;
023
024import javax.jcr.Node;
025import javax.jcr.RepositoryException;
026
027import org.fcrepo.kernel.models.NonRdfSource;
028import org.fcrepo.kernel.models.NonRdfSourceDescription;
029import org.slf4j.Logger;
030
031/**
032 * Abstraction for a Fedora datastream backed by a JCR node.
033 *
034 * @author ajs6f
035 * @since Feb 21, 2013
036 */
037public class NonRdfSourceDescriptionImpl extends FedoraResourceImpl implements NonRdfSourceDescription {
038
039    private static final Logger LOGGER = getLogger(NonRdfSourceDescriptionImpl.class);
040
041    /**
042     * The JCR node for this datastream
043     *
044     * @param n an existing {@link Node}
045     */
046    public NonRdfSourceDescriptionImpl(final Node n) {
047        super(n);
048    }
049
050    @Override
051    public NonRdfSource getDescribedResource() {
052        return new FedoraBinaryImpl(getContentNode());
053    }
054
055    private Node getContentNode() {
056        LOGGER.trace("Retrieved datastream content node.");
057        try {
058            return node.getNode(JCR_CONTENT);
059        } catch (final RepositoryException e) {
060            throw new RepositoryRuntimeException(e);
061        }
062    }
063
064    /**
065     * Check if the node has a fedora:datastream mixin
066     *
067     * @param node node to check
068     * @return whether the node has a fedora:datastream mixin
069     */
070    public static boolean hasMixin(final Node node) {
071        return isNonRdfSourceDescription.apply(node);
072    }
073
074}