4 // Copyright (C) 2005 Novell, Inc.
7 // Vijay K. Nanjundaswamy (knvijay@novell.com)
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.
30 using System
.Collections
;
31 using System
.Collections
.Specialized
;
35 using System
.Web
.UI
.HtmlControls
;
36 using System
.Web
.UI
.WebControls
;
38 using System
.Runtime
.Remoting
;
39 using System
.Runtime
.Remoting
.Channels
;
40 using System
.Runtime
.Remoting
.Channels
.Tcp
;
44 using Beagle
.WebService
;
46 namespace WebService_CodeBehind
{
48 public class BeagleWebPage
: System
.Web
.UI
.Page
{
50 private static string HeaderMsg
= "BeagleWebInterface: ";
52 private static string enableSessionMsg
= HeaderMsg
+ "Please <b>enable web session tracking</b> (via Cookies or modified URL) ! <p> BeagleWeb access <i>cannot</i> function without session identification information";
54 private static string localReqOnlyMsg
= HeaderMsg
+ "Beagle web service unavailable or access restricted to local address only !";
56 protected HtmlForm SearchForm
;
57 protected Label Output
, TitleLabel
;
58 protected TextBox SearchBox
;
59 protected DropDownList sourceList
;
60 protected CheckBox GlobalSearchCheckBox
;
61 protected Button Search
, Forward
, Back
;
63 const string NO_RESULTS
= "No results.";
65 protected void Page_Load(Object o
, EventArgs e
) {
67 //note: this web-form relies on availability of Session information either through
68 // cookies or modified Url. It won't work if persistent session id is not available.
70 string sessId
= Session
.SessionID
;
71 Output
.Visible
= Back
.Visible
= Forward
.Visible
= true;
73 if (Session
["ResultsOnDisplay"] != null
74 && ((string)Session
["ResultsOnDisplay"]).StartsWith(HeaderMsg
+ NO_RESULTS
)) {
75 Back
.Visible
= Forward
.Visible
= false;
78 string actionString
= null;
79 bool queryStringProcessed
= false;
81 string reqUrl
= Request
.Url
.ToString();
82 int index
= reqUrl
.IndexOf(".aspx?");
84 actionString
= reqUrl
.Substring(index
+ ".aspx?".Length
);
88 if (actionString
== null) {
89 //HTTP Get without any query string
90 if (Session
["ResultsOnDisplay"] == null) {
92 Output
.Visible
= false;
93 Back
.Visible
= Forward
.Visible
= false;
95 int index1
= reqUrl
.IndexOf(".aspx");
96 if ((index1
> 0) && (index1
+ ".aspx".Length
< reqUrl
.Length
))
97 Session
["InitialReqUrl"] = reqUrl
.Substring(0, index1
+ ".aspx".Length
);
99 Session
["InitialReqUrl"] = reqUrl
;
101 Session
["SearchString"] = "";
102 Session
["Source"] = "Anywhere";
103 sourceList
.SelectedValue
= "Anywhere";
105 if (Session
["GlobalCheckBox"] == null) {
106 //GlobalSearchCheckBox.Visible = ((string)Session["Source"]).Equals("Anywhere");
107 //By default, NetBeagleSearch checkbox is checked for local access & unchecked for ext. access
108 GlobalSearchCheckBox
.Checked
= isLocalReq()? true:false;
109 Session
["GlobalCheckBox"] = GlobalSearchCheckBox
.Checked
;
114 //Redirected from Tile-Action invocation, restore Results
115 SearchBox
.Text
= (string) Session
["SearchString"];
116 sourceList
.SelectedValue
= (string) Session
["Source"];
118 Output
.Text
= (string) Session
["ResultsOnDisplay"];
119 WebBackEnd remoteObj
= (WebBackEnd
) Session
["RemObj"];
120 if (remoteObj
!= null) {
121 Back
.Enabled
= remoteObj
.canBack(sessId
);
122 Forward
.Enabled
= remoteObj
.canForward(sessId
);
123 GlobalSearchCheckBox
.Visible
= (remoteObj
.NetworkBeagleActive
) && (sourceList
.SelectedValue
.Equals("Anywhere"));
125 if (!isLocalReq() && (((string)TitleLabel
.Text
).IndexOf("host") < 0))
126 TitleLabel
.Text
+= " from host: " + remoteObj
.HostName
;
128 GlobalSearchCheckBox
.Checked
= (bool) Session
["GlobalCheckBox"];
131 else { //HTTP-Get request with query string:
132 //Initial web query initiated via HTTP Get (firefox search bar):
134 string searchString
= null;
135 NameValueCollection nvc
= Request
.QueryString
;
137 if ((nvc
!= null) && (nvc
.Count
!= 0) &&
138 ((searchString
= nvc
["text"]) != null )) {
140 SearchBox
.Text
= searchString
;
141 Session
["SearchString"] = searchString
;
143 string source
= null;
144 if ((source
= nvc
["source"]) != null)
146 switch (source
.ToLower()) {
148 case "files": sourceList
.SelectedValue
= "Files";
150 case "addressbook": sourceList
.SelectedValue
= "Contact";
152 case "mail" : sourceList
.SelectedValue
= "MailMessage";
154 case "web" : sourceList
.SelectedValue
= "WebHistory";
156 case "chats": sourceList
.SelectedValue
= "IMLog";
159 default: sourceList
.SelectedValue
= "Anywhere";
163 sourceList
.SelectedValue
= "Anywhere";
165 Session
["Source"] = sourceList
.SelectedValue
;
166 GlobalSearchCheckBox
.Visible
= ((string)Session
["Source"]).Equals("Anywhere");
167 //By default, NetBeagleSearch checkbox is checked for local access & unchecked for ext. access
168 GlobalSearchCheckBox
.Checked
= isLocalReq()? true:false;
169 Session
["GlobalCheckBox"] = GlobalSearchCheckBox
.Checked
;
171 if (Session
["ResultsOnDisplay"] == null) {
173 int index2
= reqUrl
.IndexOf(".aspx");
174 if ((index2
> 0) && (index2
+ ".aspx".Length
< reqUrl
.Length
))
175 Session
["InitialReqUrl"] = reqUrl
.Substring(0, index2
+ ".aspx".Length
);
177 Session
["InitialReqUrl"] = reqUrl
;
180 queryStringProcessed
= true;
184 //Redirect client to initial Beagle webaccess URL:
185 Response
.Redirect((string)Session
["InitialReqUrl"]);
187 } //end else for if (actionString == null)
188 } //end if (!IsPostBack)
190 //Process Tile!Action HTTP-Get request, if user has clicked on one:
191 if (actionString
!= null && !queryStringProcessed
) {
193 WebBackEnd remoteObj
= (WebBackEnd
) Session
["RemObj"];
195 if (remoteObj
!= null) {
197 remoteObj
.dispatchAction(sessId
, actionString
);
200 Output
.Text
= enableSessionMsg
;
201 Back
.Visible
= Forward
.Visible
= GlobalSearchCheckBox
.Visible
= false;
205 //Redirect client to initial Beagle webaccess URL:
206 Response
.Redirect((string)Session
["InitialReqUrl"]);
210 private bool isLocalReq() {
211 //tells whether request originated from local machine
212 return Request
.Url
.IsLoopback
;
215 private string convertUrls(string buf
)
217 //Replace specific actions in URL's
218 string buf1
= buf
.Replace("href=\"action:", "href=\"" + Session
["InitialReqUrl"] + "?action:");
219 string buf2
= buf1
.Replace("href=\"dynaction:", "href=\"" + Session
["InitialReqUrl"] + "?dynaction:");
223 string initUrl
= (string) Session
["InitialReqUrl"];
224 int i
= initUrl
.LastIndexOf('/');
226 //Get the initial part of url: i.e. http://localhost:8888/beagle
227 string p
= initUrl
.Substring(0, i
);
229 //Check if initial url was http://localhost:8888/search.aspx & add a trailing "/"
230 if (p
.EndsWith("beagle"))
235 string s
, sep
= "\"";
236 string[] list
= buf2
.Split('\"');
237 for (int k
= 0; k
< list
.Length
; k
++) {
241 string s1
= s
.Replace("file://" + ExternalStringsHack
.KdePrefix
, p
+ "kde3");
242 string s2
= s1
.Replace("file://" + ExternalStringsHack
.GnomePrefix
, p
+ "gnome");
243 list
[k
] = s2
.Replace("file://" + ExternalStringsHack
.Prefix
, p
+ "local");
247 return String
.Join (sep
, list
);
250 protected void Search_Click(object o
, EventArgs e
) {
252 //if (IsPostBack && Session.IsNewSession)
253 if (Session
["InitialReqUrl"] == null) {
254 Output
.Text
= enableSessionMsg
;
255 Back
.Visible
= Forward
.Visible
= GlobalSearchCheckBox
.Visible
= false;
259 if (SearchBox
.Text
.Trim() == "") {
260 Output
.Text
= HeaderMsg
+ NO_RESULTS
;
261 Back
.Visible
= Forward
.Visible
= false;
263 Session
["SearchString"] = SearchBox
.Text
;
264 Session
["ResultsOnDisplay"] = Output
.Text
;
265 Session
["GlobalCheckBox"] = GlobalSearchCheckBox
.Checked
;
269 string searchSrc
= sourceList
.SelectedItem
.Value
;
270 if (searchSrc
.Equals("Anywhere"))
273 remoteChannel
.Register();
275 WebBackEnd remoteObj
= (WebBackEnd
) Session
["RemObj"];
276 if (remoteObj
== null) {
277 remoteObj
= new WebBackEnd();
280 if ( (remoteObj
== null) || !(remoteObj
.allowGlobalAccess
|| isLocalReq())) {
282 Output
.Text
= localReqOnlyMsg
;
283 Back
.Visible
= Forward
.Visible
= GlobalSearchCheckBox
.Visible
= false;
284 sourceList
.Enabled
= SearchBox
.Enabled
= Search
.Enabled
= false;
288 if (!isLocalReq() && (((string)TitleLabel
.Text
).IndexOf("host") < 0))
289 TitleLabel
.Text
+= " from host: " + remoteObj
.HostName
;
291 //Show check-box only if we have one or more NetworkedBeagle nodes configured:
292 bool showGlobalCheckBox
= (remoteObj
.NetworkBeagleActive
) && (searchSrc
== null);
294 //Setup arguments for WebBackEnd:doQuery()
295 webArgs wargs
= new webArgs();
296 wargs
.sessId
= Session
.SessionID
;
297 wargs
.searchString
= SearchBox
.Text
;
298 wargs
.searchSource
= searchSrc
;
299 wargs
.isLocalReq
= isLocalReq();
300 wargs
.globalSearch
= showGlobalCheckBox
?GlobalSearchCheckBox
.Checked
:false;
302 string response
= remoteObj
.doQuery(wargs
);
304 if (response
.StartsWith(NO_RESULTS
)) {
305 Output
.Text
= HeaderMsg
+ response
;
306 Back
.Visible
= Forward
.Visible
= false;
309 Output
.Text
= HeaderMsg
+ convertUrls(response
);
310 Back
.Enabled
= remoteObj
.canBack(Session
.SessionID
);
311 Forward
.Enabled
= remoteObj
.canForward(Session
.SessionID
);
314 GlobalSearchCheckBox
.Visible
= showGlobalCheckBox
;
315 Session
["GlobalCheckBox"] = GlobalSearchCheckBox
.Checked
;
316 Session
["RemObj"] = remoteObj
;
317 Session
["ResultsOnDisplay"] = Output
.Text
;
318 Session
["SearchString"] = SearchBox
.Text
;
319 Session
["Source"] = searchSrc
;
322 protected void Back_Click(object o
, EventArgs e
) {
324 WebBackEnd remoteObj
= (WebBackEnd
) Session
["RemObj"];
325 //if (IsPostBack && HttpContext.Current.Session.IsNewSession)
326 if (remoteObj
== null) {
327 Output
.Text
= enableSessionMsg
;
328 Back
.Visible
= Forward
.Visible
= GlobalSearchCheckBox
.Visible
= false;
332 if ( (remoteObj
== null) || !(remoteObj
.allowGlobalAccess
|| isLocalReq())) {
334 Output
.Text
= localReqOnlyMsg
;
335 Back
.Visible
= Forward
.Visible
= GlobalSearchCheckBox
.Visible
= false;
336 sourceList
.Enabled
= SearchBox
.Enabled
= Search
.Enabled
= false;
340 string sessId
= Session
.SessionID
;
342 SearchBox
.Text
= (string) Session
["SearchString"];
343 sourceList
.SelectedValue
= (string) Session
["Source"];
345 //if (remoteObj == null) { Output.Text = NO_RESULTS; return; }
346 string response
= convertUrls(remoteObj
.doBack(sessId
));
347 Session
["ResultsOnDisplay"] = Output
.Text
= HeaderMsg
+ response
;
349 GlobalSearchCheckBox
.Checked
= (bool)Session
["GlobalCheckBox"];
350 Back
.Enabled
= (remoteObj
!= null) && (remoteObj
.canBack(sessId
));
351 Forward
.Enabled
= (remoteObj
!= null) && (remoteObj
.canForward(sessId
));
354 protected void Forward_Click(object o
, EventArgs e
) {
356 WebBackEnd remoteObj
= (WebBackEnd
) Session
["RemObj"];
357 //if (IsPostBack && HttpContext.Current.Session.IsNewSession)
358 if (remoteObj
== null) {
359 Output
.Text
= enableSessionMsg
;
360 Back
.Visible
= Forward
.Visible
= GlobalSearchCheckBox
.Visible
= false;
364 if ( (remoteObj
== null) || !(remoteObj
.allowGlobalAccess
|| isLocalReq())) {
366 Output
.Text
= localReqOnlyMsg
;
367 Back
.Visible
= Forward
.Visible
= GlobalSearchCheckBox
.Visible
= false;
368 sourceList
.Enabled
= SearchBox
.Enabled
= Search
.Enabled
= false;
372 string sessId
= Session
.SessionID
;
373 SearchBox
.Text
= (string) Session
["SearchString"];
374 sourceList
.SelectedValue
= (string) Session
["Source"];
376 //if (remoteObj == null) { Output.Text = NO_RESULTS; return; }
377 string response
= convertUrls(remoteObj
.doForward(sessId
));
378 Session
["ResultsOnDisplay"] = Output
.Text
= HeaderMsg
+ response
;
380 GlobalSearchCheckBox
.Checked
= (bool)Session
["GlobalCheckBox"];
381 Back
.Enabled
= (remoteObj
!= null) && (remoteObj
.canBack(sessId
));
382 Forward
.Enabled
= (remoteObj
!= null) && (remoteObj
.canForward(sessId
));