@Override public List<File> importWorkspaces(String user, String... workspaceNames) { if (baseDir == null) { baseDir = getDefaultWorkspacesDir(); } List<File> files = new ArrayList<>(); File userDir = new File(baseDir, user); if (!userDir.exists()) { return files; } final String importQuery = determineImportScriptToUse(); for (String workspace : workspaceNames) { File f = new File(userDir, workspace + ".xml"); if (f.isFile() && f.exists()) { client.newServerEval() .addVariable("user", user) .addVariable("exported-workspace", new FileHandle(f).withFormat(Format.XML)) .xquery(importQuery).eval(); if (logger.isInfoEnabled()) { logger.info(format("Imported workspace from %s", f.getAbsolutePath())); } files.add(f); } } return files; }
/** * Specifies the format of the content and returns the handle * as a fluent convenience. * @param format the format of the content * @return this handle */ public FileHandle withFormat(Format format) { setFormat(format); return this; } /**
@Test public void testMerge_jsonxml_FileHandle() throws Exception { File file = new File(datasource + "bug25348.json"); FileHandle filehandle = new FileHandle(); filehandle.set(file); File file2 = new File(datasource + "relative5.xml"); FileHandle filehandle2 = new FileHandle(); filehandle2.set(file2); gmWriter.write("htp://test.sem.graph/trigM", filehandle.withMimetype(RDFMimeTypes.RDFJSON)); gmWriter.merge("htp://test.sem.graph/trigM", filehandle2.withMimetype(RDFMimeTypes.TRIPLEXML)); FileHandle handle = gmWriter.read("htp://test.sem.graph/trigM", new FileHandle().withMimetype(RDFMimeTypes.RDFJSON)); File readFile = handle.get(); String expectedContent = convertFileToString(readFile); assertTrue("Did not insert document or inserted empty doc", expectedContent.contains("http://example.com/ns/person#firstName") && expectedContent.contains("Anna's Homepage")); }
public static void writeNTriplesFromFile(String filename, String graphName) throws KeyManagementException, NoSuchAlgorithmException, Exception { File file = new File(datasource + filename); FileHandle filehandle = new FileHandle(); GraphManager sparqlGmgr = writeclient.newGraphManager(); filehandle.set(file); sparqlGmgr.write(graphName, filehandle.withMimetype(RDFMimeTypes.TURTLE)); }
@Test public void testJSONFormatOnText() throws KeyManagementException, NoSuchAlgorithmException, IOException, ParserConfigurationException, SAXException, XpathException { System.out.println("Running testJSONFormatOnText"); String filename = "text-original.txt"; String uri = "/json-format-txt-file/"; // connect the client DatabaseClient client = getDatabaseClient("rest-writer", "x", getConnType()); // create doc manager DocumentManager docMgr = client.newDocumentManager(); File file = new File("src/test/java/com/marklogic/client/functionaltest/data/" + filename); // create a handle on the content FileHandle handle = new FileHandle(file); handle.set(file); handle.setFormat(Format.JSON); // create docId String docId = uri + filename; String exception = ""; String expectedException = ""; try { docMgr.write(docId, handle); } catch (Exception e) { exception = e.toString(); } boolean isExceptionThrown = exception.contains(expectedException); assertTrue("Exception is not thrown", isExceptionThrown); // release the client client.release(); }
@Test public void testUnmatchedMimetypeOnXML() throws KeyManagementException, NoSuchAlgorithmException, IOException, ParserConfigurationException, SAXException, XpathException { System.out.println("Running testUnmatchedMimetypeOnXML"); String filename = "flipper.xml"; String uri = "/xml-mimetype/"; // connect the client DatabaseClient client = getDatabaseClient("rest-writer", "x", getConnType()); // create doc manager DocumentManager docMgr = client.newDocumentManager(); File file = new File("src/test/java/com/marklogic/client/functionaltest/data/" + filename); // create a handle on the content FileHandle handle = new FileHandle(file); handle.set(file); handle.setMimetype("image/svg+xml"); // create docId String docId = uri + filename; docMgr.write(docId, handle); String expectedUri = uri + filename; String docUri = docMgr.exists(expectedUri).getUri(); assertEquals("URI is not found", expectedUri, docUri); // read document mimetype docMgr.read(docId, handle); String format = handle.getFormat().name(); String expectedFormat = "XML"; assertEquals("Format does not match", expectedFormat, format); // release the client client.release(); }
FileHandle handle = new FileHandle(file); handle.set(file); handle.setFormat(Format.JSON); FileHandle updateHandle = new FileHandle(updateFile); updateHandle.set(updateFile); updateHandle.setFormat(Format.JSON); docMgr.write(docId, updateHandle, transaction2); FileHandle readHandle = new FileHandle(); docMgr.read(docId, readHandle); File fileRead = readHandle.get(); JsonNode readContent = mapper.readTree(fileRead);
FileHandle readHandle = new FileHandle(); File fileRead = readHandle.get();
File file1 = null, file2 = null; file1 = new File("src/test/java/com/marklogic/client/functionaltest/data/" + docId[0]); FileHandle handle1 = new FileHandle(file1); writeset.add("/1/" + docId[0], handle1.withFormat(Format.BINARY)); writeset.add("/2/" + docId[0], handle1.withFormat(Format.BINARY)); file2 = new File("src/test/java/com/marklogic/client/functionaltest/data/" + docId[1]); FileHandle handle2 = new FileHandle(file2); writeset.add("/1/" + docId[1], handle2.withFormat(Format.BINARY)); writeset.add("/2/" + docId[1], handle2.withFormat(Format.BINARY)); FileHandle readHandle1 = new FileHandle(); docMgr.read("/1/" + docId[0], readHandle1); FileHandle readHandle2 = new FileHandle(); docMgr.read("/1/" + docId[1], readHandle2); System.out.println(file1.getName() + ":" + fsize1 + " " + readHandle1.get().getName() + ":" + readHandle1.get().length()); System.out.println(file2.getName() + ":" + fsize2 + " " + readHandle2.get().getName() + ":" + readHandle2.get().length()); assertEquals("Size of the File 1" + docId[0], fsize1, readHandle1.get().length()); assertEquals("Size of the File 1" + docId[1], fsize2, readHandle2.get().length());
@Test public void testMergeSameDoc_jsonxml_FileHandle() throws Exception { gmWriter.setDefaultMimetype(RDFMimeTypes.RDFJSON); File file = new File(datasource + "bug25348.json"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write("htp://test.sem.graph/trigMSameDoc", filehandle); gmWriter.merge("htp://test.sem.graph/trigMSameDoc", filehandle); FileHandle handle = gmWriter.read("htp://test.sem.graph/trigMSameDoc", new FileHandle()); File readFile = handle.get(); String expectedContent = convertFileToString(readFile); assertTrue( "Did not insert document or inserted empty doc", expectedContent .equals("{\"http://example.com/ns/directory#m\":{\"http://example.com/ns/person#firstName\":[{\"value\":\"Michelle\", \"" + "type\":\"literal\", \"datatype\":\"http://www.w3.org/2001/XMLSchema#string\"}]}}")); }
static public FileHandle FileToHandle(File value) { return (value == null) ? null : new FileHandle(value); } static public Stream<FileHandle> FileToHandle(Stream<? extends File> values) {
@Test public void testJSONConverter() throws KeyManagementException, NoSuchAlgorithmException, IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testJSONConverter"); // String queryOptionName = "jsonConverterOpt.json"; String queryOptionName = "queryValidationOpt.json"; DatabaseClient client = getDatabaseClient("rest-admin", "x", getConnType()); // create a manager for writing query options QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); // create handle FileHandle handle = new FileHandle(new File("src/test/java/com/marklogic/client/functionaltest/queryoptions/" + queryOptionName)); handle.setFormat(Format.JSON); // write the query options to the database optionsMgr.writeOptions(queryOptionName, handle); System.out.println("Write " + queryOptionName + " to database"); // read query option with QueryOptionsHandle StringHandle readHandle = new StringHandle(); optionsMgr.readOptions(queryOptionName, readHandle); String output = readHandle.toString(); System.out.println(output); System.out.println("============================"); client.release(); }
@Test public void testWriteMergeDelete_Trx() throws Exception { Transaction trx = writerClient.openTransaction(); GraphManager gmWriter = writerClient.newGraphManager(); File file = new File(datasource + "triplexml1.xml"); gmWriter.write("htp://test.sem.graph/mergetrx", new FileHandle(file).withMimetype(RDFMimeTypes.TRIPLEXML), trx); file = new File(datasource + "bug25348.json"); gmWriter.merge("htp://test.sem.graph/mergetrx", new FileHandle(file).withMimetype(RDFMimeTypes.RDFJSON), trx); FileHandle handle = gmWriter.read("htp://test.sem.graph/mergetrx", new FileHandle(), trx); File readFile = handle.get(); String expectedContent = convertFileToString(readFile); try { assertTrue("Did not Merge document or inserted empty doc", expectedContent.contains("Michelle") && expectedContent.contains("Anna's Homepage")); gmWriter.delete("htp://test.sem.graph/mergetrx", trx); trx.commit(); trx = null; StringHandle readContent = gmWriter.read("htp://test.sem.graph/mergetrx", new StringHandle(), trx); assertTrue("Unexpected Content from read, expecting null", readContent == null); } catch (Exception e) { assertTrue("Unexpected Exception Thrown", e.toString().contains("ResourceNotFoundException")); } finally { if (trx != null) trx.commit(); trx = null; } }
public static void writeGeoDoubleFilesToDB(DatabaseClient client, String filename, String uri, String type) throws IOException, ParserConfigurationException, SAXException { // create doc manager DocumentManager docMgr = null; docMgr = documentMgrSelect(client, docMgr, type); File file = new File(datasource + filename); // create a handle on the content FileHandle handle = new FileHandle(file); handle.set(file); // write the document content DocumentWriteSet writeset = docMgr.newWriteSet(); writeset.add(uri, handle); docMgr.write(writeset); System.out.println("Write " + uri + " to database"); }
@Test public void testThings_Differentmimetypes() throws Exception { gmWriter.setDefaultMimetype(RDFMimeTypes.TRIPLEXML); String tripleGraphUri = "http://test.things.com/multiple"; File file = new File(datasource + "relative5.xml"); gmWriter.write(tripleGraphUri, new FileHandle(file)); file = new File(datasource + "semantics.ttl"); gmWriter.merge(tripleGraphUri, new FileHandle(file).withMimetype(RDFMimeTypes.TURTLE)); StringHandle things = gmWriter.things(new StringHandle(), "http://dbpedia.org/resource/Hadoop"); assertTrue("Things did not return expected Uri's", things.get().contains("Apache Hadoop")); gmWriter.delete(tripleGraphUri); }
/** * Assigns a file as the content and returns the handle * as a fluent convenience. * @param content the file * @return this handle */ public FileHandle with(File content) { set(content); return this; }
/** * Specifies the mime type of the content and returns the handle * as a fluent convenience. * @param mimetype the mime type of the content * @return this handle */ public FileHandle withMimetype(String mimetype) { setMimetype(mimetype); return this; }
@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()); }
/** * Zero-argument constructor. */ public FileHandle() { super(); setResendable(true); } /**
public static HandleFactoryRegistry registerDefaults(HandleFactoryRegistry registry) { registry.register(BytesHandle.newFactory()); registry.register(DOMHandle.newFactory()); registry.register(FileHandle.newFactory()); registry.register(InputSourceHandle.newFactory()); registry.register(InputStreamHandle.newFactory()); registry.register(ReaderHandle.newFactory()); registry.register(SourceHandle.newFactory()); registry.register(StringHandle.newFactory()); registry.register(XMLEventReaderHandle.newFactory()); registry.register(XMLStreamReaderHandle.newFactory()); return registry; }