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