/** * 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; }
writeHandle.set(jsonQueryOption); writeHandle.setFormat(Format.JSON); optionsMgr.writeOptions(queryOptionNameJson, writeHandle);
/** * Initializes the handle with a string for the content. * @param content a content string */ public StringHandle(String content) { this(); set(content); }
/** * Initializes the handle with a string for the content. * @param content a content string */ public StringHandle(String content) { this(); set(content); }
/** * 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; }
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); } }
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(); } }
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); } }
/** * 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"); }
/** * Write document using StringHandle with metadata * * @param client * @param filename * @param uri * @param metadataHandle * @param type * @throws IOException */ public void writeDocumentUsingStringHandle(DatabaseClient client, String filename, String uri, DocumentMetadataHandle metadataHandle, 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, metadataHandle, contentHandle); System.out.println("Write " + docId + " to the database"); }
/** * Update document using FileHandle * * @param client * @param filename * @param uri * @param type * @throws IOException */ public void updateDocumentUsingStringHandle(DatabaseClient client, String filename, String uri, String type) throws IOException { // create doc manager DocumentManager docMgr = null; docMgr = documentManagerSelector(client, docMgr, type); 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 an identifier for the document String docId = uri; // create a handle on the content // create handle StringHandle contentHandle = new StringHandle(); contentHandle.set(readContent); // write the document content docMgr.write(docId, contentHandle); System.out.println("Update " + docId + " to database"); }
@SuppressWarnings("deprecation") @Test public void testUserPortal() throws Exception { final String query1 = "fn:count(fn:doc())"; InetAddress addr = java.net.InetAddress.getLocalHost(); System.out.println("Hostname is : " + addr.getHostName()); DatabaseClient client = DatabaseClientFactory.newClient(localHostname, port, new CertificateAuthContext(temp + "portal.p12", "abc")); int count = client.newServerEval().xquery(query1).eval().next().getNumber().intValue(); System.out.println(count); TextDocumentManager docMgr = client.newTextDocumentManager(); String docId = "/example/texet.txt"; StringHandle handle = new StringHandle(); handle.set("A simple text document"); docMgr.write(docId, handle); System.out.println(client.newServerEval().xquery(query1)); System.out.println("Doc written"); Assert.assertEquals(count + 1, client.newServerEval().xquery(query1).eval().next().getNumber().intValue()); client.release(); }
contentHandle.set(xmlStrbuf.toString());
String docId = "/example/text.txt"; StringHandle handle = new StringHandle(); handle.set("A simple text document by SSL connection"); docMgr.write(docId, handle);
@Test public void testInvalidUserAuth() { // create the client DatabaseClient client = DatabaseClientFactory.newClient( Common.HOST, Common.PORT, new DigestAuthContext("MyFooUser", "x")); String expectedException = "com.marklogic.client.FailedRequestException: " + "Local message: write failed: Unauthorized. Server Message: Unauthorized"; String exception = ""; String docId = "/example/text.txt"; TextDocumentManager docMgr = client.newTextDocumentManager(); try { // make use of the client connection so we get an auth error StringHandle handle = new StringHandle(); handle.set("A simple text document"); docMgr.write(docId, handle); // the next line will only run if write doesn't throw an exception docMgr.delete(docId); } catch (FailedRequestException e) { exception = e.toString(); } finally { client.release(); } assertEquals(expectedException, exception); } }
@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>"); output); criteria.withFormat(Format.JSON).set( "{"+ "\"$format\":\"xml\","+
@Test public void testResourceServiceExtension() throws XpathException, SAXException, IOException { ResourceExtensionsManager extensionMgr = Common.adminClient.newServerConfigManager().newResourceExtensionsManager(); StringHandle handle = new StringHandle(); ExtensionMetadata metadata = makeMetadata(); MethodParameters[] params = makeParameters(); handle.set(resourceServices); extensionMgr.writeServices(RESOURCE_NAME, handle, metadata, params); extensionMgr.readServices(RESOURCE_NAME, handle); assertEquals("Failed to retrieve resource services", resourceServices, handle.get()); String result = extensionMgr.listServices(new StringHandle().withFormat(Format.XML), true).get(); assertNotNull("Failed to retrieve resource services list", result); assertTrue("List without resource", xpather.getMatchingNodes( "/rapi:resources/rapi:resource/rapi:name[string(.) = 'testresource']", XMLUnit.buildControlDocument(result) ).getLength() == 1); extensionMgr.deleteServices(RESOURCE_NAME); result = extensionMgr.readServices(RESOURCE_NAME, handle).get(); assertTrue("Failed to delete resource services", result == null); } }
@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); }
@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); }