2 // ExternalAccessFilter.cs
4 // Copyright (C) 2005 Novell, Inc.
7 // Vijay K. Nanjundaswamy (knvijay@novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining a
12 // copy of this software and associated documentation files (the "Software"),
13 // to deal in the Software without restriction, including without limitation
14 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 // and/or sell copies of the Software, and to permit persons to whom the
16 // Software is furnished to do so, subject to the following conditions:
18 // The above copyright notice and this permission notice shall be included in
19 // all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 // DEALINGS IN THE SOFTWARE.
32 using System
.Collections
;
37 namespace Beagle
.WebService
{
39 public class ExternalAccessFilter
42 static Logger log
= Logger
.Get ("ExternalAccessFilter");
43 string FileUriPrefix
= "file://";
44 string HttpUriBase
= "http://hostname:8888/beagle/";
45 string[] reserved_suffixes
;
46 static readonly string ConfigFile
= "publicfolders.cfg";
48 // User can specify only a sub-directory under home directory in 'publicfolders.cfg'.
49 // All entries must start with ~/ . One entry per line. The leaf folder name should
50 // be unique. This leaf name will be used for the BeagleXSP application list.
52 public ExternalAccessFilter (string HttpUriBase
, string[] reserved_suffixes
)
54 this.HttpUriBase
= HttpUriBase
;
55 this.reserved_suffixes
= reserved_suffixes
;
57 //First check for ~/.beagle/config/webservices.xml configuration:
58 ArrayList publicFolders
= Conf
.WebServices
.PublicFolders
;
60 if ((publicFolders
!= null) && (publicFolders
.Count
> 0)) {
61 SetupFilters(publicFolders
);
62 if (File
.Exists (Path
.Combine (PathFinder
.StorageDir
, ConfigFile
)))
64 log
.Warn("ExternalAccessFilter: Duplicate configuration of PublicFolders !");
65 log
.Info("ExternalAccessFilter: Remove '~/.beagle/publicfolders.cfg' file.\n Use 'beagle-config' instead to setup public folders.");
66 log
.Info("ExternalAccessFilter: Using ~/.beagle/config/webservvices.xml");
71 publicFolders
= new ArrayList();
73 //Fallback to ~/.beagle/publicfolders.cfg
74 if (File
.Exists (Path
.Combine (PathFinder
.StorageDir
, ConfigFile
))) {
76 StreamReader reader
= new StreamReader(
77 Path
.Combine (PathFinder
.StorageDir
, ConfigFile
));
80 while ( ((entry
= reader
.ReadLine ()) != null) && (entry
.Trim().Length
> 1)) {
82 publicFolders
.Add(entry
);
86 bool fa
= SetupFilters(publicFolders
);
88 // log.Warn("NetBeagleQueryable: 'publicfolders.cfg' based configuration deprecated.\n Use 'beagle-config' or 'beagle-settings' instead to configure Public Folders");
92 public void ReplaceAccessFilter(ArrayList newlist)
94 bool fa = SetupFilters(newlist);
96 if (usingPublicFoldersDotCfgFile && fa) {
97 usingPublicFoldersDotCfgFile = false;
98 log.Warn("ExternalAccessFilter: Duplicate configuration of PublicFolders in '~/.beagle/publicfolders.cfg' and '~/.beagle/config/webservices.xml' !");
99 log.Info("ExternalAccessFilter: Remove '~/.beagle/publicfolders.cfg' file. Use 'beagle-config' instead to setup public folders.");
100 log.Info("ExternalAccessFilter: Replacing PublicFoldersList with new list from \"webservices.xml\"");
106 public ArrayList Matchers
{
108 get { return matchers; }
111 public void Initialize() {
113 foreach (SimpleMatcher sm
in matchers
)
114 if (! sm
.Match
.StartsWith(FileUriPrefix
)) {
115 sm
.Match
= FileUriPrefix
+ sm
.Match
+ "/";
116 sm
.Rewrite
= HttpUriBase
+ sm
.Rewrite
+ "/";
118 Logger
.Log
.Debug("ExternalAccessFilter: Adding Match: " + sm
.Match
+ "," + sm
.Rewrite
);
122 private bool SetupFilters(ArrayList folders
)
124 bool filterAdded
= false;
125 matchers
= new ArrayList();
126 ArrayList suffixes
= new ArrayList();
128 //Populate reserved suffixes
129 suffixes
.AddRange(reserved_suffixes
);
131 //Check if 'public' folder exists and setup default mapping for it:
132 if (Directory
.Exists(PathFinder
.HomeDir
+ "/public"))
134 SimpleMatcher defaultMatcher
= new SimpleMatcher();
136 //file:///home/userid/public/
137 defaultMatcher
.Match
= PathFinder
.HomeDir
+ "/public";
138 //http://hostname:8888/beagle/public/
139 defaultMatcher
.Rewrite
= "public";
141 matchers
.Add(defaultMatcher
);
142 suffixes
.Add("public");
145 //string[] folders = entry.Split (',');
146 foreach (string d
in folders
) {
148 //Each entry must start with ~/
149 if ((d
.Trim().Length
> 1) &&
150 (d
.StartsWith("~/") || (d
.StartsWith(PathFinder
.HomeDir
+ "/"))) ) {
152 if (d
.StartsWith("~/"))
153 d2
= d
.Replace("~/", PathFinder
.HomeDir
+ "/");
157 if (!Directory
.Exists(d2
))
160 string[] comp
= d2
.Split('/');
163 for (int li
= comp
.Length
; li
> 0; --li
) {
164 if ((leaf
= comp
[li
- 1].Trim()).Length
> 0) {
165 //Check the leaf component is unique
166 if (suffixes
.Contains(leaf
))
168 Logger
.Log
.Warn("ExternalAccessFilter: Ignoring entry {0}. Reason: Entry suffix not unique", d
);
175 SimpleMatcher matcher
= new SimpleMatcher ();
178 matcher
.Rewrite
= leaf
;
179 matchers
.Add (matcher
);
188 //Returns: false, if Hit does not match any filter
189 // true, if Hit URI is part of any specified filter
190 public bool FilterHit (Hit hit
)
192 if ((hit
== null) || (matchers
.Count
== 0))
195 string uri
= hit
.Uri
.ToString();
196 foreach (SimpleMatcher matcher
in matchers
)
198 if (uri
.IndexOf (matcher
.Match
) == -1)
207 //Returns: null, if Hit does not match any filter
208 // Uri, after Translation
209 public string TranslateHit (Hit hit
)
211 if ((hit
== null) || (matchers
.Count
== 0))
214 string uri
= hit
.Uri
.ToString();
215 string newuri
= null;
217 foreach (SimpleMatcher matcher
in matchers
)
219 if (uri
.IndexOf (matcher
.Match
) == -1)
222 newuri
= uri
.Replace (matcher
.Match
, matcher
.Rewrite
);
223 //Console.WriteLine("TranslateHit: " + newuri);
228 return null; //Hit does not match any specified filter
232 public class SimpleMatcher
235 public string Rewrite
;