@Before public void setUp() throws Exception { // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); // create new connection for each test below client = DatabaseClientFactory.newClient( "localhost", restPort, "usr1", "password", Authentication.DIGEST); }
// clean up by deleting the example resource extension public static void tearDownExample( String host, int port, String user, String password, Authentication authType) { DatabaseClient client = DatabaseClientFactory.newClient(host, port, user, password, authType); ResourceExtensionsManager resourceMgr = client.newServerConfigManager().newResourceExtensionsManager(); resourceMgr.deleteServices(DictionaryManager.NAME); client.release(); }
// install the resource extension on the server public static void installResourceExtension( String host, int port, String user, String password, Authentication authType) throws IOException { // create the client DatabaseClient client = DatabaseClientFactory.newClient(host, port, user, password, authType); // use either shortcut or strong typed IO installResourceExtensionShortcut(client); installResourceExtensionStrongTyped(client); // release the client client.release(); }
// use the resource manager public static void useResource( String host, int port, String user, String password, Authentication authType) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException { // create the client DatabaseClient client = DatabaseClientFactory.newClient(host, port, user, password, authType); // create the resource extension client DictionaryManager dictionaryMgr = new DictionaryManager(client); // specify the identifier for the dictionary String uri = "/example/metasyn.xml"; // create the dictionary String[] words = {"foo", "bar", "baz", "qux", "quux", "wibble", "wobble", "wubble"}; dictionaryMgr.createDictionary(uri, words); System.out.println("Created a dictionary on the server at " + uri); // check the validity of the dictionary Document[] list = dictionaryMgr.checkDictionaries(uri); if (list == null || list.length == 0) System.out.println("Could not check the validity of the dictionary at " + uri); else System.out.println( "Checked the validity of the dictionary at " + uri + ": " + !"invalid".equals(list[0].getDocumentElement().getNodeName())); // use a resource service to check the correctness of a word String word = "biz"; if (!dictionaryMgr.isCorrect(word, uri)) { System.out.println("Confirmed that '" + word + "' is not in the dictionary at " + uri); // use a resource service to look up suggestions String[] suggestions = dictionaryMgr.suggest(word, null, null, uri); System.out.println("Nearest matches for '" + word + "' in the dictionary at " + uri); for (String suggestion : suggestions) { System.out.println(" " + suggestion); } } // delete the dictionary dictionaryMgr.deleteDictionary(uri); // release the client client.release(); }
public static void run(ExampleProperties props) throws IOException { System.out.println("example: " + StreamReadWrite.class.getName() + "\n"); // create the client DatabaseClient client = DatabaseClientFactory.newClient( props.host, props.port, props.adminUser, props.adminPassword, props.authType); writeBinaryContent(client); readWriteBinaryStream(client); readBinaryBuffer(client); // release the client client.release(); }
public static void run(ExampleProperties props) throws IOException { System.out.println("example: " + DocumentOutputStream.class.getName()); final int MAX_BUF = 8192; final String FILENAME = "flipper.xml"; // create the client DatabaseClient client = DatabaseClientFactory.newClient( props.host, props.port, props.writerUser, props.writerPassword, props.authType); // create a manager for XML documents XMLDocumentManager docMgr = client.newXMLDocumentManager(); // create an identifier for the document String docId = "/example/" + FILENAME; // create an anonymous class with a callback method OutputStreamSender sender = new OutputStreamSender() { // the callback receives the output stream public void write(OutputStream out) throws IOException { // acquire the content InputStream docStream = Util.openStream("data" + File.separator + FILENAME); if (docStream == null) throw new IOException("Could not read document example"); // copy content to the output stream byte[] buf = new byte[MAX_BUF]; int byteCount = 0; while ((byteCount = docStream.read(buf)) != -1) { out.write(buf, 0, byteCount); } } }; // create a handle for writing the content OutputStreamHandle handle = new OutputStreamHandle(sender); // write the document content docMgr.write(docId, handle); System.out.println("Wrote /example/" + FILENAME + " content"); tearDownExample(docMgr, docId); // release the client client.release(); }
@Bean @Scope("singleton") public DatabaseClient databaseClient() { final String host = Play.application().configuration().getString("ml.host"); final int port = Play.application().configuration().getInt("ml.port"); final String user = "******"; final String password = "******"; DatabaseClient db = DatabaseClientFactory.newClient(host, port, user, password, Authentication.DIGEST); String options = "<search:options " + "xmlns:search='http://marklogic.com/appservices/search'>" + "<search:return-results>true</search:return-results>" + "<search:transform-results apply='raw' />" + "</search:options>"; QueryOptionsManager optionsMgr = db.newServerConfigManager().newQueryOptionsManager(); optionsMgr.writeOptions("default", new StringHandle(options)); return db; }
@Before public void setUp() throws Exception { client = DatabaseClientFactory.newClient( "localhost", restPort, "rest-admin", "x", Authentication.DIGEST); }
@Test public void testWithVariousGrammarAndWordQuery() throws IOException, ParserConfigurationException, SAXException, XpathException { System.out.println("Running testWithVariousGrammarAndWordQuery"); String[] filenames = { "constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml" }; String queryOptionName = "absRangeConstraintWithVariousGrammarAndWordQueryOpt.xml"; DatabaseClient client = DatabaseClientFactory.newClient( "localhost", 8011, "rest-admin", "x", Authentication.DIGEST); // write docs for (String filename : filenames) { writeDocumentUsingInputStreamHandle(client, filename, "/abs-range-constraint/", "XML"); } setQueryOption(client, queryOptionName); QueryManager queryMgr = client.newQueryManager(); // create query def StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); querydef.setCriteria("(pop:high OR pop:medium) AND price:medium AND intitle:served"); // create handle DOMHandle resultsHandle = new DOMHandle(); queryMgr.search(querydef, resultsHandle); // get the result Document resultDoc = resultsHandle.get(); assertXpathEvaluatesTo( "1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); assertXpathEvaluatesTo( "Vannevar served", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); assertXpathEvaluatesTo( "12.34", "string(//*[local-name()='result'][1]//@*[local-name()='amt'])", resultDoc); assertXpathEvaluatesTo( "5", "string(//*[local-name()='result'][1]//*[local-name()='popularity'])", resultDoc); assertXpathEvaluatesTo( "1", "string(//*[local-name()='facet-value']//@*[local-name()='count'])", resultDoc); assertXpathEvaluatesTo("High", "string(//*[local-name()='facet-value'])", resultDoc); // String expectedSearchReport = "(cts:search(fn:collection(), // cts:and-query((cts:or-query((cts:element-range-query(fn:QName(\"\", \"popularity\"), // \">=\", xs:int(\"5\"), (), 1), cts:and-query((cts:element-range-query(fn:QName(\"\", // \"popularity\"), \">=\", xs:int(\"3\"), (), 1), cts:element-range-query(fn:QName(\"\", // \"popularity\"), \"<\", xs:int(\"5\"), (), 1)), ()))), // cts:element-attribute-range-query(fn:QName(\"http://cloudbank.com\", \"price\"), // fn:QName(\"\", \"amt\"), \">=\", 3.0, (), 1), // cts:element-attribute-range-query(fn:QName(\"http://cloudbank.com\", \"price\"), // fn:QName(\"\", \"amt\"), \"<\", 14.0, (), 1), cts:element-word-query(fn:QName(\"\", // \"title\"), \"served\", (\"lang=en\"), 1)), ()), (\"score-logtfidf\"), 1))[1 to 10]"; // assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", // resultDoc); // release client client.release(); }