Skip to content
This repository was archived by the owner on Dec 31, 2024. It is now read-only.

Add ManyFormDatasWriter #92

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
language: java

jdk:
- oraclejdk8
- openjdk8

install:
./mvnw --settings .settings.xml install -DskipTests=true -Dmaven.javadoc.skip=true -Dgpg.skip -B -V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@
import feign.codec.Encoder;
import feign.form.multipart.ByteArrayWriter;
import feign.form.multipart.DelegateWriter;
import feign.form.multipart.FormDataWriter;
import feign.form.multipart.ManyFilesWriter;
import feign.form.multipart.ManyFormDatasWriter;
import feign.form.multipart.ManyParametersWriter;
import feign.form.multipart.Output;
import feign.form.multipart.PojoWriter;
import feign.form.multipart.SingleFileWriter;
import feign.form.multipart.SingleFormDataWriter;
import feign.form.multipart.SingleParameterWriter;
import feign.form.multipart.Writer;

Expand All @@ -64,7 +65,8 @@ public class MultipartFormContentProcessor implements ContentProcessor {
public MultipartFormContentProcessor (Encoder delegate) {
writers = new LinkedList<Writer>();
addWriter(new ByteArrayWriter());
addWriter(new FormDataWriter());
addWriter(new SingleFormDataWriter());
addWriter(new ManyFormDatasWriter());
addWriter(new SingleFileWriter());
addWriter(new ManyFilesWriter());
addWriter(new SingleParameterWriter());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package feign.form.multipart;

import static lombok.AccessLevel.PRIVATE;

import feign.codec.EncodeException;

import feign.form.FormData;
import lombok.experimental.FieldDefaults;
import lombok.val;

/**
*
* @author Sébastien Etronnier
*/
@FieldDefaults(level = PRIVATE, makeFinal = true)
public class ManyFormDatasWriter extends AbstractWriter {

SingleFormDataWriter formDataWriter = new SingleFormDataWriter();

@Override
public boolean isApplicable (Object value) {
if (value instanceof FormData[]) {
return true;
}
if (!(value instanceof Iterable)) {
return false;
}
val iterable = (Iterable<?>) value;
val iterator = iterable.iterator();
return iterator.hasNext() && iterator.next() instanceof FormData;
}

@Override
public void write (Output output, String boundary, String key, Object value) throws EncodeException {
if (value instanceof FormData[]) {
val formDatas = (FormData[]) value;
for (val formData : formDatas) {
formDataWriter.write(output, boundary, key, formData);
}
} else if (value instanceof Iterable) {
val iterable = (Iterable<?>) value;
for (val formData : iterable) {
formDataWriter.write(output, boundary, key, formData);
}
} else {
throw new IllegalArgumentException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package feign.form.multipart;

import feign.codec.EncodeException;
import feign.form.FormData;

import lombok.val;

/**
*
* @author Guillaume Simard
* @since 24.03.2018
*/
public class SingleFormDataWriter extends AbstractWriter {

@Override
public boolean isApplicable (Object value) {
return value instanceof FormData;
}

@Override
protected void write (Output output, String key, Object value) throws EncodeException {
val formData = (FormData) value;
writeFileMetadata(output, key, formData.getFileName(), formData.getContentType());
output.write(formData.getData());
}
}