New test.
[mono.git] / mcs / class / System / System.Net / WebPermissionAttribute.cs
1 //
2 // System.Net.WebPermissionAttribute.cs
3 //
4 // Authors:
5 //      Andreas Nahr (ClassDevelopment@A-SoftTech.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2003 Andreas Nahr
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Security;
32 using System.Security.Permissions;
33
34 namespace System.Net {
35
36         [AttributeUsage (AttributeTargets.Assembly | AttributeTargets.Class 
37                  | AttributeTargets.Struct | AttributeTargets.Constructor 
38                  | AttributeTargets.Method, AllowMultiple = true, Inherited = false)]   
39         [Serializable]
40         public sealed class WebPermissionAttribute : CodeAccessSecurityAttribute {
41
42                 // Fields
43                 object m_accept;
44                 object m_connect;
45
46                 // Constructors
47                 public WebPermissionAttribute (SecurityAction action)
48                         : base (action)
49                 {
50                 }
51
52                 // Properties
53
54                 public string Accept {
55                         get {
56 #if NET_2_0
57                                 if (m_accept == null)
58                                         return null;
59 #endif
60                                 return (m_accept as WebPermissionInfo).Info; 
61                         }
62                         set { 
63                                 if (m_accept != null)
64                                         AlreadySet ("Accept", "Accept");
65 #if NET_2_0
66                                 m_accept = new WebPermissionInfo (WebPermissionInfoType.InfoString, value);
67 #else
68                                 if (value == null)
69                                         m_accept = null;
70                                 else
71                                         m_accept = new WebPermissionInfo (WebPermissionInfoType.InfoString, value);
72 #endif
73                         }
74                 }
75
76                 public string AcceptPattern {
77                         get {
78                                 if (m_accept == null)
79                                         return null;
80                                 return (m_accept as WebPermissionInfo).Info; 
81                         }
82                         set { 
83                                 if (m_accept != null)
84                                         AlreadySet ("Accept", "AcceptPattern");
85                                 if (value == null) 
86                                         throw new ArgumentNullException ("AcceptPattern");
87
88                                 m_accept = new WebPermissionInfo (WebPermissionInfoType.InfoUnexecutedRegex , value); 
89                         }
90                 }
91
92                 public string Connect {
93                         get {
94 #if NET_2_0
95                                 if (m_connect == null)
96                                         return null;
97 #endif
98                                 return (m_connect as WebPermissionInfo).Info; 
99                         }
100                         set { 
101                                 if (m_connect != null)
102                                         AlreadySet ("Connect", "Connect");
103 #if NET_2_0
104                                 m_connect = new WebPermissionInfo (WebPermissionInfoType.InfoString, value);
105 #else
106                                 if (value == null)
107                                         m_connect = null;
108                                 else
109                                         m_connect = new WebPermissionInfo (WebPermissionInfoType.InfoString, value);
110 #endif
111                         }
112                 }
113
114                 public string ConnectPattern {
115                         get {
116                                 if (m_connect == null)
117                                         return null;
118                                 return (m_connect as WebPermissionInfo).Info; 
119                         }
120                         set { 
121                                 if (m_connect != null)
122                                         AlreadySet ("Connect", "ConnectConnectPattern");
123                                 if (value == null) 
124                                         throw new ArgumentNullException ("ConnectPattern");
125
126                                 m_connect = new WebPermissionInfo (WebPermissionInfoType.InfoUnexecutedRegex , value);
127                         }
128                 }
129
130                 // Methods
131
132                 public override IPermission CreatePermission () 
133                 {
134                         if (this.Unrestricted)
135                                 return new WebPermission (PermissionState.Unrestricted);
136
137                         WebPermission newPermission = new WebPermission ();
138                         if (m_accept != null) {
139                                 newPermission.AddPermission (NetworkAccess.Accept, (WebPermissionInfo) m_accept);
140                         }
141                         if (m_connect != null) {
142                                 newPermission.AddPermission (NetworkAccess.Connect, (WebPermissionInfo) m_connect);
143                         }
144                         return newPermission;
145                 }
146
147                 // helpers
148
149                 internal void AlreadySet (string parameter, string property)
150                 {
151                         string msg = Locale.GetText ("The parameter '{0}' can be set only once.");
152                         throw new ArgumentException (String.Format (msg, parameter), property);
153                 }
154         }
155