Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / WebServices / WebServiceFrontEnd.cs
blob8ad4fc5263dff31e718d087beac4e5227c27edd7
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.WebService;
44 namespace WebService_CodeBehind {
46 struct remoteChannel {
47 //.Net Remoting channel to beagledWeb, WebServiceBackEnd within beagled
48 private static bool registerChannelDone = false;
50 public static void Register() {
52 if (registerChannelDone)
53 return;
55 ChannelServices.RegisterChannel(new TcpChannel());
57 WellKnownClientTypeEntry WKCTE_Web =
58 new WellKnownClientTypeEntry(typeof(WebBackEnd),
59 "tcp://localhost:8347/WebBackEnd.rem");
60 RemotingConfiguration.RegisterWellKnownClientType(WKCTE_Web);
62 WellKnownClientTypeEntry WKCTE_WebService =
63 new WellKnownClientTypeEntry(typeof(WebServiceBackEnd),
64 "tcp://localhost:8347/WebServiceBackEnd.rem");
65 RemotingConfiguration.RegisterWellKnownClientType(WKCTE_WebService);
67 registerChannelDone = true;
71 [WebService(Description = "Web Service Interface to Beagle",
72 Namespace = "http://www.gnome.org/projects/beagle/webservices",
73 Name = "BeagleWebService")]
74 public class BeagleWebService: System.Web.Services.WebService {
76 private WebServiceBackEnd remoteObj = null;
78 private SearchResult initialQuery(SearchRequest BeagleQueryRequest)
80 SearchResult sr;
82 if (BeagleQueryRequest.text == null || BeagleQueryRequest.text.Length == 0 ||
83 (BeagleQueryRequest.text.Length == 1 && BeagleQueryRequest.text[0].Trim()== "")) {
85 sr = new SearchResult();
86 sr.statusCode = WebServiceBackEnd.SC_INVALID_QUERY;
87 sr.statusMsg = "No search terms specified";
88 return sr;
91 remoteChannel.Register();
93 if (remoteObj == null)
94 remoteObj = new WebServiceBackEnd();
96 bool isLocalReq = HttpContext.Current.Request.Url.IsLoopback;
98 if ((remoteObj == null) || !(remoteObj.allowGlobalAccess || isLocalReq) ) {
100 return restrictedAccessResult();
103 sr = remoteObj.doQuery(BeagleQueryRequest, isLocalReq);
104 return sr;
107 [WebMethod(Description = "Full object interface to Beagle")]
108 [System.Web.Services.Protocols.SoapDocumentMethodAttribute(
109 "http://www.gnome.org/projects/beagle/webservices/BeagleQuery",
110 RequestNamespace="http://www.gnome.org/projects/beagle/webservices",
111 ResponseNamespace="http://www.gnome.org/projects/beagle/webservices")]
112 public SearchResult BeagleQuery(SearchRequest BeagleQueryRequest)
114 return initialQuery(BeagleQueryRequest);
117 [WebMethod(Description = "Simple Interface to Beagle")]
118 [System.Web.Services.Protocols.SoapDocumentMethodAttribute(
119 "http://www.gnome.org/projects/beagle/webservices/SimpleQuery",
120 RequestNamespace="http://www.gnome.org/projects/beagle/webservices",
121 ResponseNamespace="http://www.gnome.org/projects/beagle/webservices")]
122 public SearchResult SimpleQuery(string text)
124 SearchResult sr;
126 if (text == null || text.Trim() == "") {
128 sr = new SearchResult();
129 sr.statusCode = WebServiceBackEnd.SC_INVALID_QUERY;
130 sr.statusMsg = "No search terms specified";
131 return sr;
134 SearchRequest srq = new SearchRequest();
136 srq.text = new string[1];
137 srq.text[0] = text.Trim();
139 srq.qdomain = Beagle.QueryDomain.Neighborhood;
141 return initialQuery(srq);
144 [WebMethod(Description = "Full text Interface to Beagle")]
145 [System.Web.Services.Protocols.SoapDocumentMethodAttribute(
146 "http://www.gnome.org/projects/beagle/webservices/SimpleQuery2",
147 RequestNamespace="http://www.gnome.org/projects/beagle/webservices",
148 ResponseNamespace="http://www.gnome.org/projects/beagle/webservices")]
149 public SearchResult SimpleQuery2(string text, string mimeType, string source, string queryDomain)
151 SearchResult sr;
153 if (text == null || text.Trim() == "") {
155 sr = new SearchResult();
156 sr.statusCode = WebServiceBackEnd.SC_INVALID_QUERY;
157 sr.statusMsg = "No search terms specified";
158 return sr;
161 SearchRequest srq = new SearchRequest();
163 srq.text = new string[1];
164 srq.text[0] = text.Trim();
166 if (mimeType != null && mimeType != "") {
168 srq.mimeType = new string[1];
169 srq.mimeType[0] = mimeType.Trim();
172 srq.qdomain = Beagle.QueryDomain.Neighborhood;
173 switch (queryDomain.Trim().ToLower()) {
175 case "local" : srq.qdomain = Beagle.QueryDomain.Local;
176 break;
178 case "system" : srq.qdomain = Beagle.QueryDomain.System;
179 break;
181 case "neighborhood" :
182 srq.qdomain = Beagle.QueryDomain.Neighborhood;
183 break;
185 case "global" : srq.qdomain = Beagle.QueryDomain.Global;
186 break;
188 default: srq.qdomain = Beagle.QueryDomain.Neighborhood;
189 break;
192 string sourceSelector = null;
193 srq.searchSources = new string[1];
195 switch (source.Trim().ToLower()) {
197 case "files" : sourceSelector = "Files";
198 break;
200 case "addressbook" :
201 sourceSelector = "Contact";
202 break;
204 case "mail" : sourceSelector = "MailMessage";
205 break;
207 case "web": sourceSelector = "WebHistory";
208 break;
210 case "chats": sourceSelector = "IMLog";
211 break;
213 default: sourceSelector = null;
214 break;
217 srq.searchSources[0] = sourceSelector;
218 return initialQuery(srq);
221 [WebMethod(Description = "Common Interface to get more results from Beagle")]
222 [System.Web.Services.Protocols.SoapDocumentMethodAttribute(
223 "http://www.gnome.org/projects/beagle/webservices/GetMoreResults",
224 RequestNamespace="http://www.gnome.org/projects/beagle/webservices",
225 ResponseNamespace="http://www.gnome.org/projects/beagle/webservices")]
226 public SearchResult GetMoreResults(string searchToken, int index)
228 SearchResult sr;
230 if (searchToken == null | searchToken == "") {
231 sr = new SearchResult();
232 sr.statusCode = WebServiceBackEnd.SC_INVALID_SEARCH_TOKEN;
233 sr.statusMsg = "Invalid Search Token";
236 remoteChannel.Register();
238 if (remoteObj == null)
239 remoteObj = new WebServiceBackEnd();
241 bool isLocalReq = HttpContext.Current.Request.Url.IsLoopback;
243 if ((remoteObj == null) || !(remoteObj.allowGlobalAccess || isLocalReq) ) {
245 return restrictedAccessResult();
248 sr = remoteObj.getMoreResults(searchToken, index, isLocalReq);
249 return sr;
252 [WebMethod(Description = "Common Interface to get Snippets for results")]
253 [System.Web.Services.Protocols.SoapDocumentMethodAttribute(
254 "http://www.gnome.org/projects/beagle/webservices/GetSnippets",
255 RequestNamespace="http://www.gnome.org/projects/beagle/webservices",
256 ResponseNamespace="http://www.gnome.org/projects/beagle/webservices")]
257 public HitSnippet[] GetSnippets(string searchToken, int[] hitIds)
259 HitSnippet[] response;
261 if (searchToken == null | searchToken == "") {
262 response = new HitSnippet[0];
263 return response;
266 remoteChannel.Register();
268 if (remoteObj == null)
269 remoteObj = new WebServiceBackEnd();
271 bool isLocalReq = HttpContext.Current.Request.Url.IsLoopback;
273 if ((remoteObj == null) || !(remoteObj.allowGlobalAccess || isLocalReq) ) {
275 response = new HitSnippet[0];
276 return response;
279 if (hitIds.Length < 1)
280 response = new HitSnippet[0];
281 else
282 response = remoteObj.getSnippets(searchToken, hitIds);
284 return response;
287 private static string localReqOnlyMsg = "Beagle web service unavailable or access restricted to local address only !";
289 private SearchResult restrictedAccessResult()
291 SearchResult sr = new SearchResult();
293 sr.totalResults = 0;
295 sr.statusCode = WebServiceBackEnd.SC_UNAUTHORIZED_ACCESS;
296 sr.statusMsg = localReqOnlyMsg;
298 return sr;