Debian Codegraph @

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

raw
1 package org.astrogrid.samp.web;
2
3 import java.util.HashSet;
4 import java.util.Set;
5 import java.util.logging.Level;
6 import java.util.logging.Logger;
7
8 /**
9 * Utility class containing OriginAuthorizer implementations.
10 *
11 * @author Mark Taylor
12 * @since 2 Feb 2011
13 */
14 public class OriginAuthorizers {
15
16 /** OriginAuthorizer which always denies access. */
17 public static final OriginAuthorizer FALSE =
18 createFixedOriginAuthorizer( false, false );
19
20 /** OriginAuthorizer which always permits access. */
21 public static final OriginAuthorizer TRUE =
22 createFixedOriginAuthorizer( true, true );
23
24 /** OriginAuthorizer which queries the user via a popup dialogue. */
25 public static final OriginAuthorizer SWING =
26 createMemoryOriginAuthorizer(
27 createLoggingOriginAuthorizer( new SwingOriginAuthorizer( null ),
28 Level.INFO, Level.WARNING ) );
29 private static final Logger logger_ =
30 Logger.getLogger( OriginAuthorizers.class.getName() );
31
32 /**
33 * Private constructor prevents instantiation.
34 */
35 private OriginAuthorizers() {
36 }
37
38 /**
39 * Returns an OriginAuthorizer with fixed responses, regardless of input.
40 *
41 * @param individualPolicy invariable response of
42 * <code>authorize</code> method
43 * @param generalPolicy invariable response of
44 * <code>authorizeAll</code> method
45 */
46 public static OriginAuthorizer
47 createFixedOriginAuthorizer( final boolean individualPolicy,
48 final boolean generalPolicy ) {
49 return new OriginAuthorizer() {
50 public boolean authorize( String origin ) {
51 return individualPolicy;
52 }
53 public boolean authorizeAll() {
54 return generalPolicy;
55 }
56 };
57 }
58
59 /**
60 * Returns an OriginAuthorizer based on an existing one which logs
61 * responses.
62 *
63 * @param auth base authorizer
64 * @param acceptLevel level at which acceptances will be logged
65 * @param refuseLevel level at which refusals will be logged
66 */
67 public static OriginAuthorizer
68 createLoggingOriginAuthorizer( final OriginAuthorizer auth,
69 final Level acceptLevel,
70 final Level refuseLevel ) {
71 return new OriginAuthorizer() {
72 public synchronized boolean authorize( String origin ) {
73 boolean accept = auth.authorize( origin );
74 log( accept, "\"" + origin + "\"" );
75 return accept;
76 }
77 public synchronized boolean authorizeAll() {
78 boolean accept = auth.authorizeAll();
79 log( accept, "all origins" );
80 return accept;
81 }
82 private void log( boolean accept, String domain ) {
83 if ( accept ) {
84 logger_.log( acceptLevel,
85 "Accepted cross-origin requests for "
86 + domain );
87 }
88 else {
89 logger_.log( refuseLevel,
90 "Rejected cross-origin requests for "
91 + domain );
92 }
93 }
94 };
95 }
96
97 /**
98 * Returns an OriginAuthorizer based on an existing one which caches
99 * responses.
100 *
101 * @param auth base authorizer
102 */
103 public static OriginAuthorizer
104 createMemoryOriginAuthorizer( final OriginAuthorizer auth ) {
105 return new OriginAuthorizer() {
106 private final OriginAuthorizer baseAuth_ = auth;
107 private final Set acceptedSet_ = new HashSet();
108 private final Set refusedSet_ = new HashSet();
109 private Boolean authorizeAll_;
110
111 public synchronized boolean authorize( String origin ) {
112 if ( refusedSet_.contains( origin ) ) {
113 return false;
114 }
115 else if ( acceptedSet_.contains( origin ) ) {
116 return true;
117 }
118 else {
119 boolean accepted = baseAuth_.authorize( origin );
120 ( accepted ? acceptedSet_ : refusedSet_ ).add( origin );
121 return accepted;
122 }
123 }
124
125 public synchronized boolean authorizeAll() {
126 if ( authorizeAll_ == null ) {
127 authorizeAll_ = Boolean.valueOf( baseAuth_.authorizeAll() );
128 }
129 return authorizeAll_.booleanValue();
130 }
131 };
132 }
133 }