static public InputStreamHandle InputStreamToHandle(InputStream value) { return (value == null) ? null : new InputStreamHandle(value); } static public Stream<InputStreamHandle> InputStreamToHandle(Stream<? extends InputStream> values) {
/** * Creates a factory to create an InputStreamHandle instance for an input stream. * @return the factory */ static public ContentHandleFactory newFactory() { return new ContentHandleFactory() { @Override public Class<?>[] getHandledClasses() { return new Class<?>[]{ InputStream.class }; } @Override public boolean isHandled(Class<?> type) { return InputStream.class.isAssignableFrom(type); } @Override public <C> ContentHandle<C> newHandle(Class<C> type) { @SuppressWarnings("unchecked") ContentHandle<C> handle = isHandled(type) ? (ContentHandle<C>) new InputStreamHandle() : null; return handle; } }; }
/** * Creates a factory to create an InputStreamHandle instance for an input stream. * @return the factory */ static public ContentHandleFactory newFactory() { return new ContentHandleFactory() { @Override public Class<?>[] getHandledClasses() { return new Class<?>[]{ InputStream.class }; } @Override public boolean isHandled(Class<?> type) { return InputStream.class.isAssignableFrom(type); } @Override public <C> ContentHandle<C> newHandle(Class<C> type) { @SuppressWarnings("unchecked") ContentHandle<C> handle = isHandled(type) ? (ContentHandle<C>) new InputStreamHandle() : null; return handle; } }; }
@Override public <C> ContentHandle<C> newHandle(Class<C> type) { @SuppressWarnings("unchecked") ContentHandle<C> handle = isHandled(type) ? (ContentHandle<C>) new InputStreamHandle() : null; return handle; } };
@Override public InputStreamHandle asInputStreamHandle() { return (responseBody == null) ? null : new InputStreamHandle(asInputStream()); } @Override
@Override public <C> ContentHandle<C> newHandle(Class<C> type) { @SuppressWarnings("unchecked") ContentHandle<C> handle = isHandled(type) ? (ContentHandle<C>) new InputStreamHandle() : null; return handle; } };
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(); } }
@Override public AbstractWriteHandle getContent() { if (modifiedContent != null) { StringHandle h = new StringHandle(modifiedContent); return format != null ? h.withFormat(format) : h; } InputStreamHandle h = null; try { h = new InputStreamHandle(resource.getInputStream()); } catch (IOException e) { throw new RuntimeException(e); } return format != null ? h.withFormat(format) : h; }
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; }
/** * Reading document using InputStreamHandle * * @param client * @param uri * @param type * @return */ public InputStreamHandle readDocumentUsingInputStreamHandle(DatabaseClient client, String uri, String type) { // create doc manager DocumentManager docMgr = null; docMgr = documentManagerSelector(client, docMgr, type); // create handle InputStreamHandle contentHandle = new InputStreamHandle(); // create doc id String readDocId = uri; System.out.println("Read " + readDocId + " from database"); docMgr.read(readDocId, contentHandle); return contentHandle; }
public void writeDocumentUsingInputStreamHandle(DatabaseClient client, String filename, String uri, Transaction transaction, String type) throws FileNotFoundException { // create doc manager DocumentManager docMgr = null; docMgr = documentManagerSelector(client, docMgr, type); // create handle InputStreamHandle contentHandle = new InputStreamHandle(); // get the file InputStream inputStream = new FileInputStream("src/test/java/com/marklogic/client/functionaltest/data/" + filename); // set uri String docId = uri + filename; contentHandle.set(inputStream); // write doc docMgr.write(docId, contentHandle, transaction); System.out.println("Write " + docId + " to database"); }
@Override public Stream<InputStreamHandle> asStreamOfInputStreamHandle() { try { if (multipart == null) { return Stream.empty(); } int partCount = multipart.getCount(); Stream.Builder<InputStreamHandle> builder = Stream.builder(); for (int i=0; i < partCount; i++) { BodyPart bodyPart = multipart.getBodyPart(i); builder.accept(new InputStreamHandle(bodyPart.getInputStream())); } return builder.build(); } catch (MessagingException e) { throw new MarkLogicIOException(e); } catch (IOException e) { throw new MarkLogicIOException(e); } } @Override
@Test public void evalAndInvokeXQuery() throws ParserConfigurationException, DatatypeConfigurationException, JsonProcessingException, IOException, SAXException { InputStreamHandle xquery = new InputStreamHandle( this.getClass().getClassLoader().getResourceAsStream("evaltest.xqy")); // first read it locally and run it as ad-hoc eval runAndTestXQuery( Common.evalClient.newServerEval().xquery(xquery) ); // run the same code, this time as a module we'll invoke xquery = new InputStreamHandle( this.getClass().getClassLoader().getResourceAsStream("evaltest.xqy")); xquery.setFormat(Format.TEXT); // libMgr is connected with admin privileges (as rest-admin user) libMgr.write("/ext/test/evaltest.xqy", xquery); // now module is installed, let's invoke it runAndTestXQuery( Common.evalClient.newServerEval().modulePath("/ext/test/evaltest.xqy") ); // clean up module we no longer need libMgr.delete("/ext/test/evaltest.xqy"); }
public void runImpl() { System.out.println("writing " + id); if (isResendable) docMgr.write(id, new StringHandle(resendableDoc)); else docMgr.write(id, new InputStreamHandle(onetimeDoc)); System.out.println(id + "=" + docMgr.read(id, new StringHandle()).get()); docMgr.delete(id); System.out.println("finished " + id); } }
@Test public void testXqueryReturningDifferentTypesAndFormatsWithHandle() throws KeyManagementException, NoSuchAlgorithmException, Exception { InputStream inputStream = new FileInputStream("src/test/java/com/marklogic/client/functionaltest/data/xqueries.txt"); InputStreamHandle ish = new InputStreamHandle(); ish.set(inputStream); EvalResultIterator evr = null; try { evr = client.newServerEval().xquery(ish).eval(); this.validateReturnTypes(evr); } catch (Exception e) { throw e; } finally { inputStream.close(); } evr.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 } }
protected void installModule(String path, String localPath) { InputStreamHandle handle = new InputStreamHandle(HubTestBase.class.getClassLoader().getResourceAsStream(localPath)); String ext = FilenameUtils.getExtension(path); DocumentMetadataHandle permissions = new DocumentMetadataHandle() .withPermission(getHubAdminConfig().getHubRoleName(), DocumentMetadataHandle.Capability.EXECUTE, UPDATE, READ); switch(ext) { case "xml": handle.setFormat(Format.XML); break; case "json": handle.setFormat(Format.JSON); break; default: handle.setFormat(Format.TEXT); } modMgr.write(path, permissions, handle); clearFlowCache(); }
protected void installModules(Map<String, String> modules) { DocumentWriteSet writeSet = modMgr.newWriteSet(); modules.forEach((String path, String localPath) -> { InputStreamHandle handle = new InputStreamHandle(HubTestBase.class.getClassLoader().getResourceAsStream(localPath)); String ext = FilenameUtils.getExtension(path); switch(ext) { case "xml": handle.setFormat(Format.XML); break; case "json": handle.setFormat(Format.JSON); break; default: handle.setFormat(Format.TEXT); } DocumentMetadataHandle permissions = new DocumentMetadataHandle() .withPermission(getHubAdminConfig().getHubRoleName(), DocumentMetadataHandle.Capability.EXECUTE, UPDATE, READ); writeSet.add(path, permissions, handle); }); modMgr.write(writeSet); clearFlowCache(); }
@Before public void setUp() throws KeyManagementException, NoSuchAlgorithmException, Exception { int restPort = getRestServerPort(); String appServerHostname = getRestAppServerHostName(); client = getDatabaseClientOnDatabase(appServerHostname, restPort, dbName, "eval-user", "x", getConnType()); resourceMgr = client.newServerConfigManager().newResourceExtensionsManager(); ExtensionMetadata resextMetadata = new ExtensionMetadata(); resextMetadata.setTitle("BasicJSTest"); resextMetadata.setDescription("Testing resource extension for java script"); System.out.println(resextMetadata.getScriptLanguage()); resextMetadata.setScriptLanguage(ScriptLanguage.JAVASCRIPT); System.out.println(resextMetadata.getScriptLanguage()); resextMetadata.setVersion("1.0"); MethodParameters getParams = new MethodParameters(MethodType.GET); getParams.add("my-uri", "xs:string?"); FileInputStream myStream = new FileInputStream("src/test/java/com/marklogic/client/functionaltest/data/JSResource.js"); InputStreamHandle handle = new InputStreamHandle(myStream); handle.set(myStream); resourceMgr.writeServices("simpleJSResourceModule", handle, resextMetadata, getParams); }
@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()); }