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