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)); } }
/** * 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()); }
/** * 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(); }
/** * 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()); }
setFilename(filename); setStreamEntity(streamEntity, mediaType); } else { setStreamEntity(streamEntity, getDefaultMediaType());
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()); }
/** * 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());
/** * Adds an attachment from a {@link InputStream}. * * @param is an stream to read the attachment * @return this builder */ public MultipartBuilder attachment(InputStream is) { return bodyPart(new StreamDataBodyPart(ATTACHMENT_NAME, is)); }
/** * 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()); }
/** * 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(); }
/** * 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());
/** * Adds an attachment directly by content. * * @param content the content of the attachment * @param filename the filename of the attachment * @return this builder */ public MultipartBuilder attachment(String content, String filename) { ByteArrayInputStream is = new ByteArrayInputStream(content.getBytes()); return bodyPart(new StreamDataBodyPart(ATTACHMENT_NAME, is, filename)); }
/** * 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()); }
/** * 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(); }
/** * 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());
/** * Adds a named attachment. * * @param is an stream to read the attachment * @param filename the filename to give to the attachment * @return this builder */ public MultipartBuilder attachment(InputStream is, String filename) { return bodyPart(new StreamDataBodyPart(ATTACHMENT_NAME, is, filename)); }