Author: rwesten
Date: Fri Sep 30 04:21:23 2011
New Revision: 1177503

URL: http://svn.apache.org/viewvc?rev=1177503&view=rev
Log:
Several Fixes related to STANBOL-329 (wrong Character encoding)

Corrected Bugs:

* FreeMakerViewProcessor: Added DefaultEncoding and OutputEncoding to the 
default configuration. Both is set to "urn-8"
* The OutputStreamWriter used to write the data generated by FreeMaker to the 
OutputStream of the HTTP response now uses "utf-8" instead of the platform 
default as charset.
* JsonLdSerializerProvider: Similar to the FreeMakerViewProcessor this Provider 
also was not defining "utf-8" as charset on the OutputStreamWriter and used 
therefore the Plattform specific encoding.

Improvements:

* All MessageBodyWriter of the "commons.web.base", "enhancer.jersey" and 
"entityhub.jersey" do no longer use the "@Produces" annotation but manually 
check the MediaType in the isWriteable(..) method. This is a requirement to 
allow the definition of the charset parameter in the "Content-Type" header of 
the response. Jersey does not support parameters to this header field and would 
therefore not match Content-Types that include the charset parameter with types 
defined by the "@Produces" header.
* All **Resource classes of the Entityhub now define the charset parameter in 
responses. For the Enhancer this changes are still commented. This is mainly to 
allow testing if the definition of the charset is a requirement. Because of 
JERSEY-318 the definition of the "Content-Type" parameter including the charset 
parameter must be done by manually setting the this header. The build in 
methods for setting the MediaType can not be used.

Still present potential issues:

* It need to be tested if the setting of the charset parameter for the 
"Content-Type" header of reposes is a requirement. To test the difference the 
charset parameter is currently used for the Entityhub and not used for the 
Enhancer.
* MessageBodyWriter need to get the charset parameter from the "Content-Type" 
header and use it to serialize responses. This is already done for XML and JSON 
responses, but not for all Clerezza based serializers because the interface of 
Clerezza does not support the definition of a charset.
* The changes mentioned here are only applied to the Entityhub, Enhancer and 
the common.web.* modules. Similar changes would be also needed for the other 
modules that define web interfaces

best
Rupert

Modified:
    
incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/processor/FreemarkerViewProcessor.java
    
incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/GraphWriter.java
    
incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/JenaModelWriter.java
    
incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/JsonLdSerializerProvider.java
    
incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/ResultSetWriter.java
    
incubator/stanbol/trunk/enhancer/jersey/src/main/java/org/apache/stanbol/enhancer/jersey/resource/ContentItemResource.java
    
incubator/stanbol/trunk/enhancer/jersey/src/main/java/org/apache/stanbol/enhancer/jersey/resource/EnginesRootResource.java
    
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/resource/EntityhubRootResource.java
    
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/resource/ReferencedSiteRootResource.java
    
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/resource/SiteManagerRootResource.java
    
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/writers/JettisonWriter.java
    
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/writers/QueryResultListWriter.java
    
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/writers/RepresentationWriter.java
    
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/writers/SignWriter.java

Modified: 
incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/processor/FreemarkerViewProcessor.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/processor/FreemarkerViewProcessor.java?rev=1177503&r1=1177502&r2=1177503&view=diff
==============================================================================
--- 
incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/processor/FreemarkerViewProcessor.java
 (original)
+++ 
incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/processor/FreemarkerViewProcessor.java
 Fri Sep 30 04:21:23 2011
@@ -148,6 +148,8 @@ public class FreemarkerViewProcessor imp
 
         // don't cache for more that 2s
         config.setTemplateUpdateDelay(2);
+        config.setDefaultEncoding("utf-8");
+        config.setOutputEncoding("utf-8");
         log.info("Assigned default freemarker configuration");
     }
 
@@ -212,7 +214,7 @@ public class FreemarkerViewProcessor imp
         // override custom variables if any
         vars.putAll(getVariablesForTemplate(vars));
 
-        final OutputStreamWriter writer = new OutputStreamWriter(out);
+        final OutputStreamWriter writer = new OutputStreamWriter(out,"utf-8");
         try {
             template.process(vars, writer);
         } catch (Throwable t) {

Modified: 
incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/GraphWriter.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/GraphWriter.java?rev=1177503&r1=1177502&r2=1177503&view=diff
==============================================================================
--- 
incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/GraphWriter.java
 (original)
+++ 
incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/GraphWriter.java
 Fri Sep 30 04:21:23 2011
@@ -29,6 +29,9 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
 
 import javax.servlet.ServletContext;
 import javax.ws.rs.Produces;
@@ -41,12 +44,26 @@ import javax.ws.rs.ext.Provider;
 
 import org.apache.clerezza.rdf.core.TripleCollection;
 import org.apache.clerezza.rdf.core.serializedform.Serializer;
+import org.apache.clerezza.rdf.core.serializedform.SupportedFormat;
 import org.apache.stanbol.commons.web.base.ContextHelper;
 
 
 @Provider
-@Produces({TEXT_PLAIN, N3, N_TRIPLE, RDF_XML, TURTLE, X_TURTLE, RDF_JSON, 
APPLICATION_JSON})
+//@Produces({TEXT_PLAIN, N3, N_TRIPLE, RDF_XML, TURTLE, X_TURTLE, RDF_JSON, 
APPLICATION_JSON})
 public class GraphWriter implements MessageBodyWriter<TripleCollection> {
+    public static final Set<String> supportedMediaTypes;
+    static {
+        Set<String> types = new HashSet<String>();
+        types.add(TEXT_PLAIN);
+        types.add(N3);
+        types.add(N_TRIPLE);
+        types.add(RDF_XML);
+        types.add(TURTLE);
+        types.add(X_TURTLE);
+        types.add(RDF_JSON);
+        types.add(APPLICATION_JSON);
+        supportedMediaTypes = Collections.unmodifiableSet(types);
+    }
 
     @Context
     protected ServletContext servletContext;
@@ -57,7 +74,8 @@ public class GraphWriter implements Mess
 
     public boolean isWriteable(Class<?> type, Type genericType,
             Annotation[] annotations, MediaType mediaType) {
-        return TripleCollection.class.isAssignableFrom(type);
+        String mediaTypeString = 
mediaType.getType()+'/'+mediaType.getSubtype();
+        return TripleCollection.class.isAssignableFrom(type) && 
supportedMediaTypes.contains(mediaTypeString);
     }
 
     public long getSize(TripleCollection t, Class<?> type, Type genericType,
@@ -70,11 +88,11 @@ public class GraphWriter implements Mess
             MultivaluedMap<String, Object> httpHeaders,
             OutputStream entityStream) throws IOException,
             WebApplicationException {
-
-        if (mediaType == null || mediaType.isWildcardType() || 
TEXT_PLAIN.equals(mediaType.toString())) {
+        String mediaTypeString = 
mediaType.getType()+'/'+mediaType.getSubtype();
+        if (mediaType.isWildcardType() || TEXT_PLAIN.equals(mediaTypeString)) {
             getSerializer().serialize(entityStream, t, APPLICATION_JSON);
         } else {
-            getSerializer().serialize(entityStream, t, mediaType.toString());
+            getSerializer().serialize(entityStream, t, mediaTypeString);
         }
     }
 }

Modified: 
incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/JenaModelWriter.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/JenaModelWriter.java?rev=1177503&r1=1177502&r2=1177503&view=diff
==============================================================================
--- 
incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/JenaModelWriter.java
 (original)
+++ 
incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/JenaModelWriter.java
 Fri Sep 30 04:21:23 2011
@@ -20,6 +20,9 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
 
 import javax.ws.rs.Produces;
 import javax.ws.rs.WebApplicationException;
@@ -41,11 +44,17 @@ import com.hp.hpl.jena.rdf.model.Model;
 
 
 @Provider
-@Produces({"application/rdf+xml", "application/xml", "text/xml"})
-
+//@Produces({"application/rdf+xml", "application/xml", "text/xml"})
 public class JenaModelWriter implements MessageBodyWriter<Model> {
 
-       
+    public static final Set<String> supportedMediaTypes;
+    static {
+        Set<String> types = new HashSet<String>();
+        types.add("application/rdf+xml");
+        types.add("application/xml");
+        types.add("text/xml");
+        supportedMediaTypes = Collections.unmodifiableSet(types);
+    }
        
        public static final String ENCODING = "UTF-8";
        
@@ -55,24 +64,27 @@ public class JenaModelWriter implements 
        }
 
        public boolean isWriteable(Class<?> arg0, Type arg1, Annotation[] arg2,
-                       MediaType arg3) {
-
-               return Model.class.isAssignableFrom(arg0);
+                       MediaType mediaType) {
+           String mediaTypeString = 
mediaType.getType()+'/'+mediaType.getSubtype();
+               return Model.class.isAssignableFrom(arg0) && 
supportedMediaTypes.contains(mediaTypeString);
        }
 
        public void writeTo(Model model, Class<?> arg1, Type arg2,
-                       Annotation[] arg3, MediaType arg4,
+                       Annotation[] arg3, MediaType mediaType,
                        MultivaluedMap<String, Object> arg5, OutputStream 
outputStream)
        throws IOException, WebApplicationException {
                Document doc = null;
-
+               String encoding = mediaType.getParameters().get("charset");
+               if(encoding == null){
+                   encoding = ENCODING;
+               }
                try {
                        doc = new JenaModelTransformer().toDocument(model);
                        DOMSource domSource = new DOMSource(doc);
                        StreamResult streamResult = new 
StreamResult(outputStream);
                        TransformerFactory tf = 
TransformerFactory.newInstance();
                        Transformer serializer = tf.newTransformer();
-                       
serializer.setOutputProperty(OutputKeys.ENCODING,ENCODING);
+                       
serializer.setOutputProperty(OutputKeys.ENCODING,encoding);
                        serializer.setOutputProperty(OutputKeys.INDENT,"yes");
                        serializer.transform(domSource, streamResult);
                } catch(TransformerException te) {

Modified: 
incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/JsonLdSerializerProvider.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/JsonLdSerializerProvider.java?rev=1177503&r1=1177502&r2=1177503&view=diff
==============================================================================
--- 
incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/JsonLdSerializerProvider.java
 (original)
+++ 
incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/JsonLdSerializerProvider.java
 Fri Sep 30 04:21:23 2011
@@ -113,7 +113,7 @@ public class JsonLdSerializerProvider im
         }
 
         try {
-            BufferedWriter writer = new BufferedWriter(new 
OutputStreamWriter(serializedGraph));
+            BufferedWriter writer = new BufferedWriter(new 
OutputStreamWriter(serializedGraph,"utf-8"));
             jsonLd.setUseTypeCoercion(this.useTypeCoercion);
             writer.write(jsonLd.toString(this.indentation));
             writer.flush();

Modified: 
incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/ResultSetWriter.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/ResultSetWriter.java?rev=1177503&r1=1177502&r2=1177503&view=diff
==============================================================================
--- 
incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/ResultSetWriter.java
 (original)
+++ 
incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/ResultSetWriter.java
 Fri Sep 30 04:21:23 2011
@@ -16,10 +16,22 @@
 */
 package org.apache.stanbol.commons.web.base.writers;
 
+import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
+import static javax.ws.rs.core.MediaType.TEXT_PLAIN;
+import static org.apache.clerezza.rdf.core.serializedform.SupportedFormat.N3;
+import static 
org.apache.clerezza.rdf.core.serializedform.SupportedFormat.N_TRIPLE;
+import static 
org.apache.clerezza.rdf.core.serializedform.SupportedFormat.RDF_JSON;
+import static 
org.apache.clerezza.rdf.core.serializedform.SupportedFormat.RDF_XML;
+import static 
org.apache.clerezza.rdf.core.serializedform.SupportedFormat.TURTLE;
+import static 
org.apache.clerezza.rdf.core.serializedform.SupportedFormat.X_TURTLE;
+
 import java.io.IOException;
 import java.io.OutputStream;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
 
 import javax.ws.rs.Produces;
 import javax.ws.rs.WebApplicationException;
@@ -44,8 +56,16 @@ import org.w3c.dom.Document;
  * quite a lot of Clerezza dependencies that we don't really need.
  */
 @Provider
-@Produces({"application/sparql-results+xml", "application/xml", "text/xml"})
+//@Produces({"application/sparql-results+xml", "application/xml", "text/xml"})
 public class ResultSetWriter implements MessageBodyWriter<ResultSet> {
+    public static final Set<String> supportedMediaTypes;
+    static {
+        Set<String> types = new HashSet<String>();
+        types.add("application/sparql-results+xml");
+        types.add("application/xml");
+        types.add("text/xml");
+        supportedMediaTypes = Collections.unmodifiableSet(types);
+    }
 
     public static final String ENCODING = "UTF-8";
 
@@ -57,7 +77,8 @@ public class ResultSetWriter implements 
 
     @Override
     public boolean isWriteable(Class<?> type, Type genericType, Annotation[] 
annotations, MediaType mediaType) {
-        return ResultSet.class.isAssignableFrom(type);
+        String mediaTypeString = 
mediaType.getType()+'/'+mediaType.getSubtype();
+        return ResultSet.class.isAssignableFrom(type) && 
supportedMediaTypes.contains(mediaTypeString);
     }
 
     @Override
@@ -65,14 +86,17 @@ public class ResultSetWriter implements 
             Annotation[] annotations, MediaType mediaType,
             MultivaluedMap<String, Object> httpHeaders, OutputStream 
entityStream)
             throws IOException, WebApplicationException {
-
+        String encoding = mediaType.getParameters().get("charset");
+        if(encoding == null){
+            encoding = ENCODING;
+        }
         try {
             Document doc = new ResultSetToXml().toDocument(resultSet);
             DOMSource domSource = new DOMSource(doc);
             StreamResult streamResult = new StreamResult(entityStream);
             TransformerFactory tf = TransformerFactory.newInstance();
             Transformer serializer = tf.newTransformer();
-            serializer.setOutputProperty(OutputKeys.ENCODING,ENCODING);
+            serializer.setOutputProperty(OutputKeys.ENCODING,encoding);
             serializer.setOutputProperty(OutputKeys.INDENT,"yes");
             serializer.transform(domSource, streamResult);
         } catch(TransformerException te) {

Modified: 
incubator/stanbol/trunk/enhancer/jersey/src/main/java/org/apache/stanbol/enhancer/jersey/resource/ContentItemResource.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/jersey/src/main/java/org/apache/stanbol/enhancer/jersey/resource/ContentItemResource.java?rev=1177503&r1=1177502&r2=1177503&view=diff
==============================================================================
--- 
incubator/stanbol/trunk/enhancer/jersey/src/main/java/org/apache/stanbol/enhancer/jersey/resource/ContentItemResource.java
 (original)
+++ 
incubator/stanbol/trunk/enhancer/jersey/src/main/java/org/apache/stanbol/enhancer/jersey/resource/ContentItemResource.java
 Fri Sep 30 04:21:23 2011
@@ -25,6 +25,7 @@ import static org.apache.stanbol.enhance
 import static org.apache.stanbol.enhancer.servicesapi.rdf.Properties.GEO_LONG;
 import static 
org.apache.stanbol.enhancer.servicesapi.rdf.Properties.NIE_PLAINTEXTCONTENT;
 
+import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
@@ -41,6 +42,7 @@ import java.util.TreeMap;
 import javax.servlet.ServletContext;
 import javax.ws.rs.GET;
 import javax.ws.rs.Produces;
+import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
 
@@ -67,6 +69,7 @@ import org.apache.clerezza.rdf.core.spar
 import org.apache.clerezza.rdf.core.sparql.query.SelectQuery;
 import org.apache.clerezza.rdf.utils.GraphNode;
 import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang.StringUtils;
 import org.apache.stanbol.commons.web.base.resource.BaseStanbolResource;
 import org.apache.stanbol.enhancer.servicesapi.ContentItem;
 import org.apache.stanbol.enhancer.servicesapi.rdf.Properties;
@@ -513,13 +516,17 @@ public class ContentItemResource extends
         }
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         serializer.serialize(out, g, SupportedFormat.RDF_JSON);
-        return out.toString("utf-8");
+        
+        String rdfString = out.toString("utf-8");
+        return rdfString;
     }
 
     @GET
     @Produces(TEXT_HTML)
     public Response get() {
-        return Response.ok(new Viewable("index", this), TEXT_HTML).build();
+        return Response.ok(new Viewable("index", this),TEXT_HTML).build();
+//        return Response.ok(new Viewable("index", this))
+//        .header(HttpHeaders.CONTENT_TYPE, TEXT_HTML+"; 
charset=utf-8").build();
     }
 
 }

Modified: 
incubator/stanbol/trunk/enhancer/jersey/src/main/java/org/apache/stanbol/enhancer/jersey/resource/EnginesRootResource.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/jersey/src/main/java/org/apache/stanbol/enhancer/jersey/resource/EnginesRootResource.java?rev=1177503&r1=1177502&r2=1177503&view=diff
==============================================================================
--- 
incubator/stanbol/trunk/enhancer/jersey/src/main/java/org/apache/stanbol/enhancer/jersey/resource/EnginesRootResource.java
 (original)
+++ 
incubator/stanbol/trunk/enhancer/jersey/src/main/java/org/apache/stanbol/enhancer/jersey/resource/EnginesRootResource.java
 Fri Sep 30 04:21:23 2011
@@ -98,7 +98,9 @@ public class EnginesRootResource extends
     @GET
     @Produces(TEXT_HTML)
     public Response get() {
-        return Response.ok(new Viewable("index", this), TEXT_HTML).build();
+        return Response.ok(new Viewable("index", this),TEXT_HTML).build();
+//        return Response.ok(new Viewable("index", this))
+//        .header(HttpHeaders.CONTENT_TYPE, TEXT_HTML+"; 
charset=utf-8").build();
     }
 
     public List<EnhancementEngine> getActiveEngines() {
@@ -185,19 +187,27 @@ public class EnginesRootResource extends
                     tcManager, serializer, servletContext);
             contentItemResource.setRdfSerializationFormat(format);
             Viewable ajaxView = new Viewable("/ajax/contentitem", 
contentItemResource);
-            return Response.ok(ajaxView).type(TEXT_HTML).build();
+            return Response.ok(ajaxView,TEXT_HTML).build();
+//            return Response.ok(ajaxView)
+//            .header(HttpHeaders.CONTENT_TYPE, TEXT_HTML+"; 
charset=UTF-8").build();
         }
         if (format != null) {
             // force mimetype from form params
-            return Response.ok(graph, format).build();
+            return Response.ok(graph,format).build();
+//            return Response.ok(graph)
+//            .header(HttpHeaders.CONTENT_TYPE, format+"; 
charset=UTF-8").build();
         }
         if (headers.getAcceptableMediaTypes().contains(APPLICATION_JSON_TYPE)) 
{
             // force RDF JSON media type (TODO: move this logic
-            return Response.ok(graph, RDF_JSON).build();
+            return Response.ok(graph,RDF_JSON).build();
+//            return Response.ok(graph)
+//            .header(HttpHeaders.CONTENT_TYPE, RDF_JSON+"; 
charset=UTF-8").build();
         } else if (headers.getAcceptableMediaTypes().isEmpty()) {
             // use RDF/XML as default format to keep compat with OpenCalais
             // clients
-            return Response.ok(graph, RDF_XML).build();
+            return Response.ok(graph,RDF_XML).build();
+//            return Response.ok(graph)
+//            .header(HttpHeaders.CONTENT_TYPE, RDF_XML+"; 
charset=UTF-8").build();
         }
 
         // traditional response lookup

Modified: 
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/resource/EntityhubRootResource.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/resource/EntityhubRootResource.java?rev=1177503&r1=1177502&r2=1177503&view=diff
==============================================================================
--- 
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/resource/EntityhubRootResource.java
 (original)
+++ 
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/resource/EntityhubRootResource.java
 Fri Sep 30 04:21:23 2011
@@ -112,7 +112,8 @@ public class EntityhubRootResource exten
     @GET
     @Produces(TEXT_HTML)
     public Response get() {
-        return Response.ok(new Viewable("index", this), TEXT_HTML).build();
+        return Response.ok(new Viewable("index", this))
+        .header(HttpHeaders.CONTENT_TYPE, TEXT_HTML+"; charset=utf-8").build();
     }
 
     @GET
@@ -127,7 +128,8 @@ public class EntityhubRootResource exten
             APPLICATION_JSON_TYPE);
         if(acceptedMediaType.isCompatible(TEXT_HTML_TYPE) && symbolId == null){
             //return HTML docu
-            return Response.ok(new Viewable("entity", this), 
TEXT_HTML).build();
+            return Response.ok(new Viewable("entity", this))
+            .header(HttpHeaders.CONTENT_TYPE, TEXT_HTML+"; 
charset=utf-8").build();
         }
         if (symbolId == null || symbolId.isEmpty()) {
             // TODO: how to parse an error message
@@ -143,7 +145,8 @@ public class EntityhubRootResource exten
         if (entity == null) {
             throw new WebApplicationException(NOT_FOUND);
         } else {
-            return Response.ok(entity, acceptedMediaType).build();
+            return Response.ok(entity)
+            .header(HttpHeaders.CONTENT_TYPE, acceptedMediaType+"; 
charset=utf-8").build();
         }
     }
         
@@ -162,7 +165,8 @@ public class EntityhubRootResource exten
             APPLICATION_JSON_TYPE);
         if(acceptedMediaType.isCompatible(TEXT_HTML_TYPE) && reference == 
null){
             //return docu
-            return Response.ok(new Viewable("lookup", this), 
TEXT_HTML).build();
+            return Response.ok(new Viewable("lookup", this))
+            .header(HttpHeaders.CONTENT_TYPE, TEXT_HTML+"; 
charset=utf-8").build();
         } else {
             if (reference == null || reference.isEmpty()) {
                 // TODO: how to parse an error message
@@ -179,7 +183,8 @@ public class EntityhubRootResource exten
                 return Response.status(Status.NOT_FOUND).entity("No symbol 
found for '" + reference + "'.")
                         .header(HttpHeaders.ACCEPT, acceptedMediaType).build();
             } else {
-                return Response.ok(entity, acceptedMediaType).build();
+                return Response.ok(entity)
+                .header(HttpHeaders.CONTENT_TYPE, acceptedMediaType+"; 
charset=utf-8").build();
             }
         }
     }
@@ -243,7 +248,8 @@ public class EntityhubRootResource exten
                     "parsed id "+id+" is not managed by the Entityhub")
                     .header(HttpHeaders.ACCEPT, accepted).build();
         } else {
-            return Response.ok(entity,accepted).build();
+            return Response.ok(entity)
+            .header(HttpHeaders.CONTENT_TYPE, accepted+"; 
charset=utf-8").build();
         }
     }
     /**
@@ -334,7 +340,8 @@ public class EntityhubRootResource exten
             } else {
                 //return Response.noContent().build();
                 //As alternative return the modified entity
-                return Response.ok(entity, accepted).build();
+                return Response.ok(entity)
+                .header(HttpHeaders.CONTENT_TYPE, accepted+"; 
charset=utf-8").build();
                 
             }
 //            if (updated.size() == 1){
@@ -386,7 +393,8 @@ public class EntityhubRootResource exten
         if(name == null || name.isEmpty()){
             if(acceptedMediaType.isCompatible(TEXT_HTML_TYPE)){
                 //return HTML docu
-                return Response.ok(new Viewable("find", this), 
TEXT_HTML).build();
+                return Response.ok(new Viewable("find", this))
+                .header(HttpHeaders.CONTENT_TYPE, TEXT_HTML+"; 
charset=utf-8").build();
             } else {
                 return Response.status(Status.BAD_REQUEST)
                     .entity("The name must not be null nor empty for find 
requests. Missing parameter name.\n")
@@ -423,7 +431,8 @@ public class EntityhubRootResource exten
     @GET
     @Path("/query")
     public Response getQueryDocumentation(){
-        return Response.ok(new Viewable("query", this), TEXT_HTML).build();    
    
+        return Response.ok(new Viewable("query", this))
+        .header(HttpHeaders.CONTENT_TYPE, TEXT_HTML+"; 
charset=utf-8").build();        
     }
     /**
      * Allows to parse any kind of {@link FieldQuery} in its JSON 
Representation.
@@ -461,7 +470,8 @@ public class EntityhubRootResource exten
     private Response executeQuery(FieldQuery query, MediaType 
acceptedMediaType) throws WebApplicationException {
         Entityhub entityhub = 
ContextHelper.getServiceFromContext(Entityhub.class, context);
         try {
-            return Response.ok(entityhub.find(query), 
acceptedMediaType).build();
+            return Response.ok(entityhub.find(query))
+            .header(HttpHeaders.CONTENT_TYPE, acceptedMediaType+"; 
charset=utf-8").build();
         } catch (EntityhubException e) {
             log.error("Exception while performing the FieldQuery on the 
EntityHub", e);
             log.error("Query:\n" + query);
@@ -489,7 +499,8 @@ public class EntityhubRootResource exten
         if (reference == null || reference.isEmpty()) {
             //if HTML -> print the docu of the restfull service
             if(TEXT_HTML_TYPE.isCompatible(acceptedMediaType)){
-              return Response.ok(new Viewable("mapping", this), 
TEXT_HTML).build();
+              return Response.ok(new Viewable("mapping", this))
+              .header(HttpHeaders.CONTENT_TYPE, TEXT_HTML+"; 
charset=utf-8").build();
             } else {
                 return Response.status(Status.BAD_REQUEST).entity("The mapping 
id (URI) is missing.\n").header(
                     HttpHeaders.ACCEPT, acceptedMediaType).build();
@@ -507,7 +518,8 @@ public class EntityhubRootResource exten
             return Response.status(Status.NOT_FOUND).entity("No mapping found 
for '" + reference + "'.\n")
                     .header(HttpHeaders.ACCEPT, acceptedMediaType).build();
         } else {
-            return Response.ok(mapping, acceptedMediaType).build();
+            return Response.ok(mapping)
+            .header(HttpHeaders.CONTENT_TYPE, acceptedMediaType+"; 
charset=utf-8").build();
         }
     }
 // see NOTE to RESTful Service Documentation in the header
@@ -515,7 +527,8 @@ public class EntityhubRootResource exten
 //    @Path("/entity")
 //    @Produces(MediaType.TEXT_HTML)
 //    public Response getEntityMappingPage() {
-//        return Response.ok(new Viewable("entity", this), TEXT_HTML).build();
+//        return Response.ok(new Viewable("entity", this))
+//        .header(HttpHeaders.CONTENT_TYPE, TEXT_HTML+"; 
charset=utf-8").build();
 //    }
 
     @GET
@@ -534,7 +547,8 @@ public class EntityhubRootResource exten
         if (entity == null || entity.isEmpty()) {
             //if HTML -> print the docu of the restfull service
             if(TEXT_HTML_TYPE.isCompatible(acceptedMediaType)){
-                return Response.ok(new Viewable("mapping_entity", this), 
TEXT_HTML).build();
+                return Response.ok(new Viewable("mapping_entity", this))
+                .header(HttpHeaders.CONTENT_TYPE, TEXT_HTML+"; 
charset=utf-8").build();
             } else {
                 return Response.status(Status.BAD_REQUEST).entity("No entity 
given. Missing parameter id.\n")
                     .header(HttpHeaders.ACCEPT, acceptedMediaType).build();
@@ -552,7 +566,8 @@ public class EntityhubRootResource exten
             return Response.status(Status.NOT_FOUND).entity("No mapping found 
for entity '" + entity + "'.\n")
                     .header(HttpHeaders.ACCEPT, acceptedMediaType).build();
         } else {
-            return Response.ok(mapping, acceptedMediaType).build();
+            return Response.ok(mapping)
+            .header(HttpHeaders.CONTENT_TYPE, acceptedMediaType+"; 
charset=utf-8").build();
         }
     }
  // see NOTE to RESTful Service Documentation in the header
@@ -560,7 +575,8 @@ public class EntityhubRootResource exten
 //    @Path("mapping/symbol")
 //    @Produces(MediaType.TEXT_HTML)
 //    public Response getSymbolMappingPage() {
-//        return Response.ok(new Viewable("symbol", this), TEXT_HTML).build();
+//        return Response.ok(new Viewable("symbol", this))
+//        .header(HttpHeaders.CONTENT_TYPE, TEXT_HTML+"; 
charset=utf-8").build();
 //    }
     
     @GET
@@ -579,7 +595,8 @@ public class EntityhubRootResource exten
         if (symbol == null || symbol.isEmpty()) {
             //if HTML -> print the docu of the restfull service
             if(TEXT_HTML_TYPE.isCompatible(acceptedMediaType)){
-                return Response.ok(new Viewable("mapping_symbol", this), 
TEXT_HTML).build();
+                return Response.ok(new Viewable("mapping_symbol", this))
+                .header(HttpHeaders.CONTENT_TYPE, TEXT_HTML+"; 
charset=utf-8").build();
             } else {
                 return Response.status(Status.BAD_REQUEST).entity("No symbol 
given. Missing parameter id.\n")
                     .header(HttpHeaders.ACCEPT, acceptedMediaType).build();
@@ -600,7 +617,8 @@ public class EntityhubRootResource exten
             // For now use a pseudo QueryResultList
             QueryResultList<Entity> mappingResultList = new 
QueryResultListImpl<Entity>(null,
                     mappings, Entity.class);
-            return Response.ok(mappingResultList, acceptedMediaType).build();
+            return Response.ok(mappingResultList)
+            .header(HttpHeaders.CONTENT_TYPE, acceptedMediaType+"; 
charset=utf-8").build();
         }
     }
 }

Modified: 
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/resource/ReferencedSiteRootResource.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/resource/ReferencedSiteRootResource.java?rev=1177503&r1=1177502&r2=1177503&view=diff
==============================================================================
--- 
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/resource/ReferencedSiteRootResource.java
 (original)
+++ 
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/resource/ReferencedSiteRootResource.java
 Fri Sep 30 04:21:23 2011
@@ -143,7 +143,8 @@ public class ReferencedSiteRootResource 
     @GET
     @Produces(value=MediaType.TEXT_HTML)
     public Response getHtmlInfo(){
-        return Response.ok(new Viewable("index", this), TEXT_HTML).build();
+        return Response.ok(new Viewable("index", this))
+        .header(HttpHeaders.CONTENT_TYPE, TEXT_HTML+"; charset=utf-8").build();
     }
     /**
      * Provides metadata about this referenced site as representation
@@ -156,7 +157,8 @@ public class ReferencedSiteRootResource 
     public Response getInfo(@Context HttpHeaders headers,
                             @Context UriInfo uriInfo) {
         MediaType acceptedMediaType = 
JerseyUtils.getAcceptableMediaType(headers, 
REPRESENTATION_SUPPORTED_MEDIA_TYPES,MediaType.APPLICATION_JSON_TYPE);
-        return 
Response.ok(site2Representation(uriInfo.getAbsolutePath().toString()), 
acceptedMediaType).build();
+        return 
Response.ok(site2Representation(uriInfo.getAbsolutePath().toString()))
+        .header(HttpHeaders.CONTENT_TYPE, acceptedMediaType+"; 
charset=utf-8").build();
     }
     @GET
     @Path(value=ReferencedSiteRootResource.LICENSE_PATH+"/{name}")
@@ -179,7 +181,8 @@ public class ReferencedSiteRootResource 
                             count++;
                         }
                         if(Integer.toString(count).equals(numberString)){
-                            return 
Response.ok(license2Representation(uriInfo.getAbsolutePath().toString(),license),acceptedMediaType).build();
+                            return 
Response.ok(license2Representation(uriInfo.getAbsolutePath().toString(),license))
+                            .header(HttpHeaders.CONTENT_TYPE, 
acceptedMediaType+"; charset=utf-8").build();
                         }
                     }
                 }
@@ -214,7 +217,8 @@ public class ReferencedSiteRootResource 
             supported, MediaType.APPLICATION_JSON_TYPE);
         if (id == null || id.isEmpty()) {
             if(MediaType.TEXT_HTML_TYPE.isCompatible(acceptedMediaType)){
-                return Response.ok(new Viewable("entity", this), 
TEXT_HTML).build();        
+                return Response.ok(new Viewable("entity", this))
+                .header(HttpHeaders.CONTENT_TYPE, TEXT_HTML+"; 
charset=utf-8").build();        
             } else {
                 return Response.status(Status.BAD_REQUEST)
                     .entity("No or empty ID was parsed. Missing parameter 
id.\n")
@@ -231,7 +235,8 @@ public class ReferencedSiteRootResource 
             throw new WebApplicationException(e, 
Response.Status.INTERNAL_SERVER_ERROR);
         }
         if (entity != null) {
-            return Response.ok(entity, acceptedMediaType).build();
+            return Response.ok(entity)
+            .header(HttpHeaders.CONTENT_TYPE, acceptedMediaType+"; 
charset=utf-8").build();
         } else {
             // TODO: How to parse an ErrorMessage?
             // create an Response with the the Error?
@@ -272,7 +277,8 @@ public class ReferencedSiteRootResource 
             headers, supported, MediaType.APPLICATION_JSON_TYPE);
         if(name == null || name.isEmpty()){
             if(MediaType.TEXT_HTML_TYPE.isCompatible(acceptedMediaType)){
-                return Response.ok(new Viewable("find", this), 
TEXT_HTML).build();        
+                return Response.ok(new Viewable("find", this))
+                .header(HttpHeaders.CONTENT_TYPE, TEXT_HTML+"; 
charset=utf-8").build();        
             } else {
                 return Response.status(Status.BAD_REQUEST)
                     .entity("The name must not be null nor empty for find 
requests. Missing parameter name.\n")
@@ -317,7 +323,8 @@ public class ReferencedSiteRootResource 
     @Path("/query")
     @Produces(TEXT_HTML)
     public Response getQueryDocumentation(){
-        return Response.ok(new Viewable("query", this), TEXT_HTML).build();    
    
+        return Response.ok(new Viewable("query", this))
+        .header(HttpHeaders.CONTENT_TYPE, TEXT_HTML+"; 
charset=utf-8").build();        
     }
     
     /**
@@ -332,7 +339,8 @@ public class ReferencedSiteRootResource 
      */
     private Response executeQuery(FieldQuery query, MediaType mediaType) 
throws WebApplicationException {
         try {
-            return Response.ok(site.find(query), mediaType).build();
+            return Response.ok(site.find(query))
+                .header(HttpHeaders.CONTENT_TYPE, mediaType+"; 
charset=utf-8").build();
         } catch (ReferencedSiteException e) {
             log.error("ReferencedSiteException while accessing Site " +
                 site.getConfiguration().getName() + " (id="

Modified: 
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/resource/SiteManagerRootResource.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/resource/SiteManagerRootResource.java?rev=1177503&r1=1177502&r2=1177503&view=diff
==============================================================================
--- 
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/resource/SiteManagerRootResource.java
 (original)
+++ 
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/resource/SiteManagerRootResource.java
 Fri Sep 30 04:21:23 2011
@@ -93,7 +93,8 @@ public class SiteManagerRootResource ext
     @GET
     @Produces(MediaType.TEXT_HTML)
     public Response getSitesPage() {
-        return Response.ok(new Viewable("index", this), TEXT_HTML).build();
+        return Response.ok(new Viewable("index", this))
+        .header(HttpHeaders.CONTENT_TYPE, TEXT_HTML+"; charset=utf-8").build();
     }
 
 // removed to allow request with Accept headers other than text/html to return
@@ -102,7 +103,8 @@ public class SiteManagerRootResource ext
 //    @Path("/referenced")
 //    @Produces(MediaType.TEXT_HTML)
 //    public Response getReferencedSitesPage() {
-//        return Response.ok(new Viewable("referenced", this), 
TEXT_HTML).build();
+//        return Response.ok(new Viewable("referenced", this))
+//        .header(HttpHeaders.CONTENT_TYPE, TEXT_HTML+"; 
charset=utf-8").build();
 //    }
     
     /**
@@ -119,7 +121,8 @@ public class SiteManagerRootResource ext
            Arrays.asList(MediaType.APPLICATION_JSON,MediaType.TEXT_HTML) ,
            MediaType.APPLICATION_JSON_TYPE);
         if(MediaType.TEXT_HTML_TYPE.isCompatible(acceptable)){
-            return Response.ok(new Viewable("referenced", this), 
TEXT_HTML).build();
+            return Response.ok(new Viewable("referenced", this))
+            .header(HttpHeaders.CONTENT_TYPE, TEXT_HTML+"; 
charset=utf-8").build();
         } else {
             JSONArray referencedSites = new JSONArray();
             ReferencedSiteManager referencedSiteManager = 
ContextHelper.getServiceFromContext(
@@ -127,7 +130,8 @@ public class SiteManagerRootResource ext
             for (String site : referencedSiteManager.getReferencedSiteIds()) {
                 referencedSites.put(String.format("%sentityhub/site/%s/", 
uriInfo.getBaseUri(), site));
             }
-            return Response.ok(referencedSites,acceptable).build();
+            return Response.ok(referencedSites)
+            .header(HttpHeaders.CONTENT_TYPE, acceptable+"; 
charset=utf-8").build();
         }
     }
     
@@ -152,7 +156,8 @@ public class SiteManagerRootResource ext
             headers, supported, MediaType.APPLICATION_JSON_TYPE);
         if (id == null || id.isEmpty()) {
             if(MediaType.TEXT_HTML_TYPE.isCompatible(acceptedMediaType)){
-                return Response.ok(new Viewable("entity", this), 
TEXT_HTML).build();        
+                return Response.ok(new Viewable("entity", this))
+                .header(HttpHeaders.CONTENT_TYPE, TEXT_HTML+"; 
charset=utf-8").build();        
             } else {
                 return Response.status(Status.BAD_REQUEST)
                     .entity("No or empty ID was parsed. Missing parameter 
id.\n")
@@ -169,7 +174,8 @@ public class SiteManagerRootResource ext
         // throw new WebApplicationException(e, 
Response.Status.INTERNAL_SERVER_ERROR);
         // }
         if (sign != null) {
-            return Response.ok(sign, acceptedMediaType).build();
+            return Response.ok(sign)
+            .header(HttpHeaders.CONTENT_TYPE, acceptedMediaType+"; 
charset=utf-8").build();
         } else {
             // TODO: How to parse an ErrorMessage?
             // create an Response with the the Error?
@@ -184,7 +190,8 @@ public class SiteManagerRootResource ext
 //    @Path("/find")
 //    @Produces(MediaType.TEXT_HTML)
 //    public Response getFindPage() {
-//        return Response.ok(new Viewable("find", this), TEXT_HTML).build();
+//        return Response.ok(new Viewable("find", this))
+//        .header(HttpHeaders.CONTENT_TYPE, TEXT_HTML+"; 
charset=utf-8").build();
 //    }
     
     @GET
@@ -215,7 +222,8 @@ public class SiteManagerRootResource ext
             headers, supported, MediaType.APPLICATION_JSON_TYPE);
         if(name == null || name.isEmpty()){
             if(MediaType.TEXT_HTML_TYPE.isCompatible(acceptedMediaType)){
-                return Response.ok(new Viewable("find", this), 
TEXT_HTML).build();        
+                return Response.ok(new Viewable("find", this))
+                .header(HttpHeaders.CONTENT_TYPE, TEXT_HTML+"; 
charset=utf-8").build();        
             } else {
                 return Response.status(Status.BAD_REQUEST)
                     .entity("The name must not be null nor empty for find 
requests. Missing parameter name.\n")
@@ -234,12 +242,14 @@ public class SiteManagerRootResource ext
             ReferencedSiteManager.class, context);
         FieldQuery query = JerseyUtils.createFieldQueryForFindRequest(name, 
field, language,
             limit == null || limit < 1 ? DEFAULT_FIND_RESULT_LIMIT : limit, 
offset);
-        return Response.ok(referencedSiteManager.find(query), 
acceptedMediaType).build();
+        return Response.ok(referencedSiteManager.find(query))
+        .header(HttpHeaders.CONTENT_TYPE, acceptedMediaType+"; 
charset=utf-8").build();
     }
     @GET
     @Path("/query")
     public Response getQueryDocumentation(){
-        return Response.ok(new Viewable("query", this), TEXT_HTML).build();    
    
+        return Response.ok(new Viewable("query", this))
+        .header(HttpHeaders.CONTENT_TYPE, TEXT_HTML+"; 
charset=utf-8").build();        
     }
     /**
      * Allows to parse any kind of {@link FieldQuery} in its JSON 
Representation.
@@ -276,7 +286,8 @@ public class SiteManagerRootResource ext
                     .header(HttpHeaders.ACCEPT, acceptedMediaType).build();
             }
         } else {
-            return Response.ok(referencedSiteManager.find(query), 
acceptedMediaType).build();
+            return Response.ok(referencedSiteManager.find(query))
+            .header(HttpHeaders.CONTENT_TYPE, acceptedMediaType+"; 
charset=utf-8").build();
         }
     }
 

Modified: 
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/writers/JettisonWriter.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/writers/JettisonWriter.java?rev=1177503&r1=1177502&r2=1177503&view=diff
==============================================================================
--- 
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/writers/JettisonWriter.java
 (original)
+++ 
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/writers/JettisonWriter.java
 Fri Sep 30 04:21:23 2011
@@ -22,6 +22,7 @@ import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
 
 import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.ext.MessageBodyWriter;
@@ -31,6 +32,8 @@ import org.codehaus.jettison.json.JSONOb
 
 public class JettisonWriter implements MessageBodyWriter<Object> {
 
+    public static final String DEFAULT_ENCODING = "UTF-8";
+ 
     @Override
     public long getSize(Object result, Class<?> type, Type genericType,
             Annotation[] annotations, MediaType mediaType) {
@@ -48,7 +51,11 @@ public class JettisonWriter implements M
             Annotation[] annotations, MediaType mediaType,
             MultivaluedMap<String, Object> httpHeaders, OutputStream 
entityStream)
             throws IOException, WebApplicationException {
-        entityStream.write(value.toString().getBytes("UTF-8"));
+        String encoding = mediaType.getParameters().get("charset");
+        if(encoding == null){
+            encoding = DEFAULT_ENCODING;
+        }
+        entityStream.write(value.toString().getBytes(encoding));
     }
 
 }

Modified: 
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/writers/QueryResultListWriter.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/writers/QueryResultListWriter.java?rev=1177503&r1=1177502&r2=1177503&view=diff
==============================================================================
--- 
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/writers/QueryResultListWriter.java
 (original)
+++ 
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/writers/QueryResultListWriter.java
 Fri Sep 30 04:21:23 2011
@@ -29,6 +29,9 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
 
 import javax.servlet.ServletContext;
 import javax.ws.rs.Produces;
@@ -51,14 +54,30 @@ import org.codehaus.jettison.json.JSONOb
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import scala.actors.threadpool.Arrays;
+
 
 /**
  * TODO: Replace with Serializer infrastructure similar to {@link Serializer}
  */
 @Provider
-@Produces({APPLICATION_JSON, N3, N_TRIPLE, RDF_XML, TURTLE, X_TURTLE, 
RDF_JSON})
+//@Produces({APPLICATION_JSON, N3, N_TRIPLE, RDF_XML, TURTLE, X_TURTLE, 
RDF_JSON})
 public class QueryResultListWriter implements 
MessageBodyWriter<QueryResultList<?>> {
 
+    protected static final Set<String> produces;
+    static {
+        Set<String> p = new HashSet<String>();
+        p.add(APPLICATION_JSON);
+        p.add(N3);
+        p.add(N_TRIPLE);
+        p.add(RDF_XML);
+        p.add(TURTLE);
+        p.add(X_TURTLE);
+        p.add(RDF_JSON);
+        produces = Collections.unmodifiableSet(p);
+    }
+    public static final String DEFAULT_ENCODING = "UTF-8";
+    
     @SuppressWarnings("unused")
     private final Logger log = 
LoggerFactory.getLogger(QueryResultListWriter.class);
     
@@ -76,24 +95,31 @@ public class QueryResultListWriter imple
 
     @Override
     public boolean isWriteable(Class<?> type, Type genericType, Annotation[] 
annotations, MediaType mediaType) {
-        return QueryResultList.class.isAssignableFrom(type);
+        String mediaTypeString = 
mediaType.getType()+'/'+mediaType.getSubtype();
+        return QueryResultList.class.isAssignableFrom(type) &&
+            produces.contains(mediaTypeString);
+        
     }
 
     @Override
     public void writeTo(QueryResultList<?> resultList, Class<?> doNotUse, Type 
genericType,
             Annotation[] annotations, MediaType mediaType, 
MultivaluedMap<String, Object> httpHeaders,
             OutputStream entityStream) throws IOException, 
WebApplicationException {
-//        Class<?> genericClass = (Class<?>) genericType;
-        if (APPLICATION_JSON.equals(mediaType.toString())) {
+        String mediaTypeString = 
mediaType.getType()+'/'+mediaType.getSubtype();
+        String encoding = mediaType.getParameters().get("charset");
+        if(encoding == null){
+            encoding = DEFAULT_ENCODING;
+        }
+        if (APPLICATION_JSON.equals(mediaTypeString)) {
             try {
-                
IOUtils.write(QueryResultsToJSON.toJSON(resultList).toString(4), 
entityStream,"UTF-8");
+                
IOUtils.write(QueryResultsToJSON.toJSON(resultList).toString(4), 
entityStream,encoding);
             } catch (JSONException e) {
                 throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
             }
         } else { //RDF
             MGraph resultGraph = QueryResultsToRDF.toRDF(resultList);
             addFieldQuery(resultList.getQuery(),resultGraph);
-            getSerializer().serialize(entityStream, resultGraph, 
mediaType.toString());
+            getSerializer().serialize(entityStream, resultGraph, 
mediaTypeString);
         }
     }
     private void addFieldQuery(FieldQuery query, MGraph resultGraph) {

Modified: 
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/writers/RepresentationWriter.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/writers/RepresentationWriter.java?rev=1177503&r1=1177502&r2=1177503&view=diff
==============================================================================
--- 
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/writers/RepresentationWriter.java
 (original)
+++ 
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/writers/RepresentationWriter.java
 Fri Sep 30 04:21:23 2011
@@ -42,9 +42,9 @@ import org.apache.stanbol.entityhub.serv
 import org.codehaus.jettison.json.JSONException;
 
 @Provider
-@Produces( {MediaType.APPLICATION_JSON, SupportedFormat.N3, 
SupportedFormat.N_TRIPLE,
-            SupportedFormat.RDF_XML, SupportedFormat.TURTLE, 
SupportedFormat.X_TURTLE,
-            SupportedFormat.RDF_JSON})
+//@Produces( {MediaType.APPLICATION_JSON, SupportedFormat.N3, 
SupportedFormat.N_TRIPLE,
+//            SupportedFormat.RDF_XML, SupportedFormat.TURTLE, 
SupportedFormat.X_TURTLE,
+//            SupportedFormat.RDF_JSON})
 public class RepresentationWriter implements MessageBodyWriter<Representation> 
{
 
     public static final Set<String> supportedMediaTypes;
@@ -59,6 +59,7 @@ public class RepresentationWriter implem
         types.add(SupportedFormat.X_TURTLE);
         supportedMediaTypes = Collections.unmodifiableSet(types);
     }
+    public static final String DEFAULT_ENCODING = "UTF-8";
     @Context
     protected ServletContext servletContext;
 
@@ -73,7 +74,8 @@ public class RepresentationWriter implem
 
     @Override
     public boolean isWriteable(Class<?> type, Type genericType, Annotation[] 
annotations, MediaType mediaType) {
-        return Representation.class.isAssignableFrom(type) && 
supportedMediaTypes.contains(mediaType.toString());
+        String mediaTypeString = 
mediaType.getType()+'/'+mediaType.getSubtype();
+        return Representation.class.isAssignableFrom(type) && 
supportedMediaTypes.contains(mediaTypeString);
     }
 
     @Override
@@ -84,15 +86,20 @@ public class RepresentationWriter implem
                         MediaType mediaType,
                         MultivaluedMap<String,Object> httpHeaders,
                         OutputStream entityStream) throws IOException, 
WebApplicationException {
-        if (mediaType == null || 
MediaType.APPLICATION_JSON.equals(mediaType.toString())) {
+        String mediaTypeString = 
mediaType.getType()+'/'+mediaType.getSubtype();
+        String encoding = mediaType.getParameters().get("charset");
+        if(encoding == null){
+            encoding = DEFAULT_ENCODING;
+        }
+        if (MediaType.APPLICATION_JSON.equals(mediaTypeString)) {
             try {
-                IOUtils.write(EntityToJSON.toJSON(rep).toString(4), 
entityStream);
+                IOUtils.write(EntityToJSON.toJSON(rep).toString(4), 
entityStream,encoding);
             } catch (JSONException e) {
                 throw new WebApplicationException(e, 
Status.INTERNAL_SERVER_ERROR);
             }
         } else { // RDF
             Serializer ser = 
ContextHelper.getServiceFromContext(Serializer.class, servletContext);
-            ser.serialize(entityStream, EntityToRDF.toRDF(rep), 
mediaType.toString());
+            ser.serialize(entityStream, EntityToRDF.toRDF(rep), 
mediaTypeString);
         }
         
     }

Modified: 
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/writers/SignWriter.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/writers/SignWriter.java?rev=1177503&r1=1177502&r2=1177503&view=diff
==============================================================================
--- 
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/writers/SignWriter.java
 (original)
+++ 
incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/writers/SignWriter.java
 Fri Sep 30 04:21:23 2011
@@ -49,9 +49,9 @@ import org.codehaus.jettison.json.JSONEx
  * 
  */
 @Provider
-@Produces( {MediaType.APPLICATION_JSON, SupportedFormat.N3, 
SupportedFormat.N_TRIPLE,
-            SupportedFormat.RDF_XML, SupportedFormat.TURTLE, 
SupportedFormat.X_TURTLE,
-            SupportedFormat.RDF_JSON})
+//@Produces( {MediaType.APPLICATION_JSON, SupportedFormat.N3, 
SupportedFormat.N_TRIPLE,
+//            SupportedFormat.RDF_XML, SupportedFormat.TURTLE, 
SupportedFormat.X_TURTLE,
+//            SupportedFormat.RDF_JSON})
 public class SignWriter implements MessageBodyWriter<Entity> {
     
     public static final Set<String> supportedMediaTypes;
@@ -66,6 +66,7 @@ public class SignWriter implements Messa
         types.add(SupportedFormat.X_TURTLE);
         supportedMediaTypes = Collections.unmodifiableSet(types);
     }
+    public static final String DEFAULT_ENCODING = "UTF-8";
 
     @Context
     protected ServletContext servletContext;
@@ -85,7 +86,8 @@ public class SignWriter implements Messa
     
     @Override
     public boolean isWriteable(Class<?> type, Type genericType, Annotation[] 
annotations, MediaType mediaType) {
-        return Entity.class.isAssignableFrom(type) && 
supportedMediaTypes.contains(mediaType.toString());
+        String mediaTypeString = 
mediaType.getType()+'/'+mediaType.getSubtype();
+        return Entity.class.isAssignableFrom(type) && 
supportedMediaTypes.contains(mediaTypeString);
     }
     
     @Override
@@ -96,14 +98,19 @@ public class SignWriter implements Messa
                         MediaType mediaType,
                         MultivaluedMap<String,Object> httpHeaders,
                         OutputStream entityStream) throws IOException, 
WebApplicationException {
-        if (mediaType == null || 
MediaType.APPLICATION_JSON.equals(mediaType.toString())) {
+        String mediaTypeString = 
mediaType.getType()+'/'+mediaType.getSubtype();
+        String encoding = mediaType.getParameters().get("charset");
+        if(encoding == null){
+            encoding = DEFAULT_ENCODING;
+        }
+        if (MediaType.APPLICATION_JSON.equals(mediaTypeString)) {
             try {
-                IOUtils.write(EntityToJSON.toJSON(sign).toString(4), 
entityStream);
+                IOUtils.write(EntityToJSON.toJSON(sign).toString(4), 
entityStream,encoding);
             } catch (JSONException e) {
                 throw new WebApplicationException(e, 
Status.INTERNAL_SERVER_ERROR);
             }
         } else { // RDF
-            getSerializer().serialize(entityStream, EntityToRDF.toRDF(sign), 
mediaType.toString());
+            getSerializer().serialize(entityStream, EntityToRDF.toRDF(sign), 
mediaTypeString);
         }
     }
     


Reply via email to