use of org.apache.stanbol.enhancer.servicesapi.ContentItem in project stanbol by apache.
the class ContentFunction method apply.
@Override
public Collection<RDFTerm> apply(ContentItemBackend backend, RDFTerm context, Collection<RDFTerm>... args) throws IllegalArgumentException {
ContentItem ci = backend.getContentItem();
Set<String> mimeTypes;
if (args == null || args.length < 1) {
mimeTypes = null;
} else {
mimeTypes = new HashSet<String>();
for (Iterator<RDFTerm> params = Collections.concat(args).iterator(); params.hasNext(); ) {
RDFTerm param = params.next();
String mediaTypeString = backend.stringValue(param);
try {
mimeTypes.add(parseMimeType(mediaTypeString).get(null));
} catch (IllegalArgumentException e) {
log.warn(String.format("Invalid mediaType '%s' (based on RFC 2046) parsed!", mediaTypeString), e);
}
}
}
Collection<RDFTerm> result;
Blob blob;
if (mimeTypes == null || mimeTypes.isEmpty()) {
blob = ci.getBlob();
} else {
Entry<IRI, Blob> entry = ContentItemHelper.getBlob(ci, mimeTypes);
blob = entry != null ? entry.getValue() : null;
}
if (blob == null) {
result = java.util.Collections.emptySet();
} else {
String charset = blob.getParameter().get("charset");
try {
if (charset != null) {
result = java.util.Collections.singleton(backend.createLiteral(IOUtils.toString(blob.getStream(), charset)));
} else {
//binary content
byte[] data = IOUtils.toByteArray(blob.getStream());
result = java.util.Collections.singleton((RDFTerm) lf.createTypedLiteral(data));
}
} catch (IOException e) {
throw new IllegalStateException("Unable to read contents from Blob '" + blob.getMimeType() + "' of ContentItem " + ci.getUri(), e);
}
}
return result;
}
use of org.apache.stanbol.enhancer.servicesapi.ContentItem in project stanbol by apache.
the class ContentItemBackendTest method testContentWithAdditionalMetadata.
@Test
public void testContentWithAdditionalMetadata() throws IOException, LDPathParseException {
byte[] content = "text content".getBytes();
IRI uri = ContentItemHelper.makeDefaultUrn(content);
ContentItem contentItem = ciFactory.createContentItem(uri, new ByteArraySource(content, "text/plain; charset=UTF-8"));
Graph tc = new SimpleGraph();
Literal literal = LiteralFactory.getInstance().createTypedLiteral("Michael Jackson");
IRI subject = new IRI("dummyUri");
tc.add(new TripleImpl(subject, new IRI("http://xmlns.com/foaf/0.1/givenName"), literal));
contentItem.addPart(new IRI(uri.getUnicodeString() + "_additionalMetadata"), tc);
ContentItemBackend ciBackend = new ContentItemBackend(contentItem, true);
LDPath<RDFTerm> ldPath = new LDPath<RDFTerm>(ciBackend, EnhancerLDPath.getConfig());
Collection<RDFTerm> result = ldPath.pathQuery(subject, "foaf:givenName", null);
assertTrue("Additional metadata cannot be found", result.contains(literal));
}
use of org.apache.stanbol.enhancer.servicesapi.ContentItem in project stanbol by apache.
the class GenericEnhancerUiResource method enhanceFromForm.
/**
* Form-based OpenCalais-compatible interface
*
* TODO: should we parse the OpenCalais paramsXML and find the closest Stanbol Enhancer semantics too?
*
* Note: the format parameter is not part of the official API
*
* @throws EngineException
* if the content is somehow corrupted
* @throws IOException
*/
@POST
@Consumes(APPLICATION_FORM_URLENCODED)
public Response enhanceFromForm(@FormParam("content") String content, @FormParam("format") String format, @FormParam("ajax") boolean buildAjaxview, @Context HttpHeaders headers) throws EnhancementException, IOException {
log.info("enhance from From: " + content);
if (content == null) {
// content parameter
throw new WebApplicationException(Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).entity("Parsing Content as 'application/x-www-form-urlencoded' is not supported!" + "Please directly POST the content and set the 'Content-Type' " + "header to the media type of the parsed content. 'application/" + "octet-stream' SHOULD BE used if the media type of the parsed " + "content is not known.\n").build());
}
ContentItem ci = ciFactory.createContentItem(new StringSource(content));
if (!buildAjaxview) {
//rewrite to a normal EnhancementRequest
return enhanceFromData(ci, false, null, false, null, false, null, headers);
} else {
//enhance and build the AJAX response
EnhancementException enhancementException;
try {
enhance(ci, null);
enhancementException = null;
} catch (EnhancementException e) {
enhancementException = e;
}
ContentItemResource contentItemResource = new ContentItemResource(null, ci, getUriInfo(), "", serializer, getLayoutConfiguration(), enhancementException);
contentItemResource.setRdfSerializationFormat(format);
Viewable ajaxView = new Viewable("/ajax/contentitem", contentItemResource, ContentItemResource.class);
ResponseBuilder rb = Response.ok(ajaxView);
rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=UTF-8");
//addCORSOrigin(servletContext, rb, headers);
return rb.build();
}
}
use of org.apache.stanbol.enhancer.servicesapi.ContentItem in project stanbol by apache.
the class ContentItemTest method removeNonExistentPartByUri.
@Test(expected = NoSuchPartException.class)
public void removeNonExistentPartByUri() throws IOException {
ContentItem ci = createContentItem(contentSource);
ci.removePart(new IRI("urn:does.not.exist:and.can.not.be.removed"));
}
use of org.apache.stanbol.enhancer.servicesapi.ContentItem in project stanbol by apache.
the class ContentItemTest method replaceMainPart.
/**
* The ContentItem MUST NOT allow to replace the main content part (the
* Blob stored at index 0)
*/
@Test(expected = IllegalArgumentException.class)
public void replaceMainPart() throws IOException {
ContentItem ci = createContentItem(contentSource);
IRI mainPart = ci.getPartUri(0);
ci.addPart(mainPart, new Date());
}
Aggregations