diff --git a/feign-form/src/main/java/feign/form/MultipartFormContentProcessor.java b/feign-form/src/main/java/feign/form/MultipartFormContentProcessor.java index 6e2f84e..bd49e8c 100644 --- a/feign-form/src/main/java/feign/form/MultipartFormContentProcessor.java +++ b/feign-form/src/main/java/feign/form/MultipartFormContentProcessor.java @@ -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; @@ -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); diff --git a/feign-form/src/main/java/feign/form/multipart/EnumWriter.java b/feign-form/src/main/java/feign/form/multipart/EnumWriter.java new file mode 100644 index 0000000..5c998fb --- /dev/null +++ b/feign-form/src/main/java/feign/form/multipart/EnumWriter.java @@ -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); + } +} diff --git a/feign-form/src/test/java/feign/form/BasicClientTest.java b/feign-form/src/test/java/feign/form/BasicClientTest.java index daf32dd..37c97d0 100644 --- a/feign-form/src/test/java/feign/form/BasicClientTest.java +++ b/feign-form/src/test/java/feign/form/BasicClientTest.java @@ -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 { diff --git a/feign-form/src/test/java/feign/form/Server.java b/feign-form/src/test/java/feign/form/Server.java index d6c5a36..c776a0c 100644 --- a/feign-form/src/test/java/feign/form/Server.java +++ b/feign-form/src/test/java/feign/form/Server.java @@ -98,6 +98,17 @@ public ResponseEntity upload (@RequestParam("file") MultipartFile file) { } return ResponseEntity.status(status).body(file.getSize()); } + + @RequestMapping(path = "/upload/content", method = POST) + public ResponseEntity 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 upload (@RequestParam("files") MultipartFile[] files) { diff --git a/feign-form/src/test/java/feign/form/TestClient.java b/feign-form/src/test/java/feign/form/TestClient.java index a881e40..c6eefea 100644 --- a/feign-form/src/test/java/feign/form/TestClient.java +++ b/feign-form/src/test/java/feign/form/TestClient.java @@ -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);