@Override public Iterator<String> listGraphUris() { final String uriString = services.getGraphUris(requestLogger, new StringHandle()).get(); String[] uris = uriString.split("\n"); return Arrays.asList(uris).iterator(); }
@Override public AbstractWriteHandle getHandle() { // TODO: maybe serialize plan to JSON using JSON writer? return new StringHandle(getAst()).withFormat(Format.JSON); }
private void prepareQueryOption() { QueryOptionsManager qryOptMgr = connectionClient.newServerConfigManager().newQueryOptionsManager(); if (StringUtils.isNotEmpty(settings.queryOptionLiterals)) { StringHandle strHandle = new StringHandle(); switch (settings.queryLiteralType) { case "JSON": { strHandle.withFormat(Format.JSON); break; } case "XML": { strHandle.withFormat(Format.XML); break; } } strHandle.set(settings.queryOptionLiterals); qryOptMgr.writeOptions(settings.queryOptionName, strHandle); } }
@Test public void testBug19092WithJson() throws KeyManagementException, NoSuchAlgorithmException, IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testBug19092WithJson"); DatabaseClient client = getDatabaseClient("rest-admin", "x", getConnType()); // create query options manager QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); // create query options handle String xmlOptions = "<search:options xmlns:search='http://marklogic.com/appservices/search'>" + "<search:term>" + "<search:term-option>case-sensitive</search:term-option>" + "</search:term>" + "</search:options>"; StringHandle handle = new StringHandle(xmlOptions); // write query options optionsMgr.writeOptions("DefaultTermOpt", handle); // read query option StringHandle readHandle = new StringHandle(); readHandle.setFormat(Format.JSON); optionsMgr.readOptions("DefaultTermOpt", readHandle); String output = readHandle.get(); System.out.println(output); assertTrue("Default term is not correct", output.contains("{\"options\":{\"term\":{\"term-option\":[\"case-sensitive\"]}}}")); // release client client.release(); }
public String putJSON(String docUri) { RequestParameters params = new RequestParameters(); params.add("uri", docUri); String input = "{\"argument1\":\"hello\", \"argument2\":\"Earth\", \"content\":\"This is a JSON document\", \"response\":[200, \"OK\"], \"outputTypes\":\"application/json\"}"; StringHandle readHandle = new StringHandle(); // call the service getServices().put(params, new StringHandle(input).withFormat(Format.JSON), readHandle); // iterate over the results return readHandle.get(); }
@Test public void testWriteTextDoc() { System.out.println("Running testWriteTextDoc method"); String docId = "/foo/test/myFoo.txt"; TextDocumentManager docMgr = client.newTextDocumentManager(); docMgr.write(docId, new StringHandle().with("This is so foo")); assertEquals("Text document write difference", "This is so foo", docMgr.read(docId, new StringHandle()).get()); }
@Test public void testByExampleSearch() throws IOException, SAXException, XpathException { StringHandle criteria = new StringHandle().withFormat(Format.XML); criteria.set("<q:query xmlns:q='" + RawQueryByExampleDefinition.QBE_NS + "'>" + "<favorited>true</favorited>" + "</q:query>"); String output = queryMgr.validate(qbe, new StringHandle()).get(); assertNotNull("Empty XML validation", output); assertXMLEqual("Failed to validate QBE", output, "<q:valid-query xmlns:q=\"http://marklogic.com/appservices/querybyexample\"/>"); output = queryMgr.convert(qbe, new StringHandle()).get(); assertNotNull("Empty XML conversion", output); assertXpathEvaluatesTo( output); criteria.withFormat(Format.JSON).set( "{"+ "\"$format\":\"xml\","+ "}" ); output = queryMgr.search(qbe, new StringHandle().withFormat(Format.JSON)).get(); assertNotNull("Empty JSON output", output); assertTrue("Output without a match",
@Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (isFlowPropertiesFile(file) && modulesManager.hasFileBeenModifiedSinceLastLoaded(file.toFile())) { Flow flow = flowManager.getFlowFromProperties(file); StringHandle handle = new StringHandle(flow.serialize()); handle.setFormat(Format.XML); documentWriteSet.add(flow.getFlowDbPath(), handle); modulesManager.saveLastLoadedTimestamp(file.toFile(), new Date()); } return FileVisitResult.CONTINUE; } });
/** * Write document using StringHandle * * @param client * @param filename * @param uri * @param type * @throws IOException */ public void writeDocumentUsingStringHandle(DatabaseClient client, String filename, String uri, String type) throws IOException { // acquire the content File file = new File("src/test/java/com/marklogic/client/functionaltest/data/" + filename); FileInputStream fis = new FileInputStream(file); Scanner scanner = new Scanner(fis).useDelimiter("\\Z"); String readContent = scanner.next(); fis.close(); scanner.close(); // create doc manager DocumentManager docMgr = null; docMgr = documentManagerSelector(client, docMgr, type); String docId = uri + filename; // create handle StringHandle contentHandle = new StringHandle(); contentHandle.set(readContent); // write the doc docMgr.write(docId, contentHandle); System.out.println("Write " + docId + " to the database"); }
StringHandle readHandle = new StringHandle(); readHandle.setFormat(Format.JSON); optionsMgr.readOptions(queryOptionName, readHandle); String jsonQueryOption = readHandle.get(); StringHandle writeHandle = new StringHandle(); writeHandle.set(jsonQueryOption); writeHandle.setFormat(Format.JSON); optionsMgr.writeOptions(queryOptionNameJson, writeHandle); System.out.println("Write " + queryOptionNameJson + " to database");
public void writeXSLTransform(TransformExtensionsManager extensionMgr) throws ResourceNotFoundException, ResourceNotResendableException, ForbiddenUserException, FailedRequestException { extensionMgr.writeXSLTransform( XSLT_NAME, new StringHandle().withFormat(Format.XML).withMimetype("application/xslt+xml").with(xslTransform), makeXSLTMetadata() ); } }
public void writeXQueryTransform(TransformExtensionsManager extensionMgr) throws ResourceNotFoundException, ResourceNotResendableException, ForbiddenUserException, FailedRequestException { extensionMgr.writeXQueryTransform( XQUERY_NAME, new StringHandle().withFormat(Format.TEXT).with(xqueryTransform), makeXQueryMetadata() ); } public void writeXSLTransform(TransformExtensionsManager extensionMgr)
@Test public void testFailedSearch() throws IOException { StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); StructuredQueryDefinition t = qb.term("criteriaThatShouldNotMatchAnyDocument"); RawCombinedQueryDefinition queryDef = queryMgr.newRawCombinedQueryDefinition( new StringHandle(head + t.serialize() + tail) .withMimetype("application/xml") ); SearchHandle results = queryMgr.search(queryDef, new SearchHandle()); assertNotNull(results); MatchDocumentSummary[] summaries = results.getMatchResults(); assertTrue(summaries == null || summaries.length == 0); StringHandle criteria = new StringHandle().withFormat(Format.XML); criteria.set("<q:query xmlns:q='" + RawQueryByExampleDefinition.QBE_NS + "'>" + "<q:word>criteriaThatShouldNotMatchAnyDocument</q:word>" + "</q:query>"); RawQueryByExampleDefinition qbe = queryMgr.newRawQueryByExampleDefinition(criteria); results = queryMgr.search(qbe, new SearchHandle()); assertNotNull(results); summaries = results.getMatchResults(); assertTrue(summaries == null || summaries.length == 0); }
@Test public void jsonQbe() { JSONDocumentManager jdm = Common.client.newJSONDocumentManager(); QueryManager qm = Common.client.newQueryManager(); String queryAsString = "{ \"$query\": { \"kind\": \"bird\" } }"; StringHandle handle = new StringHandle(); handle.withFormat(Format.JSON).set(queryAsString); RawQueryByExampleDefinition query = qm.newRawQueryByExampleDefinition(handle); StringHandle report = qm.validate(query, new StringHandle()); System.out.println(report.toString()); SearchHandle results = qm.search(query, new SearchHandle()); assertEquals("6 json results should have matched", results.getTotalResults(), 6); DocumentPage documents = jdm.search(query, 1); assertEquals("6 json documents should have matched", documents.getTotalSize(), 6); documents = jdm.search(query, 1, new JacksonHandle()); assertEquals("6 json documents should have matched", documents.getTotalSize(), 6); documents = jdm.search(query, 1, new SearchHandle()); assertEquals("6 json documents should have matched", documents.getTotalSize(), 6); }
StringHandle handle = new StringHandle(); handle.set("A simple text document by SSL connection"); docMgr.write(docId, handle); handle = new StringHandle(); docMgr.read(docId, handle); assertEquals(handle.get(), "A simple text document by SSL connection"); docMgr.delete(docId);
@Test public void testRead_nonirin3_FileHandle() throws Exception { StringHandle handle = new StringHandle(); File file = new File(datasource + "non-iri.n3"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write("htp://test.sem.graph/n3noniri", filehandle.withMimetype(RDFMimeTypes.N3)); try { handle = gmWriter.read("htp://test.sem.graph/n3noniri", new StringHandle().withMimetype(RDFMimeTypes.N3)); } catch (Exception e) { assertTrue("Did not receive expected Error message, Expecting ::SEM-NOTRDF \n Received::" + e, e.toString().contains("SEM-NOTRDF") && e != null); } assertTrue("Read content has un-expected content ", handle.get().toString().contains("p0:named_graph")); }
public static void writeSPARQLDataFromString(String content, String graphName) throws KeyManagementException, NoSuchAlgorithmException, Exception { // Create Grapgh manager GraphManager sparqlGmgr = writeclient.newGraphManager(); StringHandle handle = new StringHandle(); handle.set(content.toString()); // write the triples in the doc into named graph sparqlGmgr.write(graphName, handle.withMimetype(RDFMimeTypes.TURTLE)); // Wait for index. Need a better way to handle this. try { Thread.sleep(5000); } catch (InterruptedException e) { System.out.print("Exception from writeSPARQLDataFromString " + e.getMessage()); e.printStackTrace(); } }
@Test public void testWrite_nquads_FileHandle() throws Exception { File file = new File(datasource + "relative2.nq"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write("htp://test.sem.graph/nquads", filehandle.withMimetype("application/n-quads")); StringHandle handle = gmWriter.read("htp://test.sem.graph/nquads", new StringHandle().withMimetype(RDFMimeTypes.NQUADS)); assertTrue("Did not insert document or inserted empty doc", handle.toString().contains("<#electricVehicle2>")); }
@Override public <C> ContentHandle<C> newHandle(Class<C> type) { @SuppressWarnings("unchecked") ContentHandle<C> handle = isHandled(type) ? (ContentHandle<C>) new StringHandle() : null; return handle; } };
/** * Assigns a string as the content and returns the handle * as a fluent convenience. * @param content a string * @return this handle */ public StringHandle with(String content) { set(content); return this; }