Package org.apache.chemistry.opencmis.commons.data

Examples of org.apache.chemistry.opencmis.commons.data.ContentStream


                put(PROP_ID_INT, Integer.valueOf(-100));
                put(PROP_ID_DECIMAL, Double.valueOf(-4.0E24d));
                put(PROP_ID_DATETIME, gc1);
                put(PROP_ID_BOOLEAN, true);
            }};
        ContentStream content1 = createContent("I have a cat.");
        doc1 = createDocument("alpha", rootFolderId, COMPLEX_TYPE, propertyMap1, content1);
        assertNotNull(doc1);

        final GregorianCalendar gc2 = new GregorianCalendar(TZ);
        gc2.clear();
        gc2.set(1618, 4, 23);

        final Map<String, Object> propertyMap2 =
            new HashMap<String, Object>() {
            {
                put(PROP_ID_STRING, "Beta");
                put(PROP_ID_INT, Integer.valueOf(-50));
                put(PROP_ID_DECIMAL, Double.valueOf(-1.6E-5d));
                put(PROP_ID_DATETIME, gc2);
                put(PROP_ID_BOOLEAN, false);
            }};
        ContentStream content2 = createContent("I have a cat named Kitty Katty.");
        doc2 = createDocument("beta", rootFolderId, COMPLEX_TYPE, propertyMap2, content2);
        assertNotNull(doc2);

        final Map<String, Object> propertyMap3 =
            new HashMap<String, Object>() {
            {
                put(PROP_ID_STRING, "Gamma");
                put(PROP_ID_INT, Integer.valueOf(0));
                put(PROP_ID_DECIMAL, Double.valueOf(Math.PI));
                put(PROP_ID_DATETIME, new GregorianCalendar(TZ));
                put(PROP_ID_BOOLEAN, true);
            }};
       
        ContentStream content3 = createContent("I have a dog.");
        doc3 = createDocument("gamma", rootFolderId, COMPLEX_TYPE, propertyMap3, content3);
        assertNotNull(doc3);

        final GregorianCalendar gc4 = new GregorianCalendar(TZ);
        gc4.clear();
        gc4.set(2038, 0, 20);

        final Map<String, Object> propertyMap4 =
            new HashMap<String, Object>() {
            {
                put(PROP_ID_STRING, "Delta");
                put(PROP_ID_INT, Integer.valueOf(50));
                put(PROP_ID_DECIMAL, Double.valueOf(1.23456E-6));
                put(PROP_ID_DATETIME, gc4);
                put(PROP_ID_BOOLEAN, true);
            }};
        ContentStream content4 = createContent("I have a cat and a dog.");
        doc4 = createDocument("delta", rootFolderId, COMPLEX_TYPE, propertyMap4, content4);
        assertNotNull(doc4);

        final GregorianCalendar gc5 = new GregorianCalendar(TZ);
        gc5.clear();
        gc5.set(2345, 6, 14);

        final Map<String, Object> propertyMap5 =
            new HashMap<String, Object>() {
            {
                put(PROP_ID_STRING, "Epsilon");
                put(PROP_ID_INT, Integer.valueOf(100));
                put(PROP_ID_DECIMAL, Double.valueOf(1.2345E12));
                put(PROP_ID_DATETIME, gc5);
                put(PROP_ID_BOOLEAN, false);
            }};
        ContentStream content5 = createContent("I hate having pets.");
        doc5 = createDocument("epsilon", rootFolderId, COMPLEX_TYPE, propertyMap5, content5);
        assertNotNull(doc5);

    }
View Full Code Here


    public ContentStream getContentStream(String streamId) {
        String objectId = getObjectId();

        // get the stream
        ContentStream contentStream;
        try {
            contentStream = getBinding().getObjectService().getContentStream(getRepositoryId(), objectId, streamId,
                    null, null, null);
        } catch (CmisConstraintException e) {
            // no content stream
            return null;
        }

        // handle incompliant repositories
        if (contentStream == null) {
            return null;
        }

        // the AtomPub binding doesn't return a file name
        // -> get the file name from properties, if present
        String filename = contentStream.getFileName();
        if (filename == null) {
            filename = getContentStreamFileName();
        }

        long length = (contentStream.getBigLength() == null ? -1 : contentStream.getBigLength().longValue());

        // convert and return stream object
        return getSession().getObjectFactory().createContentStream(filename, length, contentStream.getMimeType(),
                contentStream.getStream());
    }
View Full Code Here

        byte[] contentBytes = null;
        Document result = null;
        try {
            contentBytes = content.getBytes("UTF-8");
            ContentStream contentStream = new ContentStreamImpl(name, BigInteger.valueOf(contentBytes.length),
                    "text/plain", new ByteArrayInputStream(contentBytes));

            // create the document
            result = parent.createDocument(properties, contentStream, versioningState, null, null, null,
                    SELECT_ALL_NO_CACHE_OC);
View Full Code Here

                        createResult(FAILURE, "Content properties are not set but the document type demands content!"));
            }
        }

        // get the content stream
        ContentStream contentStream = doc.getContentStream();

        if (contentStream == null) {
            if (hasContentProperties && doc.getContentStreamLength() > 0) {
                addResult(results,
                        createResult(FAILURE, "Content properties have values but the document has no content!"));
            }

            if (type.getContentStreamAllowed() == ContentStreamAllowed.REQUIRED) {
                addResult(results,
                        createResult(FAILURE, "The document type demands content but the document has no content!"));
            }

            return;
        }

        if (type.getContentStreamAllowed() == ContentStreamAllowed.NOTALLOWED) {
            addResult(results, createResult(FAILURE, "Document type doesn't allow content but document has content!"));
        }

        // file name check
        f = createResult(FAILURE, "Content file names don't match!");
        addResult(results, assertEquals(doc.getContentStreamFileName(), contentStream.getFileName(), null, f));

        if (doc.getContentStreamLength() > -1 && contentStream.getLength() > -1) {
            f = createResult(FAILURE, "Content lengths don't match!");
            addResult(results, assertEquals(doc.getContentStreamLength(), contentStream.getLength(), null, f));
        }

        // MIME type check
        String docMimeType = doc.getContentStreamMimeType();
        if (docMimeType != null) {
            int x = docMimeType.indexOf(';');
            if (x > -1) {
                docMimeType = docMimeType.substring(0, x);
            }
            docMimeType = docMimeType.trim();
        }

        String contentMimeType = contentStream.getMimeType();
        if (contentMimeType != null) {
            int x = contentMimeType.indexOf(';');
            if (x > -1) {
                contentMimeType = contentMimeType.substring(0, x);
            }
            contentMimeType = contentMimeType.trim();
        }

        f = createResult(FAILURE, "Content MIME types don't match!");
        addResult(results, assertEquals(docMimeType, contentMimeType, null, f));

        if (contentStream.getMimeType() != null) {
            if (contentMimeType.equals(docMimeType)) {
                f = createResult(WARNING, "Content MIME types don't match!");
                addResult(results, assertEquals(doc.getContentStreamMimeType(), contentStream.getMimeType(), null, f));
            }

            f = createResult(FAILURE, "Content MIME types is invalid: " + contentStream.getMimeType());
            addResult(
                    results,
                    assertIsTrue(contentStream.getMimeType().length() > 2
                            && contentStream.getMimeType().indexOf('/') > 0, null, f));
        }

        // check stream
        InputStream stream = contentStream.getStream();
        if (stream == null) {
            addResult(results, createResult(FAILURE, "Docuemnt has no content stream!"));
            return;
        }

        try {
            long bytes = 0;
            byte[] buffer = new byte[64 * 1024];
            int b = stream.read(buffer);
            while (b > -1) {
                bytes += b;
                b = stream.read(buffer);
            }
            stream.close();

            // check content length
            if (doc.getContentStreamLength() > -1) {
                f = createResult(FAILURE,
                        "Content stream length property value doesn't match the actual content length!");
                addResult(results, assertEquals(doc.getContentStreamLength(), bytes, null, f));
            }

            if (contentStream.getLength() > -1) {
                f = createResult(FAILURE, "Content length value doesn't match the actual content length!");
                addResult(results, assertEquals(contentStream.getLength(), bytes, null, f));
            }
        } catch (Exception e) {
            addResult(results, createResult(FAILURE, "Reading content failed: " + e, e, false));
        } finally {
            try {
View Full Code Here

                                        + rend.getStreamId());
                        addResult(results, assertIsTrue(rend.getWidth() > 0, null, f));
                    }

                    // check the content
                    ContentStream contentStream = rend.getContentStream();
                    f = createResult(FAILURE, "A rendition has no content stream! Stream id: " + rend.getStreamId());
                    addResult(results, assertNotNull(contentStream, null, f));

                    if (contentStream != null) {
                        InputStream stream = contentStream.getStream();

                        f = createResult(FAILURE, "A rendition has no stream! Stream id: " + rend.getStreamId());
                        addResult(results, assertNotNull(stream, null, f));

                        if (stream != null) {
View Full Code Here

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss ZZZ");
        return sdf.format(cal.getTime());
    }

    public static void download(Component component, CmisObject object, String streamId) {
        ContentStream content = getContentStream(object, streamId);
        if (content == null) {
            return;
        }

        String filename = content.getFileName();
        if (filename == null) {
            if (object instanceof Document) {
                filename = ((Document) object).getContentStreamFileName();
            } else {
                filename = object.getName();
            }
        }

        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setSelectedFile(new File(filename));

        int chooseResult = fileChooser.showDialog(component, "Download");
        if (chooseResult == JFileChooser.APPROVE_OPTION) {
            try {
                storeStream(content.getStream(), fileChooser.getSelectedFile());
            } catch (Exception e) {
                showError(component, e);
            }
        }
    }
View Full Code Here

        return tempFile;
    }

    public static File createTempFileFromDocument(CmisObject object, String streamId) throws Exception {
        ContentStream content = getContentStream(object, streamId);
        if (content == null) {
            throw new Exception("No content!");
        }

        String filename = content.getFileName();
        if ((filename == null) || (filename.length() == 0)) {
            if (object instanceof Document) {
                filename = ((Document) object).getContentStreamFileName();
            }
        }
        if ((filename == null) || (filename.length() == 0)) {
            filename = object.getName();
        }
        if ((filename == null) || (filename.length() == 0)) {
            filename = "content";
        }

        String ext = MimeTypes.getExtension(content.getMimeType());
        if (ext.length() > 0 && !filename.endsWith(ext)) {
            filename = filename + ext;
        }

        File tempFile = ClientHelper.createTempFile(filename);
        try {
            storeStream(content.getStream(), tempFile);
        } catch (CmisConstraintException e) {
            // there is no content - leave the temp file empty
        }

        return tempFile;
View Full Code Here

        LOG.debug("start createDocumentFromSource()");
        StoredObject so = validator.createDocumentFromSource(context, repositoryId, sourceId, folderId, extension);
        TypeDefinition td = getTypeDefinition(repositoryId, so)// typedefinition may be copied from source object
       
        ContentStream content = getContentStream(context, repositoryId, sourceId, null, BigInteger.valueOf(-1),
                BigInteger.valueOf(-1), null);

        if (so == null) {
            throw new CmisObjectNotFoundException("Unknown object id: " + sourceId);
        }
View Full Code Here

        if (!(so instanceof Content)) {
            throw new CmisConstraintException("Id" + objectId
                    + " does not refer to a document or version, but only those can have content");
        }

        ContentStream csd = getContentStream(so, streamId, offset, length);

        if (null == csd) {
            throw new CmisConstraintException("Object " + so.getId() + " does not have content.");
        }
View Full Code Here

        if (streamId != null) {
            return null;
        }
        long lOffset = offset == null ? 0 : offset.longValue();
        long lLength = length == null ? -1 : length.longValue();
        ContentStream csd = ((Content) so).getContent(lOffset, lLength);
        return csd;
    }
View Full Code Here

TOP

Related Classes of org.apache.chemistry.opencmis.commons.data.ContentStream

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.