1 |
tdb |
1.1 |
//---PACKAGE DECLARATION--- |
2 |
|
|
//package uk.org.iscream.cms.server.util; |
3 |
|
|
|
4 |
|
|
//---IMPORTS--- |
5 |
|
|
import uk.org.iscream.cms.server.util.*; |
6 |
|
|
import java.net.ServerSocket; |
7 |
|
|
import java.net.Socket; |
8 |
|
|
import java.net.InetAddress; |
9 |
|
|
import java.io.IOException; |
10 |
|
|
|
11 |
|
|
/** |
12 |
tdb |
1.2 |
* Access Control List ServerSocket wrapper. Used |
13 |
|
|
* to wrap a ServerSocket with an ACL, whilst extending |
14 |
|
|
* ServerSocket to make integration into code easier. |
15 |
|
|
* Once the ACL has been set, accept() can be used exactly |
16 |
|
|
* as it would be with ServerSocket. |
17 |
tdb |
1.1 |
* |
18 |
|
|
* @author $Author$ |
19 |
|
|
* @version $Id$ |
20 |
|
|
*/ |
21 |
|
|
public class ACLServerSocket extends ServerSocket { |
22 |
|
|
|
23 |
|
|
//---FINAL ATTRIBUTES--- |
24 |
|
|
|
25 |
|
|
/** |
26 |
|
|
* The current CVS revision of this class |
27 |
|
|
*/ |
28 |
|
|
public static final String REVISION = "$Revision$"; |
29 |
|
|
|
30 |
|
|
//---STATIC METHODS--- |
31 |
|
|
|
32 |
|
|
//---CONSTRUCTORS--- |
33 |
|
|
|
34 |
tdb |
1.2 |
/** |
35 |
|
|
* See relevant ServerSocket constructor. |
36 |
|
|
*/ |
37 |
tdb |
1.1 |
public ACLServerSocket(int port) throws IOException { |
38 |
|
|
super(port); |
39 |
|
|
setACL(new ACL()); |
40 |
|
|
} |
41 |
|
|
|
42 |
tdb |
1.2 |
/** |
43 |
|
|
* See relevant ServerSocket constructor. |
44 |
|
|
*/ |
45 |
tdb |
1.1 |
public ACLServerSocket(int port, int backlog) throws IOException { |
46 |
|
|
super(port, backlog); |
47 |
|
|
setACL(new ACL()); |
48 |
|
|
} |
49 |
|
|
|
50 |
tdb |
1.2 |
/** |
51 |
|
|
* See relevant ServerSocket constructor. |
52 |
|
|
*/ |
53 |
tdb |
1.1 |
public ACLServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException { |
54 |
|
|
super(port, backlog, bindAddr); |
55 |
|
|
setACL(new ACL()); |
56 |
|
|
} |
57 |
|
|
|
58 |
tdb |
1.2 |
/** |
59 |
|
|
* See relevant ServerSocket constructor, and |
60 |
|
|
* sets the default acl. |
61 |
|
|
*/ |
62 |
tdb |
1.1 |
public ACLServerSocket(ACL acl, int port) throws IOException { |
63 |
|
|
super(port); |
64 |
|
|
setACL(acl); |
65 |
|
|
} |
66 |
|
|
|
67 |
tdb |
1.2 |
/** |
68 |
|
|
* See relevant ServerSocket constructor, and |
69 |
|
|
* sets the default acl. |
70 |
|
|
*/ |
71 |
tdb |
1.1 |
public ACLServerSocket(ACL acl, int port, int backlog) throws IOException { |
72 |
|
|
super(port, backlog); |
73 |
|
|
setACL(acl); |
74 |
|
|
} |
75 |
|
|
|
76 |
tdb |
1.2 |
/** |
77 |
|
|
* See relevant ServerSocket constructor, and |
78 |
|
|
* sets the default acl. |
79 |
|
|
*/ |
80 |
tdb |
1.1 |
public ACLServerSocket(ACL acl, int port, int backlog, InetAddress bindAddr) throws IOException { |
81 |
|
|
super(port, backlog, bindAddr); |
82 |
|
|
setACL(acl); |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
//---PUBLIC METHODS--- |
86 |
|
|
|
87 |
tdb |
1.2 |
/** |
88 |
|
|
* Essentially has the same behaviour as the |
89 |
|
|
* ServerSocket.accept() method, except won't |
90 |
|
|
* return a Socket unless it is permitted by |
91 |
|
|
* the ACL. Closes Socket's immediately that |
92 |
|
|
* aren't permitted by the ACL. |
93 |
|
|
*/ |
94 |
tdb |
1.1 |
public Socket accept() throws IOException { |
95 |
|
|
while(true) { |
96 |
|
|
Socket s = super.accept(); |
97 |
|
|
if(_acl.check(s.getInetAddress())) { |
98 |
|
|
return s; |
99 |
|
|
} |
100 |
|
|
else { |
101 |
|
|
s.close(); |
102 |
|
|
} |
103 |
|
|
} |
104 |
|
|
} |
105 |
|
|
|
106 |
tdb |
1.2 |
/** |
107 |
|
|
* Set the ACL at any point in the operation |
108 |
|
|
* of the ServerSocket. |
109 |
|
|
* |
110 |
|
|
* @param acl the new ACL to apply |
111 |
|
|
*/ |
112 |
tdb |
1.1 |
public void setACL(ACL acl) { |
113 |
|
|
_acl = acl; |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
/** |
117 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
118 |
|
|
* method to provide clean logging (every class should have this). |
119 |
|
|
* |
120 |
|
|
* This uses the uk.org.iscream.cms.server.util.FormatName class |
121 |
|
|
* to format the toString() |
122 |
|
|
* |
123 |
|
|
* @return the name of this class and its CVS revision |
124 |
|
|
*/ |
125 |
|
|
public String toString() { |
126 |
|
|
return FormatName.getName( |
127 |
|
|
_name, |
128 |
|
|
getClass().getName(), |
129 |
|
|
REVISION); |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
//---PRIVATE METHODS--- |
133 |
|
|
|
134 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
135 |
|
|
|
136 |
|
|
//---ATTRIBUTES--- |
137 |
|
|
|
138 |
|
|
/** |
139 |
|
|
* This is the friendly identifier of the |
140 |
|
|
* component this class is running in. |
141 |
|
|
* eg, a Filter may be called "filter1", |
142 |
|
|
* If this class does not have an owning |
143 |
|
|
* component, a name from the configuration |
144 |
|
|
* can be placed here. This name could also |
145 |
|
|
* be changed to null for utility classes. |
146 |
|
|
*/ |
147 |
|
|
private String _name = null; |
148 |
|
|
|
149 |
tdb |
1.2 |
/** |
150 |
|
|
* The ACL used by this ACLServerSocket. |
151 |
|
|
*/ |
152 |
tdb |
1.1 |
private ACL _acl; |
153 |
|
|
|
154 |
|
|
//---STATIC ATTRIBUTES--- |
155 |
|
|
|
156 |
|
|
//---INNER CLASSES--- |
157 |
|
|
|
158 |
|
|
} |