@Override public long setContent( Node parentNode, String resourceName, InputStream newContent, String contentType, String characterEncoding ) throws RepositoryException, IOException { Node contentNode; if (parentNode.hasNode(CONTENT_NODE_NAME)) { contentNode = parentNode.getNode(CONTENT_NODE_NAME); } else { contentNode = parentNode.addNode(CONTENT_NODE_NAME, newContentPrimaryType); } // contentNode.setProperty(MIME_TYPE_PROP_NAME, contentType != null ? contentType : "application/octet-stream"); contentNode.setProperty(ENCODING_PROP_NAME, characterEncoding != null ? characterEncoding : "UTF-8"); org.modeshape.jcr.api.Binary binary = (org.modeshape.jcr.api.Binary)parentNode.getSession() .getValueFactory() .createBinary(newContent); contentNode.setProperty(DATA_PROP_NAME, binary); contentNode.setProperty(MODIFIED_PROP_NAME, Calendar.getInstance()); // Copy the content to the property, THEN re-read the content from the Binary value to avoid discarding the first // bytes of the stream if (contentType == null) { contentType = binary.getMimeType(resourceName); } if (contentType != null) { contentNode.setProperty(MIME_TYPE_PROP_NAME, contentType); } return contentNode.getProperty(DATA_PROP_NAME).getLength(); }
/** * Allows subclasses to process the stream of binary value property in "safe" fashion, making sure the stream is closed at the * end of the operation. * * @param binary a {@link org.modeshape.jcr.api.Binary} who is expected to contain a non-null binary value. * @param operation a {@link org.modeshape.jcr.api.text.TextExtractor.BinaryOperation} which should work with the stream * @param <T> the return type of the binary operation * @return whatever type of result the stream operation returns * @throws Exception if there is an error processing the stream */ protected final <T> T processStream( Binary binary, BinaryOperation<T> operation ) throws Exception { InputStream stream = binary.getStream(); if (stream == null) { throw new IllegalArgumentException("The binary value is empty"); } try { return operation.execute(stream); } finally { stream.close(); } }
/** * Get the hexadecimal form of the SHA-1 hash of the contents. * * @return the hexadecimal form of hash, or a null string if the hash could not be computed or is not known */ private String getHash() { try { Node contentNode = getContextNode(); Property data = contentNode.getProperty(Property.JCR_DATA); Binary bin = (Binary) data.getBinary(); return String.format("{%s}%s", HASH_ALGORITHM, bin.getHexHash()); } catch (RepositoryException e) { return ""; } } }
String mimeType = binaryValue.getMimeType(inputFileName); try (InputStream stream = binaryValue.getStream()) { sequencePowerpoint(sequencedNode, context.valueFactory(), stream); return true; try (InputStream stream = binaryValue.getStream()) { sequenceWord(sequencedNode, context.valueFactory(), stream); return true; try (InputStream stream = binaryValue.getStream()) { sequenceExcel(sequencedNode, context.valueFactory(), stream); return true;
assertThat(binary, is(instanceOf(StoredBinaryValue.class))); assertThat(binary.getHexHash(), is(expectedSha1)); assertThat(binary.getSize(), is(numBytes)); InputStream actual = binary.getStream(); byte[] buffer1 = new byte[1024]; byte[] buffer2 = new byte[1024];
protected Binary storeAndCheck( int contentIndex, Class<? extends Binary> valueClass ) throws Exception { String content = CONTENT[contentIndex]; String sha1 = CONTENT_HASHES[contentIndex]; InputStream stream = new ByteArrayInputStream(content.getBytes()); Stopwatch sw = new Stopwatch(); sw.start(); Binary binary = store.storeValue(stream, false); sw.stop(); if (print) System.out.println("Time to store 18MB file: " + sw.getTotalDuration()); if (valueClass != null) { assertThat(binary, is(instanceOf(valueClass))); } if (content.length() == 0) { assertThat(binary, is(instanceOf(EmptyBinaryValue.class))); } else if (content.length() < MIN_BINARY_SIZE) { assertThat(binary, is(instanceOf(InMemoryBinaryValue.class))); } else { assertThat(binary, is(instanceOf(StoredBinaryValue.class))); } assertThat(binary.getHexHash(), is(sha1)); String binaryContent = IoUtil.read(binary.getStream()); assertThat(binaryContent, is(content)); return binary; }
@Test @FixFor("MODE-1573") public void shouldPerformRoundTripOnDocumentViewWithBinaryContent() throws Exception { JcrTools tools = new JcrTools(); File binaryFile = new File("src/test/resources/io/binary.pdf"); assert (binaryFile.exists() && binaryFile.isFile()); File outputFile = File.createTempFile("modeshape_import_export_" + System.currentTimeMillis(), "_test"); outputFile.deleteOnExit(); tools.uploadFile(session, "file", binaryFile); session.save(); session.exportDocumentView("/file", new FileOutputStream(outputFile), false, false); assertTrue(outputFile.length() > 0); session.getRootNode().getNode("file").remove(); session.save(); //sleep so that the binary can be properly cleaned up (this is done via a listener) Thread.sleep(200); session.getWorkspace().importXML("/", new FileInputStream(outputFile), ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW); assertNotNull(session.getNode("/file")); assertNotNull(session.getNode("/file/jcr:content")); Property data = session.getNode("/file/jcr:content").getProperty("jcr:data"); assertNotNull(data); Binary binary = (Binary)data.getBinary(); assertNotNull(binary); assertEquals(binaryFile.length(), binary.getSize()); }
String mimeType = binaryValue.getMimeType(inputFileName); try (InputStream stream = binaryValue.getStream()) { sequencePowerpoint(sequencedNode, context.valueFactory(), stream); return true; try (InputStream stream = binaryValue.getStream()) { sequenceWord(sequencedNode, context.valueFactory(), stream); return true; try (InputStream stream = binaryValue.getStream()) { sequenceExcel(sequencedNode, context.valueFactory(), stream); return true;
String unused = binaries.iterator().next().getHexHash(); BinaryKey unusedKey = new BinaryKey(unused); store.markAsUnused(Collections.singleton(unusedKey)); InputStream stream = binary.getStream(); String content = IoUtil.read(stream); assertThat(content.length() != 0, is(true));
/** * Returns the default mime-type of a given binary property. * * @param binaryProperty a non-null {@link Property} * @return a non-null String which represents the mime-type of the binary property. * @throws RepositoryException if any JCR related operation involving the binary property fail. */ public String getDefaultMimeType( Property binaryProperty ) throws RepositoryException { try { Binary binary = binaryProperty.getBinary(); return binary instanceof org.modeshape.jcr.api.Binary ? ((org.modeshape.jcr.api.Binary)binary) .getMimeType() : DEFAULT_MIME_TYPE; } catch (IOException e) { logger.warn("Cannot determine default mime-type", e); return DEFAULT_MIME_TYPE; } }
Binary binaryValue = (Binary) inputProperty.getBinary(); CheckArg.isNotNull(binaryValue, "binary"); final String mimeType = binaryValue.getMimeType(); boolean isValid = false; AudioMetadata metadata = null; try (InputStream stream = binaryValue.getStream()) { metadata = new AudioMetadata(stream, mimeType); isValid = metadata.check();
/** * Allows subclasses to process the stream of binary value property in "safe" fashion, making sure the stream is closed at the * end of the operation. * * @param binary a {@link org.modeshape.jcr.api.Binary} who is expected to contain a non-null binary value. * @param operation a {@link org.modeshape.jcr.api.text.TextExtractor.BinaryOperation} which should work with the stream * @param <T> the return type of the binary operation * @return whatever type of result the stream operation returns * @throws Exception if there is an error processing the stream */ protected final <T> T processStream( Binary binary, BinaryOperation<T> operation ) throws Exception { InputStream stream = binary.getStream(); if (stream == null) { throw new IllegalArgumentException("The binary value is empty"); } try { return operation.execute(stream); } finally { stream.close(); } }
/** * Add necessary information to node * @param dsNode The target binary node to add information to * @param descNode The description node associated with the binary node * @param checksums The checksum information * @throws RepositoryException on error */ private static void decorateContentNode(final Node dsNode, final Node descNode, final Collection<URI> checksums) throws RepositoryException { if (dsNode == null) { LOGGER.warn("{} node appears to be null!", JCR_CONTENT); return; } if (dsNode.hasProperty(JCR_DATA)) { final Property dataProperty = dsNode.getProperty(JCR_DATA); final Binary binary = (Binary) dataProperty.getBinary(); final String dsChecksum = binary.getHexHash(); checksums.add(ContentDigest.asURI(SHA1.algorithm, dsChecksum)); final String[] checksumArray = new String[checksums.size()]; checksums.stream().map(Object::toString).collect(Collectors.toSet()).toArray(checksumArray); if (descNode != null) { descNode.setProperty(CONTENT_DIGEST, checksumArray); descNode.setProperty(CONTENT_SIZE, dataProperty.getLength()); } LOGGER.debug("Decorated data property at path: {}", dataProperty.getPath()); } }
private void addFileContent( ZipInputStream zipInputStream, ZipEntry entry, Context context, Node zipFileNode ) throws RepositoryException, IOException { Node contentNode = zipFileNode.addNode(JcrConstants.JCR_CONTENT, JcrConstants.NT_RESOURCE); // on session pre-save the appropriate properties should be set automatically contentNode.addMixin(JcrConstants.MIX_LAST_MODIFIED); // set the content bytes byte[] contentBytes = readContent(zipInputStream); org.modeshape.jcr.api.Binary contentBinary = context.valueFactory().createBinary(contentBytes); contentNode.setProperty(JcrConstants.JCR_DATA, contentBinary); // Figure out the mime type ... String mimeType = contentBinary.getMimeType(entry.getName()); if (mimeType != null) { contentNode.setProperty(JcrConstants.JCR_MIME_TYPE, mimeType); } }
protected void assertBinaryContains( Binary binaryValue, byte[] expectedContent ) throws IOException, RepositoryException { byte[] actual = IoUtil.readBytes(binaryValue.getStream()); assertThat(actual, is(expectedContent)); }
final String dsSHA1 = ((Binary) dataProperty.getBinary()).getHexHash(); final URI dsSHA1Uri = ContentDigest.asURI(SHA1.algorithm, dsSHA1);
mimeType = ((org.modeshape.jcr.api.Binary)binary).getMimeType(parent.getName());
String str = null; if (value instanceof Binary) { byte[] bytes = IoUtil.readBytes(((Binary)value).getStream()); str = StringUtil.getHexString(bytes); } else {
sha1 = fieldValue.toString(); } else if (fieldValue instanceof org.modeshape.jcr.api.Binary && !(fieldValue instanceof InMemoryBinaryValue)) { sha1 = ((org.modeshape.jcr.api.Binary)fieldValue).getHexHash();
mimeType = ((org.modeshape.jcr.api.Binary)binary).getMimeType(parent.getName());