Juan Hernandez has uploaded a new change for review.

Change subject: restapi: Include charset in JSON Content-Type
......................................................................

restapi: Include charset in JSON Content-Type

Currently the JSON responses generated by the REST API don't specify the
character set in the Content-Type header. This patch modifies the JSON
provider so that it will include it.

Change-Id: I9156fe168451f103219902b821236b5a4703819d
Bug-Url: https://bugzilla.redhat.com/862781
Signed-off-by: Juan Hernandez <[email protected]>
---
M 
backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/json/JsonJAXBAnnotationAwareProvider.java
1 file changed, 19 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/04/20104/1

diff --git 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/json/JsonJAXBAnnotationAwareProvider.java
 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/json/JsonJAXBAnnotationAwareProvider.java
index 44eba33..2859bfe 100644
--- 
a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/json/JsonJAXBAnnotationAwareProvider.java
+++ 
b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/json/JsonJAXBAnnotationAwareProvider.java
@@ -1,5 +1,10 @@
 package org.ovirt.engine.api.restapi.json;
 
+import java.io.IOException;
+import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+
 import org.codehaus.jackson.jaxrs.Annotations;
 import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
 import org.codehaus.jackson.map.SerializationConfig;
@@ -7,6 +12,7 @@
 import javax.ws.rs.Consumes;
 import javax.ws.rs.Produces;
 import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.ext.Provider;
 
 /**
@@ -24,6 +30,10 @@
 @Consumes(MediaType.APPLICATION_JSON)
 @Produces(MediaType.APPLICATION_JSON)
 public class JsonJAXBAnnotationAwareProvider extends JacksonJsonProvider {
+    // This is the content type that we will use in all the responses:
+    private static final String CONTENT_TYPE_HEADER = "Content-Type";
+    private static final MediaType CONTENT_TYPE_MEDIA = 
MediaType.valueOf(MediaType.APPLICATION_JSON + "; charset=UTF-8");
+
     public JsonJAXBAnnotationAwareProvider() {
         // We want a mapper that only takes into account the JAXB annotations:
         super(Annotations.JAXB);
@@ -32,4 +42,13 @@
         configure(SerializationConfig.Feature.WRITE_NULL_PROPERTIES, false);
         configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
     }
+
+    @Override
+    public void writeTo(Object value, Class<?> type, Type genericType, 
Annotation[] annotations, MediaType mediaType,
+            MultivaluedMap<String, Object> httpHeaders, OutputStream 
entityStream) throws IOException {
+        // Jackson always generates the output using UTF-8, regardless of what 
is requested, so we can safely set the
+        // charset to UTF-8:
+        httpHeaders.putSingle(CONTENT_TYPE_HEADER, CONTENT_TYPE_MEDIA);
+        super.writeTo(value, type, genericType, annotations, mediaType, 
httpHeaders, entityStream);
+    }
 }


-- 
To view, visit http://gerrit.ovirt.org/20104
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I9156fe168451f103219902b821236b5a4703819d
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Juan Hernandez <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to