001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.api;
020
021import java.io.File;
022import java.io.IOException;
023import java.io.InputStream;
024
025import org.nuxeo.ecm.core.api.impl.blob.ByteArrayBlob;
026import org.nuxeo.ecm.core.api.impl.blob.FileBlob;
027import org.nuxeo.ecm.core.api.impl.blob.JSONBlob;
028import org.nuxeo.ecm.core.api.impl.blob.StringBlob;
029
030import com.fasterxml.jackson.databind.ObjectMapper;
031
032/**
033 * This class consists exclusively of static methods that operate on {@link Blob}s.
034 */
035public class Blobs {
036
037    private Blobs() {
038    }
039
040    /**
041     * Creates a {@link Blob} backed by the given {@link File}.
042     *
043     * @param file file
044     */
045    public static Blob createBlob(File file) throws IOException {
046        return new FileBlob(file, null, null);
047    }
048
049    /**
050     * Creates a {@link Blob} backed by the given {@link File}.
051     *
052     * @param file file
053     * @param mimeType the MIME type
054     */
055    public static Blob createBlob(File file, String mimeType) throws IOException {
056        return new FileBlob(file, mimeType, null);
057    }
058
059    /**
060     * Creates a {@link Blob} backed by the given {@link File}.
061     *
062     * @param file file
063     * @param mimeType the MIME type
064     * @param encoding the encoding
065     */
066    public static Blob createBlob(File file, String mimeType, String encoding) throws IOException {
067        return new FileBlob(file, mimeType, encoding);
068    }
069
070    /**
071     * Creates a {@link Blob} backed by the given {@link File}.
072     *
073     * @param file file
074     * @param mimeType the MIME type
075     * @param encoding the encoding
076     * @param filename the blob filename
077     */
078    public static Blob createBlob(File file, String mimeType, String encoding, String filename) throws IOException {
079        return new FileBlob(file, mimeType, encoding, filename, null);
080    }
081
082    /**
083     * Creates a {@link Blob} backed by an empty temporary {@link File} with the given extension.
084     *
085     * @param ext the extension
086     */
087    public static Blob createBlobWithExtension(String ext) throws IOException {
088        return new FileBlob(ext);
089    }
090
091    /**
092     * Creates a {@link Blob} from an {@link InputStream}, by saving it to a temporary file.
093     * <p>
094     * The input stream is closed.
095     *
096     * @param in the input stream, which is closed after use
097     */
098    public static Blob createBlob(InputStream in) throws IOException {
099        return new FileBlob(in, null, null, null);
100    }
101
102    /**
103     * Creates a {@link Blob} from an {@link InputStream}, by saving it to a temporary file.
104     * <p>
105     * The input stream is closed.
106     *
107     * @param in the input stream, which is closed after use
108     * @param mimeType the MIME type
109     */
110    public static Blob createBlob(InputStream in, String mimeType) throws IOException {
111        return new FileBlob(in, mimeType, null, null);
112    }
113
114    /**
115     * Creates a {@link Blob} from an {@link InputStream}, by saving it to a temporary file.
116     * <p>
117     * The input stream is closed.
118     *
119     * @param in the input stream, which is closed after use
120     * @param mimeType the MIME type
121     * @param encoding the encoding
122     */
123    public static Blob createBlob(InputStream in, String mimeType, String encoding) throws IOException {
124        return new FileBlob(in, mimeType, encoding, null);
125    }
126
127    /**
128     * Creates a {@link Blob} from an {@link InputStream}, by saving it to a temporary file.
129     * <p>
130     * The input stream is closed.
131     *
132     * @param in the input stream, which is closed after use
133     * @param mimeType the MIME type
134     * @param encoding the encoding
135     * @param tmpDir the temporary directory for file creation
136     */
137    public static Blob createBlob(InputStream in, String mimeType, String encoding, File tmpDir) throws IOException {
138        return new FileBlob(in, mimeType, encoding, tmpDir);
139    }
140
141    /**
142     * Creates a {@link Blob} backed by the given bytes array.
143     *
144     * @param bytes the bytes array
145     */
146    public static Blob createBlob(byte[] bytes) {
147        return new ByteArrayBlob(bytes, null, null);
148    }
149
150    /**
151     * Creates a {@link Blob} backed by the given bytes array.
152     *
153     * @param bytes the bytes array
154     * @param mimeType the MIME type
155     */
156    public static Blob createBlob(byte[] bytes, String mimeType) throws IOException {
157        return new ByteArrayBlob(bytes, mimeType, null);
158    }
159
160    /**
161     * Creates a {@link Blob} backed by the given bytes array.
162     *
163     * @param bytes the bytes array
164     * @param mimeType the MIME type
165     * @param encoding the encoding
166     */
167    public static Blob createBlob(byte[] bytes, String mimeType, String encoding) throws IOException {
168        return new ByteArrayBlob(bytes, mimeType, encoding);
169    }
170
171    /**
172     * Creates a {@link Blob} backed by the given string, with MIME type "text/plain" and encoding "UTF-8".
173     *
174     * @param string the string
175     */
176    public static Blob createBlob(String string) {
177        return new StringBlob(string); // "text/plain", "UTF-8"
178    }
179
180    /**
181     * Creates a {@link Blob} backed by the given string, with encoding "UTF-8".
182     *
183     * @param string the string
184     * @param mimeType the MIME type
185     */
186    public static Blob createBlob(String string, String mimeType) {
187        return new StringBlob(string, mimeType); // "UTF-8"
188    }
189
190    /**
191     * Creates a {@link Blob} backed by the given string.
192     *
193     * @param string the string
194     * @param mimeType the MIME type
195     * @param encoding the encoding
196     */
197    public static Blob createBlob(String string, String mimeType, String encoding) {
198        return new StringBlob(string, mimeType, encoding);
199    }
200
201    /**
202     * Creates a {@link Blob} backed by the given string.
203     *
204     * @param string the string
205     * @param mimeType the MIME type
206     * @param encoding the encoding
207     * @param filename the blob filename
208     */
209    public static Blob createBlob(String string, String mimeType, String encoding, String filename) {
210        return new StringBlob(string, mimeType, encoding, filename);
211    }
212
213    /**
214     * Create a {@link Blob} backed by the given JSON string.
215     *
216     * @param json the JSON string
217     * @since 9.2
218     */
219    public static Blob createJSONBlob(String json) {
220        return new JSONBlob(json);
221    }
222
223    /**
224     * Create a {@link Blob} backed by the JSON for an arbitrary value.
225     * <p>
226     * The value's internal classes may be annotated with Jackson 2 annotations.
227     *
228     * @param value the value
229     * @since 9.2
230     */
231    public static Blob createJSONBlobFromValue(Object value) throws IOException {
232        String json = new ObjectMapper().writeValueAsString(value);
233        return new JSONBlob(json);
234    }
235
236}