Debian Codegraph @

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

raw
1 package org.astrogrid.samp.web;
2
3 import java.io.IOException;
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.logging.Level;
7 import org.astrogrid.samp.hub.HubProfile;
8 import org.astrogrid.samp.hub.HubProfileFactory;
9 import org.astrogrid.samp.hub.KeyGenerator;
10 import org.astrogrid.samp.hub.MessageRestriction;
11 import org.astrogrid.samp.xmlrpc.internal.InternalServer;
12
13 /**
14 * HubProfileFactory implementation for Web Profile.
15 *
16 * @author Mark Taylor
17 * @since 2 Feb 2011
18 */
19 public class WebHubProfileFactory implements HubProfileFactory {
20
21 private static final String logUsage_ = "[-web:log none|http|xml|rpc]";
22 private static final String authUsage_ =
23 "[-web:auth swing|true|false|extreme]";
24 private static final String corsUsage_ = "[-web:[no]cors]";
25 private static final String flashUsage_ = "[-web:[no]flash]";
26 private static final String silverlightUsage_ = "[-web:[no]silverlight]";
27 private static final String urlcontrolUsage_ = "[-web:[no]urlcontrol]";
28 private static final String restrictMtypeUsage_ =
29 "[-web:[no]restrictmtypes]";
30
31 /**
32 * Returns "web".
33 */
34 public String getName() {
35 return "web";
36 }
37
38 public String[] getFlagsUsage() {
39 return new String[] {
40 logUsage_,
41 authUsage_,
42 corsUsage_,
43 flashUsage_,
44 silverlightUsage_,
45 urlcontrolUsage_,
46 restrictMtypeUsage_,
47 };
48 }
49
50 public HubProfile createHubProfile( List flagList ) throws IOException {
51
52 // Process flags.
53 String logType = "none";
54 String authType = "swing";
55 boolean useCors = true;
56 boolean useFlash = true;
57 boolean useSilverlight = false;
58 boolean urlControl = true;
59 boolean restrictMtypes = true;
60 for ( Iterator it = flagList.iterator(); it.hasNext(); ) {
61 String arg = (String) it.next();
62 if ( arg.equals( "-web:log" ) ) {
63 it.remove();
64 if ( it.hasNext() ) {
65 logType = (String) it.next();
66 it.remove();
67 }
68 else {
69 throw new IllegalArgumentException( "Usage: " + logUsage_ );
70 }
71 }
72 else if ( arg.equals( "-web:auth" ) ) {
73 it.remove();
74 if ( it.hasNext() ) {
75 authType = (String) it.next();
76 it.remove();
77 }
78 else {
79 throw new IllegalArgumentException( "Usage: "
80 + authUsage_ );
81 }
82 }
83 else if ( arg.equals( "-web:cors" ) ) {
84 it.remove();
85 useCors = true;
86 }
87 else if ( arg.equals( "-web:nocors" ) ) {
88 it.remove();
89 useCors = false;
90 }
91 else if ( arg.equals( "-web:flash" ) ) {
92 it.remove();
93 useFlash = true;
94 }
95 else if ( arg.equals( "-web:noflash" ) ) {
96 it.remove();
97 useFlash = false;
98 }
99 else if ( arg.equals( "-web:silverlight" ) ) {
100 it.remove();
101 useSilverlight = true;
102 }
103 else if ( arg.equals( "-web:nosilverlight" ) ) {
104 it.remove();
105 useSilverlight = false;
106 }
107 else if ( arg.equals( "-web:urlcontrol" ) ) {
108 it.remove();
109 urlControl = true;
110 }
111 else if ( arg.equals( "-web:nourlcontrol" ) ) {
112 it.remove();
113 urlControl = false;
114 }
115 else if ( arg.equals( "-web:restrictmtypes" ) ) {
116 it.remove();
117 restrictMtypes = true;
118 }
119 else if ( arg.equals( "-web:norestrictmtypes" ) ) {
120 it.remove();
121 restrictMtypes = false;
122 }
123 }
124
125 // Prepare HTTP server.
126 WebHubProfile.ServerFactory sfact = new WebHubProfile.ServerFactory();
127 try {
128 sfact.setLogType( logType );
129 }
130 catch ( IllegalArgumentException e ) {
131 throw (IllegalArgumentException)
132 new IllegalArgumentException( "Unknown log type " + logType
133 + "; Usage: " + logUsage_ )
134 .initCause( e );
135 }
136 sfact.setOriginAuthorizer( useCors ? OriginAuthorizers.TRUE
137 : OriginAuthorizers.FALSE );
138 sfact.setAllowFlash( useFlash );
139 sfact.setAllowSilverlight( useSilverlight );
140
141 // Prepare client authorizer.
142 final ClientAuthorizer clientAuth;
143 if ( "swing".equalsIgnoreCase( authType ) ) {
144 clientAuth =
145 ClientAuthorizers
146 .createLoggingClientAuthorizer(
147 new HubSwingClientAuthorizer( null,
148 WebCredentialPresenter
149 .INSTANCE ),
150 Level.INFO, Level.INFO );
151 }
152 else if ( "extreme".equalsIgnoreCase( authType ) ) {
153 clientAuth = ClientAuthorizers
154 .createLoggingClientAuthorizer(
155 new ExtremeSwingClientAuthorizer( null ),
156 Level.WARNING, Level.INFO );
157 }
158 else if ( "true".equalsIgnoreCase( authType ) ) {
159 clientAuth = ClientAuthorizers.TRUE;
160 }
161 else if ( "false".equalsIgnoreCase( authType ) ) {
162 clientAuth = ClientAuthorizers.FALSE;
163 }
164 else {
165 throw new IllegalArgumentException( "Unknown authorizer type "
166 + authType + "; Usage: "
167 + authUsage_ );
168 }
169
170 // Prepare subscriptions mask.
171 MessageRestriction mrestrict = restrictMtypes
172 ? ListMessageRestriction.DEFAULT
173 : null;
174
175 // Construct and return an appropriately configured hub profile.
176 return new WebHubProfile( sfact, clientAuth, mrestrict,
177 WebHubProfile.createKeyGenerator(),
178 urlControl );
179 }
180
181 public Class getHubProfileClass() {
182 return WebHubProfile.class;
183 }
184 }