Search in sources :

Example 46 with ContentItem

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;
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Blob(org.apache.stanbol.enhancer.servicesapi.Blob) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) IOException(java.io.IOException) ContentItem(org.apache.stanbol.enhancer.servicesapi.ContentItem)

Example 47 with ContentItem

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));
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) LDPath(org.apache.marmotta.ldpath.LDPath) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) Literal(org.apache.clerezza.commons.rdf.Literal) ContentItemBackend(org.apache.stanbol.enhancer.ldpath.backend.ContentItemBackend) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) ContentItem(org.apache.stanbol.enhancer.servicesapi.ContentItem) ByteArraySource(org.apache.stanbol.enhancer.servicesapi.impl.ByteArraySource) Test(org.junit.Test)

Example 48 with ContentItem

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();
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) EnhancementException(org.apache.stanbol.enhancer.servicesapi.EnhancementException) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) StringSource(org.apache.stanbol.enhancer.servicesapi.impl.StringSource) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) ContentItem(org.apache.stanbol.enhancer.servicesapi.ContentItem) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 49 with ContentItem

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"));
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) ContentItem(org.apache.stanbol.enhancer.servicesapi.ContentItem) Test(org.junit.Test)

Example 50 with ContentItem

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());
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) ContentItem(org.apache.stanbol.enhancer.servicesapi.ContentItem) Date(java.util.Date) Test(org.junit.Test)

Aggregations

ContentItem (org.apache.stanbol.enhancer.servicesapi.ContentItem)73 Test (org.junit.Test)62 IRI (org.apache.clerezza.commons.rdf.IRI)46 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)18 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)18 HashMap (java.util.HashMap)15 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)15 Blob (org.apache.stanbol.enhancer.servicesapi.Blob)15 StringSource (org.apache.stanbol.enhancer.servicesapi.impl.StringSource)13 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)12 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)11 Graph (org.apache.clerezza.commons.rdf.Graph)8 Date (java.util.Date)6 SimpleGraph (org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)6 Hashtable (java.util.Hashtable)5 AnalysedText (org.apache.stanbol.enhancer.nlp.model.AnalysedText)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 MediaType (javax.ws.rs.core.MediaType)3 Triple (org.apache.clerezza.commons.rdf.Triple)3