protected void writeDocumentToFile(DocumentRecord documentRecord, File file) throws IOException { file.getParentFile().mkdirs(); FileOutputStream fos = new FileOutputStream(file); try { InputStream in = documentRecord.getContent(new InputStreamHandle()).get(); byte[] buffer = new byte[4096]; int bytesRead = -1; while ((bytesRead = in.read(buffer)) != -1) { fos.write(buffer, 0, bytesRead); } fos.flush(); } finally { fos.close(); } }
public Graph readGraph(String uri) { InputStreamHandle handle = new InputStreamHandle(); Graph graph = GraphFactory.createDefaultGraph(); try { this.graphManager.read(uri, handle, currentTransaction); RDFDataMgr.read(graph, handle.get(), Lang.NTRIPLES); } catch (NullPointerException e) { log.debug("RDF Manager Throws execption for empty results"); } catch (ResourceNotFoundException e) { log.debug("No graph returned by graph manage. Returning empty graph", e.getMessage()); } // close handle? return graph; }
InputStreamHandle results = client.executeSelect(qdef, new InputStreamHandle()); return new QuadsIterator(results.get());
@Override public Content adapt(DocumentWriteOperation operation) { String uri = operation.getUri(); ContentCreateOptions options = adaptMetadata(operation.getMetadata()); AbstractWriteHandle handle = operation.getContent(); if (handle instanceof StringHandle) { return ContentFactory.newContent(uri, ((StringHandle) handle).get(), options); } else if (handle instanceof FileHandle) { return ContentFactory.newContent(uri, ((FileHandle) handle).get(), options); } else if (handle instanceof BytesHandle) { return ContentFactory.newContent(uri, ((BytesHandle) handle).get(), options); } else if (handle instanceof InputStreamHandle) { try { return ContentFactory.newContent(uri, ((InputStreamHandle) handle).get(), options); } catch (IOException e) { throw new RuntimeException("Unable to read content input stream: " + e.getMessage(), e); } } else if (handle instanceof DOMHandle) { return ContentFactory.newContent(uri, ((DOMHandle) handle).get(), options); } else throw new IllegalArgumentException("No support yet for content class: " + handle.getClass().getName()); }
InputStreamHandle results = client.executeSelect(qdef, new InputStreamHandle()); return results.get();
@Test public void testWrite_n3_BytesHandle() throws Exception { File file = new File(datasource + "relative7.n3"); FileInputStream fileinputstream = new FileInputStream(file); ByteArrayOutputStream byteoutputstream = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; // Get triples into bytes from file for (int readNum; (readNum = fileinputstream.read(buf)) != -1;) { byteoutputstream.write(buf, 0, readNum); } byte[] bytes = byteoutputstream.toByteArray(); fileinputstream.close(); byteoutputstream.close(); BytesHandle contentHandle = new BytesHandle(); contentHandle.set(bytes); // write triples in bytes to DB gmWriter.write("http://test.n3.com/byte", contentHandle.withMimetype(RDFMimeTypes.N3)); InputStreamHandle read = gmWriter.read("http://test.n3.com/byte", new InputStreamHandle().withMimetype(RDFMimeTypes.N3)); InputStream fileRead = read.get(); String readContent = convertInputStreamToString(fileRead); assertTrue("Did not find expected content after inserting the triples:: Found: " + readContent, readContent.contains("/publications/journals/Journal1/1940")); read.close(); }
@Test public void testStack20170725() throws IOException { XMLDocumentManager documentManager = Common.client.newXMLDocumentManager(); Transaction transaction = Common.client.openTransaction(); DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); documentManager.writeAs("all_well.xml", new FileHandle( new File("../marklogic-client-api-functionaltests/src/test/java/com" + "/marklogic/client/functionaltest/data/all_well.xml"))); InputStreamHandle handle = documentManager.read("all_well.xml", metadataHandle, new InputStreamHandle(), transaction); try { InputStream stream = handle.get(); try { while ( stream.read() > -1 ) {} } finally { stream.close(); } } finally { handle.close(); } // if we ran without throwing any exceptions, then this test passes } }
isHandle.get();
@Test public void testAuthenticationBasic() throws KeyManagementException, NoSuchAlgorithmException, IOException { if (!IsSecurityEnabled()) { setAuthentication("basic", restServerName); setDefaultUser("rest-writer", restServerName); System.out.println("Running testAuthenticationBasic"); String filename = "text-original.txt"; // connect the client SecurityContext secContext = new DatabaseClientFactory.BasicAuthContext("rest-writer", "x"); DatabaseClient client = DatabaseClientFactory.newClient(hostName, restPort, secContext, getConnType()); // write doc writeDocumentUsingStringHandle(client, filename, "/write-text-doc-basic/", "Text"); // read docs InputStreamHandle contentHandle = readDocumentUsingInputStreamHandle(client, "/write-text-doc-basic/" + filename, "Text"); // get the contents InputStream fileRead = contentHandle.get(); String readContent = convertInputStreamToString(fileRead); String expectedContent = "hello world, welcome to java API"; assertEquals("Write Text difference", expectedContent.trim(), readContent.trim()); // release client client.release(); setAuthentication("digest", restServerName); setDefaultUser("nobody", restServerName); } }
@Test public void testReadWrite() throws IOException { String docId = "/test/testWrite1.txt"; String text = "A simple text document"; TextDocumentManager docMgr = Common.client.newTextDocumentManager(); docMgr.write(docId, new StringHandle().with(text)); assertEquals("Text document write difference",text,docMgr.read(docId, new StringHandle()).get()); BytesHandle bytesHandle = new BytesHandle(); docMgr.read(docId, bytesHandle); assertEquals("Text document mismatch reading bytes", bytesHandle.get().length,text.length()); InputStreamHandle inputStreamHandle = new InputStreamHandle(); docMgr.read(docId, inputStreamHandle); byte[] b = Common.streamToBytes(inputStreamHandle.get()); assertEquals("Text document mismatch reading input stream",new String(b),text); Reader reader = docMgr.read(docId, new ReaderHandle()).get(); String s = Common.readerToString(reader); assertEquals("Text document mismatch with reader",s,text); File file = docMgr.read(docId, new FileHandle()).get(); assertEquals("Text document mismatch with file",text.length(),file.length()); }
readNode = mapper.readTree(inputStreamHandle.get()); assertTrue("JSON document mismatch reading input stream", sourceNode.equals(readNode));
@Test public void testReadWrite() throws IOException, XpathException { String docId = "/test/binary-sample.png"; String mimetype = "image/png"; BinaryDocumentManager docMgr = Common.client.newBinaryDocumentManager(); docMgr.setMetadataExtraction(MetadataExtraction.PROPERTIES); docMgr.write(docId, new BytesHandle().with(BYTES_BINARY).withMimetype(mimetype)); DocumentDescriptor desc = docMgr.exists(docId); assertTrue("Binary exists did not get number of bytes", desc.getByteLength() != DocumentDescriptor.UNKNOWN_LENGTH); assertEquals("Binary exists got wrong number of bytes", BYTES_BINARY.length, desc.getByteLength()); byte[] buf = docMgr.read(docId, new BytesHandle()).get(); assertEquals("Binary document read wrong number of bytes", BYTES_BINARY.length, buf.length); buf = Common.streamToBytes(docMgr.read(docId, new InputStreamHandle()).get()); assertTrue("Binary document read binary empty input stream",buf.length > 0); BytesHandle handle = new BytesHandle(); buf = docMgr.read(docId, handle, 9, 10).get(); assertEquals("Binary range read wrong number of bytes", 10, buf.length); assertEquals("Binary range did not set length in handle", 10, handle.getByteLength()); docMgr.setMetadataCategories(Metadata.PROPERTIES); Document metadataDocument = docMgr.readMetadata(docId, new DOMHandle()).get(); assertXpathEvaluatesTo("image/png","string(/*[local-name()='metadata']/*[local-name()='properties']/*[local-name()='content-type'])", metadataDocument); assertXpathEvaluatesTo("text HD-HTML","string(/*[local-name()='metadata']/*[local-name()='properties']/*[local-name()='filter-capabilities'])", metadataDocument); assertXpathEvaluatesTo("815","string(/*[local-name()='metadata']/*[local-name()='properties']/*[local-name()='size'])", metadataDocument); }