/*
* @(#)Connect.java
*
* This file is part of webCDwriter - Network CD/DVD Writing.
*
* Copyright (C) 1999-2006 Jörg P. M. Haeger
*
* webCDwriter is free software. See CDcreator.java for details.
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.security.*;
import java.security.cert.*;
import javax.net.ssl.*;
import javax.swing.*;
import javax.swing.event.*;
/**
* The start of a connection to CDWserver.
*
* @version 20060422
* @author Jörg P. M. Haeger
*/
public class Connect extends Command {
private String serverName;
private int portNo;
private Socket socket = null;
private JOptionPane pane;
private static JEditorPane viewer;
public Connect(String serverName, int portNo) {
this.serverName = serverName;
this.portNo = portNo;
}
void cancelCommand() {
System.exit(0);
}
protected JComponent createFirstDialog() {
return new JOptionPane(
i18n("tryToConnect"),
JOptionPane.INFORMATION_MESSAGE,
JOptionPane.DEFAULT_OPTION,
null, cancelOption);
}
private JComponent createGreetingPane() {
JPanel vbox = new JPanel();
vbox.setLayout(new BoxLayout(vbox, BoxLayout.Y_AXIS));
String encryption = "";
if (CDcreator.socket instanceof SSLSocket) {
// encryption = " "
// + i18n(".SSLEncryption") + "";
encryption = " " + i18n(".SSLEncryption");
}
String server = serverName;
if (portNo != 80)
server += ":" + portNo;
vbox.add(new JLabel(""
+ i18n(".connectedTo")
// + ""
+ " " + server + ""
// + ""
+ encryption
+ ""));
String title = infoStr, info = infoStr;
int p = title.indexOf("\n");
if (p >= 0) {
title = title.substring(0, p);
info = info.substring(p + 1);
}
String license = "";
int p2 = title.indexOf("(");
if (p2 >= 0) {
license = title.substring(p2 + 1, title.length() - 1);
title = title.substring(0, p2);
}
if (license.startsWith("Licensed to "))
license = license.substring(12);
vbox.add(Box.createVerticalStrut(10));
viewer = CDcreator.getHTML("welcome.html");
if (viewer == null || CDcreator.isNetscapeVM()) {
String type = "text/plain";
if (info.toLowerCase().indexOf("") >= 0)
type = "text/html";
viewer = new JEditorPane(type, info);
viewer.setEditable(false);
}
viewer.setAlignmentX(Box.LEFT_ALIGNMENT);
// viewer.setPreferredSize(new Dimension(400, 200));
viewer.addHyperlinkListener(new MyHyperlinkListener());
JScrollPane scroller = new JScrollPane();
// scroller.setPreferredSize(new Dimension(560, 280));
scroller.setAlignmentX(Box.LEFT_ALIGNMENT);
scroller.getViewport().add(viewer);
vbox.add(scroller);
vbox.add(Box.createVerticalStrut(10));
vbox.add(new JLabel(i18n(".product") + ": " + title
+ " / " + Version.product + " " + Version.version));
vbox.add(new JLabel(""
+ i18n(".licensee") + ": " + license + ""
+ ""));
return vbox;
}
int getDialogDelay() {
return 0;
}
int getInt(String str, String key) {
int p0 = str.indexOf(key + "=");
if (p0 < 0)
return -1;
p0 += key.length() + 1;
int res = 0;
while (p0 < str.length()) {
char ch = str.charAt(p0++);
if (ch < '0' || ch > '9')
break;
res = 10 * res + ch - '0';
}
return res;
}
protected void handleWindowEvent(WindowEvent e) {
Version.debug("show", "handleWindowEvent(" + e + ")");
cancelCommand();
}
void processHint(String str) {
super.processHint(str);
Integer val;
str = str.trim();
if (str.indexOf("anonymousUse") >= 0)
CDcreator.anonymousUse = true;
else
if (str.indexOf("askForProjectInfo") >= 0)
CDcreator.askForProjectInfo = true;
else
if (str.indexOf("audioCDSupport") >= 0)
CDcreator.audioCDSupport = true;
else
if (str.indexOf("cdrdao") >= 0)
CDcreator.cdrdao = true;
else
if (str.indexOf("copy") >= 0)
CDcreator.copySupport = true;
else
if ((val = parse(str, "defaultSpeed", 1)) != null) {
Options.speed = val.intValue();
if ((val = parse(str, "defaultSpeed", 2)) != null) {
Options.speedAudio = val.intValue();
if ((val = parse(str, "defaultSpeed", 3)) != null)
Options.speedCDRW = val.intValue();
}
}
else
if (str.indexOf("exportOnly") >= 0)
CDcreator.exportOnly = true;
else
if (str.startsWith("importImageSupport"))
CDcreator.importImageSupport = true;
else
if (str.startsWith("media"))
CDcreator.supportedMedia = str + ",";
else
if (str.indexOf("managedAccounts") >= 0)
CDcreator.managedAccounts = true;
else
if ((val = parse(str, "maxCopies")) != null)
CDcreator.maxCopies = val.intValue();
else
if ((val = parse(str, "maxMBTransferPerProject")) != null)
CDcreator.maxMBTransferPerProject = val.intValue();
else
if ((val = parse(str, "maxSpeed")) != null)
CDcreator.maxSpeed = val.intValue();
else
if (str.indexOf("md5") >= 0) {
int n = getInt(str, "md5");
if (n < 0)
n = 2;
CDcreator.MD5FileSupport = n > 0;
if (!Config.hasValue(Config.MD5File))
Options.MD5File = n > 1;
Options.MD5FileAlways = n > 2;
}
else
if (str.indexOf("mp3") >= 0)
CDcreator.mp3Support = true;
else
if (str.indexOf("overburnSupport") >= 0)
CDcreator.overburnSupport = true;
else
if (str.indexOf("saveImageAs") >= 0)
CDcreator.saveImageAs = true;
else
if (str.indexOf("verify") >= 0) {
int n = getInt(str, "verify");
if (n < 0)
n = 2;
CDcreator.verifySupport = n > 0;
if (!Config.hasValue(Config.VERIFY))
Options.verify = n > 1;
Options.verifyAlways = n > 2;
}
else
if (str.indexOf("writeProtection") >= 0)
CDcreator.writeProtection = true;
else {
str = " " + str + ",";
if (str.indexOf(" DnD,") >= 0)
CDcreator.DnD = true;
if (str.indexOf(" writerSelection,") >= 0)
CDcreator.writerSelection = true;
}
}
void processInfo(String str) {
super.processInfo(str);
// if (pane != null)
// pane.setMessage(infoStr);
}
public void run() {
invokeAndWait(new Runnable() {
public void run() {
pane = (JOptionPane) createFirstDialog();
showCentered(pane, i18n("titleTryToConnect"));
preloadHTMLViewer();
}
});
// try to connect to CDWserver
socket = null;
for (int tryNo = 1; tryNo <= 2; tryNo++)
try {
try {
socket = new Socket(serverName, portNo);
} catch (Exception e) {
netscape.security.PrivilegeManager
.enablePrivilege(
"UniversalConnect");
socket = new Socket(serverName, portNo);
}
CDcreator.inStream = new BufferedInputStream(
socket.getInputStream());
CDcreator.outStream = new DataOutputStream(
socket.getOutputStream());
resetOpenCommands();
if (tryNo == 1) {
writeCommand("CDWPS");
String line = readLine();
if (line.indexOf("SSL") >= 0)
try {
socket = startSSL(socket, line.indexOf("internal") > 0);
CDcreator.inStream = new BufferedInputStream(
socket.getInputStream());
CDcreator.outStream = new DataOutputStream(
socket.getOutputStream());
}
catch (Exception e) {
Version.debug("SSL", e.getMessage());
continue;
}
}
else
writeCommand("CDWP");
// get greeting
readResult();
break;
}
catch (Exception e) {
socket = null;
}
CDcreator.socket = socket;
if (socket != null && lastResultOK()) {
execCommand("client " + Version.product
+ " " + Version.version);
execCommand("client locale "
+ CDcreator.locale.getLanguage());
execCommand("client LnF "
+ UIManager.getSystemLookAndFeelClassName());
execCommand("client config" + Config.log);
execCommand("setProtocol 6");
execCommand("system " + CDcreator.osName
+ " " + CDcreator.osVersion);
execCommand("vm " + CDcreator.vmName
+ " " + CDcreator.vmVersion);
execCommand("user noID " + State.userID);
if (CDcreator.oneClick) {
super.run();
return;
}
}
super.run();
}
void showResult() {
String title = "";
if (socket == null) {
status = ERROR;
Version.showDebugInfo("no Connection");
pane = new JOptionPane(
i18n("noConnection"),
JOptionPane.WARNING_MESSAGE,
JOptionPane.DEFAULT_OPTION,
null, okOption);
title = i18n("error");
}
else
if (!lastResultOK()) {
CDcreator.socket = null;
pane = new JOptionPane(
MainWin.createViewer(lastResult),
JOptionPane.WARNING_MESSAGE,
JOptionPane.DEFAULT_OPTION,
null, okOption, okOption[0]);
title = i18n("error");
}
else {
pane = new JOptionPane(
createGreetingPane(),
JOptionPane.INFORMATION_MESSAGE,
JOptionPane.DEFAULT_OPTION,
null, okOption);
title = i18n("titleConnectedTo");
}
if (CDcreator.socket == null)
showCentered(pane, title);
else
show(pane, title);
}
SSLSocket startSSL(Socket aSocket, boolean internal) throws Exception {
KeyStore ks = KeyStore.getInstance("JKS");
// ks.load(new FileInputStream("truststore"),
// "changeit".toCharArray());
ks.load(null, null);
ks.setCertificateEntry("webCDwriter", Certificate.get());
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ks);
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, tmf.getTrustManagers(), null);
SSLSocketFactory factory
= (SSLSocketFactory) context.getSocketFactory();
if (!internal)
factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket socket
= (SSLSocket) factory.createSocket(
aSocket, serverName, portNo, true);
socket.setUseClientMode(true);
socket.startHandshake();
Version.debug("SSL", "protocol = "
+ socket.getSession().getProtocol());
Version.debug("SSL", "cipher = "
+ socket.getSession().getCipherSuite());
return(socket);
}
class MyHyperlinkListener implements HyperlinkListener {
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() != HyperlinkEvent.EventType.ACTIVATED)
return;
Version.debug("link", "open " + e.getURL());
String str = e.getDescription();
if (str.indexOf(".jnlp?") >= 0) {
int p0 = str.indexOf(".jnlp?");
String args = str.substring(p0 + 6) + "&";
p0 = args.toUpperCase().indexOf("URL=");
int p1 = args.indexOf("&", p0);
if (p0 >= 0 && p1 >= 0)
CDcreator.projectURL
= args.substring(p0 + 4, p1);
CDcreator.oneClick = true;
// Connect.this.closeDialog();
execSemaphore.setDone();
}
else
if (str.startsWith("http://"))
CDcreator.showDocument(e.getURL());
else {
JEditorPane pane = CDcreator.getHTML(str);
try {
viewer.setPage(pane.getPage());
}
catch (Exception e2) {
}
}
}
}
}