Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Enum<?> as Feign client parameters for multipart payload #76

Open
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import feign.codec.Encoder;
import feign.form.multipart.ByteArrayWriter;
import feign.form.multipart.DelegateWriter;
import feign.form.multipart.EnumWriter;
import feign.form.multipart.FormDataWriter;
import feign.form.multipart.ManyFilesWriter;
import feign.form.multipart.ManyParametersWriter;
Expand Down Expand Up @@ -69,6 +70,7 @@ public MultipartFormContentProcessor (Encoder delegate) {
addWriter(new ManyFilesWriter());
addWriter(new SingleParameterWriter());
addWriter(new ManyParametersWriter());
addWriter(new EnumWriter());
addWriter(new PojoWriter(writers));

defaultPerocessor = new DelegateWriter(delegate);
Expand Down
48 changes: 48 additions & 0 deletions feign-form/src/main/java/feign/form/multipart/EnumWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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 feign.form.ContentProcessor.CRLF;

import feign.codec.EncodeException;
import lombok.val;

/**
*
* @author Andriy Redkp
*/
public class EnumWriter extends AbstractWriter {

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

@Override
protected void write (Output output, String key, Object value) throws EncodeException {
val name = ((Enum<?>) value).name();

val string = new StringBuilder()
.append("Content-Disposition: form-data; name=\"").append(key).append('"').append(CRLF)
.append("Content-Type: text/plain; charset=").append(output.getCharset().name()).append(CRLF)
.append(CRLF)
.append(name)
.toString();

output.write(string);
}
}
9 changes: 9 additions & 0 deletions feign-form/src/test/java/feign/form/BasicClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ public void testUpload () throws Exception {
val stringResponse = API.upload(path.toFile());
Assert.assertEquals(Files.size(path), Long.parseLong(stringResponse));
}

@Test
public void testUploadContent () throws Exception {
val path = Paths.get(Thread.currentThread().getContextClassLoader().getResource("file.txt").toURI());
Assert.assertTrue(Files.exists(path));

val stringResponse = API.upload(ContentType.MULTIPART, path.toFile());
Assert.assertEquals(Files.size(path), Long.parseLong(stringResponse));
}

@Test
public void testUploadWithParam () throws Exception {
Expand Down
11 changes: 11 additions & 0 deletions feign-form/src/test/java/feign/form/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ public ResponseEntity<Long> upload (@RequestParam("file") MultipartFile file) {
}
return ResponseEntity.status(status).body(file.getSize());
}

@RequestMapping(path = "/upload/content", method = POST)
public ResponseEntity<Long> upload (@RequestParam("content-type") ContentType contentType, @RequestParam("file") MultipartFile file) {
HttpStatus status;
if (contentType == null) {
status = BAD_REQUEST;
} else {
status = OK;
}
return ResponseEntity.status(status).body(file.getSize());
}

@RequestMapping(path = "/upload/files", method = POST)
public ResponseEntity<Long> upload (@RequestParam("files") MultipartFile[] files) {
Expand Down
4 changes: 4 additions & 0 deletions feign-form/src/test/java/feign/form/TestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public interface TestClient {
@Headers("Content-Type: multipart/form-data")
String upload (@Param("id") Integer id, @Param("public") Boolean isPublic, @Param("file") File file);

@RequestLine("POST /upload/content")
@Headers("Content-Type: multipart/form-data")
String upload (@Param("content-type") ContentType contentType, @Param("file") File file);

@RequestLine("POST /upload")
@Headers("Content-Type: multipart/form-data")
String upload (@Param("file") File file);
Expand Down