DASL Search Example
Here is a quick example of using the Slide client library and DASL to execute a search against a Slide.
Assumptions
- You have a Slide server running at localhost:8080/slide
- You have a user john:john that can access the server
You have a file somewhere under /slide/files/articles that contains the word TERMS
Example Code
/*
* Copyright 1999-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package test;
import java.io.IOException;
import java.util.Enumeration;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.HttpURL;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.webdav.lib.WebdavResource;
import org.apache.webdav.lib.WebdavState;
import org.apache.webdav.lib.methods.SearchMethod;
public class SearchTest {
public static void main(String[] args) throws HttpException, IOException {
String path = "/slide/files";
String query =
"<D:searchrequest xmlns:D =\"DAV:\">" +
"<D:basicsearch>" +
"<D:select>" +
"<D:prop>" +
"<D:getcontentlength/>" +
"</D:prop>" +
"</D:select>" +
"<D:from>" +
"<D:scope>" +
"<D:href>articles</D:href>" +
"<D:depth>infinity</D:depth>" +
"</D:scope>" +
"</D:from>" +
"<D:where>" +
"<D:contains>TERMS</D:contains>" +
"</D:where>" +
"</D:basicsearch>" +
"</D:searchrequest>";
SearchMethod method = new SearchMethod(path, query);
HttpURL httpURL = new HttpURL("http://localhost:8080/slide/");
HttpClient client = new HttpClient();
client.setState(new WebdavState());
HostConfiguration hostConfig = client.getHostConfiguration();
hostConfig.setHost(httpURL);
Credentials hostCredentials =
new UsernamePasswordCredentials("john","john");
if (hostCredentials != null) {
HttpState clientState = client.getState();
clientState.setCredentials(null, httpURL.getHost(),
hostCredentials);
clientState.setAuthenticationPreemptive(true);
}
int state = client.executeMethod(method);
System.out.println("State: " + state);
Header[] headers = method.getResponseHeaders();
for (int i = 0; i < headers.length; i++) {
System.out.println(headers[i].toString());
}
Enumeration e = method.getAllResponseURLs();
while (e.hasMoreElements()) {
System.out.println(e.nextElement());
}
}
}