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.utils;
019
020import static org.slf4j.LoggerFactory.getLogger;
021
022import java.io.InputStream;
023import java.net.MalformedURLException;
024import java.net.URI;
025
026import javax.jcr.Property;
027import javax.jcr.RepositoryException;
028import org.slf4j.Logger;
029
030import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
031
032/**
033 * Cache entry that wraps a binary stream for External Resource
034 *
035 * @author lsitu
036 * @since 2017-09-18
037 */
038public class ExternalResourceCacheEntry extends BinaryCacheEntry {
039    private static final Logger LOGGER = getLogger(ExternalResourceCacheEntry.class);
040    /**
041     * Create a new ExternalResourceCacheEntry
042     * @param property the given property
043     */
044    public ExternalResourceCacheEntry(final Property property) {
045        super(property);
046    }
047
048    /*
049     * (non-Javadoc)
050     * @see org.fcrepo.kernel.api.utils.CacheEntry#getInputStream()
051     */
052    @Override
053    public InputStream getInputStream() {
054        try {
055            LOGGER.debug("getInputStream getExternalIdentifier: {} {} ", property().getName(), getExternalIdentifier());
056            return URI.create(getExternalIdentifier()).toURL().openStream();
057        } catch (final MalformedURLException e) {
058            throw new RepositoryRuntimeException("Malformed URL: " + getExternalIdentifier(), e);
059        } catch (final Exception e) {
060            throw new RepositoryRuntimeException(e);
061        }
062    }
063
064    /*
065     * (non-Javadoc)
066     * @see org.fcrepo.kernel.api.utils.CacheEntry#getExternalIdentifier()
067     */
068    @Override
069    public String getExternalIdentifier() {
070        try {
071            LOGGER.debug("getExternalIdentifier for property {} ", property().getName());
072            return property().getValue().getString();
073        } catch (final RepositoryException e) {
074            throw new RepositoryRuntimeException(e);
075        }
076    }
077}