diff --git a/pom.xml b/pom.xml index 1701b05..78bbde1 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ foxhttp - 1.3 + 1.3.1 GroundWork - FoxHttp diff --git a/src/main/java/ch/viascom/groundwork/foxhttp/annotation/processor/FoxHttpAnnotationRequestBuilder.java b/src/main/java/ch/viascom/groundwork/foxhttp/annotation/processor/FoxHttpAnnotationRequestBuilder.java index b3ecea2..18b71c9 100644 --- a/src/main/java/ch/viascom/groundwork/foxhttp/annotation/processor/FoxHttpAnnotationRequestBuilder.java +++ b/src/main/java/ch/viascom/groundwork/foxhttp/annotation/processor/FoxHttpAnnotationRequestBuilder.java @@ -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; @@ -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); } } @@ -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); diff --git a/src/main/java/ch/viascom/groundwork/foxhttp/annotation/types/SerializeContentType.java b/src/main/java/ch/viascom/groundwork/foxhttp/annotation/types/SerializeContentType.java new file mode 100644 index 0000000..1797e59 --- /dev/null +++ b/src/main/java/ch/viascom/groundwork/foxhttp/annotation/types/SerializeContentType.java @@ -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(); +} diff --git a/src/main/java/ch/viascom/groundwork/foxhttp/body/request/FoxHttpRequestBody.java b/src/main/java/ch/viascom/groundwork/foxhttp/body/request/FoxHttpRequestBody.java index 7e3c853..01d9de8 100644 --- a/src/main/java/ch/viascom/groundwork/foxhttp/body/request/FoxHttpRequestBody.java +++ b/src/main/java/ch/viascom/groundwork/foxhttp/body/request/FoxHttpRequestBody.java @@ -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 @@ -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; @@ -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); diff --git a/src/test/java/ch/viascom/groundwork/foxhttp/FoxHttpAnnotationTest.java b/src/test/java/ch/viascom/groundwork/foxhttp/FoxHttpAnnotationTest.java index 91df096..ddfc24d 100644 --- a/src/test/java/ch/viascom/groundwork/foxhttp/FoxHttpAnnotationTest.java +++ b/src/test/java/ch/viascom/groundwork/foxhttp/FoxHttpAnnotationTest.java @@ -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; @@ -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."); @@ -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 diff --git a/src/test/java/ch/viascom/groundwork/foxhttp/FoxHttpRequestTest.java b/src/test/java/ch/viascom/groundwork/foxhttp/FoxHttpRequestTest.java index a7c9c65..60dd1d2 100644 --- a/src/test/java/ch/viascom/groundwork/foxhttp/FoxHttpRequestTest.java +++ b/src/test/java/ch/viascom/groundwork/foxhttp/FoxHttpRequestTest.java @@ -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")); @@ -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 diff --git a/src/test/java/ch/viascom/groundwork/foxhttp/models/User.java b/src/test/java/ch/viascom/groundwork/foxhttp/models/User.java index 6b3d799..55a5ec9 100644 --- a/src/test/java/ch/viascom/groundwork/foxhttp/models/User.java +++ b/src/test/java/ch/viascom/groundwork/foxhttp/models/User.java @@ -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; @@ -8,6 +9,7 @@ * @author patrick.boesch@viascom.ch */ @Data +@SerializeContentType(mimetype = "application/json", charset = "UTF-8") public class User implements Serializable { private String username = "foxhttp@viascom.ch"; private String firstname = "Fox";