001// Generated by delombok at Fri Oct 11 21:56:29 CEST 2019
002/**
003 * Copyright (c) 2010-2019 Mark Allen, Norbert Bartels.
004 *
005 * Permission is hereby granted, free of charge, to any person obtaining a copy
006 * of this software and associated documentation files (the "Software"), to deal
007 * in the Software without restriction, including without limitation the rights
008 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
009 * copies of the Software, and to permit persons to whom the Software is
010 * furnished to do so, subject to the following conditions:
011 *
012 * The above copyright notice and this permission notice shall be included in
013 * all copies or substantial portions of the Software.
014 *
015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
016 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
017 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
019 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
020 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
021 * THE SOFTWARE.
022 */
023package com.restfb;
024
025import static java.lang.String.format;
026import java.io.ByteArrayInputStream;
027import java.io.IOException;
028import java.io.InputStream;
029import java.net.URLConnection;
030import com.restfb.util.ObjectUtil;
031import com.restfb.util.ReflectionUtils;
032
033/**
034 * Represents a binary file that can be uploaded to Facebook.
035 * <p>
036 * Normally this would be a photo or video.
037 * 
038 * @author <a href="http://restfb.com">Mark Allen</a>
039 * @author Marcel Stoer
040 * @since 1.6.5
041 */
042public class BinaryAttachment {
043  private static final String FIELD_NAME_CANNOT_BE_NULL = "Field name cannot be null.";
044  private String filename;
045  private byte[] data;
046  private InputStream dataStream;
047  private String contentType;
048  private String fieldName;
049
050  /**
051   * Creates a new binary attachment.
052   * 
053   * @param filename
054   *          The attachment's filename.
055   * @param data
056   *          The attachment's data.
057   * @throws IllegalArgumentException
058   *           If {@code data} is {@code null} or {@code filename} is {@code null} or blank.
059   * @deprecated use the stream-less API passing a {@code byte[]} for data
060   */
061  @Deprecated
062  protected BinaryAttachment(String filename, InputStream data) {
063    ObjectUtil.requireNotEmpty(filename, "Binary attachment filename cannot be blank.");
064    ObjectUtil.verifyParameterPresence("data", data);
065    this.filename = filename;
066    this.dataStream = data;
067  }
068
069  /**
070   * Creates a new binary attachment.
071   *
072   * @param filename
073   *          The attachment's filename.
074   * @param data
075   *          The attachment's data.
076   * @param fieldName
077   *          The field name the binary belongs to
078   * @throws IllegalArgumentException
079   *           If {@code data} is {@code null} or {@code filename} is {@code null} or blank.
080   * @deprecated use the stream-less API passing a {@code byte[]} for data
081   */
082  @Deprecated
083  protected BinaryAttachment(String fieldName, String filename, InputStream data) {
084    this(filename, data);
085    ObjectUtil.requireNotEmpty(fieldName, FIELD_NAME_CANNOT_BE_NULL);
086    this.fieldName = fieldName;
087  }
088
089  /**
090   * Creates a new binary attachment.
091   * 
092   * @param filename
093   *          The attachment's filename.
094   * @param data
095   *          The attachment's data.
096   * @param contentType
097   *          The attachment's contentType.
098   * @throws IllegalArgumentException
099   *           If {@code data} is {@code null}, {@code filename} is {@code null} or blank, or {@code contentType} is
100   *           {@code null} or blank.
101   * @deprecated use the stream-less API passing a {@code byte[]} for data
102   * @since 1.6.13
103   */
104  @Deprecated
105  protected BinaryAttachment(String filename, InputStream data, String contentType) {
106    this(filename, data);
107    ObjectUtil.requireNotEmpty(contentType, "ContentType cannot be null.");
108    this.contentType = contentType;
109  }
110
111  /**
112   * Creates a new binary attachment.
113   *
114   * @param filename
115   *          The attachment's filename.
116   * @param data
117   *          The attachment's data.
118   * @param contentType
119   *          The attachment's contentType.
120   * @param fieldName
121   *          The field name the binary belongs to
122   * @throws IllegalArgumentException
123   *           If {@code data} is {@code null}, {@code filename} is {@code null} or blank, or {@code contentType} is
124   *           {@code null} or blank.
125   * @deprecated use the stream-less API passing a {@code byte[]} for data
126   * @since 1.6.13
127   */
128  @Deprecated
129  protected BinaryAttachment(String fieldName, String filename, InputStream data, String contentType) {
130    this(filename, data, contentType);
131    ObjectUtil.requireNotEmpty(fieldName, FIELD_NAME_CANNOT_BE_NULL);
132    this.fieldName = fieldName;
133  }
134
135  /**
136   * Creates a new binary attachment.
137   *
138   * @param filename
139   *          The attachment's filename.
140   * @param data
141   *          The attachment's data.
142   * @throws IllegalArgumentException
143   *           If {@code data} is {@code null} or {@code filename} is {@code null} or blank.
144   * @since 1.6.17
145   */
146  protected BinaryAttachment(String filename, byte[] data) {
147    ObjectUtil.requireNotEmpty(filename, "Binary attachment filename cannot be blank.");
148    ObjectUtil.verifyParameterPresence("data", data);
149    this.filename = filename;
150    this.data = data;
151  }
152
153  /**
154   * Creates a new binary attachment.
155   *
156   * @param filename
157   *          The attachment's filename.
158   * @param data
159   *          The attachment's data.
160   * @param fieldName
161   *          The field name the binary belongs to
162   * @throws IllegalArgumentException
163   *           If {@code data} is {@code null} or {@code filename} is {@code null} or blank.
164   * @since 1.6.17
165   */
166  protected BinaryAttachment(String fieldName, String filename, byte[] data) {
167    this(filename, data);
168    ObjectUtil.requireNotEmpty(fieldName, FIELD_NAME_CANNOT_BE_NULL);
169    this.fieldName = fieldName;
170  }
171
172  /**
173   * Creates a new binary attachment.
174   *
175   * @param filename
176   *          The attachment's filename.
177   * @param data
178   *          The attachment's data.
179   * @param contentType
180   *          The attachment's contentType.
181   * @throws IllegalArgumentException
182   *           If {@code data} is {@code null}, {@code filename} is {@code null} or blank, or {@code contentType} is
183   *           {@code null} or blank.
184   * @since 1.6.17
185   */
186  protected BinaryAttachment(String filename, byte[] data, String contentType) {
187    this(filename, data);
188    ObjectUtil.requireNotEmpty(contentType, "ContentType cannot be null.");
189    this.contentType = contentType;
190  }
191
192  /**
193   * Creates a new binary attachment.
194   *
195   * @param filename
196   *          The attachment's filename.
197   * @param data
198   *          The attachment's data.
199   * @param contentType
200   *          The attachment's contentType.
201   * @param fieldName
202   *          The field name the binary belongs to
203   * @throws IllegalArgumentException
204   *           If {@code data} is {@code null}, {@code filename} is {@code null} or blank, or {@code contentType} is
205   *           {@code null} or blank.
206   * @since 1.6.17
207   */
208  protected BinaryAttachment(String fieldName, String filename, byte[] data, String contentType) {
209    this(filename, data, contentType);
210    ObjectUtil.requireNotEmpty(fieldName, FIELD_NAME_CANNOT_BE_NULL);
211    this.fieldName = fieldName;
212  }
213
214  /**
215   * Creates a binary attachment.
216   * 
217   * @param filename
218   *          The attachment's filename.
219   * @param data
220   *          The attachment's data.
221   * @return A binary attachment.
222   * @throws IllegalArgumentException
223   *           If {@code data} is {@code null} or {@code filename} is {@code null} or blank.
224   * @deprecated use the stream-less API passing a {@code byte[]} for data
225   */
226  @Deprecated
227  public static BinaryAttachment with(String filename, InputStream data) {
228    return new BinaryAttachment(filename, data);
229  }
230
231  /**
232   * Creates a binary attachment.
233   *
234   * @param filename
235   *          The attachment's filename.
236   * @param data
237   *          The attachment's data.
238   * @param fieldName
239   *          The field name the binary belongs to
240   * @return A binary attachment.
241   * @throws IllegalArgumentException
242   *           If {@code data} is {@code null} or {@code filename} is {@code null} or blank.
243   * @deprecated use the stream-less API passing a {@code byte[]} for data
244   */
245  @Deprecated
246  public static BinaryAttachment with(String fieldName, String filename, InputStream data) {
247    return new BinaryAttachment(fieldName, filename, data);
248  }
249
250  /**
251   * Creates a binary attachment.
252   * 
253   * @param filename
254   *          The attachment's filename.
255   * @param data
256   *          The attachment's data.
257   * @param contentType
258   *          The attachment's contentType.
259   * @return A binary attachment.
260   * @throws IllegalArgumentException
261   *           If {@code data} is {@code null} or {@code filename} is {@code null} or blank.
262   * @deprecated use the stream-less API passing a {@code byte[]} for data instead
263   */
264  @Deprecated
265  public static BinaryAttachment with(String filename, InputStream data, String contentType) {
266    return new BinaryAttachment(filename, data, contentType);
267  }
268
269  /**
270   * Creates a binary attachment.
271   *
272   * @param filename
273   *          The attachment's filename.
274   * @param data
275   *          The attachment's data.
276   * @param fieldName
277   *          The field name the binary belongs to
278   * @return A binary attachment.
279   * @throws IllegalArgumentException
280   *           If {@code data} is {@code null} or {@code filename} is {@code null} or blank.
281   * @deprecated use the stream-less API passing a {@code byte[]} for data
282   */
283  @Deprecated
284  public static BinaryAttachment with(String fieldName, String filename, InputStream data, String contentType) {
285    return new BinaryAttachment(fieldName, filename, data, contentType);
286  }
287
288  /**
289   * Creates a binary attachment.
290   *
291   * @param filename
292   *          The attachment's filename.
293   * @param data
294   *          The attachment's data.
295   * @return A binary attachment.
296   * @throws IllegalArgumentException
297   *           If {@code data} is {@code null} or {@code filename} is {@code null} or blank.
298   * @since 1.6.17
299   */
300  public static BinaryAttachment with(String filename, byte[] data) {
301    return new BinaryAttachment(filename, data);
302  }
303
304  /**
305   * Creates a binary attachment.
306   *
307   * @param filename
308   *          The attachment's filename.
309   * @param data
310   *          The attachment's data.
311   * @param fieldName
312   *          The field name the binary belongs to
313   * @return A binary attachment.
314   * @throws IllegalArgumentException
315   *           If {@code data} is {@code null} or {@code filename} is {@code null} or blank.
316   * @since 1.6.17
317   */
318  public static BinaryAttachment with(String fieldName, String filename, byte[] data) {
319    return new BinaryAttachment(fieldName, filename, data);
320  }
321
322  /**
323   * Creates a binary attachment.
324   * 
325   * @param filename
326   *          The attachment's filename.
327   * @param data
328   *          The attachment's data.
329   * @param contentType
330   *          The attachment's contentType.
331   * @return A binary attachment.
332   * @throws IllegalArgumentException
333   *           If {@code data} is {@code null} or {@code filename} is {@code null} or blank.
334   * @since 1.6.17
335   */
336  public static BinaryAttachment with(String filename, byte[] data, String contentType) {
337    return new BinaryAttachment(filename, data, contentType);
338  }
339
340  /**
341   * Creates a binary attachment.
342   *
343   * @param filename
344   *          The attachment's filename.
345   * @param data
346   *          The attachment's data.
347   * @param contentType
348   *          The attachment's contentType.
349   * @param fieldName
350   *          The field name the binary belongs to
351   * @return A binary attachment.
352   * @throws IllegalArgumentException
353   *           If {@code data} is {@code null} or {@code filename} is {@code null} or blank.
354   * @since 1.6.17
355   */
356  public static BinaryAttachment with(String fieldName, String filename, byte[] data, String contentType) {
357    return new BinaryAttachment(fieldName, filename, data, contentType);
358  }
359
360  @Override
361  public int hashCode() {
362    return ReflectionUtils.hashCode(this);
363  }
364
365  @Override
366  public boolean equals(Object that) {
367    return ReflectionUtils.equals(this, that);
368  }
369
370  @Override
371  public String toString() {
372    return format("[filename=%s]", getFilename());
373  }
374
375  /**
376   * The attachment's data.
377   * 
378   * @return The attachment's data.
379   */
380  public InputStream getData() {
381    if (data != null) {
382      return new ByteArrayInputStream(data);
383    } else if (dataStream != null) {
384      return dataStream;
385    } else {
386      throw new IllegalStateException("Either the byte[] or the stream mustn\'t be null at this point.");
387    }
388  }
389
390  /**
391   * return the given content type or try to guess from stream or file name. Depending of the available data.
392   * 
393   * @return the content type
394   */
395  public String getContentType() {
396    if (contentType != null) {
397      return contentType;
398    }
399    if (dataStream != null) {
400      try {
401        contentType = URLConnection.guessContentTypeFromStream(dataStream);
402      } catch (IOException ioe) {
403      }
404    }
405    // ignore exception
406    if (data != null) {
407      contentType = URLConnection.guessContentTypeFromName(filename);
408    }
409    // fallback - if we have no contenttype and cannot detect one, use 'application/octet-stream'
410    if (contentType == null) {
411      contentType = "application/octet-stream";
412    }
413    return contentType;
414  }
415
416  @java.lang.SuppressWarnings("all")
417  public String getFilename() {
418    return this.filename;
419  }
420
421  @java.lang.SuppressWarnings("all")
422  public String getFieldName() {
423    return this.fieldName;
424  }
425}