Author: rwesten
Date: Tue May 13 05:35:51 2014
New Revision: 1594132
URL: http://svn.apache.org/r1594132
Log:
merged fixes for STANBOL-1339 and STANBOL-1340 to trunk
Modified:
stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/GraphWriter.java
stanbol/trunk/enhancer/jersey/src/main/java/org/apache/stanbol/enhancer/jersey/writers/ContentItemWriter.java
stanbol/trunk/integration-tests/src/test/java/org/apache/stanbol/commons/httpqueryheaders/it/HttpQueryHeaderPostTest.java
stanbol/trunk/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/DefaultChainTest.java
Modified:
stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/GraphWriter.java
URL:
http://svn.apache.org/viewvc/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/GraphWriter.java?rev=1594132&r1=1594131&r2=1594132&view=diff
==============================================================================
---
stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/GraphWriter.java
(original)
+++
stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/GraphWriter.java
Tue May 13 05:35:51 2014
@@ -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.IOException;
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 @@ import org.slf4j.LoggerFactory;
// @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 @@ public class GraphWriter implements Mess
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 @@ public class GraphWriter implements Mess
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);
}
}
Modified:
stanbol/trunk/enhancer/jersey/src/main/java/org/apache/stanbol/enhancer/jersey/writers/ContentItemWriter.java
URL:
http://svn.apache.org/viewvc/stanbol/trunk/enhancer/jersey/src/main/java/org/apache/stanbol/enhancer/jersey/writers/ContentItemWriter.java?rev=1594132&r1=1594131&r2=1594132&view=diff
==============================================================================
---
stanbol/trunk/enhancer/jersey/src/main/java/org/apache/stanbol/enhancer/jersey/writers/ContentItemWriter.java
(original)
+++
stanbol/trunk/enhancer/jersey/src/main/java/org/apache/stanbol/enhancer/jersey/writers/ContentItemWriter.java
Tue May 13 05:35:51 2014
@@ -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 @@ public class ContentItemWriter implement
*/
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 @@ public class ContentItemWriter implement
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 @@ public class ContentItemWriter implement
@Override
public void writeTo(OutputStream out) throws IOException {
- getSerializer().serialize(out, graph,
getMediaType()+'/'+getSubType());
+ serializer.serialize(out, graph, getMediaType()+'/'+getSubType());
}
}
Modified:
stanbol/trunk/integration-tests/src/test/java/org/apache/stanbol/commons/httpqueryheaders/it/HttpQueryHeaderPostTest.java
URL:
http://svn.apache.org/viewvc/stanbol/trunk/integration-tests/src/test/java/org/apache/stanbol/commons/httpqueryheaders/it/HttpQueryHeaderPostTest.java?rev=1594132&r1=1594131&r2=1594132&view=diff
==============================================================================
---
stanbol/trunk/integration-tests/src/test/java/org/apache/stanbol/commons/httpqueryheaders/it/HttpQueryHeaderPostTest.java
(original)
+++
stanbol/trunk/integration-tests/src/test/java/org/apache/stanbol/commons/httpqueryheaders/it/HttpQueryHeaderPostTest.java
Tue May 13 05:35:51 2014
@@ -68,7 +68,7 @@ public class HttpQueryHeaderPostTest ext
)
.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\"");
Modified:
stanbol/trunk/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/DefaultChainTest.java
URL:
http://svn.apache.org/viewvc/stanbol/trunk/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/DefaultChainTest.java?rev=1594132&r1=1594131&r2=1594132&view=diff
==============================================================================
---
stanbol/trunk/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/DefaultChainTest.java
(original)
+++
stanbol/trunk/integration-tests/src/test/java/org/apache/stanbol/enhancer/it/DefaultChainTest.java
Tue May 13 05:35:51 2014
@@ -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 @@ public class DefaultChainTest extends En
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 @@ public class DefaultChainTest extends En
.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 @@ public class DefaultChainTest extends En
.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 @@ public class DefaultChainTest extends En
.withContent("Nothing")
)
.assertStatus(200)
+ .assertCharset(UTF8.name())
.assertContentType(ACCEPT_FORMAT_TEST_DATA[i+1])
.assertContentRegexp(ACCEPT_FORMAT_TEST_DATA[i+2])
.generateDocumentation(documentor,