| 1 |
package org.astrogrid.samp.web; |
| 2 |
|
| 3 |
import java.awt.Color; |
| 4 |
import java.awt.Component; |
| 5 |
import java.awt.Font; |
| 6 |
import java.awt.GraphicsEnvironment; |
| 7 |
import java.awt.HeadlessException; |
| 8 |
import java.net.URL; |
| 9 |
import java.util.Map; |
| 10 |
import javax.swing.BorderFactory; |
| 11 |
import javax.swing.Box; |
| 12 |
import javax.swing.ImageIcon; |
| 13 |
import javax.swing.JComponent; |
| 14 |
import javax.swing.JLabel; |
| 15 |
import javax.swing.JOptionPane; |
| 16 |
import javax.swing.border.Border; |
| 17 |
import org.astrogrid.samp.Client; |
| 18 |
import org.astrogrid.samp.client.SampException; |
| 19 |
import org.astrogrid.samp.gui.IconStore; |
| 20 |
import org.astrogrid.samp.httpd.HttpServer; |
| 21 |
|
| 22 |
/** |
| 23 |
* Client authorizer implementation that does its very best to discourage |
| 24 |
* users from accepting regitrations. |
| 25 |
* |
| 26 |
* @author Mark Taylor |
| 27 |
* @since 29 Sep 2011 |
| 28 |
*/ |
| 29 |
public class ExtremeSwingClientAuthorizer implements ClientAuthorizer { |
| 30 |
|
| 31 |
private final Component parent_; |
| 32 |
|
| 33 |
/** |
| 34 |
* Constructor. |
| 35 |
* |
| 36 |
* @param parent parent component, may be null |
| 37 |
*/ |
| 38 |
public ExtremeSwingClientAuthorizer( Component parent ) { |
| 39 |
parent_ = parent; |
| 40 |
if ( GraphicsEnvironment.isHeadless() ) { |
| 41 |
throw new HeadlessException( "No graphics - lucky escape" ); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
public void authorize( HttpServer.Request request, Map securityMap ) |
| 46 |
throws SampException { |
| 47 |
JComponent panel = Box.createVerticalBox(); |
| 48 |
JComponent linePic = Box.createHorizontalBox(); |
| 49 |
URL imageUrl = Client.class.getResource( "images/danger.jpeg" ); |
| 50 |
linePic.add( Box.createHorizontalGlue() ); |
| 51 |
linePic.add( new JLabel( new ImageIcon( imageUrl ) ) ); |
| 52 |
linePic.add( Box.createHorizontalGlue() ); |
| 53 |
panel.add( linePic ); |
| 54 |
panel.add( Box.createVerticalStrut( 5 ) ); |
| 55 |
JComponent line1 = Box.createHorizontalBox(); |
| 56 |
String appName = ClientAuthorizers.getAppName( securityMap ); |
| 57 |
line1.add( new JLabel( "Client \"" + appName |
| 58 |
+ "\" is requesting Web Profile registration." ) ); |
| 59 |
line1.add( Box.createHorizontalGlue() ); |
| 60 |
line1.setBorder( createBorder( false ) ); |
| 61 |
panel.add( line1 ); |
| 62 |
JLabel deathLabel = new JLabel( "CERTAIN DEATH" ); |
| 63 |
deathLabel.setForeground( Color.RED ); |
| 64 |
deathLabel.setFont( deathLabel.getFont() |
| 65 |
.deriveFont( deathLabel.getFont().getSize() + 2f ) ); |
| 66 |
JComponent line2 = Box.createHorizontalBox(); |
| 67 |
line2.add( new JLabel( "Accepting this request will lead to " ) ); |
| 68 |
line2.add( deathLabel ); |
| 69 |
line2.add( new JLabel( "!" ) ); |
| 70 |
line2.add( Box.createHorizontalGlue() ); |
| 71 |
line2.setBorder( createBorder( true ) ); |
| 72 |
panel.add( line2 ); |
| 73 |
if ( JOptionPane |
| 74 |
.showOptionDialog( parent_, panel, "Registration Request", |
| 75 |
JOptionPane.YES_NO_OPTION, |
| 76 |
JOptionPane.QUESTION_MESSAGE, |
| 77 |
IconStore.createEmptyIcon( 0 ), |
| 78 |
new String[] { "Accept", "Reject" }, |
| 79 |
"Reject" ) != 0 ) { |
| 80 |
throw new SampException( "User denied authorization as advised" ); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Returns a new border of fixed dimensions which may or may not include |
| 86 |
* an element of highlighting. |
| 87 |
* |
| 88 |
* @param highlight true to highlight border |
| 89 |
* @return new border |
| 90 |
*/ |
| 91 |
private Border createBorder( boolean highlight ) { |
| 92 |
Color color = new Color( 0x00ff0000, ! highlight ); |
| 93 |
return BorderFactory.createCompoundBorder( |
| 94 |
BorderFactory.createLineBorder( new Color( 0xff0000, !highlight ) ), |
| 95 |
BorderFactory.createEmptyBorder( 2, 2, 2, 2 ) |
| 96 |
); |
| 97 |
} |
| 98 |
} |