| 1 |
package org.astrogrid.samp.web; |
| 2 |
|
| 3 |
import java.awt.Component; |
| 4 |
import java.awt.Dimension; |
| 5 |
import java.awt.GraphicsEnvironment; |
| 6 |
import java.awt.GridBagConstraints; |
| 7 |
import java.awt.GridBagLayout; |
| 8 |
import java.awt.HeadlessException; |
| 9 |
import java.awt.Insets; |
| 10 |
import java.awt.Window; |
| 11 |
import java.util.ArrayList; |
| 12 |
import java.util.Arrays; |
| 13 |
import java.util.Iterator; |
| 14 |
import java.util.List; |
| 15 |
import java.util.Map; |
| 16 |
import java.util.ResourceBundle; |
| 17 |
import java.util.logging.Logger; |
| 18 |
import javax.swing.BorderFactory; |
| 19 |
import javax.swing.Box; |
| 20 |
import javax.swing.JComponent; |
| 21 |
import javax.swing.JDialog; |
| 22 |
import javax.swing.JLabel; |
| 23 |
import javax.swing.JOptionPane; |
| 24 |
import javax.swing.JPanel; |
| 25 |
import javax.swing.JTextField; |
| 26 |
import org.astrogrid.samp.client.SampException; |
| 27 |
import org.astrogrid.samp.httpd.HttpServer; |
| 28 |
|
| 29 |
/** |
| 30 |
* ClientAuthorizer implementation that queries the user for permission |
| 31 |
* via a popup dialogue. |
| 32 |
* |
| 33 |
* @author Mark Taylor |
| 34 |
* @since 2 Feb 2011 |
| 35 |
*/ |
| 36 |
public class HubSwingClientAuthorizer implements ClientAuthorizer { |
| 37 |
|
| 38 |
private final Component parent_; |
| 39 |
private final CredentialPresenter presenter_; |
| 40 |
private static final int MAX_POPUP_WIDTH = 500; |
| 41 |
private static final Logger logger_ = |
| 42 |
Logger.getLogger( HubSwingClientAuthorizer.class.getName() ); |
| 43 |
|
| 44 |
/** |
| 45 |
* Constructor. |
| 46 |
* |
| 47 |
* @param parent parent component |
| 48 |
* @param presenter handles credential presentation to the user |
| 49 |
*/ |
| 50 |
public HubSwingClientAuthorizer( Component parent, |
| 51 |
CredentialPresenter presenter ) { |
| 52 |
parent_ = parent; |
| 53 |
presenter_ = presenter; |
| 54 |
if ( GraphicsEnvironment.isHeadless() ) { |
| 55 |
throw new HeadlessException( "Client authorization dialogues " |
| 56 |
+ "impossible - no graphics" ); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
public void authorize( HttpServer.Request request, Map securityMap ) |
| 61 |
throws SampException { |
| 62 |
|
| 63 |
// Prepare an internationalised query dialogue. |
| 64 |
AuthResourceBundle.Content authContent = |
| 65 |
AuthResourceBundle |
| 66 |
.getAuthContent( ResourceBundle |
| 67 |
.getBundle( AuthResourceBundle.class.getName() ) ); |
| 68 |
Object[] qmsg = getMessageLines( request, securityMap, authContent ); |
| 69 |
String noOpt = authContent.noWord(); |
| 70 |
String yesOpt = authContent.yesWord(); |
| 71 |
|
| 72 |
// Just calling showOptionDialog can end up with the popup being |
| 73 |
// obscured by other windows on the desktop, at least for win XP. |
| 74 |
JOptionPane jop = |
| 75 |
new JOptionPane( qmsg, JOptionPane.WARNING_MESSAGE, |
| 76 |
JOptionPane.YES_NO_OPTION, null, |
| 77 |
new String[] { noOpt, yesOpt }, noOpt ); |
| 78 |
JDialog dialog = jop.createDialog( parent_, authContent.windowTitle() ); |
| 79 |
attemptSetAlwaysOnTop( dialog, true ); |
| 80 |
dialog.setModal( true ); |
| 81 |
dialog.setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE ); |
| 82 |
|
| 83 |
// It seems to be OK to call Dialog.setVisible on a modal dialogue |
| 84 |
// from threads other than the AWT Event Dispatch Thread. |
| 85 |
// I admit though that I haven't seen document which assures that |
| 86 |
// this is true however. |
| 87 |
dialog.setVisible( true ); |
| 88 |
dialog.dispose(); |
| 89 |
if ( jop.getValue() != yesOpt ) { |
| 90 |
throw new SampException( "User denied authorization" ); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Returns a "message" object describing the applying client to the user. |
| 96 |
* The return value is suitable for use as the <code>msg</code> argument |
| 97 |
* of one of <code>JOptionPane</code>'s methods. |
| 98 |
* |
| 99 |
* @param request HTTP request bearing the application |
| 100 |
* @param securityMap information supplied explicitly by application |
| 101 |
* @param authContent content of AuthResourceBundle bundle |
| 102 |
* @return message array describing the applicant to the user |
| 103 |
* @throws SampExecution if registration is to be rejected out of hand |
| 104 |
* @see javax.swing.JOptionPane |
| 105 |
*/ |
| 106 |
private Object[] getMessageLines( HttpServer.Request request, |
| 107 |
Map securityMap, |
| 108 |
AuthResourceBundle.Content authContent ) |
| 109 |
throws SampException { |
| 110 |
Map headerMap = request.getHeaderMap(); |
| 111 |
CredentialPresenter.Presentation presentation = |
| 112 |
presenter_.createPresentation( request, securityMap, authContent ); |
| 113 |
List lineList = new ArrayList(); |
| 114 |
lineList.addAll( toLineList( authContent.appIntroductionLines() ) ); |
| 115 |
lineList.add( "\n" ); |
| 116 |
lineList.add( createLabelledFields( presentation.getAuthEntries(), |
| 117 |
authContent.undeclaredWord() ) ); |
| 118 |
lineList.add( "\n" ); |
| 119 |
Object[] lines = presentation.getAuthLines(); |
| 120 |
lineList.addAll( Arrays.asList( lines ) ); |
| 121 |
if ( lines.length > 0 ) { |
| 122 |
lineList.add( "\n" ); |
| 123 |
} |
| 124 |
lineList.addAll( toLineList( authContent.privilegeWarningLines() ) ); |
| 125 |
lineList.add( "\n" ); |
| 126 |
lineList.addAll( toLineList( authContent.adviceLines() ) ); |
| 127 |
lineList.add( "\n" ); |
| 128 |
lineList.add( authContent.questionLine() ); |
| 129 |
return lineList.toArray(); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Returns a component displaying name/value pairs represented by |
| 134 |
* a given String->String map. |
| 135 |
* |
| 136 |
* @param infoMap String->String map of key->value pairs |
| 137 |
* @param undeclaredWord text to use to indicate a null value |
| 138 |
* @return display component |
| 139 |
*/ |
| 140 |
private JComponent createLabelledFields( Map infoMap, |
| 141 |
String undeclaredWord ) { |
| 142 |
GridBagLayout layer = new GridBagLayout(); |
| 143 |
JComponent box = new JPanel( layer ) { |
| 144 |
public Dimension getPreferredSize() { |
| 145 |
Dimension size = super.getPreferredSize(); |
| 146 |
return new Dimension( Math.min( size.width, MAX_POPUP_WIDTH ), |
| 147 |
size.height ); |
| 148 |
} |
| 149 |
}; |
| 150 |
GridBagConstraints keyCons = new GridBagConstraints(); |
| 151 |
GridBagConstraints valCons = new GridBagConstraints(); |
| 152 |
keyCons.gridy = 0; |
| 153 |
valCons.gridy = 0; |
| 154 |
keyCons.gridx = 0; |
| 155 |
valCons.gridx = 1; |
| 156 |
keyCons.anchor = GridBagConstraints.WEST; |
| 157 |
valCons.anchor = GridBagConstraints.WEST; |
| 158 |
keyCons.fill = GridBagConstraints.NONE; |
| 159 |
valCons.fill = GridBagConstraints.HORIZONTAL; |
| 160 |
keyCons.weighty = 1; |
| 161 |
valCons.weighty = 1; |
| 162 |
keyCons.weightx = 0; |
| 163 |
valCons.weightx = 1; |
| 164 |
valCons.insets = new Insets( 1, 1, 1, 1 ); |
| 165 |
|
| 166 |
JComponent stack = Box.createVerticalBox(); |
| 167 |
for ( Iterator it = infoMap.keySet().iterator(); it.hasNext(); ) { |
| 168 |
String key = (String) it.next(); |
| 169 |
String value = (String) infoMap.get( key ); |
| 170 |
String valtxt = value == null ? undeclaredWord : value; |
| 171 |
JComponent keyComp = new JLabel( key + ": " ); |
| 172 |
JTextField valueField = new JTextField( valtxt ); |
| 173 |
valueField.setEditable( false ); |
| 174 |
layer.setConstraints( keyComp, keyCons ); |
| 175 |
layer.setConstraints( valueField, valCons ); |
| 176 |
box.add( keyComp ); |
| 177 |
box.add( valueField ); |
| 178 |
keyCons.gridy++; |
| 179 |
valCons.gridy++; |
| 180 |
} |
| 181 |
box.setBorder( BorderFactory.createEmptyBorder( 0, 0, 0, 0 ) ); |
| 182 |
return box; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Turns a multi-line string into an array of strings. |
| 187 |
* |
| 188 |
* @param linesTxt string perhaps with embedded \n characters |
| 189 |
* @return array of lines |
| 190 |
*/ |
| 191 |
private static String[] toLines( String linesTxt ) { |
| 192 |
return linesTxt.split( "\\n" ); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Turns a multi-line string into a List of strings. |
| 197 |
* |
| 198 |
* @param linesTxt string perhaps with embedded \n characters |
| 199 |
* @return list of String lines |
| 200 |
*/ |
| 201 |
private static List toLineList( String linesTxt ) { |
| 202 |
return Arrays.asList( toLines( linesTxt ) ); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Tries to set the always-on-top property of a window. |
| 207 |
* This is only possible in JRE1.5 and later, so it's done here by |
| 208 |
* reflection. If it fails, a logging message is emitted. |
| 209 |
* |
| 210 |
* @param win window to set |
| 211 |
* @param isOnTop true for on top, false for not |
| 212 |
*/ |
| 213 |
private static void attemptSetAlwaysOnTop( Window win, boolean isOnTop ) { |
| 214 |
try { |
| 215 |
Window.class.getMethod( "setAlwaysOnTop", |
| 216 |
new Class[] { boolean.class } ) |
| 217 |
.invoke( win, new Object[] { Boolean.valueOf( isOnTop ) } ); |
| 218 |
} |
| 219 |
catch ( Throwable e ) { |
| 220 |
logger_.info( "Can't set window on top, not J2SE5" ); |
| 221 |
} |
| 222 |
} |
| 223 |
} |