Package org.apache.stanbol.enhancer.servicesapi

Examples of org.apache.stanbol.enhancer.servicesapi.ContentItem


        ContentItem ci = createContentItem(contentSource);
        ci.removePart(new UriRef("urn:does.not.exist:and.can.not.be.removed"));
    }
    @Test(expected=NoSuchPartException.class)
    public void removeNonExistentPartByIndex() throws IOException {
        ContentItem ci = createContentItem(contentSource);
        ci.removePart(12345);
    }
View Full Code Here


        ContentItem ci = createContentItem(contentSource);
        ci.removePart(12345);
    }
    @Test
    public void removeRemoveByUri() throws IOException {
        ContentItem ci = createContentItem(contentSource);
        UriRef uri = new UriRef("urn:content.part:remove.test");
        ci.addPart(uri, new Date());
        try {
            ci.getPart(uri, Date.class);
        }catch (NoSuchPartException e) {
            assertFalse("The part with the uri "+uri+" was not added correctly",
                true);
        }
        ci.removePart(uri);
        try {
            ci.getPart(uri, Date.class);
            assertFalse("The part with the uri "+uri+" was not removed correctly",
                true);
        }catch (NoSuchPartException e) {
            // expected
        }
View Full Code Here

            // expected
        }
    }
    @Test
    public void removeRemoveByIndex() throws IOException {
        ContentItem ci = createContentItem(contentSource);
        UriRef uri = new UriRef("urn:content.part:remove.test");
        ci.addPart(uri, new Date());
        int index = -1;
        try {
            for(int i=0; index < 0; i++){
                UriRef partUri = ci.getPartUri(i);
                if(partUri.equals(uri)){
                    index = i;
                }
            }
        }catch (NoSuchPartException e) {
            assertFalse("The part with the uri "+uri+" was not added correctly",
                true);
        }
        ci.removePart(index);
        try {
            ci.getPart(index, Date.class);
            assertTrue("The part with the uri "+uri+" was not removed correctly",
                false);
        }catch (NoSuchPartException e) {
            // expected
        }
View Full Code Here

        }
        String mimeType = d.getContentStreamMimeType();
        Map<String,List<Object>> constraints = getConstraintsFromDocument(d);
        String id = d.getId();
        id = attachBaseURI(id);
        ContentItem ci = null;
        try {
            ci = solrStore.create(content, id, d.getName(), mimeType);
            ci.addPart(ADDITIONAL_METADATA_URI, constraints);
            solrStore.enhanceAndPut(ci, indexName, null);
        } catch (StoreException e) {
            log.error(e.getMessage(), e);
        }
        log.info("Document submitted to Contenthub.");
        log.info("Id: {}", ci.getUri().getUnicodeString());
        log.info("Mime type: {}", ci.getMimeType());
    }
View Full Code Here

                                Annotation[] annotations,
                                MediaType mediaType,
                                MultivaluedMap<String,String> httpHeaders,
                                InputStream entityStream) throws IOException, WebApplicationException {
        //boolean withMetadata = withMetadata(httpHeaders);
        ContentItem contentItem = null;
        UriRef contentItemId = getContentItemId();
        Set<String> parsedContentIds = new HashSet<String>();
        if(mediaType.isCompatible(MULTIPART)){
            //try to read ContentItem from "multipart/from-data"
            MGraph 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());
                        }
                        //the metadata may define the ID for the contentItem
                        //only used if not parsed as query param
                        if(contentItemId == null && fis.getName() != null && !fis.getName().isEmpty()){
                            contentItemId = new UriRef(fis.getName());
                        }
                        metadata = new IndexedMGraph();
                        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(ENHANCEMENT_PROPERTIES_URI.getUnicodeString())){
                        //parse the enhancementProperties
                        if(contentItem == null){
                            throw new WebApplicationException(
                                Response.status(Response.Status.BAD_REQUEST)
                                .entity("Multipart MIME parts for " +
                                    "EnhancementProperties 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("EnhancementProperties (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> enhancementProperties = getEnhancementProperties(contentItem);
                        try {
                            enhancementProperties.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 EnhancementProperties 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());
                        }
                        MGraph graph = new IndexedMGraph();
                        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());
                        }
                        UriRef contentPartId = new UriRef(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
        getEnhancementProperties(contentItem).put(PARSED_CONTENT_URIS,
            Collections.unmodifiableSet(parsedContentIds));
        return contentItem;
View Full Code Here

    @Test
    public void testReader() throws Exception {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        MediaType contentType = serializeContentItem(out);
        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
        ContentItem ci = ciReader.readFrom(ContentItem.class, null, null, contentType, null, in);
        //assert ID
        assertEquals(contentItem.getUri(), ci.getUri());
        //assert metadata
        MGraph copy = new SimpleMGraph();
        copy.addAll(contentItem.getMetadata());
        assertTrue(copy.removeAll(ci.getMetadata()));
        assertTrue(copy.isEmpty());
        //assert Blob
        assertEquals(contentItem.getBlob().getMimeType(), ci.getBlob().getMimeType());
        String content = IOUtils.toString(contentItem.getStream(),"UTF-8");
        String readContent = IOUtils.toString(ci.getStream(), "UTF-8");
        assertEquals(content, readContent);
        Iterator<Entry<UriRef,Blob>> contentItemBlobsIt = ContentItemHelper.getContentParts(contentItem, Blob.class).entrySet().iterator();
        Iterator<Entry<UriRef,Blob>> ciBlobsIt = ContentItemHelper.getContentParts(ci, Blob.class).entrySet().iterator();
        Set<String> expectedParsedContentIds = new HashSet<String>(); //later used to validate enhancementMetadata
        while(contentItemBlobsIt.hasNext() && ciBlobsIt.hasNext()){
            Entry<UriRef,Blob> contentItemBlobPart = contentItemBlobsIt.next();
            Entry<UriRef,Blob> ciBlobPart = ciBlobsIt.next();
            expectedParsedContentIds.add(ciBlobPart.getKey().getUnicodeString());
            assertEquals(contentItemBlobPart.getKey(), ciBlobPart.getKey());
            String partContentType = contentItemBlobPart.getValue().getMimeType();
            String readPartContentType = ciBlobPart.getValue().getMimeType();
            assertEquals(partContentType, readPartContentType);
            String partContent = IOUtils.toString(contentItemBlobPart.getValue().getStream(),"UTF-8");
            String readPartContent = IOUtils.toString(ciBlobPart.getValue().getStream(), "UTF-8");
            assertEquals(partContent, readPartContent);
        }
        //validate ExecutionMetadata
        MGraph executionMetadata = contentItem.getPart(ExecutionMetadata.CHAIN_EXECUTION, MGraph.class);
        MGraph readExecutionMetadata = ci.getPart(ExecutionMetadata.CHAIN_EXECUTION, MGraph.class);
        assertNotNull(executionMetadata);
        assertNotNull(readExecutionMetadata);
        assertEquals(executionMetadata.size(), readExecutionMetadata.size());
        //validate EnhancemetnProperties
        Map<String,Object> properties = getEnhancementProperties(ci);
View Full Code Here

     * @throws FileUploadException if the parsed contents are not correctly
     * encoded Multipoart MIME
     */
    private ContentItem createContentItem(UriRef id, MGraph 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)){
            //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,content.getContentType());
                    contentItem = ciFactory.createContentItem(id,
                        new StreamSource(fis.openStream(),fis.getContentType()),
                        metadata);
                } else {
                    Blob blob = ciFactory.createBlob(new StreamSource(fis.openStream(), fis.getContentType()));
                    UriRef contentPartId = null;
                    if(fis.getFieldName() != null && !fis.getFieldName().isEmpty()){
                        contentPartId = new UriRef(fis.getFieldName());
                    } else {
                        //generating a random ID might break metadata
                        //TODO maybe we should throw an exception instead
                        contentPartId = new UriRef("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;
    }
View Full Code Here

    /**
     * Test that the generated ID starts with the parsed prefix
     */
    @Test
    public void testPrefix() throws IOException{
        ContentItem ci = contentItemFactory.createContentItem(PREFIX, TEST_CS);
        assertNotNull(ci);
        assertNotNull(ci.getUri());
        assertTrue("The ID of the created ContentItem MUST start with the parsed prefix",
            ci.getUri().getUnicodeString().startsWith(PREFIX));
       
        ci = contentItemFactory.createContentItem(PREFIX, TEST_CS,new SimpleMGraph());
        assertNotNull(ci);
        assertNotNull(ci.getUri());
        assertTrue("The ID of the created ContentItem MUST start with the parsed prefix",
            ci.getUri().getUnicodeString().startsWith(PREFIX));
    }
View Full Code Here

    /**
     * Test that the parsed URI is used as ID of the ContentItem
     */
    @Test
    public void testURI() throws IOException {
        ContentItem ci = contentItemFactory.createContentItem(ID, TEST_CS);
        assertNotNull(ci);
        assertNotNull(ci.getUri());
        assertTrue("The ID of the created ContentItem MUST be equals to the parsed ID",
            ci.getUri().equals(ID));
       
        ci = contentItemFactory.createContentItem(ID, TEST_CS,new SimpleMGraph());
        assertNotNull(ci);
        assertNotNull(ci.getUri());
        assertTrue("The ID of the created ContentItem MUST be equals to the parsed ID",
            ci.getUri().equals(ID));
    }
View Full Code Here

     * Test the generation of valid IDs if no or <code>null</code> is parsed
     * as id
     */
    @Test
    public void testDefaultId() throws IOException {
        ContentItem ci = contentItemFactory.createContentItem(TEST_CS);
        assertNotNull(ci);
        assertNotNull(ci.getUri());
        ci = contentItemFactory.createContentItem((UriRef)null,TEST_CS);
        assertNotNull(ci);
        assertNotNull(ci.getUri());
        ci = contentItemFactory.createContentItem((UriRef)null,TEST_CS, new SimpleMGraph());
        assertNotNull(ci);
        assertNotNull(ci.getUri());
    }
View Full Code Here

TOP

Related Classes of org.apache.stanbol.enhancer.servicesapi.ContentItem

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.