Differences between revisions 2 and 3
Revision 2 as of 2005-05-04 04:21:38
Size: 3819
Editor: JamesMason
Comment: Fixed a typo
Revision 3 as of 2007-10-08 10:09:59
Size: 3809
Editor: alphatan
Comment: revise: enum => e, because enum is a keyword in jdk5
Deletions are marked like this. Additions are marked like this.
Line 15: Line 15:
 * Copyright 1999-2004 The Apache Software Foundation   * Copyright 1999-2004 The Apache Software Foundation
Line 92: Line 92:
        Enumeration enum = method.getAllResponseURLs();
        while (enum.hasMoreElements()) {
            System.out.println(enum.nextElement());
        Enumeration e = method.getAllResponseURLs();
        while (e.hasMoreElements()) {
            System.out.println(e.nextElement());

DASL Search Example

Here is a quick example of using the Slide client library and DASL to execute a search against a Slide.

Assumptions

  1. You have a Slide server running at localhost:8080/slide
  2. You have a user john:john that can access the server
  3. 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());
        }
    }
}

DASLSearch (last edited 2009-09-20 22:02:49 by localhost)