Has anyone using the Mozilla Webclient Java interface to embedding tried
setting a proxy server via the org.mozilla.webclient.Preferences interface?
I'm trying something along the lines of the following:

    // prefs is a reference to org.mozilla.webclient.Preferences
    prefs.setPref("network.proxy.type", "1");
    prefs.setPref("network.proxy.http", "proxy.somehost.net");
    prefs.setPref("network.proxy.http_port", "8080");

What seems to happen is that the type and port are set just fine, but the
host setting does not take. Sometimes it works the first time, but if I then
change it while Webclient is running, it does not use the new proxy host.

I know cross-posting is a no no, but I think the problem is occurring below
the Java Webclient interface. I used Java because that's what I'm familiar
with. A similar issue seemed to occur in May 2000, but I did not see any
responses to it (see
http://groups.google.com/groups?q=mozilla+embedding+proxy&hl=en&ie=ISO-8859-
1&oe=ISO-8859-1&selm=8gn6di%24ppu1%40secnews.netscape.com&rnum=2   the
message id is <8gn6di$[EMAIL PROTECTED]>#1/1 and the post was sent
by [EMAIL PROTECTED] to the netscape.public.mozilla.embedding
newsgroup).

I'm using Webclient 1.1 with Netscape 6.1 with JDK 1.3.1 on Windows 2000. I
know this is far from the latest version of the browser and it's not even a
Mozilla.org build, but this is the version recommended for use with
Webclient andthe Mozilla equivalent of NS 6.1 does not seem to work). If
this problem was corrected with a later version, I would implore the
Webclient team to release a new build that works with a later version of
Mozilla.

I've included some sample code that lets you play with the settings. If you
run the application, enter new settings in the rightmost column of the table
and press the button. The middle column should then show the new settings.
Also be sure to enter only integers for the proxy type and port; you'll get
an exception otherwise. If it works the way it did on my setup, the type and
port will take hold, but the hostname will not. Even stranger, if you click
on "network.proxy.http" (in the leftmost column) after you've set new
values, you'll get a weird string that includes the new value. This probably
happens when the TableCellRenderer redraws, but that value is very strange,
given that the only value I ever put in that cell was a literal string.
Could the proxy host be added as a key instead?

Thanks in advance for wading through this lengthy message,
-Michael Epstein

----------------------------------------------------------------------------
------------

/*
 * MozProxyPrefsTester.java
 *
 * Created on March 15, 2002, 2:54 PM
 */

import java.awt.*;
import java.awt.event.*;

import java.io.FileNotFoundException;

import java.util.Properties;

import javax.swing.table.TableModel;

import org.mozilla.webclient.*;

public class MozProxyPrefsTester extends javax.swing.JFrame {

    private static final int CURRENT_SETTINGS_COL = 1,
                             NEW_SETTINGS_COL = 2,
                             TYPE_ROW = 0,
                             NAME_ROW = 1,
                             PORT_ROW = 2;

    /** Creates new form MozProxyPrefsTester */
    public MozProxyPrefsTester(String mozillaFiveHome)
           throws ClassNotFoundException, FileNotFoundException,
                  IllegalAccessException, InstantiationException {
        BrowserControlFactory.setAppData(mozillaFiveHome);
        moz = BrowserControlFactory.newBrowserControl();
        navigator = (Navigation)
moz.queryInterface(BrowserControl.NAVIGATION_NAME);
        prefs = (Preferences)
moz.queryInterface(BrowserControl.PREFERENCES_NAME);

        browserWin = new Frame("Simple Browser");
        browserWin.setLayout(new BorderLayout());
        browserWin.add(((Component) moz.queryInterface(
                        BrowserControl.BROWSER_CONTROL_CANVAS_NAME)),
                        BorderLayout.CENTER);
        browserWin.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                shutdown();
            }
        });
/* Uncommenting the follwoing lines will add a navigation bar to the browser
 * window. Since displaying a browser in a window other than this class
 * (which is a subclass of Frame) seems to cause an
 * org.mozilla.util.AssertionFailureException, this window is not shown, so
 * this section is not used and thus commented out.
 */
/*
        Panel navPanel = new Panel();
        navPanel.add(new Label("Location: "));
        browserWin.add(navPanel, BorderLayout.NORTH);

        navField = new TextField(30);
        navField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                navigator.loadURL(navField.getText());
            }
        });
        navPanel.add(navField);
*/
        initComponents();
        browserWin.show();
/* Navigating to a page in this window causes an exception (see comment
above),
 * so since we don't need to show it to test preferences, we'll hide to keep
it
 * out of trouble.
 */
browserWin.hide();
        getProxySettings();
        this.show();
    }

    private void getProxySettings() {
        Properties mozPrefs = prefs.getPrefs();
        TableModel model = prefsTable.getModel();
        model.setValueAt(mozPrefs.getProperty("network.proxy.type"),
TYPE_ROW, CURRENT_SETTINGS_COL);
        model.setValueAt(mozPrefs.getProperty("network.proxy.http"),
NAME_ROW, CURRENT_SETTINGS_COL);
        model.setValueAt(mozPrefs.getProperty("network.proxy.http_port"),
PORT_ROW, CURRENT_SETTINGS_COL);
    } // getProxySettings

    private void setProxySettings() {
        prefsTable.getCellEditor().stopCellEditing(); // finish an editing
session if one is in progress
        TableModel model = prefsTable.getModel();
        prefs.setPref("network.proxy.type",
String.valueOf(model.getValueAt(TYPE_ROW, NEW_SETTINGS_COL)));
        model.setValueAt("", TYPE_ROW, NEW_SETTINGS_COL);
        prefs.setPref("network.proxy.http",
String.valueOf(model.getValueAt(NAME_ROW, NEW_SETTINGS_COL)));
        model.setValueAt("", NAME_ROW, NEW_SETTINGS_COL);
        prefs.setPref("network.proxy.http_port",
String.valueOf(model.getValueAt(PORT_ROW, NEW_SETTINGS_COL)));
        model.setValueAt("", PORT_ROW, NEW_SETTINGS_COL);
        getProxySettings();
    } // setProxySettings

    private void shutdown() {
        try {
            this.hide();
            this.dispose();
            browserWin.hide();
            browserWin.dispose();
            BrowserControlFactory.deleteBrowserControl(moz);
            System.exit(0);
        } // try
        catch (Exception e) {
            System.exit(1);
        }
    } // shutdown

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {
            infoPanel = new javax.swing.JPanel();
            prefsTableScroller = new javax.swing.JScrollPane();
            prefsTable = new javax.swing.JTable();
            applySettingsButton = new javax.swing.JButton();

            setTitle("Mozilla Proxy Preferences Tester");

setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
            addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowClosing(java.awt.event.WindowEvent evt) {
                    exitForm(evt);
                }
            });

            infoPanel.setLayout(new java.awt.GridBagLayout());
            java.awt.GridBagConstraints gridBagConstraints1;

            prefsTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"network.proxy.type", null, null},
                {"network.proxy.http", null, null},
                {"network.proxy.http_port", null, null}
            },
            new String [] {
                "Property", "Current Value", "New Value"
            }
            ) {
                boolean[] canEdit = new boolean [] {
                    false, false, true
                };

                public boolean isCellEditable(int rowIndex, int columnIndex)
{
                    return canEdit [columnIndex];
                }
            });
            prefsTableScroller.setViewportView(prefsTable);

            gridBagConstraints1 = new java.awt.GridBagConstraints();
          gridBagConstraints1.gridx = 0;
          gridBagConstraints1.gridy = 0;
          gridBagConstraints1.insets = new java.awt.Insets(0, 0, 3, 0);
          infoPanel.add(prefsTableScroller, gridBagConstraints1);

          applySettingsButton.setText("Apply New Settings");
          applySettingsButton.addActionListener(new
java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  applySettingsButtonActionPerformed(evt);
              }
          });

          gridBagConstraints1 = new java.awt.GridBagConstraints();
          gridBagConstraints1.gridx = 0;
          gridBagConstraints1.gridy = 1;
          gridBagConstraints1.insets = new java.awt.Insets(3, 0, 0, 0);
          infoPanel.add(applySettingsButton, gridBagConstraints1);

          getContentPane().add(infoPanel, java.awt.BorderLayout.CENTER);

        pack();
    }

    private void
applySettingsButtonActionPerformed(java.awt.event.ActionEvent evt) {
        setProxySettings();
    }

    private void exitForm(java.awt.event.WindowEvent evt) {
        shutdown();
    }

    public static void main(String args[]) {
        try {
            if (args.length > 0) {
                new MozProxyPrefsTester(args[0]);
            }
            else {
                new MozProxyPrefsTester("C:\\PROGRA~1\\Netscape\\NETSCA~1");
            }
        } // try
        catch (Exception e) {
            e.printStackTrace();
        }
    }


    // Variables declaration - do not modify
    private javax.swing.JPanel infoPanel;
    private javax.swing.JScrollPane prefsTableScroller;
    private javax.swing.JTable prefsTable;
    private javax.swing.JButton applySettingsButton;
    // End of variables declaration

    private Frame browserWin;
    private BrowserControl moz;
    private TextField navField;
    private Navigation navigator;
    private Preferences prefs;
}

Reply via email to