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 // Exported Folders: The leaf folder name should be unique.
49 // This leaf name will be used for the BeagleXSP application list.
51 public ExternalAccessFilter (string HttpUriBase
, string[] reserved_suffixes
)
53 this.HttpUriBase
= HttpUriBase
;
54 this.reserved_suffixes
= reserved_suffixes
;
56 //Check for ~/.beagle/config/webservices.xml configuration:
57 ArrayList publicFolders
= Conf
.WebServices
.PublicFolders
;
59 if ((publicFolders
!= null) && (publicFolders
.Count
> 0)) {
61 SetupFilters(publicFolders
);
63 if (File
.Exists (Path
.Combine (PathFinder
.StorageDir
, ConfigFile
)))
65 log
.Warn("ExternalAccessFilter: Detected deprecated configuration file for PublicFolders !");
66 log
.Info("ExternalAccessFilter: Remove '~/.beagle/publicfolders.cfg' file.\n Use 'beagle-settings' or 'beagle-config' instead to setup public folders.");
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 SetupFilters(publicFolders
);
90 public void ReplaceAccessFilter(ArrayList newlist)
92 bool fa = SetupFilters(newlist);
94 if (usingPublicFoldersDotCfgFile && fa) {
95 usingPublicFoldersDotCfgFile = false;
96 log.Warn("ExternalAccessFilter: Duplicate configuration of PublicFolders in '~/.beagle/publicfolders.cfg' and '~/.beagle/config/webservices.xml' !");
97 log.Info("ExternalAccessFilter: Remove '~/.beagle/publicfolders.cfg' file. Use 'beagle-config' instead to setup public folders.");
98 log.Info("ExternalAccessFilter: Replacing PublicFoldersList with new list from \"webservices.xml\"");
104 public ArrayList Matchers
{
106 get { return matchers; }
109 public void Initialize() {
111 foreach (SimpleMatcher sm
in matchers
)
112 if (! sm
.Match
.StartsWith(FileUriPrefix
)) {
113 sm
.Match
= FileUriPrefix
+ sm
.Match
+ "/";
114 sm
.Rewrite
= HttpUriBase
+ sm
.Rewrite
+ "/";
116 Logger
.Log
.Debug("ExternalAccessFilter: Adding Match: " + sm
.Match
+ "," + sm
.Rewrite
);
120 private bool SetupFilters(ArrayList folders
)
122 bool filterAdded
= false;
123 matchers
= new ArrayList();
124 ArrayList suffixes
= new ArrayList();
126 //Populate reserved suffixes
127 suffixes
.AddRange(reserved_suffixes
);
129 //Check if 'public' folder exists and setup default mapping for it:
130 if (Directory
.Exists(PathFinder
.HomeDir
+ "/public"))
132 SimpleMatcher defaultMatcher
= new SimpleMatcher();
134 //file:///home/userid/public/
135 defaultMatcher
.Match
= PathFinder
.HomeDir
+ "/public";
136 //http://hostname:8888/beagle/public/
137 defaultMatcher
.Rewrite
= "public";
139 matchers
.Add(defaultMatcher
);
140 suffixes
.Add("public");
143 //string[] folders = entry.Split (',');
144 foreach (string d
in folders
) {
146 //Each entry must start with ~/
147 if ((d
.Trim().Length
> 1) &&
148 (d
.StartsWith("~/") || (d
.StartsWith(PathFinder
.HomeDir
+ "/"))) ) {
150 if (d
.StartsWith("~/"))
151 d2
= d
.Replace("~/", PathFinder
.HomeDir
+ "/");
155 if (!Directory
.Exists(d2
))
158 string[] comp
= d2
.Split('/');
161 for (int li
= comp
.Length
; li
> 0; --li
) {
162 if ((leaf
= comp
[li
- 1].Trim()).Length
> 0) {
163 //Check the leaf component is unique
164 if (suffixes
.Contains(leaf
))
166 Logger
.Log
.Warn("ExternalAccessFilter: Ignoring entry {0}. Reason: Entry suffix not unique", d
);
173 SimpleMatcher matcher
= new SimpleMatcher ();
176 matcher
.Rewrite
= leaf
;
177 matchers
.Add (matcher
);
186 //Returns: false, if Hit does not match any filter
187 // true, if Hit URI is part of any specified filter
188 public bool FilterHit (Hit hit
)
190 if ((hit
== null) || (matchers
.Count
== 0))
193 string uri
= hit
.UriAsString
;
194 foreach (SimpleMatcher matcher
in matchers
)
196 if (uri
.IndexOf (matcher
.Match
) == -1)
199 return hit
.IsFile
; //return true;
205 //Returns: null, if Hit does not match any filter
206 // Uri, after Translation
207 public string TranslateHit (Hit hit
)
209 if ((hit
== null) || (matchers
.Count
== 0))
212 string uri
= hit
.UriAsString
;
213 string newuri
= null;
215 foreach (SimpleMatcher matcher
in matchers
)
217 if (uri
.IndexOf (matcher
.Match
) == -1)
220 newuri
= uri
.Replace (matcher
.Match
, matcher
.Rewrite
);
221 //Console.WriteLine("TranslateHit: " + newuri);
226 return null; //Hit does not match any specified filter
230 public class SimpleMatcher
233 public string Rewrite
;