package sample;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.SWT;
import java.util.Properties;
import com.intentia.ec.shared.IRoutingUI;
import com.intentia.ec.shared.Manifest;
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
public class HTTPSOutPanel implements IRoutingUI,
SelectionListener {
private Text txtHost;
private Text txtPort;
private Text txtPath;
private Text txtTrustStore;
private Text txtTrustStorePwd;
private Button btnSpecUser;
private Text txtUser;
private Text txtPwd;
private Button testButton;
private Composite composite;
public HTTPSOutPanel() {}
public void createContent(final Composite parent) {
composite = new Composite(parent, SWT.NONE);
GridLayout gl = new GridLayout(1, false);
gl.verticalSpacing = 10;
gl.marginHeight = 0;
gl.marginWidth = 0;
composite.setLayout(gl);
GridData data = new GridData(GridData.FILL_HORIZONTAL);
GridLayout grpBasicLayout = new GridLayout(2, false);
grpBasicLayout.verticalSpacing = 10;
Group basicGrp = new Group(composite, SWT.NONE);
basicGrp.setText("Basic configuration");
basicGrp.setLayoutData(data);
basicGrp.setLayout(grpBasicLayout);
(new Label(basicGrp, SWT.NONE)).setText("Host:");
data = new GridData(GridData.FILL_HORIZONTAL);
txtHost = new Text(basicGrp, SWT.BORDER);
txtHost.setLayoutData(data);
(new Label(basicGrp, SWT.NONE)).setText("Port:");
data = new GridData();
data.horizontalAlignment = GridData.FILL;
data.grabExcessHorizontalSpace = true;
txtPort = new Text(basicGrp, SWT.BORDER);
txtPort.setLayoutData(data);
(new Label(basicGrp, SWT.NONE)).setText("URL path:");
data = new GridData();
data.horizontalAlignment = GridData.FILL;
data.grabExcessHorizontalSpace = true;
txtPath = new Text(basicGrp, SWT.BORDER);
txtPath.setLayoutData(data);
data = new GridData(GridData.FILL_HORIZONTAL);
GridLayout grpSSLLayout = new GridLayout(2, false);
grpSSLLayout.verticalSpacing = 10;
Group sslGrp = new Group(composite, SWT.NONE);
sslGrp.setText("Trusted certificate store");
sslGrp.setLayoutData(data);
sslGrp.setLayout(grpSSLLayout);
(new Label(sslGrp, SWT.NONE)).setText("Path:");
data = new GridData(GridData.FILL_HORIZONTAL);
txtTrustStore = new Text(sslGrp, SWT.BORDER);
txtTrustStore.setLayoutData(data);
(new Label(sslGrp, SWT.NONE)).setText("Password:");
data = new GridData(GridData.FILL_HORIZONTAL);
txtTrustStorePwd = new Text(sslGrp,
SWT.BORDER | SWT.PASSWORD);
txtTrustStorePwd.setLayoutData(data);
data = new GridData(GridData.FILL_HORIZONTAL);
GridLayout grpUserLayout = new GridLayout(2, false);
grpUserLayout.verticalSpacing = 10;
Group userGrp = new Group(composite, SWT.NONE);
userGrp.setText("User configuration");
userGrp.setLayoutData(data);
userGrp.setLayout(grpUserLayout);
(new Label(userGrp, SWT.NONE)).setText("");
data = new GridData();
btnSpecUser= new Button(userGrp, SWT.CHECK);
btnSpecUser.setText("Specify user");
btnSpecUser.addSelectionListener(this);
btnSpecUser.setLayoutData(data);
(new Label(userGrp, SWT.NONE)).setText("User:");
data = new GridData();
data = new GridData(GridData.FILL_HORIZONTAL);
txtUser = new Text(userGrp, SWT.BORDER);
txtUser.setLayoutData(data);
txtUser.setEnabled(false);
(new Label(userGrp, SWT.NONE)).setText("Password:");
data = new GridData(GridData.FILL_HORIZONTAL);
txtPwd = new Text(userGrp, SWT.BORDER | SWT.PASSWORD);
txtPwd.setLayoutData(data);
txtPwd.setEnabled(false);
data = new GridData(GridData.FILL_HORIZONTAL);
data.horizontalAlignment = GridData.END;
data.verticalAlignment = GridData.BEGINNING;
testButton = new Button(composite, SWT.PUSH);
testButton.setText("Send test message");
testButton.addSelectionListener(this);
testButton.setLayoutData(data);
}
public void setProperties(Properties props) {
if (props == null) return;
txtHost.setText(props.getProperty(HTTPSOut.HOST, ""));
txtPort.setText(props.getProperty(HTTPSOut.PORT, ""));
txtPath.setText(props.getProperty(HTTPSOut.PATH, ""));
txtUser.setText(props.getProperty(HTTPSOut.USER, ""));
txtPwd.setText(props.getProperty(HTTPSOut.PASSWORD, ""));
txtTrustStore.setText(props.getProperty
(HTTPSOut.TRUST_STORE, ""));
txtTrustStorePwd.setText(props.getProperty
(HTTPSOut.TRUST_STORE_PWD, ""));
btnSpecUser.setSelection(txtUser.getText().length() > 0);
txtUser.setEnabled(txtUser.getText().length() > 0);
txtPwd.setEnabled(txtUser.getText().length() > 0);
}
private boolean checkFields() {
if (txtHost.getText().length() == 0) {
openError(composite.getShell(), "Invalid value",
"Host cannot be empty."); return false;
}
if (btnSpecUser.getSelection() &&
txtUser.getText().length() == 0) {
openError(composite.getShell(), "Invalid value",
"User cannot be empty."); return false;
}
try {
if (Integer.parseInt(txtPort.getText()) <= 0) {
throw new NumberFormatException();
}
}
catch (NumberFormatException e) {
openError(composite.getShell(), "Invalid value",
"Illegal port number: " + txtPort.getText());
return false;
}
if (txtTrustStore.getText().length() == 0) {
openError(composite.getShell(), "Invalid value",
"Trusted store path cannot be empty.");
return false;
}
return true;
}
public Properties getProperties() {
Properties props = new Properties();
if (!checkFields()) return null;
props.setProperty(HTTPSOut.HOST, txtHost.getText().trim());
props.setProperty(HTTPSOut.PORT, txtPort.getText().trim());
props.setProperty(HTTPSOut.PATH, txtPath.getText().trim());
if (btnSpecUser.getSelection()) {
props.setProperty(HTTPSOut.USER,
txtUser.getText().trim());
props.setProperty(HTTPSOut.PASSWORD,
txtPwd.getText().trim());
}
props.setProperty(HTTPSOut.TRUST_STORE,
txtTrustStore.getText().trim());
props.setProperty(HTTPSOut.TRUST_STORE_PWD,
txtTrustStorePwd.getText().trim());
return props;
}
public void widgetSelected(SelectionEvent event) {
if (event.widget == btnSpecUser) {
if (!btnSpecUser.getSelection()) {
txtUser.setData(txtUser.getText());
txtPwd.setData(txtPwd.getText());
txtUser.setText("");
txtPwd.setText("");
}
else {
txtUser.setText(txtUser.getData() != null ?
(String)txtUser.getData() : "");
txtPwd.setText(txtPwd.getData() != null ?
(String)txtPwd.getData() : "");
}
txtUser.setEnabled(btnSpecUser.getSelection());
txtPwd.setEnabled(btnSpecUser.getSelection());
}
else if (event.widget == testButton) {
if (!checkFields()) return;
Properties props = new Properties();
props.put(HTTPSOut.HOST, txtHost.getText().trim());
props.put(HTTPSOut.PORT, txtPort.getText().trim());
props.put(HTTPSOut.PATH, txtPath.getText().trim());
props.put(HTTPSOut.USER, txtUser.getText().trim());
props.put(HTTPSOut.PASSWORD, txtPwd.getText().trim());
props.put(HTTPSOut.TRUST_STORE, txtTrustStore.getText().trim());
props.put(HTTPSOut.TRUST_STORE_PWD,
txtTrustStorePwd.getText().trim());
FileDialog dialog = new FileDialog(composite.getShell(),
SWT.OPEN); dialog.open();
if (dialog.getFilterPath() != null &&
dialog.getFilterPath().length() > 0
&& dialog.getFileName() != null &&
dialog.getFileName().length() > 0) {
String fileName = dialog.getFilterPath() +
File.separator + dialog.getFileName();
File file = new File(fileName);
if (!file.isFile()) {
openError(composite.getShell(), "Invalid file",
"Chosen file is not a file:" + file.getName());
return;
}
else if (file.length() <= 0) {
openError(composite.getShell(), "Invalid file",
"Chosen file is empty:" + file.getName());
return;
}
composite.getShell().setCursor
(Display.getCurrent().getSystemCursor(SWT.CURSOR_WAIT));
InputStream ins = null;
try {
ins = new FileInputStream(file);
HTTPSOut out = new HTTPSOut();
Manifest manifest = Manifest.getManifest();
out.send(ins, manifest, props);
openInformation(composite.getShell(), "Success",
"Successfully sent file: " + file.getName());
composite.getShell().setCursor(null);
}
catch (Throwable e) {
composite.getShell().setCursor(null);
openError(composite.getShell(), "Error sending",
"Sending test message failed. " + e.getMessage());
}
finally {
try {
ins.close();
}
catch (Exception e) {}
}
}
return;
}
}
public void widgetDefaultSelected(SelectionEvent e) {
widgetSelected(e);
}
private static void openError(Shell shell,
String title, String msg) {
int style = SWT.ICON_ERROR | SWT.OK;
MessageBox box = new MessageBox(shell, style);
box.setText(title == null ? "Error " : title);
box.setMessage(msg);
box.open();
}
private static void openInformation(Shell shell,
String title, String msg) {
int style = SWT.ICON_INFORMATION | SWT.OK;
MessageBox box = new MessageBox(shell, style);
box.setText(title == null ? "" : title);
box.setMessage(msg);
box.open();
}
}