Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / WebServices / ExternalAccessFilter.cs
blob9b41641d1fe1a5e273915406311d08eb33b6f67f
1 //
2 // ExternalAccessFilter.cs
3 //
4 // Copyright (C) 2005 Novell, Inc.
5 //
6 // Authors:
7 // Vijay K. Nanjundaswamy (knvijay@novell.com)
8 //
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.
30 using System;
31 using System.IO;
32 using System.Collections;
34 using Beagle;
35 using Beagle.Util;
37 namespace Beagle.WebService {
39 public class ExternalAccessFilter
41 ArrayList matchers;
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");
68 return;
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));
79 string entry;
80 while ( ((entry = reader.ReadLine ()) != null) && (entry.Trim().Length > 1)) {
81 if (entry[0] != '#')
82 publicFolders.Add(entry);
86 bool fa = SetupFilters(publicFolders);
87 //if (fa)
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\"");
103 //Initialize();
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 + "/"))) ) {
151 string d2;
152 if (d.StartsWith("~/"))
153 d2 = d.Replace("~/", PathFinder.HomeDir + "/");
154 else
155 d2 = d;
157 if (!Directory.Exists(d2))
158 continue;
160 string[] comp = d2.Split('/');
161 string leaf;
162 if (comp.Length > 1)
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);
169 break;
171 else
172 suffixes.Add(leaf);
174 filterAdded = true;
175 SimpleMatcher matcher = new SimpleMatcher ();
177 matcher.Match = d2;
178 matcher.Rewrite = leaf;
179 matchers.Add (matcher);
180 break;
183 } //end if
184 } //end foreach
185 return filterAdded;
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))
193 return false;
195 string uri = hit.Uri.ToString();
196 foreach (SimpleMatcher matcher in matchers)
198 if (uri.IndexOf (matcher.Match) == -1)
199 continue;
201 return true;
204 return false;
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))
212 return null;
214 string uri = hit.Uri.ToString();
215 string newuri = null;
217 foreach (SimpleMatcher matcher in matchers)
219 if (uri.IndexOf (matcher.Match) == -1)
220 continue;
222 newuri = uri.Replace (matcher.Match, matcher.Rewrite);
223 //Console.WriteLine("TranslateHit: " + newuri);
225 return newuri;
228 return null; //Hit does not match any specified filter
232 public class SimpleMatcher
234 public string Match;
235 public string Rewrite;