KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webdav > lib > methods > SearchMethod


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/methods/SearchMethod.java,v 1.5 2004/08/02 15:45:47 unico Exp $
3  * $Revision: 1.5 $
4  * $Date: 2004/08/02 15:45:47 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.webdav.lib.methods;
25
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Vector JavaDoc;
30 import org.apache.commons.httpclient.HttpConnection;
31 import org.apache.commons.httpclient.HttpException;
32 import org.apache.commons.httpclient.HttpState;
33 import org.apache.webdav.lib.util.WebdavStatus;
34
35 /**
36  * This class implements the WebDAV SEARCH Method.
37  *
38  * <P> The SEARCH method initiates a server-side search. The body of the
39  * request defines the query. The server responds with a text/xml entity
40  * matching the WebDAV PROPFIND response.
41  *
42  * <P> According to
43  * <ahref="http://www.webdav.org/dasl/protocol/draft-dasl-protocol-00.html">
44  * the DASL draft</a> a typical request looks like this:
45  *
46  * <PRE>
47  * SEARCH /folder/ HTTP/1.1
48  * Host: www.foo.bar
49  * Content-type: text/xml; charset="utf-8"
50  * Content-Length: xxxx
51  *
52  * &lt;?xml version="1.0"?>
53  * &lt;D:searchrequest xmlns:D = "DAV:" >
54  * &lt;D:basicsearch>
55  * &lt;D:select>
56  * &lt;D:prop>&lt;D:getcontentlength/>&lt;/D:prop>
57  * &lt;/D:select>
58  * &lt;D:from>
59  * &lt;D:scope>
60  * &lt;D:href>/folder/&lt;/D:href>
61  * &lt;D:depth>infinity&lt;/D:depth>
62  * &lt;/D:scope>
63  * &lt;/D:from>
64  * &lt;/D:basicsearch>
65  * &lt;/D:searchrequest>
66  * </PRE>
67  *
68  * <P> However, other query grammars may be used. A typical request using
69  * the
70  * <ahref="http://msdn.microsoft.com/library/default.asp?URL=/library/psdk/exchsv2k/_exch2k_sql_web_storage_system_sql.htm">
71  * SQL-based grammar</a> implemented in Microsoft's Web Storage System
72  * (currently shipping with Exchange 2000 and SharePoint Portal Server)
73  * looks like this:
74  *
75  * <PRE>
76  * SEARCH /folder/ HTTP/1.1
77  * Host: www.foo.bar
78  * Content-type: text/xml; charset="utf-8"
79  * Content-Length: xxxx
80  *
81  * &lt;?xml version="1.0"?>
82  * &lt;D:searchrequest xmlns:D = "DAV:" >
83  * &lt;D:sql>
84  * SELECT "DAV:contentclass", "DAV:displayname"
85  * FROM "/folder/"
86  * WHERE "DAV:ishidden" = false
87  * AND "DAV:isfolder" = false
88  * &lt;/D:sql>
89  * &lt;/D:searchrequest>
90  * </PRE>
91  *
92  */

93
94 public class SearchMethod extends XMLResponseMethodBase {
95
96
97     // -------------------------------------------------------------- Constants
98

99
100     // ----------------------------------------------------------- Constructors
101

102
103     /**
104      * Method constructor.
105      */

106     public SearchMethod() {
107     }
108
109
110     /**
111      * Method constructor.
112      */

113     public SearchMethod(String JavaDoc path) {
114         super(path);
115     }
116
117
118     /**
119      * Construct a SearchMethod using the given XML request body.
120      *
121      * @param path Relative path to the WebDAV resource
122      * (presumably a collection).
123      * @param query Complete request body in XML including a search query in
124      * your favorite grammar.
125      */

126     public SearchMethod(String JavaDoc path, String JavaDoc query) {
127         this(path);
128         preloadedQuery = query;
129     }
130
131
132     // ----------------------------------------------------- Instance Variables
133

134
135     /**
136      * The namespace abbreviation that prefixes DAV tags
137      */

138     protected String JavaDoc prefix = null;
139
140     private String JavaDoc preloadedQuery = null;
141
142     // ------------------------------------------------------------- Properties
143

144
145     // --------------------------------------------------- WebdavMethod Methods
146

147     public String JavaDoc getName() {
148         return "SEARCH";
149     }
150
151     public void recycle() {
152         super.recycle();
153         prefix = null;
154     }
155
156
157     /**
158      * Generate additional headers needed by the request.
159      *
160      * @param state State token
161      * @param conn the connection
162      */

163     public void addRequestHeaders(HttpState state, HttpConnection conn)
164     throws IOException JavaDoc, HttpException {
165
166         // set the default utf-8 encoding, if not already present
167
if (getRequestHeader("Content-Type") == null ) super.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
168         super.addRequestHeaders(state, conn);
169
170     }
171
172     /**
173      * DAV requests that contain a body must override this function to
174      * generate that body.
175      *
176      * <p>The default behavior simply returns an empty body.</p>
177      */

178     protected String JavaDoc generateRequestBody() {
179
180         if (preloadedQuery == null || preloadedQuery.trim().length() < 1) {
181             // TODO Must support some mechanism for delegating the
182
// generation of the query to a pluggable query grammar
183
// support class or package. Right now, executing this
184
// method object without first explicitly setting the
185
// query is an error.
186
return "";
187         } else {
188             return preloadedQuery;
189         }
190
191     }
192
193     /**
194      * This method returns an enumeration of URL paths. If the PropFindMethod
195      * was sent to the URL of a collection, then there will be multiple URLs.
196      * The URLs are picked out of the <code>&lt;D:href&gt;</code> elements
197      * of the response.
198      *
199      * @return an enumeration of URL paths as Strings
200      */

201     public Enumeration JavaDoc getAllResponseURLs() {
202         checkUsed();
203         return getResponseURLs().elements();
204     }
205
206
207     /**
208      * Returns an enumeration of <code>Property</code> objects.
209      */

210     public Enumeration JavaDoc getResponseProperties(String JavaDoc urlPath) {
211         checkUsed();
212
213         Response response = (Response) getResponseHashtable().get(urlPath);
214         if (response != null) {
215             return response.getProperties();
216         } else {
217             return (new Vector JavaDoc()).elements();
218         }
219
220     }
221
222     /**
223      * Parse response.
224      *
225      * @param input Input stream
226      */

227     public void parseResponse(InputStream JavaDoc input, HttpState state, HttpConnection conn)
228         throws IOException JavaDoc, HttpException {
229         try
230         {
231             int code = getStatusLine().getStatusCode();
232             if (code == WebdavStatus.SC_BAD_REQUEST ||
233                 code == WebdavStatus.SC_MULTI_STATUS ||
234                 code == WebdavStatus.SC_FORBIDDEN ||
235                 code == WebdavStatus.SC_CONFLICT ) {
236                 parseXMLResponse(input);
237             }
238         }
239         catch (IOException JavaDoc e) {
240                 // FIX ME: provide a way to deliver non xml data
241
}
242     }
243
244
245
246 }
247
248
249
250
251
252
Popular Tags