import edge.client.HostData; import edge.client.api.ProteusInterface; import edge.client.api.ProteusListener; import edge.client.api.ProteusListenerException; import edge.utils.layoutManagers.StackLayout; import edge.utils.misc.Dates; import com.taligent.widget.BorderPanel; import jclass.bwt.JCAlignerLayout; import java.applet.Applet; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Color; import java.awt.Container; import java.awt.Frame; import java.awt.Label; import java.awt.Panel; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import java.util.Enumeration; import java.util.Hashtable; import java.util.Properties; public class ProteusApp extends Applet implements ProteusListener { public String queryURL = ""; public String updateURL = ""; public String port = ""; public String docBase; private BorderPanel statePanel; public boolean running = false; private boolean isApplication = false; private boolean WinNT = false; private boolean Win32 = false; private void postMsg(String action, String msgId, String tagName, Hashtable tagAttributesAsNameValuePairs, String otherTagName, Hashtable otherTagAttributesAsNameValuePairs) throws ProteusListenerException { StringBuffer data = new StringBuffer(); data.append("action=").append(action); data.append("&user=").append(URLEncoder.encode(HostData.getUserID())); data.append("&msgid=").append(URLEncoder.encode(msgId)); data.append("&tagname=").append(URLEncoder.encode(tagName)); StringBuffer tagParms = new StringBuffer(); for (Enumeration tagKeys = tagAttributesAsNameValuePairs.keys(); tagKeys.hasMoreElements(); /* null */ ) { String currentName = (String) tagKeys.nextElement(); if (currentName.equalsIgnoreCase("CONTENT")) { data.append("&tagcontent="); data.append(URLEncoder.encode((String)tagAttributesAsNameValuePairs.get(currentName))); } else { tagParms.append(currentName).append("=\""); tagParms.append((String)tagAttributesAsNameValuePairs.get(currentName)).append("\" "); } } if (tagParms.length() > 0) { data.append("&tagparms=").append(URLEncoder.encode(tagParms.toString())); } if (otherTagName != null) { data.append("&newtagname="); data.append(URLEncoder.encode(otherTagName)); tagParms = new StringBuffer(); for (Enumeration tagKeys = otherTagAttributesAsNameValuePairs.keys(); tagKeys.hasMoreElements(); /* null */ ) { String currentName = (String) tagKeys.nextElement(); if (currentName.equalsIgnoreCase("CONTENT")) { data.append("&newtagcontent="); data.append(URLEncoder.encode((String)otherTagAttributesAsNameValuePairs.get(currentName))); } else { tagParms.append(currentName).append("=\""); tagParms.append((String)otherTagAttributesAsNameValuePairs.get(currentName)); tagParms.append("\" "); } } if (tagParms.length() > 0) { data.append("&newtagparms=").append(URLEncoder.encode(tagParms.toString())); } } String returnValue; try { returnValue = postURL(new URL(updateURL), data.toString()); } catch (Exception e) { throw new ProteusListenerException(e + " :: " + e.getMessage()); } if ((returnValue != null) && (returnValue.indexOf("ERROR", 0) >= 0)) { throw new ProteusListenerException(returnValue); } } // $AUTO: Interface: edge.client.api.ProteusListener public void addTagToMsg(String MsgIdToAddTagTo, String newTagName, Hashtable tagAttributesAsNameValuePairs) throws ProteusListenerException { postMsg("ADD", MsgIdToAddTagTo, newTagName, tagAttributesAsNameValuePairs, null, null); } // $AUTO: Interface: edge.client.api.ProteusListener public void deleteMsg(String msgIdOfMsgToDelete) throws ProteusListenerException { Hashtable newData = new Hashtable(); newData.put("VALUE", "true"); newData.put("AUTHOR", HostData.getUserID()); newData.put("DATE", Dates.YYYYMMDDHHMM()); addTagToMsg(msgIdOfMsgToDelete, "DELETED", newData); } // $AUTO: Interface: edge.client.api.ProteusListener public void deleteTagFromMsg(String MsgIdToDeleteTagFrom, String tagNameToDelete, Hashtable tagAttributesAsNameValuePairs) throws ProteusListenerException { postMsg("DELETE", MsgIdToDeleteTagFrom, tagNameToDelete, tagAttributesAsNameValuePairs, null, null); } // $AUTO: Interface: edge.client.api.ProteusListener public void launchURL(URL newURL) throws ProteusListenerException { if (isApplication) { String cmdLine; if (Win32) { cmdLine = "start " + newURL; if (WinNT) { cmdLine = "cmd.exe /c start " + newURL; } try { Process p = Runtime.getRuntime().exec(cmdLine); } catch (Exception e) { e.printStackTrace(); } } else { String theURL = newURL.toString(); if (theURL.startsWith("http")) { cmdLine = "netscape -remote 'openURL(" + theURL + ")'"; } else { cmdLine = "netscape '" + theURL + "'"; } try { Process p = Runtime.getRuntime().exec(new String[] {"/bin/sh", "-c", cmdLine}); p.waitFor(); if (p.exitValue() != 0) { cmdLine = "netscape " + newURL; Process p2 = Runtime.getRuntime().exec(cmdLine); } } catch (Exception e) { e.printStackTrace(); } } } else { getAppletContext().showDocument(newURL, "display_area"); } } // $AUTO: Interface: edge.client.api.ProteusListener public void proteusHasClosed() { if (isApplication) { System.exit(0); } } // $AUTO: Interface: edge.client.api.ProteusListener public void replaceTagInMsg(String MsgIdToReplaceTagIn, String tagNameToReplace, Hashtable replacedTagAttributesAsNameValuePairs, String newTagName, Hashtable newTagAttributesAsNameValuePairs) throws ProteusListenerException { postMsg("REPLACE", MsgIdToReplaceTagIn, tagNameToReplace, replacedTagAttributesAsNameValuePairs, newTagName, newTagAttributesAsNameValuePairs); } // $AUTO: Interface: edge.client.api.ProteusListener public String retrieveFullMsg(String MsgIdToRetrieve) throws ProteusListenerException { try { URL message = new URL(queryURL + "?msgid=" + MsgIdToRetrieve); URLConnection dataConnection = message.openConnection(); BufferedInputStream inData = new BufferedInputStream(dataConnection.getInputStream()); ByteArrayOutputStream holdOut = new ByteArrayOutputStream(); byte[] buffer = new byte[512]; int bytesRead = 0; while ((bytesRead = inData.read(buffer)) >= 0) { holdOut.write(buffer, 0, bytesRead); } if (holdOut.size() > 0) { // 1.1 deprecated: limits high byte to 0! return new String(holdOut.toByteArray(), 0); } else { return new String(); } } catch (Exception e) { throw new ProteusListenerException(e + " :: " + e.getMessage()); } } // $AUTO: Interface: edge.client.api.ProteusListener public void statusInfo(String informationalNoteFromProteus) { // System.err.println("Status :: " + informationalNoteFromProteus); statePanel.removeAll(); statePanel.add("Center", new Label(informationalNoteFromProteus, Label.CENTER)); statePanel.setText("Current Status"); edge.utils.misc.Invalidate.invalidateUp(statePanel); Container top = (Container)edge.utils.misc.Invalidate.getTop(this); edge.utils.misc.Invalidate.invalidateDown(top); validate(); repaint(); } // $AUTO: Interface: edge.client.api.ProteusListener public void doQuery (String someQuery) { // System.err.println("Doing Query : " + someQuery); try { URL query = new URL(queryURL + "?query=test+query&minimal"); ProteusInterface.setWorkingSet(query.openConnection().getInputStream()); } catch (Exception e) { } } // $AUTO: Overrides: java.applet.Applet.getDocumentBase public URL getDocumentBase() { if (isApplication) { try { return new URL(docBase); } catch (Exception e) { return null; } } return super.getDocumentBase(); } // $AUTO: Overrides: java.applet.Applet.init public void init() { if (running) { return; } try { queryURL = this.getParameter("QUERY_URL"); updateURL = this.getParameter("UPDATE_URL"); port = this.getParameter("SERVER_PORT"); if (queryURL == null) { queryURL = "http://wilma/proteus-cgi/plugquery.cgi"; } } catch (Exception e) { } ProteusInterface.setProteusListener(this, this); ProteusInterface.setMsgUtilizationState( ProteusInterface.TWO_STAGE_PARTIAL_THEN_FULL); ProteusInterface.setParameter(ProteusInterface.BROWSER_SUPPORTS_UDP, "yes"); setLayout(new BorderLayout()); add("Center", createPanels()); } private Panel createPanels() { BorderPanel dataPanel; dataPanel = new BorderPanel(BorderPanel.SOLID, 2); dataPanel.setColor(HostData.proteusBlue()); dataPanel.setLayout(new BorderLayout(5, 5)); dataPanel.setBackground(HostData.proteusGray()); dataPanel.setForeground(HostData.proteusBlue()); dataPanel.add("North", new jclass.bwt.JCLabel("", HostData.grayLogo(), jclass.bwt.BWTEnum.STRING_TOP)); statePanel = new BorderPanel("Please Login"); statePanel.setLayout(new BorderLayout()); statePanel.setTextFont(HostData.proteusFont14()); statePanel.setColor(HostData.proteusBlue()); statePanel.add("Center", new Password(this)); dataPanel.add("Center", statePanel); dataPanel.add("South", new jclass.bwt.JCLabel("Do NOT close this window while Proteus is running.\nClosing this window will close Proteus.\nIconifying this window will have no impact upon Proteus.")); dataPanel.add("East", new Label(" ")); dataPanel.add("West", new Label(" ")); BorderPanel corePanel = new BorderPanel(BorderPanel.RAISED, 5); corePanel.setLayout(new BorderLayout(5, 5)); corePanel.add("Center", dataPanel); return(corePanel); } public void login(String userName, String password) { if (ProteusInterface.login(userName, password)) { this.statusInfo("Running."); } else { // Calling PI.login necessitates the statePanel being rebuilt. statePanel.removeAll(); statePanel.add("Center", new Password(this)); statePanel.setText("Please Try Again"); edge.utils.misc.Invalidate.invalidateUp(statePanel); Container top = (Container)edge.utils.misc.Invalidate.getTop(this); edge.utils.misc.Invalidate.invalidateDown(top); validate(); repaint(); } } public String postURL(URL outURL, String postData) throws Exception { URLConnection outConnection = outURL.openConnection(); outConnection.setDoInput(true); outConnection.setDoOutput(true); outConnection.setUseCaches(false); outConnection.setRequestProperty("Content-Type", "text/plain"); DataOutputStream printOut = new DataOutputStream(outConnection.getOutputStream()); printOut.writeBytes(postData); printOut.flush(); printOut.close(); DataInputStream outRead = new DataInputStream(outConnection.getInputStream()); StringBuffer output = new StringBuffer(); String holdOut; while ((holdOut = outRead.readLine()) != null) { output.append(holdOut).append("\n"); } outRead.close(); if (output.length() > 0) { return output.toString(); } else { return new String(); } } public void quit() { try { running = false; ProteusInterface.closeProteus(); } catch (Exception e) { System.err.println("Error - " + e + "; " + e.getMessage()); } } public void showHTML(String htmlFile) { URL displayURL; try { if (htmlFile.startsWith("http")) { displayURL = new URL(htmlFile); } else { displayURL = new URL(getDocumentBase(), htmlFile); } launchURL(displayURL); } catch (Exception e) { System.err.println("Error - " + e + "; " + e.getMessage()); } } // $AUTO: Overrides: java.applet.Applet.start public void start() { if (running) { return; } running = true; setPlatForm(); if (port != null) { ProteusInterface.setParameter(ProteusInterface.SERVER_PORT, port); } } // $AUTO: Overrides: java.applet.Applet.start public void destroy() { if (running) { return; } super.destroy(); } public static void main(String argv[]) { Properties taskProps = ProteusApp.getTaskProperties(argv[0]); ProteusApp applet = new ProteusApp(); applet.isApplication = true; applet.docBase = taskProps.getProperty("Proteus.docBase"); Frame proteusWin = new Frame("Proteus"); applet.init(); try { new edge.utils.misc.SplashScreen(proteusWin, HostData.getImage("images/proteus2.jpg")); Thread.sleep(3000); } catch (Exception e) { } applet.queryURL = taskProps.getProperty("Proteus.queryURL"); applet.updateURL = taskProps.getProperty("Proteus.updateURL"); applet.port = taskProps.getProperty("Proteus.port"); applet.start(); proteusWin.setIconImage(edge.client.HostData.icon()); proteusWin.add("Center", applet); proteusWin.pack(); java.awt.Dimension frameDim = proteusWin.getSize(); java.awt.Dimension screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); proteusWin.setLocation(((screen.width - frameDim.width) / 2), ((screen.height - frameDim.height) / 2)); proteusWin.show(); proteusWin.toFront(); proteusWin.requestFocus(); } private static Properties getTaskProperties(String fileName) { Properties taskProps = new Properties(); if (fileName == null) { fileName = "Proteus.cfg"; } try { FileInputStream in = new FileInputStream(fileName); taskProps.load(in); in.close(); } catch (Exception e) { System.err.println("Proteus.getTaskProps: Error loading file " + fileName); } return(taskProps); } private static Properties sysProps = null; public void getProps() { if (sysProps != null) { return; } if (isApplication) { sysProps = System.getProperties(); } else { // Oh well, no access to everything. // The following list of properties are generally // available in any context. String keys[] = { "java.vendor", "java.vendor.url", "java.version", "java.class.version", "os.name", "os.arch", // "os.version", "file.separator", "path.separator", "line.separator" //, "browser" }; sysProps = new Properties(); String key; String value; for (int i = 0; i < keys.length; i++) { key = keys[i]; value = System.getProperty(key); sysProps.put(key, value); } } // sysProps.list(System.err); } public void setPlatForm() { getProps(); if (sysProps.getProperty("os.name").indexOf("Win") >= 0) { Win32 = true; if (sysProps.getProperty("os.name").indexOf("NT") >= 0) { WinNT = true; } ProteusInterface.setParameter(ProteusInterface.PLATFORM, "WIN32"); } else { ProteusInterface.setParameter(ProteusInterface.PLATFORM, "X11"); } } } class Password extends Panel implements ActionListener { ProteusApp applet; TextField userField; TextField passField; public Password(ProteusApp applet) { this.applet = applet; userField = new TextField(8); userField.setBackground(Color.white); passField = new TextField(8); passField.setBackground(Color.white); passField.setEchoChar('*'); passField.addActionListener(this); Button loginButton = new Button("Login"); loginButton.addActionListener(this); Panel panel = new Panel(); panel.setLayout(new JCAlignerLayout()); panel.add(new Label("UserID:")); panel.add(userField); panel.add(new Label("Password:")); panel.add(passField); panel.add(new Label(" ")); panel.add(loginButton); this.setLayout(new StackLayout()); this.add("Center", panel); } public void actionPerformed(ActionEvent e) { String user = userField.getText(); String pass = passField.getText(); userField.setText(""); passField.setText(""); applet.login(user, pass); } }