Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Commit

Permalink
Add Charset support to FoxHttpRequestBody (fix issue #5)
Browse files Browse the repository at this point in the history
- Add Charset support to FoxHttpRequestBody
- Add new annotation SerializeContentType which allows to set the content type in interface, method or model level
- Update unit test to check for correct encoding
  • Loading branch information
itsmefox committed Jan 3, 2018
1 parent 55bbabe commit 18562f7
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</parent>

<artifactId>foxhttp</artifactId>
<version>1.3</version>
<version>1.3.1</version>


<name>GroundWork - FoxHttp</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import ch.viascom.groundwork.foxhttp.header.FoxHttpHeader;
import ch.viascom.groundwork.foxhttp.header.HeaderEntry;
import ch.viascom.groundwork.foxhttp.query.FoxHttpRequestQuery;
import ch.viascom.groundwork.foxhttp.type.ContentType;
import ch.viascom.groundwork.foxhttp.util.NamedInputStream;

import java.io.File;
Expand Down Expand Up @@ -115,6 +116,8 @@ static FoxHttpRequestBody getFoxHttpRequestBody(Method method, Object[] args) th
foxHttpRequestBody = new RequestStringBody((String) bodyObject);
} else if (Serializable.class.isAssignableFrom(bodyClass)) {
foxHttpRequestBody = new RequestObjectBody((Serializable) bodyObject);
ContentType contentType = getRequestBodyContentType(method,bodyObject);
foxHttpRequestBody.setOutputContentType(contentType);
}
}

Expand All @@ -135,6 +138,35 @@ private static Object getRequestBody(Method method, Object[] args) {
return null;
}

private static ContentType getRequestBodyContentType(Method method, Object bodyObject) {
Charset charset = null;
String mimetype = "*/*";
SerializeContentType serializeContentType = null;

//Check Interface
if (FoxHttpAnnotationUtil.hasTypeAnnotation(SerializeContentType.class, method)) {
serializeContentType = method.getDeclaringClass().getAnnotation(SerializeContentType.class);
}

//Check Method
if (FoxHttpAnnotationUtil.hasMethodAnnotation(SerializeContentType.class, method)) {
serializeContentType = method.getAnnotation(SerializeContentType.class);
}

//Check Model
if(bodyObject.getClass().isAnnotationPresent(SerializeContentType.class)){
serializeContentType = bodyObject.getClass().getAnnotation(SerializeContentType.class);
}

if(serializeContentType != null){
charset = Charset.forName(serializeContentType.charset());
mimetype = serializeContentType.mimetype();
}


return ContentType.create(mimetype, charset);
}

@SuppressWarnings("unchecked")
private static RequestMultipartBody getRequestMultipartBody(Method method, Object[] args) throws FileNotFoundException, FoxHttpRequestException {
MultipartBody multipartBody = method.getAnnotation(MultipartBody.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ch.viascom.groundwork.foxhttp.annotation.types;

import java.lang.annotation.*;

@Documented
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface SerializeContentType {
String charset();
String mimetype();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
import ch.viascom.groundwork.foxhttp.type.ContentType;
import ch.viascom.groundwork.foxhttp.type.HeaderTypes;
import lombok.Getter;
import lombok.Setter;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

/**
* Abstract FoxHttpRequestBody
Expand All @@ -29,6 +32,7 @@
public abstract class FoxHttpRequestBody implements FoxHttpBody {
@Getter
protected ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
@Setter
ContentType outputContentType = ContentType.WILDCARD;

public abstract void setBody(FoxHttpRequestBodyContext context) throws FoxHttpRequestException;
Expand All @@ -47,9 +51,18 @@ protected void executeInterceptor(FoxHttpRequestBodyContext context) throws FoxH
public void writeBody(FoxHttpRequestBodyContext context, String json) throws FoxHttpRequestException {
try {
DataOutputStream wr = new DataOutputStream(outputStream);
wr.writeBytes(json);
wr.flush();
wr.close();

//Check for Charset and use OutputStreamWriter with the correct Charset if needed
if(outputContentType.getCharset() == null) {
wr.writeBytes(json);
wr.flush();
wr.close();
}else{
Writer osw = new OutputStreamWriter(wr, outputContentType.getCharset());
osw.write(json);
osw.flush();
osw.close();
}

//Execute interceptor
executeInterceptor(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import ch.viascom.groundwork.foxhttp.header.HeaderEntry;
import ch.viascom.groundwork.foxhttp.interfaces.FoxHttpExceptionInterfaceTest;
import ch.viascom.groundwork.foxhttp.interfaces.FoxHttpInterfaceTest;
import ch.viascom.groundwork.foxhttp.log.FoxHttpLoggerLevel;
import ch.viascom.groundwork.foxhttp.log.SystemOutFoxHttpLogger;
import ch.viascom.groundwork.foxhttp.models.GetResponse;
import ch.viascom.groundwork.foxhttp.models.PostResponse;
Expand Down Expand Up @@ -131,11 +132,11 @@ public void objectOptionalGet() throws Exception {
GetResponse getResponse = foxHttpInterfaceTest.objectOptionalGet(null);
assertThat(getResponse.getArgs().entrySet().isEmpty()).isEqualTo(true);

GetResponse getResponse2 = foxHttpInterfaceTest.objectGet(new QueryObjectModel("Fox",null));
GetResponse getResponse2 = foxHttpInterfaceTest.objectGet(new QueryObjectModel("Fox", null));
assertThat(getResponse2.getArgs().get("user-id")).isEqualTo("Fox");

try {
GetResponse getResponse3 = foxHttpInterfaceTest.objectGet(new QueryObjectModel(null,"test"));
GetResponse getResponse3 = foxHttpInterfaceTest.objectGet(new QueryObjectModel(null, "test"));
assertThat(false).isEqualTo(true);
} catch (FoxHttpException e) {
assertThat(e.getMessage()).isEqualTo("The query parameter attribute userId in QueryObjectModel is not optional and can't be null because of this.");
Expand Down Expand Up @@ -201,6 +202,7 @@ public void postStringNative() throws Exception {
public void postObject() throws Exception {
//Set Gson parser, register placeholder
FoxHttpClientBuilder foxHttpClientBuilder = new FoxHttpClientBuilder(new GsonParser())
.setFoxHttpLogger(new SystemOutFoxHttpLogger(true, "Body-Request", FoxHttpLoggerLevel.DEBUG))
.addFoxHttpPlaceholderEntry("host", endpoint);

//Request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public void postRequest() throws Exception {
FoxHttpClient foxHttpClient = new FoxHttpClient();
foxHttpClient.setFoxHttpResponseParser(new GsonParser());

FoxHttpRequestBody requestBody = new RequestStringBody("test string body 1234 - ?");
FoxHttpRequestBody requestBody = new RequestStringBody("test string body 1234 - ? éàèäöü$!ë");

FoxHttpRequest foxHttpRequest = new FoxHttpRequest(foxHttpClient);
foxHttpRequest.setUrl(new URL(endpoint + "post"));
Expand All @@ -287,7 +287,7 @@ public void postRequest() throws Exception {

PostResponse postResponse = foxHttpResponse.getParsedBody(PostResponse.class);

assertThat(postResponse.getData()).isEqualTo("test string body 1234 - ?");
assertThat(postResponse.getData()).isEqualTo("test string body 1234 - ? éàèäöü$!ë");
}

@Test
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/ch/viascom/groundwork/foxhttp/models/User.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.viascom.groundwork.foxhttp.models;

import ch.viascom.groundwork.foxhttp.annotation.types.SerializeContentType;
import lombok.Data;

import java.io.Serializable;
Expand All @@ -8,6 +9,7 @@
* @author [email protected]
*/
@Data
@SerializeContentType(mimetype = "application/json", charset = "UTF-8")
public class User implements Serializable {
private String username = "[email protected]";
private String firstname = "Fox";
Expand Down

0 comments on commit 18562f7

Please sign in to comment.