use of org.apache.stanbol.enhancer.servicesapi.impl.StreamSource in project stanbol by apache.
the class FstLinkingEngineTest method setupTest.
/**
* Initialises the {@link #ci} and {@link #content} fields for tests.
* It creates a ContentItem containing a '<code>plain/text</code>'
* {@link Blob} for the {@value #TEST_TEXT_FILE} and an {@link AnalysedText}
* filled with the NLP analysis results stored in
* {@link #TEST_TEXT_NLP_FILE}
* @return the {@link ContentItem} as used for the tests
* @throws IOException on any IO releated error while reading the test files
*/
@Before
public void setupTest() throws IOException {
// create a contentItem for the plain text used for testing
InputStream is = FstLinkingEngineTest.class.getClassLoader().getResourceAsStream(TEST_TEXT_FILE);
Assert.assertNotNull("Unable to load '" + TEST_TEXT_FILE + "' via classpath", is);
ContentItem ci = cif.createContentItem(new StreamSource(is, "text/plain"));
AnalysedText at = atf.createAnalysedText(ci, ci.getBlob());
is.close();
// parse the prepared NLP results and add it to the ContentItem
is = FstLinkingEngineTest.class.getClassLoader().getResourceAsStream(TEST_TEXT_NLP_FILE);
Assert.assertNotNull("Unable to load '" + TEST_TEXT_NLP_FILE + "' via classpath", is);
AnalyzedTextParser.getDefaultInstance().parse(is, Charset.forName("UTF-8"), at);
is.close();
// set the language of the contentItem
ci.getMetadata().add(new TripleImpl(ci.getUri(), DC_LANGUAGE, EN_LANGUAGE));
// set the contentItem and also the content
this.ci = ci;
this.content = at.getText().toString();
}
use of org.apache.stanbol.enhancer.servicesapi.impl.StreamSource in project stanbol by apache.
the class ContentItemReader method readFrom.
@Override
public ContentItem readFrom(Class<ContentItem> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
// boolean withMetadata = withMetadata(httpHeaders);
ContentItem contentItem = null;
IRI contentItemId = getContentItemId();
if (log.isTraceEnabled()) {
// NOTE: enabling TRACE level logging will copy the parsed content
// into a BYTE array
log.trace("Parse ContentItem from");
log.trace(" - MediaType: {}", mediaType);
log.trace(" - Headers:");
for (Entry<String, List<String>> header : httpHeaders.entrySet()) {
log.trace(" {}: {}", header.getKey(), header.getValue());
}
byte[] content = IOUtils.toByteArray(entityStream);
log.trace("content: \n{}", new String(content, "UTF-8"));
IOUtils.closeQuietly(entityStream);
entityStream = new ByteArrayInputStream(content);
}
Set<String> parsedContentIds = new HashSet<String>();
if (mediaType.isCompatible(MULTIPART)) {
log.debug(" - parse Multipart MIME ContentItem");
// try to read ContentItem from "multipart/from-data"
Graph metadata = null;
FileItemIterator fileItemIterator;
try {
fileItemIterator = fu.getItemIterator(new MessageBodyReaderContext(entityStream, mediaType));
while (fileItemIterator.hasNext()) {
FileItemStream fis = fileItemIterator.next();
if (fis.getFieldName().equals("metadata")) {
if (contentItem != null) {
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("The Multipart MIME part with the 'metadata' " + "MUST BE before the MIME part containing the " + "'content'!").build());
}
// only used if not parsed as query param
if (contentItemId == null && fis.getName() != null && !fis.getName().isEmpty()) {
contentItemId = new IRI(fis.getName());
}
metadata = new IndexedGraph();
try {
getParser().parse(metadata, fis.openStream(), fis.getContentType());
} catch (Exception e) {
throw new WebApplicationException(e, Response.status(Response.Status.BAD_REQUEST).entity(String.format("Unable to parse Metadata " + "from Multipart MIME part '%s' (" + "contentItem: %s| contentType: %s)", fis.getFieldName(), fis.getName(), fis.getContentType())).build());
}
} else if (fis.getFieldName().equals("content")) {
contentItem = createContentItem(contentItemId, metadata, fis, parsedContentIds);
} else if (fis.getFieldName().equals("properties") || fis.getFieldName().equals(REQUEST_PROPERTIES_URI.getUnicodeString())) {
// parse the RequestProperties
if (contentItem == null) {
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Multipart MIME parts for " + "Request Properties MUST BE after the " + "MIME parts for 'metadata' AND 'content'").build());
}
MediaType propMediaType = MediaType.valueOf(fis.getContentType());
if (!APPLICATION_JSON_TYPE.isCompatible(propMediaType)) {
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Request Properties (Multipart MIME parts" + "with the name '" + fis.getFieldName() + "') MUST " + "BE encoded as 'appicaltion/json' (encountered: '" + fis.getContentType() + "')!").build());
}
String propCharset = propMediaType.getParameters().get("charset");
if (propCharset == null) {
propCharset = "UTF-8";
}
Map<String, Object> reqProp = ContentItemHelper.initRequestPropertiesContentPart(contentItem);
try {
reqProp.putAll(toMap(new JSONObject(IOUtils.toString(fis.openStream(), propCharset))));
} catch (JSONException e) {
throw new WebApplicationException(e, Response.status(Response.Status.BAD_REQUEST).entity("Unable to parse Request Properties from" + "Multipart MIME parts with the name 'properties'!").build());
}
} else {
// additional metadata as serialised RDF
if (contentItem == null) {
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Multipart MIME parts for additional " + "contentParts MUST BE after the MIME " + "parts for 'metadata' AND 'content'").build());
}
if (fis.getFieldName() == null || fis.getFieldName().isEmpty()) {
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Multipart MIME parts representing " + "ContentParts for additional RDF metadata" + "MUST define the contentParts URI as" + "'name' of the MIME part!").build());
}
Graph graph = new IndexedGraph();
try {
getParser().parse(graph, fis.openStream(), fis.getContentType());
} catch (Exception e) {
throw new WebApplicationException(e, Response.status(Response.Status.BAD_REQUEST).entity(String.format("Unable to parse RDF " + "for ContentPart '%s' ( contentType: %s)", fis.getName(), fis.getContentType())).build());
}
IRI contentPartId = new IRI(fis.getFieldName());
contentItem.addPart(contentPartId, graph);
}
}
if (contentItem == null) {
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("The parsed multipart content item does not contain " + "any content. The content is expected to be contained " + "in a MIME part with the name 'content'. This part can " + " be also a 'multipart/alternate' if multiple content " + "parts need to be included in requests.").build());
}
} catch (FileUploadException e) {
throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
}
} else {
// normal content
ContentItemFactory ciFactory = getContentItemFactory();
contentItem = ciFactory.createContentItem(contentItemId, new StreamSource(entityStream, mediaType.toString()));
// add the URI of the main content
parsedContentIds.add(contentItem.getPartUri(0).getUnicodeString());
}
// set the parsed contentIDs to the EnhancementProperties
Map<String, Object> ep = ContentItemHelper.initRequestPropertiesContentPart(contentItem);
parseEnhancementPropertiesFromParameters(ep);
ep.put(PARSED_CONTENT_URIS, Collections.unmodifiableSet(parsedContentIds));
// STANBOL-660: set the language of the content if explicitly parsed in the request
String contentLanguage = getContentLanguage();
if (!StringUtils.isBlank(contentLanguage)) {
// language codes are case insensitive ... so we convert to lower case
contentLanguage = contentLanguage.toLowerCase(Locale.ROOT);
createParsedLanguageAnnotation(contentItem, contentLanguage);
// previously only the dc:language property was set to the contentItem. However this
// information is only used as fallback if no Language annotation is present. However
// if a user explicitly parses the language he expects this language to be used
// so this was change with STANBOL-1417
// EnhancementEngineHelper.set(contentItem.getMetadata(), contentItem.getUri(),
// DC_LANGUAGE, new PlainLiteralImpl(contentLanguage));
}
return contentItem;
}
use of org.apache.stanbol.enhancer.servicesapi.impl.StreamSource in project stanbol by apache.
the class ContentSourceTest method checkMediaTypeForStreamSource.
/*
* Tests checking correct handling of parameters and default values
*/
@Test
public void checkMediaTypeForStreamSource() throws IOException {
ContentSource source = new StreamSource(new ByteArrayInputStream(DATA));
assertEquals(DEFAULT_MT, source.getMediaType());
source = new StreamSource(new ByteArrayInputStream(DATA), null);
assertEquals(DEFAULT_MT, source.getMediaType());
source = new StreamSource(new ByteArrayInputStream(DATA), null, HEADERS);
assertEquals(DEFAULT_MT, source.getMediaType());
source = new StreamSource(new ByteArrayInputStream(DATA), null, FILE_NAME, HEADERS);
assertEquals(DEFAULT_MT, source.getMediaType());
source = new StreamSource(new ByteArrayInputStream(DATA), MT);
assertEquals(MT, source.getMediaType());
source = new StreamSource(new ByteArrayInputStream(DATA), MT, HEADERS);
assertEquals(MT, source.getMediaType());
source = new StreamSource(new ByteArrayInputStream(DATA), MT, FILE_NAME, HEADERS);
assertEquals(MT, source.getMediaType());
// Parameters MUST BE preserved!
source = new StreamSource(new ByteArrayInputStream(DATA), MT_WITH_PARAM);
assertEquals(MT_WITH_PARAM, source.getMediaType());
source = new StreamSource(new ByteArrayInputStream(DATA), MT_WITH_PARAM, HEADERS);
assertEquals(MT_WITH_PARAM, source.getMediaType());
source = new StreamSource(new ByteArrayInputStream(DATA), MT_WITH_PARAM, FILE_NAME, HEADERS);
assertEquals(MT_WITH_PARAM, source.getMediaType());
}
use of org.apache.stanbol.enhancer.servicesapi.impl.StreamSource in project stanbol by apache.
the class ContentSourceTest method checkHeaders.
@Test
public void checkHeaders() throws IOException {
ContentSource source = new StreamSource(new ByteArrayInputStream(DATA), null, null, null);
assertNotNull(source.getHeaders());
assertTrue(source.getHeaders().isEmpty());
source = new StreamSource(new ByteArrayInputStream(DATA), null, null, HEADERS);
assertEquals(HEADERS, source.getHeaders());
source = new ByteArraySource(DATA, null, null, null);
assertNotNull(source.getHeaders());
assertTrue(source.getHeaders().isEmpty());
source = new ByteArraySource(DATA, null, null, HEADERS);
assertEquals(HEADERS, source.getHeaders());
}
use of org.apache.stanbol.enhancer.servicesapi.impl.StreamSource in project stanbol by apache.
the class ContentItemReader method createContentItem.
/**
* Creates a ContentItem
* @param id the ID or <code>null</code> if not known
* @param metadata the metadata or <code>null</code> if not parsed. NOTE that
* if <code>id == null</code> also <code>metadata == null</code> and
* <code>id != null</code> also <code>metadata != null</code>.
* @param content the {@link FileItemStream} of the MIME part representing
* the content. If {@link FileItemStream#getContentType()} is compatible with
* "multipart/*" than this will further parse for multiple parsed content
* version. In any other case the contents of the parsed {@link FileItemStream}
* will be directly add as content for the {@link ContentItem} created by
* this method.
* @param parsedContentParts used to add the IDs of parsed contentParts
* @return the created content item
* @throws IOException on any error while accessing the contents of the parsed
* {@link FileItemStream}
* @throws FileUploadException if the parsed contents are not correctly
* encoded Multipart MIME
*/
private ContentItem createContentItem(IRI id, Graph metadata, FileItemStream content, Set<String> parsedContentParts) throws IOException, FileUploadException {
MediaType partContentType = MediaType.valueOf(content.getContentType());
ContentItem contentItem = null;
ContentItemFactory ciFactory = getContentItemFactory();
if (MULTIPART.isCompatible(partContentType)) {
log.debug(" - multiple (alternate) ContentParts");
// multiple contentParts are parsed
FileItemIterator contentPartIterator = fu.getItemIterator(new MessageBodyReaderContext(content.openStream(), partContentType));
while (contentPartIterator.hasNext()) {
FileItemStream fis = contentPartIterator.next();
if (contentItem == null) {
log.debug(" - create ContentItem {} for content (type:{})", id, fis.getContentType());
contentItem = ciFactory.createContentItem(id, new StreamSource(fis.openStream(), fis.getContentType()), metadata);
} else {
log.debug(" - create Blob for content (type:{})", fis.getContentType());
Blob blob = ciFactory.createBlob(new StreamSource(fis.openStream(), fis.getContentType()));
IRI contentPartId = null;
if (fis.getFieldName() != null && !fis.getFieldName().isEmpty()) {
contentPartId = new IRI(fis.getFieldName());
} else {
// generating a random ID might break metadata
// TODO maybe we should throw an exception instead
contentPartId = new IRI("urn:contentpart:" + randomUUID());
}
log.debug(" ... add Blob {} to ContentItem {} with content (type:{})", new Object[] { contentPartId, id, fis.getContentType() });
contentItem.addPart(contentPartId, blob);
parsedContentParts.add(contentPartId.getUnicodeString());
}
}
} else {
log.debug(" - create ContentItem {} for content (type:{})", id, content.getContentType());
contentItem = ciFactory.createContentItem(id, new StreamSource(content.openStream(), content.getContentType()), metadata);
}
// add the URI of the main content to the parsed contentParts
parsedContentParts.add(contentItem.getPartUri(0).getUnicodeString());
return contentItem;
}
Aggregations