merged fixes for STANBOL-1339 and STANBOL-1340 to trunk
git-svn-id: https://svn.apache.org/repos/asf/stanbol/trunk@1594132 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/GraphWriter.java b/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/GraphWriter.java
index a8d842e..ad4a877 100644
--- a/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/GraphWriter.java
+++ b/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/GraphWriter.java
@@ -16,6 +16,7 @@
*/
package org.apache.stanbol.commons.web.base.writers;
+import static javax.ws.rs.core.HttpHeaders.CONTENT_TYPE;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM;
import static javax.ws.rs.core.MediaType.TEXT_PLAIN;
@@ -30,11 +31,13 @@
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
+import java.nio.charset.Charset;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
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;
@@ -57,6 +60,13 @@
// @Produces({TEXT_PLAIN, N3, N_TRIPLE, RDF_XML, TURTLE, X_TURTLE, RDF_JSON, APPLICATION_JSON})
public class GraphWriter implements MessageBodyWriter<TripleCollection> {
+ /**
+ * The media type for JSON-LD (<code>application/ld+json</code>)
+ */
+ private static String APPLICATION_LD_JSON = "application/ld+json";
+
+ private static final Charset UTF8 = Charset.forName("UTF-8");
+
private final Logger log = LoggerFactory.getLogger(GraphWriter.class);
public static final Set<String> supportedMediaTypes;
static {
@@ -70,6 +80,7 @@
types.add(RDF_JSON);
types.add(APPLICATION_JSON);
types.add(APPLICATION_OCTET_STREAM);
+ types.add(APPLICATION_LD_JSON);
supportedMediaTypes = Collections.unmodifiableSet(types);
}
@@ -99,15 +110,15 @@
MultivaluedMap<String,Object> httpHeaders,
OutputStream entityStream) throws IOException, WebApplicationException {
- long start = System.currentTimeMillis();
String mediaTypeString = mediaType.getType() + '/' + mediaType.getSubtype();
if (mediaType.isWildcardType() || TEXT_PLAIN.equals(mediaTypeString)
|| APPLICATION_OCTET_STREAM.equals(mediaTypeString)) {
- httpHeaders.putSingle("Content-Type", APPLICATION_JSON);
- serializer.serialize(entityStream, t, APPLICATION_JSON);
- } else {
- serializer.serialize(entityStream, t, mediaTypeString);
+ mediaTypeString = APPLICATION_LD_JSON;
}
+ httpHeaders.putSingle(CONTENT_TYPE, mediaTypeString + ";charset=" + UTF8.name());
+
+ long start = System.currentTimeMillis();
+ serializer.serialize(entityStream, t, mediaTypeString);
log.debug("Serialized {} in {}ms", t.size(), System.currentTimeMillis() - start);
}
}
diff --git a/enhancer/jersey/src/main/java/org/apache/stanbol/enhancer/jersey/writers/ContentItemWriter.java b/enhancer/jersey/src/main/java/org/apache/stanbol/enhancer/jersey/writers/ContentItemWriter.java
index c421ac8..1791438 100644
--- a/enhancer/jersey/src/main/java/org/apache/stanbol/enhancer/jersey/writers/ContentItemWriter.java
+++ b/enhancer/jersey/src/main/java/org/apache/stanbol/enhancer/jersey/writers/ContentItemWriter.java
@@ -16,7 +16,6 @@
*/
package org.apache.stanbol.enhancer.jersey.writers;
-import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;
import static javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM_TYPE;
import static javax.ws.rs.core.MediaType.MULTIPART_FORM_DATA_TYPE;
import static javax.ws.rs.core.MediaType.TEXT_PLAIN_TYPE;
@@ -120,19 +119,35 @@
*/
private static final MediaType MULTIPART = MediaType.valueOf(MULTIPART_FORM_DATA_TYPE.getType()+"/*");
private static final Charset UTF8 = Charset.forName("UTF-8");
+ /**
+ * The media type for JSON-LD (<code>application/ld+json</code>)
+ */
+ private static String APPLICATION_LD_JSON = "application/ld+json";
+ private static MediaType APPLICATION_LD_JSON_TYPE = MediaType.valueOf(APPLICATION_LD_JSON);
private static final MediaType DEFAULT_RDF_FORMAT = new MediaType(
- APPLICATION_JSON_TYPE.getType(),
- APPLICATION_JSON_TYPE.getSubtype(),
- Collections.singletonMap("charset", UTF8.toString()));
+ APPLICATION_LD_JSON_TYPE.getType(),
+ APPLICATION_LD_JSON_TYPE.getSubtype(),
+ Collections.singletonMap("charset", UTF8.name()));
@Reference
private Serializer serializer;
- protected Serializer getSerializer(){
- return serializer;
- }
-
- @Override
+ /**
+ * Default Constructor used by OSGI. This expects that the {@link #serializer}
+ * is injected
+ */
+ public ContentItemWriter(){};
+ /**
+ * Creates a {@link ContentItemWriter} by using the parsed Clerezza
+ * {@link Serializer}. Intended to be used by unit tests or when running not
+ * in an OSGI environment.
+ * @param serializer
+ */
+ public ContentItemWriter(Serializer serializer) {
+ this.serializer = serializer;
+ }
+
+ @Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return //MediaType.MULTIPART_FORM_DATA_TYPE.isCompatible(mediaType) &&
ContentItem.class.isAssignableFrom(type);
@@ -162,17 +177,19 @@
if(!MULTIPART.isCompatible(mediaType)){ //two possible cases
if(!omitMetadata){ // (1) just return the RDF data
//(1.a) Backward support for default dataType if no Accept header is set
+ StringBuilder ctb = new StringBuilder();
if (mediaType.isWildcardType() ||
TEXT_PLAIN_TYPE.isCompatible(mediaType) ||
APPLICATION_OCTET_STREAM_TYPE.isCompatible(mediaType)) {
- mediaType = new MediaType(APPLICATION_JSON_TYPE.getType(),
- APPLICATION_JSON_TYPE.getSubtype(),
- //Clerezza serialisers are hard coded to use UTF-8
- Collections.singletonMap("charset", UTF8.toString()));
- httpHeaders.putSingle("Content-Type", mediaType.toString());
+ ctb.append(APPLICATION_LD_JSON);
+ } else {
+ ctb.append(mediaType.getType()).append('/').append(mediaType.getSubtype());
}
+ ctb.append(";charset=").append(UTF8.name());
+ String contentType = ctb.toString();
+ httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, contentType);
try {
- getSerializer().serialize(entityStream, ci.getMetadata(), mediaType.toString());
+ serializer.serialize(entityStream, ci.getMetadata(), contentType);
} catch (UnsupportedSerializationFormatException e) {
throw new WebApplicationException("The enhancement results "
+ "cannot be serialized in the requested media type: "
@@ -525,7 +542,7 @@
@Override
public void writeTo(OutputStream out) throws IOException {
- getSerializer().serialize(out, graph, getMediaType()+'/'+getSubType());
+ serializer.serialize(out, graph, getMediaType()+'/'+getSubType());
}
}
diff --git a/integration-tests/src/test/java/org/apache/stanbol/commons/httpqueryheaders/it/HttpQueryHeaderPostTest.java b/integration-tests/src/test/java/org/apache/stanbol/commons/httpqueryheaders/it/HttpQueryHeaderPostTest.java
index d521d70..0a13ebd 100644
--- a/integration-tests/src/test/java/org/apache/stanbol/commons/httpqueryheaders/it/HttpQueryHeaderPostTest.java
+++ b/integration-tests/src/test/java/org/apache/stanbol/commons/httpqueryheaders/it/HttpQueryHeaderPostTest.java
@@ -68,7 +68,7 @@
)
.assertStatus(200)
//check for JSON-LD (the default content type
- .assertContentType("application/json")
+ .assertContentType("application/ld+json")
.assertContentRegexp("entity-reference\": \"http://dbpedia.org/resource/London\"",
"creator\": \"org.apache.stanbol.enhancer.engines.langdetect.LanguageDetectionEnhancementEngine\"",
"creator\": \"org.apache.stanbol.enhancer.engines.entitytagging.impl.NamedEntityTaggingEngine\"");
diff --git a/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/DefaultChainTest.java b/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/DefaultChainTest.java
index 065963c..54ff36c 100644
--- a/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/DefaultChainTest.java
+++ b/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/DefaultChainTest.java
@@ -16,6 +16,8 @@
*/
package org.apache.stanbol.enhancer.it;
+import java.nio.charset.Charset;
+
import org.apache.stanbol.commons.testing.http.RequestDocumentor;
import org.junit.Test;
import org.slf4j.Logger;
@@ -26,6 +28,8 @@
private final Logger log = LoggerFactory.getLogger(DefaultChainTest.class);
+ protected final static Charset UTF8 = Charset.forName("UTF-8");
+
private final RequestDocumentor documentor = new RequestDocumentor(getClass().getName());
/**
* Contains values grouped by three elements: Accept header,
@@ -71,6 +75,7 @@
.withContent("The Stanbol enhancer can detect famous cities such as Paris and people such as Bob Marley.")
)
.assertStatus(200)
+ .assertCharset(UTF8.name())
.assertContentRegexp(
//check execution metadata
"http://stanbol.apache.org/ontology/enhancer/executionmetadata#executionPart",
@@ -109,6 +114,7 @@
.withContent("The Stanbol enhancer can detect famous cities such as Paris and people such as Bob Marley.")
)
.assertStatus(200)
+ .assertCharset(UTF8.name())
.assertContentRegexp(
"<http://fise.iks-project.eu/ontology/extracted-from> <"+uri+"> ."
);
@@ -123,6 +129,7 @@
.withContent("Nothing")
)
.assertStatus(200)
+ .assertCharset(UTF8.name())
.assertContentType(ACCEPT_FORMAT_TEST_DATA[i+1])
.assertContentRegexp(ACCEPT_FORMAT_TEST_DATA[i+2])
.generateDocumentation(documentor,