at.spardat.xma.security.SSLCertificateViewer Maven / Gradle / Ivy
The newest version!
package at.spardat.xma.security;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import at.spardat.xma.boot.transport.XMA_URI;
import at.spardat.xma.mdl.WModel;
import at.spardat.xma.mdl.list.IListWMClient;
import at.spardat.xma.mdl.table.ITableWMClient;
import at.spardat.xma.page.PageClient;
import at.spardat.xma.page.Scaler;
import at.spardat.xma.session.XMASessionClient;
public class SSLCertificateViewer {
public static String SSL_CERT = "SSL_CERT";
public static String SSL_CERT_ISSUED_TO = "SSL_CERT_ISSUED_TO";
public static String SSL_CERT_ISSUED_BY = "SSL_CERT_ISSUED_BY";
public static String SSL_CERT_VALID_FROM = "SSL_CERT_VALID_FROM";
public static String SSL_CERT_VALID_TO = "SSL_CERT_VALID_TO";
static SSLCertificateViewer instance;
/**
* Singleton instance of this class
* @return instance of SSLCertificateViewer
*/
public static synchronized SSLCertificateViewer getInstance() {
if (instance == null) {
instance = new SSLCertificateViewer();
}
return instance;
}
private Shell dialog;
private Scaler scaler;
Locale genPageLocale;
Control[] widgets;
Composite CertificatePanelW;
Composite CertificateTableHeaderW;
Composite CertificateTableHeader_set0W;
Label CertificateTableHeader_set0_centered;
Combo cmbCertW;
IListWMClient cmbCert;
Table certificateTableW;
at.spardat.xma.mdl.table.TableLayoutManager certificateTableWLM;
ITableWMClient certificateTable;
static final int CERTIFICATETABLE_TXTKEY = 0;
static final int CERTIFICATETABLE_TXTVALUE = 1;
Text txtCertdataW;
private ResourceBundle genPageMessages;
ArrayList certChain = null;
String uriAsString = null;
WModel[] widgetModels;
/**
* Shows the dialog window with certificate details of the current application,
* if the current application is running on non secure layer then appropriate
* message will be pop up in the same dialog.
*
* @param pageClient PageClient component of the current application
*/
public void show(PageClient pageClient) {
XMASessionClient sessionClient = (XMASessionClient) pageClient
.getSession();
XMA_URI uri = sessionClient.getUri();
uriAsString = uri.toString();
dialog = new Shell(pageClient.getDialog().getShell());
Display display = pageClient.getDialog().getShell().getDisplay();
dialog.setText("CertificateForm");
genPageLocale = pageClient.getComponent().getContext().getLocale();
try {
readCertificate(uriAsString);
createWidgets(false);
initCertificate();
} catch (ClassCastException ce) {
createWidgets(true);
txtCertdataW.setText(getGenPageMessages().getString(
"CertificateForm.ClassCastException")
+ uriAsString + "\n");
} catch (Exception e) {
createWidgets(true);
txtCertdataW.setText(getGenPageMessages().getString(
"CertificateForm.Exception")
+ e);
}
dialog.open();
while (!dialog.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
dialog.dispose();
}
protected Composite getComposite() {
return dialog;
}
final protected Scaler getScaler() {
if (scaler == null) {
scaler = Scaler.getInstance(getComposite());
}
return scaler;
}
/**
* Create and initialize all SWT-Widgets of the page. It
*
* - instantiates the Widgets
* - sets constant texts (Title, Labels, Tooltips) and Images
* - registers the eventadapter
* - defines the Layout (Attachments, Distances, Sizes)
* - sets the focus
* - defines the Taborder
*
*/
protected void createWidgets(boolean OnError) {
final Scaler scaler = getScaler();
final Shell shell = dialog;
Composite CertificateFormCompW = getComposite();
FormLayout layout = new FormLayout();
layout.marginHeight = scaler.convertYToCurrent(3);
layout.marginWidth = scaler.convertXToCurrent(3);
CertificateFormCompW.setLayout(layout);
CertificatePanelW = new Composite(CertificateFormCompW,
SWT.NONE);
layout = new FormLayout();
layout.marginHeight = scaler.convertYToCurrent(3);
layout.marginWidth = scaler.convertXToCurrent(3);
CertificatePanelW.setLayout(layout);
CertificatePanelW.setData("WIDGET_ID", "CertificatePanelW");
if (OnError) {
txtCertdataW = new Text(CertificatePanelW,
SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.LEFT);
txtCertdataW.setData("WIDGET_ID", "txtCertdataW");
txtCertdataW.setEditable(false);
widgets = new Control[] { CertificatePanelW, txtCertdataW };
createWidgetsLayout(OnError);
dialog.setSize(scaler.convertXToCurrent(600),
scaler.convertYToCurrent(200));
} else {
CertificateTableHeaderW = new Composite(
CertificatePanelW, SWT.NONE);
layout = new FormLayout();
layout.marginHeight = scaler.convertYToCurrent(3);
layout.marginWidth = scaler.convertXToCurrent(3);
CertificateTableHeaderW.setLayout(layout);
CertificateTableHeaderW.setData("WIDGET_ID",
"CertificateTableHeaderW");
CertificateTableHeader_set0W = new Composite(
CertificateTableHeaderW, SWT.NONE);
layout = new FormLayout();
layout.marginHeight = scaler.convertYToCurrent(0);
layout.marginWidth = scaler.convertXToCurrent(0);
CertificateTableHeader_set0W.setLayout(layout);
CertificateTableHeader_set0W.setData("WIDGET_ID",
"CertificateTableHeader_set0W");
CertificateTableHeader_set0_centered = new Label(
CertificateTableHeader_set0W, SWT.LEFT);
CertificateTableHeader_set0_centered.setVisible(false);
CertificateTableHeader_set0_centered.setData("WIDGET_ID",
"CertificateTableHeader_set0_centered");
cmbCertW = new Combo(CertificateTableHeader_set0W,
SWT.DROP_DOWN | SWT.BORDER);
cmbCertW.setData("WIDGET_ID", "cmbCertW");
cmbCertW.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
changeCertificate();
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
certificateTableW = new Table(CertificatePanelW,
SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);
certificateTableW.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
onSelectionTableRow(e);
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
certificateTableW.setHeaderVisible(true);
certificateTableW.setLinesVisible(true);
certificateTableW.setData("WIDGET_ID", "certificateTableW");
TableColumn certificateTable_txtkey = new TableColumn(certificateTableW, SWT.LEFT);
certificateTable_txtkey.setText(getGenPageMessages().getString(
"CertificateForm.certificateTable_txtkey"));
TableColumn certificateTable_txtvalue = new TableColumn(certificateTableW, SWT.LEFT);
certificateTable_txtvalue.setText(getGenPageMessages().getString(
"CertificateForm.certificateTable_txtvalue"));
Menu certificateTableWCM = new Menu(certificateTableW.getShell());
MenuItem certificateTableWCMI = new MenuItem(certificateTableWCM,
SWT.PUSH);
certificateTableWCMI.setText("Copy text");
certificateTableW.setMenu(certificateTableWCM);
certificateTableWLM = new at.spardat.xma.mdl.table.TableLayoutManager();
certificateTableWLM.manageTable(certificateTableW,
at.spardat.xma.mdl.table.TableLayoutManager.NONE);
certificateTableWLM.setMinLimit(scaler.convertXToCurrent(10));
certificateTableWLM.setAbsolutWidth(0,
scaler.convertXToCurrent(200));
certificateTableWLM.setAbsolutWidth(1,
scaler.convertXToCurrent(400));
txtCertdataW = new Text(CertificatePanelW,
SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.LEFT);
txtCertdataW.setData("WIDGET_ID", "txtCertdataW");
txtCertdataW.setEditable(false);
widgets = new Control[] { CertificatePanelW,
CertificateTableHeaderW, CertificateTableHeader_set0W,
CertificateTableHeader_set0_centered, cmbCertW,
certificateTableW, txtCertdataW };
createWidgetsLayout(false);
// createWidgetsTabOrder();
dialog.setSize(scaler.convertXToCurrent(640),
scaler.convertYToCurrent(480));
}
}
/*
* returns the message resource bundle used for this page
*/
final protected ResourceBundle getGenPageMessages() {
if (genPageMessages == null) {
genPageMessages = ResourceBundle.getBundle(
"at.spardat.xma.security.SSLCertificateViewer",
getGenPageLocale());
}
return genPageMessages;
}
/*
* returns the Locale used for this page
*/
final protected Locale getGenPageLocale() {
return genPageLocale;
}
/**
* Create the layout of all SWT-Widgets of the page. (Attachments,
* Distances, Sizes)
*
* @param onError
*/
public void createWidgetsLayout(boolean onError) {
FormData data;
final Scaler scaler = getScaler();
Control lastControl;
data = new FormData();
data.left = new FormAttachment(0, 100, scaler.convertXToCurrent(0));
data.right = new FormAttachment(100, 100, scaler.convertXToCurrent(0));
data.top = new FormAttachment(0, 100, scaler.convertYToCurrent(0));
CertificatePanelW.setLayoutData(data);
if (onError) {
data = new FormData();
data.height = scaler.convertYToCurrent(100);
data.left = new FormAttachment(0, 100, scaler.convertXToCurrent(0));
data.right = new FormAttachment(100, 100,
scaler.convertXToCurrent(0));
data.top = new FormAttachment(0, 100, scaler.convertYToCurrent(0));
data.bottom = new FormAttachment(100, 100,
scaler.convertYToCurrent(0));
txtCertdataW.setLayoutData(data);
} else {
data = new FormData();
data.left = new FormAttachment(0, 100, scaler.convertXToCurrent(0));
data.right = new FormAttachment(100, 100,
scaler.convertXToCurrent(0));
data.top = new FormAttachment(0, 100, scaler.convertYToCurrent(0));
CertificateTableHeaderW.setLayoutData(data);
lastControl = null;
data = new FormData();
data.left = new FormAttachment(0, 100, scaler.convertXToCurrent(0));
data.right = new FormAttachment(100, 100,
scaler.convertXToCurrent(0));
data.top = new FormAttachment(0, 100, scaler.convertYToCurrent(0));
CertificateTableHeader_set0W.setLayoutData(data);
lastControl = CertificateTableHeader_set0W;
data = new FormData();
data.width = scaler.convertXToCurrent(1);
data.left = new FormAttachment(0, 100, scaler.convertXToCurrent(0));
data.top = new FormAttachment(0, 100, scaler.convertYToCurrent(0));
data.bottom = new FormAttachment(100, 100,
scaler.convertYToCurrent(0));
CertificateTableHeader_set0_centered.setLayoutData(data);
data = new FormData();
data.width = scaler.convertXToCurrent(250);
data.left = new FormAttachment(0, 100, scaler.convertXToCurrent(0));
data.top = new FormAttachment(CertificateTableHeader_set0_centered,
scaler.convertYToCurrent(0), SWT.CENTER);
cmbCertW.setLayoutData(data);
data = new FormData();
data.height = scaler.convertYToCurrent(200);
data.left = new FormAttachment(0, 100, scaler.convertXToCurrent(0));
data.right = new FormAttachment(100, 100,
scaler.convertXToCurrent(0));
data.top = new FormAttachment(CertificateTableHeaderW,
scaler.convertYToCurrent(0), SWT.BOTTOM);
certificateTableW.setLayoutData(data);
data = new FormData();
data.height = scaler.convertYToCurrent(100);
data.left = new FormAttachment(0, 100, scaler.convertXToCurrent(0));
data.right = new FormAttachment(100, 100,
scaler.convertXToCurrent(0));
data.top = new FormAttachment(certificateTableW,
scaler.convertYToCurrent(0), SWT.BOTTOM);
data.bottom = new FormAttachment(100, 100,
scaler.convertYToCurrent(0));
txtCertdataW.setLayoutData(data);
}
}
/**
* Create the tab order of all SWT-Widgets of the page. (sets the focus and
* defines the tab order)
*/
protected void createWidgetsTabOrder() {
Control[] tabOrder;
final Shell shell = dialog;
final Composite CertificateFormCompW = getComposite();
dialog.setTabList(new Control[] { CertificateFormCompW.getParent() });
tabOrder = new Control[1];
tabOrder[0] = CertificatePanelW;
CertificateFormCompW.setTabList(tabOrder);
tabOrder = new Control[3];
tabOrder[0] = CertificateTableHeaderW;
tabOrder[1] = certificateTableW;
tabOrder[2] = txtCertdataW;
CertificatePanelW.setTabList(tabOrder);
tabOrder = new Control[1];
tabOrder[0] = CertificateTableHeader_set0W;
CertificateTableHeaderW.setTabList(tabOrder);
tabOrder = new Control[1];
tabOrder[0] = cmbCertW;
CertificateTableHeader_set0W.setTabList(tabOrder);
}
protected void onSelectionTableRow(SelectionEvent event) {
TableItem tb[] = certificateTableW.getSelection();
String sSelCol = tb[0].getText(1);
if (sSelCol.indexOf(',') > 0)
txtCertdataW.setText(sSelCol.replaceAll(", ", "\n"));
else
txtCertdataW.setText(sSelCol);
}
protected void changeCertificate() {
int iselected = 0;
if (cmbCertW.getSelection() != null)
iselected = cmbCertW.getSelectionIndex();
List l = (List) certChain.get(iselected).get(SSL_CERT);
// remove previously populated rows if any
if (certificateTableW.getItemCount() > 0)
certificateTableW.removeAll();
String[] cellsdata, sdetails = new String[2];
// insert new rows of selected Certificates
for (int i = 0; i < l.size(); i++) {
cellsdata = (String[]) l.get(i);
if (i == 0)
sdetails = cellsdata;
TableItem titem = new TableItem(certificateTableW, SWT.NONE);
titem.setText(0, cellsdata[0]);
titem.setText(1, cellsdata[1]);
}
// show first row details
if (sdetails[1].indexOf(',') > 0)
txtCertdataW.setText(sdetails[1].replaceAll(", ", "\n"));
else
txtCertdataW.setText(sdetails[1]);
}
/**
* Method to read certificate from server todo check of proxy settings, if
* proxy is required for application running behind proxy chain
*
* @param uriString
* @throws Exception
*/
private void readCertificate(String uriString) throws Exception {
// Read The Certificate
SSLCertificateReader sslr = new SSLCertificateReader();
sslr.setResourceBundle(getGenPageMessages());
certChain = sslr.readCertificate(uriString);
}
protected void initCertificate() {
// Populate combo with certificate list
int j=0;
for (int i = 0; i < certChain.size(); i++)
cmbCertW.add("" + certChain.get(i).get(SSL_CERT_ISSUED_TO), j++);
cmbCertW.select(0);
// populate table with details
changeCertificate();
}
}