001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Florent Guillaume
016 *     Nelson Silva
017 */
018package org.nuxeo.ecm.core.blob;
019
020import java.io.IOException;
021import java.io.InputStream;
022import java.net.URI;
023import java.util.Collections;
024import java.util.List;
025import java.util.Map;
026
027import javax.servlet.http.HttpServletRequest;
028
029import org.nuxeo.ecm.core.api.Blob;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.core.blob.BlobManager.BlobInfo;
032import org.nuxeo.ecm.core.blob.BlobManager.UsageHint;
033import org.nuxeo.ecm.core.blob.apps.AppLink;
034import org.nuxeo.ecm.core.blob.binary.BinaryManager;
035import org.nuxeo.ecm.core.model.Document;
036
037/**
038 * Interface for a provider of {@link Blob}s, which knows how to read and write them.
039 *
040 * @since 7.2
041 */
042public interface BlobProvider {
043
044    /**
045     * Initializes the blob provider.
046     *
047     * @param blobProviderId the blob provider id for this binary manager
048     * @param properties initialization properties
049     *
050     * @since 7.3
051     */
052    void initialize(String blobProviderId, Map<String, String> properties) throws IOException;
053
054    /**
055     * Closes this blob provider and releases resources that may be held by it.
056     *
057     * @since 7.3
058     */
059    void close();
060
061    /**
062     * Reads a {@link Blob} from storage.
063     *
064     * @param blobInfo the blob information
065     * @return the blob
066     */
067    Blob readBlob(BlobInfo blobInfo) throws IOException;
068
069    /**
070     * Writes a {@link Blob} to storage and returns information about it.
071     * <p>
072     * Called to store a user-created blob.
073     *
074     * @param blob the blob
075     * @param doc the document to which this blob belongs
076     * @return the blob key
077     */
078    String writeBlob(Blob blob, Document doc) throws IOException;
079
080    /**
081     * Checks if write is supported
082     *
083     * @return {@code true} if write is supported
084     * @since 7.4
085     */
086    boolean supportsWrite();
087
088    /**
089     * Gets an {@link InputStream} for the data of a managed blob.
090     * <p>
091     * Like all {@link InputStream}, the result must be closed when done with it to avoid resource leaks.
092     *
093     * @param blob the managed blob
094     * @return the stream
095     * @since 7.3
096     */
097    default InputStream getStream(ManagedBlob blob) throws IOException {
098        return null;
099    }
100
101    /**
102     * Gets an {@link InputStream} for a thumbnail of a managed blob.
103     * <p>
104     * Like all {@link InputStream}, the result must be closed when done with it to avoid resource leaks.
105     *
106     * @param blob the managed blob
107     * @return the stream
108     * @since 7.3
109     */
110    default InputStream getThumbnail(ManagedBlob blob) throws IOException {
111        return null;
112    }
113
114    /**
115     * Gets an {@link URI} for the content of a managed blob.
116     *
117     * @param blob the managed blob
118     * @param hint {@link UsageHint}
119     * @param servletRequest the servlet request, or {@code null}
120     * @return the {@link URI}, or {@code null} if none available
121     * @since 7.4
122     */
123    default URI getURI(ManagedBlob blob, UsageHint hint, HttpServletRequest servletRequest) throws IOException {
124        return null;
125    }
126
127    /**
128     * Gets a map of available MIME type conversions and corresponding {@link URI} for a managed blob.
129     *
130     * @param blob the managed blob
131     * @param hint {@link UsageHint}
132     * @return a map of MIME types and {@link URI}, which may be empty
133     * @since 7.3
134     */
135    default Map<String, URI> getAvailableConversions(ManagedBlob blob, UsageHint hint) throws IOException {
136        return Collections.emptyMap();
137    }
138
139    /**
140     * Gets an {@link InputStream} for a conversion to the given MIME type.
141     * <p>
142     * Like all {@link InputStream}, the result must be closed when done with it to avoid resource leaks.
143     *
144     * @param blob the managed blob
145     * @param mimeType the MIME type to convert to
146     * @param doc the document that holds the blob
147     * @return the stream, or {@code null} if no conversion is available for the given MIME type
148     * @since 7.3
149     */
150    default InputStream getConvertedStream(ManagedBlob blob, String mimeType, DocumentModel doc) throws IOException {
151        return null;
152    }
153
154    /**
155     * Returns a new managed blob pointing to a fixed version of the original blob.
156     * <p>
157     *
158     * @param blob the original managed blob
159     * @param doc the document that holds the blob
160     * @return a managed blob with fixed version, or {@code null} if no change is needed
161     * @since 7.3
162     */
163    default ManagedBlob freezeVersion(ManagedBlob blob, Document doc) throws IOException {
164        return null;
165    }
166
167    /**
168     * Returns true if version of the blob is a version.
169     * <p>
170     *
171     * @param blob the managed blob
172     * @return true if the blob is a version or a revision
173     * @since 7.3
174     */
175    default boolean isVersion(ManagedBlob blob) {
176        return false;
177    }
178
179    /**
180     * Returns a list of application links for the given blob.
181     *
182     * @since 7.3
183     */
184    default List<AppLink> getAppLinks(String user, ManagedBlob blob) throws IOException {
185        return Collections.emptyList();
186    }
187
188    /**
189     * Gets the associated binary manager, if any.
190     *
191     * @return the binary manager, or {@code null}
192     * @since 7.4
193     */
194    default BinaryManager getBinaryManager() {
195        return null;
196    }
197
198}