/** * Predicts the media type of the provided {@link File}. * * @param file the file from which the media type is predicted. * @return predicted {@link MediaType}. */ protected MediaType predictMediaType(final File file) { return getPredictor().getMediaTypeFromFile(file); }
/** * Sets the fileEntity for this {@link FormDataBodyPart}. * * @param fileEntity the entity of this {@link FormDataBodyPart}. */ public void setFileEntity(final File fileEntity) { this.setFileEntity(fileEntity, predictMediaType()); }
/** * Predicts the media type of the current fileEntity. * * @return predicted {@link MediaType}. */ protected MediaType predictMediaType() { return predictMediaType(getFileEntity()); }
public static UploadResponse uploadFile(WebTarget target, Path file, Path tagsFile) { assertThat(Files.exists(file), is(true)); // MediaType of the body part will be derived from the file. FileDataBodyPart filePart = new FileDataBodyPart("file", file.toFile(), MediaType.APPLICATION_OCTET_STREAM_TYPE); FormDataMultiPart mp = new FormDataMultiPart(); mp.bodyPart(filePart); if (tagsFile != null) { FileDataBodyPart tagsFilePart = new FileDataBodyPart("tags", tagsFile.toFile(), MediaType.APPLICATION_OCTET_STREAM_TYPE); mp.bodyPart(tagsFilePart); } if (staticLogger.isDebugEnabled()) { staticLogger.debug("Rest response: {}", restCall(target, "/_upload", mp, String.class, debugOption)); } return restCall(target, "/_upload", mp, UploadResponse.class, Collections.emptyMap()); } }
/** * A utility method for predicting media type from a file name. If the file * name extension is not recognised it will return {@link MediaType} for * "*\/*", it will also return the same if the file is {@code null}. * Currently supported file extensions can be found at {@link CommonMediaTypes}. * * @param fileName the file name from which to predict the {@link MediaType} * @return the {@link MediaType} for the give file; {@code null} - if file * is null; "application/octet-stream" if extension not recognized. */ public static MediaType getMediaTypeFromFileName(final String fileName) { if (fileName == null) { return null; } CommonMediaTypes[] types = CommonMediaTypes.values(); if (types != null && types.length > 0) { for (CommonMediaTypes type : types) { if (fileName.toLowerCase().endsWith(type.getExtension())) { return type.getMediaType(); } } } return MediaType.APPLICATION_OCTET_STREAM_TYPE; } }
/** * Allows to explicitly set the value and the MIME type of the body part * entity. This method doesn't have to be invoked if one of the non-default * constructors was already called. * <p/> * Either this method or {@link StreamDataBodyPart#setStreamEntity(InputStream)} * <strong>must</strong> be invoked if the default constructor was called. * * @param streamEntity entity to be set as a body part. * @param mediaType MIME type of the {@code streamEntity} attachment. */ public void setStreamEntity(final InputStream streamEntity, MediaType mediaType) { if (streamEntity == null) { throw new IllegalArgumentException("Stream body part entity cannot be null."); } if (mediaType == null) { mediaType = getDefaultMediaType(); } this.streamEntity = streamEntity; // Be sure to hit the parent (non-overloaded) method. super.setMediaType(mediaType); super.setEntity(streamEntity); setFormDataContentDisposition(buildContentDisposition()); }
setFilename(filename); setStreamEntity(streamEntity, mediaType); } else { setStreamEntity(streamEntity, getDefaultMediaType());
/** * Allows to explicitly set the body part entity. This method assumes the * default {@link MediaType#APPLICATION_OCTET_STREAM} MIME type and doesn't * have to be invoked if one of the non-default constructors was already * called. * <p/> * Either this method or {@link StreamDataBodyPart#setStreamEntity(InputStream, MediaType)} * <strong>must</strong> be invoked if the default constructor was called. * * @param streamEntity entity to be set as a body part. */ public void setStreamEntity(final InputStream streamEntity) { this.setStreamEntity(streamEntity, getDefaultMediaType()); }
/** * Sets the {@link MediaType} and fileEntity for this {@link FormDataBodyPart}. * * @param fileEntity the entity of this body part. * @param mediaType the media type. */ public void setFileEntity(final File fileEntity, final MediaType mediaType) { super.setMediaType(mediaType); super.setEntity(fileEntity); this.fileEntity = fileEntity; if (fileEntity != null) { FormDataContentDisposition.FormDataContentDispositionBuilder builder = FormDataContentDisposition.name(getName()); builder.fileName(fileEntity.getName()); if (fileEntity.exists()) { builder.size(fileEntity.length()); builder.modificationDate(new Date(fileEntity.lastModified())); } setFormDataContentDisposition(builder.build()); } }
protected final Response getFormPostResponse(String data, String endpoint, Class<? extends OutputStream> compressingClass, String encoding) throws IOException { byte[] bytes; if (compressingClass == null) { bytes = data.getBytes(StandardCharsets.UTF_8); } else { bytes = compress(data, compressingClass); } MediaType type = encoding == null ? MediaType.TEXT_PLAIN_TYPE : new MediaType("application", encoding); InputStream in = new ByteArrayInputStream(bytes); StreamDataBodyPart filePart = new StreamDataBodyPart("data", in, "data", type); try (MultiPart multiPart = new MultiPart(MediaType.MULTIPART_FORM_DATA_TYPE)) { multiPart.getBodyParts().add(filePart); return target(endpoint).request().post( Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE)); } }
public MediaType getMediaTypeFromFileName(String fileName) { return CommonMediaTypes.getMediaTypeFromFileName(fileName); }
public MediaType getMediaTypeFromFile(File file) { return CommonMediaTypes.getMediaTypeFromFile(file); }
/** * Builds the body part content-disposition header which the specified * filename (or the default one if unspecified). * * @return ready to use content-disposition header. */ protected FormDataContentDisposition buildContentDisposition() { FormDataContentDisposition.FormDataContentDispositionBuilder builder = FormDataContentDisposition.name(getName()); if (filename != null) { builder.fileName(filename); } else { // Default is to set the name of the file as a form-field name. builder.fileName(getName()); } return builder.build(); }
/** * Predicts the media type of the provided {@link File}. * * @param file the file from which the media type is predicted. * @return predicted {@link MediaType}. */ protected MediaType predictMediaType(final File file) { return getPredictor().getMediaTypeFromFile(file); }
/** * Constructs the body part with all the attributes set for its proper * function. If this constructor is used to construct the body part then it * is not required to set any other attributes for proper behavior. * * @param name the name of body part. * @param fileEntity the file that represents the entity. * @param mediaType the {@link MediaType} of the body part. * @throws java.lang.IllegalArgumentException if the fileEntity is {@code null}. */ public FileDataBodyPart(final String name, final File fileEntity, final MediaType mediaType) throws IllegalArgumentException { super(); super.setName(name); if (mediaType != null) { setFileEntity(fileEntity, mediaType); } else { setFileEntity(fileEntity, predictMediaType(fileEntity)); } }
/** * Predicts the media type of the current fileEntity. * * @return predicted {@link MediaType}. */ protected MediaType predictMediaType() { return predictMediaType(getFileEntity()); }
/** * A utility method for predicting media type from a file name. * * @param file the file from which to predict the {@link MediaType} * @return the {@link MediaType} for the give file; {@code null} - if file * is null; "application/octet-stream" if extension not recognized. * * @see CommonMediaTypes#getMediaTypeFromFileName(java.lang.String) */ public static MediaType getMediaTypeFromFile(final File file) { if (file == null) { return null; } String fileName = file.getName(); return getMediaTypeFromFileName(fileName); }
/** * Predicts the media type of the provided {@link File}. * * @param file the file from which the media type is predicted. * @return predicted {@link MediaType}. */ protected MediaType predictMediaType(final File file) { return getPredictor().getMediaTypeFromFile(file); }
/** * Predicts the media type of the provided {@link File}. * * @param file the file from which the media type is predicted. * @return predicted {@link MediaType}. */ protected MediaType predictMediaType(final File file) { return getPredictor().getMediaTypeFromFile(file); }
/** * Predicts the media type of the provided {@link File}. * * @param file the file from which the media type is predicted. * @return predicted {@link MediaType}. */ protected MediaType predictMediaType(final File file) { return getPredictor().getMediaTypeFromFile(file); }