use of javax.jcr.Binary in project jackrabbit-oak by apache.
the class BinaryBasedBlobTest method getStream.
@Test
public void getStream() throws RepositoryException, IOException {
byte[] bytes = "just a test".getBytes();
Binary binary = new TestBinary(bytes);
Blob blob = new BinaryBasedBlob(binary);
assertEquals(bytes.length, blob.length());
InputStream expected = binary.getStream();
InputStream actual = blob.getNewStream();
try {
for (int e = expected.read(); e != -1; e = expected.read()) {
assertEquals(e, actual.read());
}
assertEquals(-1, actual.read());
} finally {
expected.close();
actual.close();
}
}
use of javax.jcr.Binary in project sling by apache.
the class JcrResourceBundleTest method test_json_dictionary.
public void test_json_dictionary() throws Exception {
Node appsI18n = getSession().getRootNode().addNode("apps").addNode("i18n", "nt:unstructured");
Node deJson = appsI18n.addNode("de.json", "nt:file");
deJson.addMixin("mix:language");
deJson.setProperty("jcr:language", "de");
Node content = deJson.addNode("jcr:content", "nt:resource");
content.setProperty("jcr:mimeType", "application/json");
// manually creating json file, good enough for the test
StringBuilder json = new StringBuilder();
json.append("{");
for (Message msg : MESSAGES_DE_APPS.values()) {
json.append("\"").append(msg.key).append("\": \"");
json.append(msg.message).append("\",\n");
}
json.append("}");
InputStream stream = new ByteArrayInputStream(json.toString().getBytes());
Binary binary = getSession().getValueFactory().createBinary(stream);
content.setProperty("jcr:data", binary);
getSession().save();
// test getString
JcrResourceBundle bundle = new JcrResourceBundle(new Locale("de"), null, resolver);
for (Message msg : MESSAGES_DE_APPS.values()) {
assertEquals(msg.message, bundle.getString(msg.key));
}
// test getKeys
Enumeration<String> keys = bundle.getKeys();
int counter = 0;
while (keys.hasMoreElements()) {
counter++;
String key = keys.nextElement();
assertTrue("bundle returned key that is not supposed to be there: " + key, MESSAGES_DE_APPS.containsKey(key));
}
assertEquals(MESSAGES_DE.size(), counter);
}
use of javax.jcr.Binary in project sling by apache.
the class DistributionPackageUtils method uploadStream.
public static void uploadStream(Resource resource, InputStream stream) throws RepositoryException {
Node parent = resource.adaptTo(Node.class);
Node file = JcrUtils.getOrAddNode(parent, "bin", NodeType.NT_FILE);
Node content = JcrUtils.getOrAddNode(file, Node.JCR_CONTENT, NodeType.NT_RESOURCE);
Binary binary = parent.getSession().getValueFactory().createBinary(stream);
content.setProperty(Property.JCR_DATA, binary);
JcrUtils.getOrAddNode(parent, "refs", NodeType.NT_UNSTRUCTURED);
}
use of javax.jcr.Binary in project sling by apache.
the class AddOrUpdateNodeCommand method updateFileLikeNodeTypes.
private void updateFileLikeNodeTypes(Node node) throws RepositoryException, IOException {
// TODO - better handling of file-like nodes - perhaps we need to know the SerializationKind here
// TODO - avoid IO
File file = new File(fileInfo.getLocation());
if (!hasFileLikePrimaryNodeType(node)) {
return;
}
Node contentNode;
if (node.hasNode(JCR_CONTENT)) {
contentNode = node.getNode(JCR_CONTENT);
} else {
if (node.getProperty(JCR_PRIMARYTYPE).getString().equals(NT_RESOURCE)) {
contentNode = node;
} else {
contentNode = node.addNode(JCR_CONTENT, NT_RESOURCE);
}
}
getLogger().trace("Updating {0} property on node at {1} ", JCR_DATA, contentNode.getPath());
try (FileInputStream inputStream = new FileInputStream(file)) {
Binary binary = node.getSession().getValueFactory().createBinary(inputStream);
contentNode.setProperty(JCR_DATA, binary);
// TODO: might have to be done differently since the client and server's clocks can differ
// and the last_modified should maybe be taken from the server's time..
contentNode.setProperty(JCR_LASTMODIFIED, Calendar.getInstance());
}
}
use of javax.jcr.Binary in project jackrabbit by apache.
the class ValueFactoryTest method testInputStream.
/**
* Tests whether a passed <code>InputStream</code> is closed
* by the implementation.
*
* @throws RepositoryException
*/
public void testInputStream() throws RepositoryException {
InputStreamWrapper in = new InputStreamWrapper(new ByteArrayInputStream(binaryValue));
valueFactory.createValue(in);
assertTrue("ValueFactory.createValue(InputStream) is expected to close the passed input stream", in.isClosed());
in = new InputStreamWrapper(new ByteArrayInputStream(binaryValue));
Binary bin = valueFactory.createBinary(in);
assertTrue("ValueFactory.createBinary(InputStream) is expected to close the passed input stream", in.isClosed());
bin.dispose();
}
Aggregations