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.api.models;
019
020import org.apache.jena.rdf.model.Resource;
021import org.fcrepo.kernel.api.exception.InvalidChecksumException;
022import org.fcrepo.kernel.api.exception.UnsupportedAlgorithmException;
023import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
024import org.fcrepo.kernel.api.RdfStream;
025import org.fcrepo.kernel.api.services.policy.StoragePolicyDecisionPoint;
026
027import java.io.InputStream;
028import java.net.URI;
029import java.util.Collection;
030
031/**
032 * @author cabeer
033 * @since 9/19/14
034 */
035public interface FedoraBinary extends FedoraResource {
036
037    /**
038     * @return The InputStream of content associated with this datastream.
039     */
040    InputStream getContent();
041
042    /**
043     * Sets the content of this Datastream.
044     *
045     * @param content  InputStream of binary content to be stored
046     * @param contentType MIME type of content (optional)
047     * @param checksums Collection of checksum URIs of the content (optional)
048     * @param originalFileName Original file name of the content (optional)
049     * @param storagePolicyDecisionPoint Policy decision point for storing the content (optional)
050     * @throws InvalidChecksumException if invalid checksum exception occurred
051     */
052    void setContent(InputStream content, String contentType, Collection<URI> checksums,
053                    String originalFileName,
054                    StoragePolicyDecisionPoint storagePolicyDecisionPoint)
055            throws InvalidChecksumException;
056
057    /**
058     * Sets the external content reference for this datastream
059     *
060     * @param contentType MIME type of content (optional)
061     * @param checksums Collection of checksum URIs of the content (optional)
062     * @param originalFileName Original file name of the content (optional)
063     * @param externalHandling What type of handling the external resource needs (proxy or redirect)
064     * @param externalUrl Url for the external resourcej
065     * @throws InvalidChecksumException if invalid checksum exception occurred
066     */
067    void setExternalContent(String contentType, Collection<URI> checksums,
068                    String originalFileName, String externalHandling, String externalUrl)
069            throws InvalidChecksumException;
070    /**
071     * @return The size in bytes of content associated with this datastream.
072     */
073    long getContentSize();
074
075    /**
076     * Get the pre-calculated content digest for the binary payload
077     * @return a URI with the format algorithm:value
078     */
079    URI getContentDigest();
080
081    /**
082     * @return Whether or not this binary is a proxy to another resource
083     */
084    Boolean isProxy();
085
086    /**
087     * @return Whether or not this binary is a redirect to another resource
088     */
089    Boolean isRedirect();
090
091    /**
092     * Get the URL that this resource is a Proxy for
093     * @return String containing URL of object to proxy
094     */
095    String getProxyURL();
096
097    /**
098     * Set the URL that this resource is a proxy for
099     * @param url - the url of the resource this is a proxy for
100     */
101    void setProxyURL(String url);
102
103    /**
104     * Get the URL this resource should redirect to
105     * @return String containing URL of object to redirect to
106     */
107    String getRedirectURL();
108
109    /**
110     * Get URL as a URI
111     * 
112     * @return URI containing the object to redirect to.
113     */
114    default URI getRedirectURI() {
115        return URI.create(getRedirectURL());
116    }
117
118    /**
119     * Set the URL that this is a redirect to
120     * 
121     * @param url - the url of the resource this redirects to
122     */
123    void setRedirectURL(String url);
124
125    /**
126     * @return The MimeType of content associated with this datastream.
127     */
128    String getMimeType();
129
130    /**
131     * Return the file name for the binary content
132     * @return original file name for the binary content, or the object's id.
133     */
134    String getFilename();
135
136    /**
137     * Get the fixity of this datastream compared to metadata stored in the repository
138     * @param idTranslator the id translator
139     * @return the fixity of this datastream compared to metadata stored in the repository
140     */
141    RdfStream getFixity(IdentifierConverter<Resource, FedoraResource> idTranslator);
142
143    /**
144     * Get the fixity of this datastream in a given repository's binary store.
145     * @param idTranslator the id translator
146     * @param contentDigest the checksum to compare against
147     * @param size the expected size of the binary
148     * @return the fixity of the datastream
149     */
150    RdfStream getFixity(IdentifierConverter<Resource, FedoraResource> idTranslator,
151                        URI contentDigest, long size);
152
153
154    /**
155     * Digest this datastream with the digest algorithms provided
156     * @param idTranslator the id translator
157     * @param algorithms the digest algorithms to be used
158     * @return the checksums of this datastream
159     * @throws org.fcrepo.kernel.api.exception.UnsupportedAlgorithmException if unsupported digest algorithm occurred
160     */
161    Collection<URI> checkFixity(IdentifierConverter<Resource, FedoraResource> idTranslator,
162            Collection<String> algorithms) throws UnsupportedAlgorithmException;
163}