Indexable is not marked _done_ until all the child indexables (including child of...
[beagle.git] / beagled / WebServices / WebServiceFrontEnd.cs
blob3bfa68cea77281a235559b11e4be0fa83919a53a
1 //
2 // WebServiceFrontEnd.cs
3 //
4 // Copyright (C) 2005 Novell, Inc.
5 //
6 // Authors:
7 // Vijay K. Nanjundaswamy (knvijay@novell.com)
8 //
10 // Permission is hereby granted, free of charge, to any person obtaining a
11 // copy of this software and associated documentation files (the "Software"),
12 // to deal in the Software without restriction, including without limitation
13 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 // and/or sell copies of the Software, and to permit persons to whom the
15 // Software is furnished to do so, subject to the following conditions:
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 // DEALINGS IN THE SOFTWARE.
29 using System;
30 using System.Threading;
31 using System.Collections;
33 using System.Web;
34 using System.Web.Services;
35 using System.Web.Services.Protocols;
37 using System.Runtime.Remoting;
38 using System.Runtime.Remoting.Channels;
39 using System.Runtime.Remoting.Channels.Tcp;
41 using Beagle.Daemon;
42 using Beagle.WebService;
45 namespace WebService_CodeBehind {
47 struct remoteChannel {
48 //.Net Remoting channel to beagledWeb, WebServiceBackEnd within beagled
49 private static bool registerChannelDone = false;
51 public static void Register() {
53 if (registerChannelDone)
54 return;
56 ChannelServices.RegisterChannel(new TcpChannel());
58 WellKnownClientTypeEntry WKCTE_Web =
59 new WellKnownClientTypeEntry(typeof(WebBackEnd),
60 "tcp://localhost:8347/WebBackEnd.rem");
61 RemotingConfiguration.RegisterWellKnownClientType(WKCTE_Web);
63 WellKnownClientTypeEntry WKCTE_WebService =
64 new WellKnownClientTypeEntry(typeof(WebServiceBackEnd),
65 "tcp://localhost:8347/WebServiceBackEnd.rem");
66 RemotingConfiguration.RegisterWellKnownClientType(WKCTE_WebService);
68 registerChannelDone = true;
72 [WebService(Description = "Web Service Interface to Beagle",
73 Namespace = "http://www.gnome.org/projects/beagle/webservices",
74 Name = "BeagleWebService")]
75 public class BeagleWebService: System.Web.Services.WebService {
77 private WebServiceBackEnd remoteObj = null;
79 private SearchResult initialQuery(SearchRequest req)
81 SearchResult sr;
83 if (req.text == null || req.text.Length == 0 ||
84 (req.text.Length == 1 && req.text[0].Trim()== "")) {
86 sr = new SearchResult();
87 sr.statusCode = WebServiceBackEnd.SC_INVALID_QUERY;
88 sr.statusMsg = "No search terms specified";
89 return sr;
92 remoteChannel.Register();
94 if (remoteObj == null)
95 remoteObj = new WebServiceBackEnd();
97 bool isLocalReq = HttpContext.Current.Request.Url.IsLoopback;
99 if ((remoteObj == null) || !(remoteObj.allowGlobalAccess || isLocalReq) ) {
101 return restrictedAccessResult();
104 sr = remoteObj.doQuery(req, isLocalReq);
105 return sr;
108 [WebMethod(Description = "Full object interface to Beagle")]
109 [System.Web.Services.Protocols.SoapDocumentMethodAttribute(
110 "http://www.gnome.org/projects/beagle/webservices/BeagleQuery",
111 RequestNamespace="http://www.gnome.org/projects/beagle/webservices",
112 ResponseNamespace="http://www.gnome.org/projects/beagle/webservices")]
113 public SearchResult BeagleQuery(SearchRequest req)
115 return initialQuery(req);
118 [WebMethod(Description = "Simple Interface to Beagle")]
119 [System.Web.Services.Protocols.SoapDocumentMethodAttribute(
120 "http://www.gnome.org/projects/beagle/webservices/SimpleQuery",
121 RequestNamespace="http://www.gnome.org/projects/beagle/webservices",
122 ResponseNamespace="http://www.gnome.org/projects/beagle/webservices")]
123 public SearchResult SimpleQuery(string text)
125 SearchResult sr;
127 if (text == null || text.Trim() == "") {
129 sr = new SearchResult();
130 sr.statusCode = WebServiceBackEnd.SC_INVALID_QUERY;
131 sr.statusMsg = "No search terms specified";
132 return sr;
135 SearchRequest srq = new SearchRequest();
137 srq.text = new string[1];
138 srq.text[0] = text.Trim();
140 srq.qdomain = Beagle.QueryDomain.Neighborhood;
142 return initialQuery(srq);
145 [WebMethod(Description = "Full text Interface to Beagle")]
146 [System.Web.Services.Protocols.SoapDocumentMethodAttribute(
147 "http://www.gnome.org/projects/beagle/webservices/SimpleQuery2",
148 RequestNamespace="http://www.gnome.org/projects/beagle/webservices",
149 ResponseNamespace="http://www.gnome.org/projects/beagle/webservices")]
150 public SearchResult SimpleQuery2(string text, string mimeType, string source, string queryDomain)
152 SearchResult sr;
154 if (text == null || text.Trim() == "") {
156 sr = new SearchResult();
157 sr.statusCode = WebServiceBackEnd.SC_INVALID_QUERY;
158 sr.statusMsg = "No search terms specified";
159 return sr;
162 SearchRequest srq = new SearchRequest();
164 srq.text = new string[1];
165 srq.text[0] = text.Trim();
167 if (mimeType != null && mimeType != "") {
169 srq.mimeType = new string[1];
170 srq.mimeType[0] = mimeType.Trim();
173 srq.qdomain = Beagle.QueryDomain.Neighborhood;
174 switch (queryDomain.Trim().ToLower()) {
176 case "local" : srq.qdomain = Beagle.QueryDomain.Local;
177 break;
179 case "system" : srq.qdomain = Beagle.QueryDomain.System;
180 break;
182 case "neighborhood" :
183 srq.qdomain = Beagle.QueryDomain.Neighborhood;
184 break;
186 case "global" : srq.qdomain = Beagle.QueryDomain.Global;
187 break;
189 default: srq.qdomain = Beagle.QueryDomain.Neighborhood;
190 break;
193 string sourceSelector = null;
194 srq.searchSources = new string[1];
196 switch (source.Trim().ToLower()) {
198 case "files" : sourceSelector = "Files";
199 break;
201 case "addressbook" :
202 sourceSelector = "Contact";
203 break;
205 case "mail" : sourceSelector = "MailMessage";
206 break;
208 case "web": sourceSelector = "WebHistory";
209 break;
211 case "chats": sourceSelector = "IMLog";
212 break;
214 default: sourceSelector = null;
215 break;
218 srq.searchSources[0] = sourceSelector;
219 return initialQuery(srq);
222 [WebMethod(Description = "Common Interface to get more results from Beagle")]
223 [System.Web.Services.Protocols.SoapDocumentMethodAttribute(
224 "http://www.gnome.org/projects/beagle/webservices/GetMoreResults",
225 RequestNamespace="http://www.gnome.org/projects/beagle/webservices",
226 ResponseNamespace="http://www.gnome.org/projects/beagle/webservices")]
227 public SearchResult GetResults(GetResultsRequest req)
229 SearchResult sr;
231 if (req.searchToken == null | req.searchToken == "") {
232 sr = new SearchResult();
233 sr.statusCode = WebServiceBackEnd.SC_INVALID_SEARCH_TOKEN;
234 sr.statusMsg = "Invalid Search Token";
237 remoteChannel.Register();
239 if (remoteObj == null)
240 remoteObj = new WebServiceBackEnd();
242 bool isLocalReq = HttpContext.Current.Request.Url.IsLoopback;
244 if ((remoteObj == null) || !(remoteObj.allowGlobalAccess || isLocalReq) ) {
246 return restrictedAccessResult();
249 sr = remoteObj.getResults(req, isLocalReq);
250 return sr;
253 [WebMethod(Description = "Common Interface to get Snippets for results")]
254 [System.Web.Services.Protocols.SoapDocumentMethodAttribute(
255 "http://www.gnome.org/projects/beagle/webservices/GetSnippets",
256 RequestNamespace="http://www.gnome.org/projects/beagle/webservices",
257 ResponseNamespace="http://www.gnome.org/projects/beagle/webservices")]
258 public HitSnippet[] GetSnippets(GetSnippetsRequest req)
260 HitSnippet[] response;
262 if (req.searchToken == null | req.searchToken == "") {
263 response = new HitSnippet[0];
264 return response;
267 remoteChannel.Register();
269 if (remoteObj == null)
270 remoteObj = new WebServiceBackEnd();
272 bool isLocalReq = HttpContext.Current.Request.Url.IsLoopback;
274 if ((remoteObj == null) || !(remoteObj.allowGlobalAccess || isLocalReq) ) {
276 response = new HitSnippet[0];
277 return response;
280 if (req.hitHashCodes.Length < 1)
281 response = new HitSnippet[0];
282 else
283 response = remoteObj.getSnippets(req);
285 return response;
288 private static string localReqOnlyMsg = "Beagle web service unavailable or access restricted to local address only !";
290 private SearchResult restrictedAccessResult()
292 SearchResult sr = new SearchResult();
294 sr.totalResults = 0;
296 sr.statusCode = WebServiceBackEnd.SC_UNAUTHORIZED_ACCESS;
297 sr.statusMsg = localReqOnlyMsg;
299 return sr;