Skip to content

Commit

Permalink
- Added javadoc for public and protected methods;
Browse files Browse the repository at this point in the history
- Refactored pom.xml files for 2 spaces tabs;
- Added gitignore rules for Netbeans IDE and test's output files.

OpenFeign/feign-form#5
  • Loading branch information
xxlabaza committed Sep 18, 2016
1 parent 8252ad6 commit 00211ed
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 20 deletions.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions feign-form-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<artifactId>spring-web</artifactId>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
Expand Down
13 changes: 13 additions & 0 deletions feign-form/src/main/java/feign/form/FormDataProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,25 @@
import java.util.Map;

/**
* Interface for form data processing.
*
* @author Artem Labazin <xxlabaza@gmail.com>
* @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<String, Object> data, RequestTemplate template);

/**
* Returns {@code FormDataProcessor} implementation supporting Content-Type.
*
* @return supported MIME Content-Type
*/
String getSupportetContentType ();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import lombok.val;

/**
* Form urlencoded implementation of {@link feign.form.FormDataProcessor}.
*
* @author Artem Labazin <xxlabaza@gmail.com>
* @since 30.04.2016
*/
Expand Down
48 changes: 45 additions & 3 deletions feign-form/src/main/java/feign/form/FormEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <b>application/x-www-form-urlencoded</b> and <b>multipart/form-data</b> Content-Type.
* <p>
* Also, the encoder has a <b>delegate</b> field for encoding non-form requests (like JSON or other).
* <p>
* Default <b>delegate</b> object is {@link feign.codec.Encoder.Default} instance.
* <p>
* Usage example:
* <p>
* <b>Declaring API interface:</b>
* <pre>
* interface SomeApi {
*
* &#064;RequestLine("POST /json")
* &#064;Headers("Content-Type: application/json")
* void json (Dto dto);
*
* &#064;RequestLine("POST /form")
* &#064;Headers("Content-Type: application/x-www-form-urlencoded")
* void from (@Param("field1") String field1, @Param("field2") String field2);
*
* }
* </pre>
* <p>
* <b>Creating Feign client instance:</b>
* <pre>
* SomeApi api = Feign.builder()
* .encoder(new FormEncoder(new JacksonEncoder()))
* .target(SomeApi.class, "http://localhost:8080");
* </pre>
* <p>
* Now it can handle JSON Content-Type by {@code feign.jackson.JacksonEncoder} and
* form request by {@link feign.form.FormEncoder}.
*
* @author Artem Labazin <xxlabaza@gmail.com>
* @since 30.04.2016
*/
Expand All @@ -34,10 +66,20 @@ public class FormEncoder implements Encoder {

private final Map<String, FormDataProcessor> processors;

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

/**
* {@code FormEncoder} constructor with delegate encoder argument.
* <p>
* @param delegate delegate encoder for processing non-form requests.
*/
public FormEncoder (Encoder delegate) {
this.deligate = delegate;
processors = new HashMap<String, FormDataProcessor>(2, 1.F);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 <xxlabaza@gmail.com>
* @since 30.04.2016
*/
Expand Down Expand Up @@ -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);
Expand All @@ -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());
Expand Down
9 changes: 4 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,14 @@

<dependencies>
<dependency>
<groupId>com.netflix.feign</groupId>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
<version>8.18.0</version>
<version>9.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand All @@ -84,9 +83,9 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.netflix.feign</groupId>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-jackson</artifactId>
<version>8.18.0</version>
<version>9.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down

0 comments on commit 00211ed

Please sign in to comment.