Debian Codegraph @

src/java/org/astrogrid/samp/web/ClientAuthorizers.java

raw
1 package org.astrogrid.samp.web;
2
3 import java.util.Map;
4 import java.util.logging.Level;
5 import java.util.logging.Logger;
6 import org.astrogrid.samp.Metadata;
7 import org.astrogrid.samp.client.SampException;
8 import org.astrogrid.samp.httpd.HttpServer;
9
10 /**
11 * Utility class containing ClientAuthorizer implementations.
12 *
13 * @author Mark Taylor
14 * @since 2 Feb 2011
15 */
16 public class ClientAuthorizers {
17
18 /**
19 * Authorizer which always denies access,
20 * with INFO logging either way.
21 */
22 public static final ClientAuthorizer FALSE =
23 createLoggingClientAuthorizer( createFixedClientAuthorizer( false ),
24 Level.INFO, Level.INFO );
25
26 /**
27 * Authorizer which always permits access,
28 * with WARNING logging either way.
29 */
30 public static final ClientAuthorizer TRUE =
31 createLoggingClientAuthorizer( createFixedClientAuthorizer( true ),
32 Level.WARNING, Level.WARNING );
33
34 /**
35 * Authorizer which queries the user via a popup dialogue,
36 * with INFO logging either way.
37 */
38 private static ClientAuthorizer swingAuth_;
39
40 private static final Logger logger_ =
41 Logger.getLogger( ClientAuthorizers.class.getName() );
42
43 private ClientAuthorizers() {
44 }
45
46 /**
47 * Returns a new authorizer instance which always produces the same
48 * authorization status.
49 *
50 * @param policy true for accept, false for deny
51 * @return new authorizer
52 */
53 public static ClientAuthorizer
54 createFixedClientAuthorizer( final boolean policy ) {
55 if ( policy ) {
56 return new ClientAuthorizer() {
57 public void authorize( HttpServer.Request request,
58 Map securityMap ) {
59 }
60 };
61 }
62 else {
63 return new ClientAuthorizer() {
64 public void authorize( HttpServer.Request request,
65 Map securityMap )
66 throws SampException {
67 throw new SampException( "All registration requests "
68 + "unconditionally denied" );
69 }
70 };
71 }
72 }
73
74 /**
75 * Returns a new authorizer instance based on an existing one which
76 * logs authorization results through the logging system.
77 *
78 * @param auth base authorizer
79 * @param acceptLevel logging level at which auth acceptances are logged
80 * @param refuseLevel logging level at which auth refusals are logged
81 * @return new authorizer
82 */
83 public static ClientAuthorizer
84 createLoggingClientAuthorizer( final ClientAuthorizer auth,
85 final Level acceptLevel,
86 final Level refuseLevel ) {
87 return new ClientAuthorizer() {
88 public synchronized void authorize( HttpServer.Request request,
89 Map securityMap )
90 throws SampException {
91 boolean isAuth;
92 try {
93 auth.authorize( request, securityMap );
94 log( true, securityMap, null );
95 }
96 catch ( SampException e ) {
97 log( false, securityMap, e );
98 throw e;
99 }
100 }
101 private void log( boolean accept, Map securityMap,
102 SampException err ) {
103 String clTxt = securityMap.containsKey( Metadata.NAME_KEY )
104 ? securityMap.get( Metadata.NAME_KEY ).toString()
105 : securityMap.toString();
106 if ( accept ) {
107 logger_.log( acceptLevel,
108 "Accepted registration for client " + clTxt );
109 }
110 else {
111 logger_.log( refuseLevel,
112 "Rejected registration for client " + clTxt
113 + "(" + err.getMessage() + ")" );
114 }
115 }
116 };
117 }
118
119 /**
120 * Returns the mandatory application name entry from the security map
121 * supplied explicitly by clients wishing to register.
122 * The relevant key is "samp.name" (Metadata.NAME_KEY).
123 * If it's not present and correct, a SampException is thrown
124 * indicating that registration is rejected.
125 *
126 * @param securityMap map supplied by client
127 * @return value of samp.name key, not null
128 * @throws SampException if name not present
129 */
130 public static String getAppName( Map securityMap ) throws SampException {
131 String nameKey = Metadata.NAME_KEY;
132 assert "samp.name".equals( nameKey );
133 Object appNameObj = securityMap.get( nameKey );
134 final String appName;
135 if ( appNameObj instanceof String ) {
136 return (String) appNameObj;
137 }
138 else if ( appNameObj == null ) {
139 throw new SampException( "No " + nameKey
140 + " entry in securityInfo map" );
141 }
142 else {
143 throw new SampException( "Wrong data type (not string) for "
144 + nameKey + " entry in securityInfo map" );
145 }
146 }
147 }