From 00211edb1e1e5c3f35ccf7465b67bc09495e09dc Mon Sep 17 00:00:00 2001 From: Artem Labazin Date: Sun, 18 Sep 2016 20:27:43 +0300 Subject: [PATCH] - Added javadoc for public and protected methods; - Refactored pom.xml files for 2 spaces tabs; - Added gitignore rules for Netbeans IDE and test's output files. https://github.com/OpenFeign/feign-form/issues/5 --- .gitignore | 13 +++++ feign-form-spring/pom.xml | 1 + .../java/feign/form/FormDataProcessor.java | 13 +++++ .../feign/form/FormEncodedDataProcessor.java | 2 + .../src/main/java/feign/form/FormEncoder.java | 48 +++++++++++++++++-- .../form/MultipartEncodedDataProcessor.java | 38 ++++++++++----- pom.xml | 9 ++-- 7 files changed, 104 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 0171a67e7d..35386e86eb 100644 --- a/.gitignore +++ b/.gitignore @@ -97,3 +97,16 @@ buildNumber.properties *.ipr *.iws out/ + +# Netbeans +nbproject/private/ +build/ +nbbuild/ +dist/ +nbdist/ +nbactions.xml +nb-configuration.xml +.nb-gradle/ + +# Test output log file +*.txt diff --git a/feign-form-spring/pom.xml b/feign-form-spring/pom.xml index c2ce645d4d..2d062834fc 100644 --- a/feign-form-spring/pom.xml +++ b/feign-form-spring/pom.xml @@ -26,6 +26,7 @@ spring-web compile + org.springframework.cloud spring-cloud-starter-feign diff --git a/feign-form/src/main/java/feign/form/FormDataProcessor.java b/feign-form/src/main/java/feign/form/FormDataProcessor.java index ea404150f9..b0465c4ab4 100644 --- a/feign-form/src/main/java/feign/form/FormDataProcessor.java +++ b/feign-form/src/main/java/feign/form/FormDataProcessor.java @@ -19,12 +19,25 @@ import java.util.Map; /** + * Interface for form data processing. + * * @author Artem Labazin * @since 30.04.2016 */ public interface FormDataProcessor { + /** + * Processing form data to request body. + * + * @param data form data, where key is a parameter name and value is...a value. + * @param template current request object. + */ void process (Map data, RequestTemplate template); + /** + * Returns {@code FormDataProcessor} implementation supporting Content-Type. + * + * @return supported MIME Content-Type + */ String getSupportetContentType (); } diff --git a/feign-form/src/main/java/feign/form/FormEncodedDataProcessor.java b/feign-form/src/main/java/feign/form/FormEncodedDataProcessor.java index 7e397a5c80..e54f3eb0ef 100644 --- a/feign-form/src/main/java/feign/form/FormEncodedDataProcessor.java +++ b/feign-form/src/main/java/feign/form/FormEncodedDataProcessor.java @@ -24,6 +24,8 @@ import lombok.val; /** + * Form urlencoded implementation of {@link feign.form.FormDataProcessor}. + * * @author Artem Labazin * @since 30.04.2016 */ diff --git a/feign-form/src/main/java/feign/form/FormEncoder.java b/feign-form/src/main/java/feign/form/FormEncoder.java index a8056b65bd..193f3165e5 100644 --- a/feign-form/src/main/java/feign/form/FormEncoder.java +++ b/feign-form/src/main/java/feign/form/FormEncoder.java @@ -15,16 +15,48 @@ */ package feign.form; +import feign.RequestTemplate; +import feign.codec.Encoder; import java.lang.reflect.Type; import java.util.Collection; import java.util.HashMap; import java.util.Map; - -import feign.RequestTemplate; -import feign.codec.Encoder; import lombok.val; /** + * Properly encodes requests with application/x-www-form-urlencoded and multipart/form-data Content-Type. + *

+ * Also, the encoder has a delegate field for encoding non-form requests (like JSON or other). + *

+ * Default delegate object is {@link feign.codec.Encoder.Default} instance. + *

+ * Usage example: + *

+ * Declaring API interface: + *

+ * interface SomeApi {
+ *
+ *     @RequestLine("POST /json")
+ *     @Headers("Content-Type: application/json")
+ *     void json (Dto dto);
+ *
+ *     @RequestLine("POST /form")
+ *     @Headers("Content-Type: application/x-www-form-urlencoded")
+ *     void from (@Param("field1") String field1, @Param("field2") String field2);
+ *
+ * }
+ * 
+ *

+ * Creating Feign client instance: + *

+ * SomeApi api = Feign.builder()
+ *       .encoder(new FormEncoder(new JacksonEncoder()))
+ *       .target(SomeApi.class, "http://localhost:8080");
+ * 
+ *

+ * Now it can handle JSON Content-Type by {@code feign.jackson.JacksonEncoder} and + * form request by {@link feign.form.FormEncoder}. + * * @author Artem Labazin * @since 30.04.2016 */ @@ -34,10 +66,20 @@ public class FormEncoder implements Encoder { private final Map processors; + /** + * Default {@code FormEncoder} constructor. + *

+ * Sets {@link feign.codec.Encoder.Default} instance as delegate encoder. + */ public FormEncoder () { this(new Encoder.Default()); } + /** + * {@code FormEncoder} constructor with delegate encoder argument. + *

+ * @param delegate delegate encoder for processing non-form requests. + */ public FormEncoder (Encoder delegate) { this.deligate = delegate; processors = new HashMap(2, 1.F); diff --git a/feign-form/src/main/java/feign/form/MultipartEncodedDataProcessor.java b/feign-form/src/main/java/feign/form/MultipartEncodedDataProcessor.java index d46b9a288b..e4f645da72 100644 --- a/feign-form/src/main/java/feign/form/MultipartEncodedDataProcessor.java +++ b/feign-form/src/main/java/feign/form/MultipartEncodedDataProcessor.java @@ -17,6 +17,7 @@ import static feign.Util.UTF_8; +import feign.RequestTemplate; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; @@ -26,12 +27,12 @@ import java.io.PrintWriter; import java.net.URLConnection; import java.util.Map; - -import feign.RequestTemplate; import lombok.SneakyThrows; import lombok.val; /** + * Multipart form data implementation of {@link feign.form.FormDataProcessor}. + * * @author Artem Labazin * @since 30.04.2016 */ @@ -89,20 +90,23 @@ public String getSupportetContentType () { return CONTENT_TYPE; } - private String createBoundary () { - return Long.toHexString(System.currentTimeMillis()); - } - + /** + * Checks is passed object a supported file's type or not. + * + * @param value form file parameter. + */ protected boolean isFile (Object value) { return value != null && (value instanceof File || value instanceof byte[]); } - private void writeParameter (PrintWriter writer, String name, String value) { - writer.append("Content-Disposition: form-data; name=\"" + name + "\"").append(CRLF); - writer.append("Content-Type: text/plain; charset=UTF-8").append(CRLF); - writer.append(CRLF).append(value); - } - + /** + * Writes file's content to output stream. + * + * @param output output stream to remote destination. + * @param writer wrapped output stream. + * @param name file's name. + * @param value file's content. + */ protected void writeFile (OutputStream output, PrintWriter writer, String name, Object value) { if (value instanceof byte[]) { writeFile(output, writer, name, (byte[]) value); @@ -111,6 +115,16 @@ protected void writeFile (OutputStream output, PrintWriter writer, String name, writeFile(output, writer, name, (File) value); } + private String createBoundary () { + return Long.toHexString(System.currentTimeMillis()); + } + + private void writeParameter (PrintWriter writer, String name, String value) { + writer.append("Content-Disposition: form-data; name=\"" + name + "\"").append(CRLF); + writer.append("Content-Type: text/plain; charset=UTF-8").append(CRLF); + writer.append(CRLF).append(value); + } + @SneakyThrows private void writeFile (OutputStream output, PrintWriter writer, String name, File file) { writeFileMeta(writer, name, file.getName()); diff --git a/pom.xml b/pom.xml index d1d7b26cfc..0a58e4a09c 100644 --- a/pom.xml +++ b/pom.xml @@ -67,15 +67,14 @@ - com.netflix.feign + io.github.openfeign feign-core - 8.18.0 + 9.3.1 provided org.projectlombok lombok - 1.16.10 compile @@ -84,9 +83,9 @@ test - com.netflix.feign + io.github.openfeign feign-jackson - 8.18.0 + 9.3.1 test