Debian Codegraph @

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

raw
1 package org.astrogrid.samp.web;
2
3 import java.util.Map;
4 import org.astrogrid.samp.Subscriptions;
5 import org.astrogrid.samp.hub.MessageRestriction;
6
7 /**
8 * General purpose implementation of MessageRestriction.
9 * It allows to either whitelist or blacklist a given list of MType
10 * patterns, with the option for client subscriptions to override
11 * this policy by setting the "x-samp.mostly-harmless" key in the
12 * annotation map corresponding to a given MType subscription.
13 *
14 * @author Mark Taylor
15 * @since 23 Nov 2011
16 */
17 public class ListMessageRestriction implements MessageRestriction {
18
19 private final boolean allow_;
20 private final boolean useSubsInfo_;
21 private final Subscriptions subs_;
22
23 /**
24 * Default list of MType patterns returned by {@link #getSafeMTypes}.
25 */
26 public static String[] DEFAULT_SAFE_MTYPES = new String[] {
27 "samp.app.*", "samp.msg.progress",
28 "table.*", "image.*", "coord.*", "spectrum.*",
29 "bibcode.*", "voresource.*", "coverage.*",
30 };
31
32 /**
33 * System property used to specify a default list of known safe MTypes,
34 * which the {@link #DEFAULT} policy will permit.
35 * The value is a comma-separated list of MType patterns.
36 */
37 public static final String SAFE_MTYPE_PROP = "jsamp.mtypes.safe";
38
39 /**
40 * Default MessageRestriction implementation.
41 * The current implementation allows a list of MTypes believed to be safe,
42 * as given by calling {@link #getSafeMTypes}, and blocks all others.
43 * However, client subscriptions may override this by annotating their
44 * subscriptions with an entry having the key
45 * "<code>x-samp.mostly-harmless</code>".
46 * If this has the value "1" the MType thus annotated is allowed,
47 * and if it has the value "0" it is blocked, regardless of the safe list.
48 */
49 public static final MessageRestriction DEFAULT =
50 new ListMessageRestriction( true, getSafeMTypes(), true );
51
52 /**
53 * MessageRestriction that permits all MTypes, except as overridden
54 * by <code>x-samp.mostly-harmless</code> annotations.
55 */
56 public static final MessageRestriction ALLOW_ALL =
57 new ListMessageRestriction( false, new String[ 0 ], true ) {
58 public String toString() {
59 return "ALLOW_ALL";
60 }
61 };
62
63 /**
64 * MessageRestriction that blocks all MTypes, except as overridden
65 * by <code>x-samp.mostly-harmless</code> annotations.
66 */
67 public static final MessageRestriction DENY_ALL =
68 new ListMessageRestriction( true, new String[ 0 ], true ) {
69 public String toString() {
70 return "DENY_ALL";
71 }
72 };
73
74 /**
75 * Constructor.
76 *
77 * @param allow whether the sense of the mtypes list is those
78 * that should be allowed (true) or blocked (false)
79 * @param mtypes mtype patterns to be allowed or blocked
80 * @param useSubsInfo if true, honour x-samp.mostly-harmless
81 * subscription annotations
82 */
83 public ListMessageRestriction( boolean allow, String[] mtypes,
84 boolean useSubsInfo ) {
85 allow_ = allow;
86 useSubsInfo_ = useSubsInfo;
87 subs_ = new Subscriptions();
88 for ( int im = 0; im < mtypes.length; im++ ) {
89 subs_.addMType( mtypes[ im ] );
90 }
91 }
92
93 public boolean permitSend( String mtype, Map subsInfo ) {
94 if ( useSubsInfo_ ) {
95 Object markedHarmless = subsInfo.get( "samp.mostly-harmless" );
96 if ( markedHarmless == null ) {
97 markedHarmless = subsInfo.get( "x-samp.mostly-harmless" );
98 }
99 if ( "0".equals( markedHarmless ) ) {
100 return false;
101 }
102 else if ( "1".equals( markedHarmless ) ) {
103 return true;
104 }
105 }
106 boolean knownSafe = ( ! allow_ ) ^ subs_.isSubscribed( mtype );
107 return knownSafe;
108 }
109
110 public String toString() {
111 StringBuffer sbuf = new StringBuffer()
112 .append( allow_ ? "Allow" : "Deny" )
113 .append( ' ' )
114 .append( subs_.keySet() );
115 if ( useSubsInfo_ ) {
116 sbuf.append( "; " )
117 .append( "honour (x-)samp.mostly-harmless" );
118 }
119 return sbuf.toString();
120 }
121
122 /**
123 * Returns a list of MType patterns which are permitted by the DEFAULT
124 * policy. If the System Property {@value SAFE_MTYPE_PROP} exists,
125 * its value is taken as a comma-separated list of known permitted MType
126 * patterns. Otherwise, the {@link #DEFAULT_SAFE_MTYPES} array is returned.
127 *
128 * @return list of MTypes treated as harmless by default
129 */
130 public static String[] getSafeMTypes() {
131 String safeMtypes = System.getProperty( SAFE_MTYPE_PROP );
132 if ( safeMtypes == null ) {
133 return DEFAULT_SAFE_MTYPES;
134 }
135 else {
136 return safeMtypes.split( "," );
137 }
138 }
139 }