From 3b9983542c4fa322c781cd573574e854dd2bd9c0 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 | 57 +-- feign-form/pom.xml | 18 +- .../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 | 341 +++++++++--------- 8 files changed, 307 insertions(+), 223 deletions(-) diff --git a/.gitignore b/.gitignore index 0171a67..35386e8 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 8bd8d30..38b5a18 100644 --- a/feign-form-spring/pom.xml +++ b/feign-form-spring/pom.xml @@ -3,36 +3,37 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - 4.0.0 - jar + 4.0.0 + jar - feign-form-spring + feign-form-spring - - io.github.openfeign.form - feign-form-parent - 2.0.1 - + + io.github.openfeign.form + feign-form-parent + 2.0.1 + - Open Feign Forms Extension for Spring + Open Feign Forms Extension for Spring - - - ${project.groupId} - feign-form - ${project.version} - compile - - - org.springframework - spring-web - compile - - - org.springframework.cloud - spring-cloud-starter-feign - test - 1.1.5.RELEASE - - + + + ${project.groupId} + feign-form + ${project.version} + compile + + + org.springframework + spring-web + compile + + + + org.springframework.cloud + spring-cloud-starter-feign + test + 1.1.5.RELEASE + + \ No newline at end of file diff --git a/feign-form/pom.xml b/feign-form/pom.xml index c781a5f..07610db 100644 --- a/feign-form/pom.xml +++ b/feign-form/pom.xml @@ -3,16 +3,16 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - 4.0.0 - jar + 4.0.0 + jar - feign-form + feign-form - - io.github.openfeign.form - feign-form-parent - 2.0.1 - + + io.github.openfeign.form + feign-form-parent + 2.0.1 + - Open Feign Forms Core + Open Feign Forms Core \ No newline at end of file diff --git a/feign-form/src/main/java/feign/form/FormDataProcessor.java b/feign-form/src/main/java/feign/form/FormDataProcessor.java index ea40415..b0465c4 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 7e397a5..e54f3eb 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 a8056b6..193f316 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 d46b9a2..e4f645d 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 684fb5f..7b3f836 100644 --- a/pom.xml +++ b/pom.xml @@ -3,186 +3,185 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - 4.0.0 + 4.0.0 - io.github.openfeign.form - feign-form-parent - 2.0.1 - pom + io.github.openfeign.form + feign-form-parent + 2.0.1 + pom - - org.springframework.boot - spring-boot-starter-parent - 1.4.0.RELEASE - + + org.springframework.boot + spring-boot-starter-parent + 1.4.0.RELEASE + - Open Feign Forms Parent - - Simple encoder for Netflix Feign project, which adds forms support (urlencoded and multipart) - - https://github.com/OpenFeign/feign-form - 2016 + Open Feign Forms Parent + + Simple encoder for Netflix Feign project, which adds forms support (urlencoded and multipart) + + https://github.com/OpenFeign/feign-form + 2016 - - - Artem Labazin - xxlabaza@gmail.com - - - Tomasz Juchniewicz - tjuchniewicz@gmail.com - - + + + Artem Labazin + xxlabaza@gmail.com + + + Tomasz Juchniewicz + tjuchniewicz@gmail.com + + - - - The Apache Software License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.txt - repo - - + + + The Apache Software License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + + - - - internal.repo - Temporary Staging Repository - file://${project.build.directory}/mvn-repo - - + + + internal.repo + Temporary Staging Repository + file://${project.build.directory}/mvn-repo + + - - Github - https://github.com/OpenFeign/feign-form/issues - + + Github + https://github.com/OpenFeign/feign-form/issues + - - UTF-8 - 1.6 - 1.6 - 1.8 - 1.8 - github - + + UTF-8 + 1.6 + 1.6 + 1.8 + 1.8 + github + - - - - org.apache.maven.plugins - maven-source-plugin - 2.4 - - - attach-sources - - jar - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 2.10.4 - - -Xdoclint:none - - - - attach-javadocs - - jar - - - - - - maven-deploy-plugin - 2.8.2 - - internal.repo::default::file://${project.build.directory}/mvn-repo - - - - com.github.github - site-maven-plugin - 0.12 - - Maven artifacts for ${project.version} - true - ${project.build.directory}/mvn-repo - refs/heads/mvn-repo - - **/* - - OpenFeign - feign-form - - - - - site - - deploy - - - - - org.codehaus.mojo - animal-sniffer-maven-plugin - 1.15 - - - signature-check - verify - - check - - - - - - org.codehaus.mojo.signature - java16 - 1.0 - - - - - + + feign-form + feign-form-spring + - - - com.netflix.feign - feign-core - 8.18.0 - provided - - - org.projectlombok - lombok - 1.16.10 - compile - - - org.springframework.boot - spring-boot-starter-web - test - - - com.netflix.feign - feign-jackson - 8.18.0 - test - - - org.springframework.boot - spring-boot-starter-test - test - - + + + + org.apache.maven.plugins + maven-source-plugin + 2.4 + + + attach-sources + + jar + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.10.4 + + -Xdoclint:none + + + + attach-javadocs + + jar + + + + + + maven-deploy-plugin + 2.8.2 + + internal.repo::default::file://${project.build.directory}/mvn-repo + + + + com.github.github + site-maven-plugin + 0.12 + + Maven artifacts for ${project.version} + true + ${project.build.directory}/mvn-repo + refs/heads/mvn-repo + + **/* + + OpenFeign + feign-form + + + + + site + + deploy + + + + + org.codehaus.mojo + animal-sniffer-maven-plugin + 1.15 + + + signature-check + verify + + check + + + + + + org.codehaus.mojo.signature + java16 + 1.0 + + + + + - - feign-form - feign-form-spring - + + + io.github.openfeign + feign-core + 9.3.1 + provided + + + org.projectlombok + lombok + compile + + + org.springframework.boot + spring-boot-starter-web + test + + + io.github.openfeign + feign-jackson + 9.3.1 + test + + + org.springframework.boot + spring-boot-starter-test + test + + \ No newline at end of file