CustomProxySelector: auto proxy (PAC) needs login info too

This commit is contained in:
Federico Fissore 2015-08-07 16:51:58 +02:00
parent 916de939e6
commit 0c7e0d6bb4
5 changed files with 158 additions and 41 deletions

View File

@ -483,25 +483,39 @@
<Group type="102" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace min="12" pref="12" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="autoProxyUsePAC" alignment="0" min="-2" max="-2" attributes="0"/>
<Group type="102" alignment="0" attributes="0">
<EmptySpace min="12" pref="12" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="autoProxyUsernameLabel" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="autoProxyPasswordLabel" alignment="0" min="-2" max="-2" attributes="0"/>
</Group>
</Group>
</Group>
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="autoProxyPACURL" max="32767" attributes="0"/>
<Group type="102" attributes="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="autoProxyUsername" min="-2" pref="178" max="-2" attributes="0"/>
<Component id="autoProxyPassword" min="-2" pref="180" max="-2" attributes="0"/>
</Group>
<EmptySpace min="0" pref="0" max="32767" attributes="0"/>
</Group>
</Group>
</Group>
<Group type="102" attributes="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="noProxy" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="autoProxy" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="manualProxy" alignment="0" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace min="0" pref="0" max="32767" attributes="0"/>
</Group>
<Group type="102" alignment="0" attributes="0">
<EmptySpace min="12" pref="12" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<Component id="autoProxyUsePAC" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="autoProxyPACURL" max="32767" attributes="0"/>
</Group>
<Group type="102" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace min="12" pref="12" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<Group type="102" alignment="0" attributes="0">
<Component id="manualProxyHTTP" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="manualProxySOCKS" min="-2" max="-2" attributes="0"/>
@ -518,15 +532,15 @@
<Component id="manualProxyHostName" min="-2" pref="541" max="-2" attributes="0"/>
<Component id="manualProxyPort" alignment="0" min="-2" pref="74" max="-2" attributes="0"/>
<Group type="103" alignment="0" groupAlignment="1" max="-2" attributes="0">
<Component id="manualProxyPassword" alignment="0" pref="180" max="32767" attributes="0"/>
<Component id="manualProxyUsername" alignment="0" max="32767" attributes="0"/>
<Component id="manualProxyPassword" alignment="0" max="32767" attributes="0"/>
<Component id="manualProxyUsername" alignment="0" min="-2" pref="180" max="-2" attributes="0"/>
</Group>
</Group>
</Group>
</Group>
<EmptySpace min="0" pref="0" max="32767" attributes="0"/>
</Group>
</Group>
<EmptySpace min="0" pref="0" max="32767" attributes="0"/>
</Group>
</Group>
<EmptySpace max="-2" attributes="0"/>
@ -546,6 +560,16 @@
<Component id="autoProxyPACURL" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="autoProxyUsername" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="autoProxyUsernameLabel" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="autoProxyPasswordLabel" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="autoProxyPassword" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
<Component id="manualProxy" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
@ -572,7 +596,7 @@
<Component id="manualProxyPassword" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="manualProxyPasswordLabel" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace pref="225" max="32767" attributes="0"/>
<EmptySpace pref="163" max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
@ -587,7 +611,7 @@
<Connection code="tr(&quot;No proxy&quot;)" type="code"/>
</Property>
<Property name="actionCommand" type="java.lang.String" editor="org.netbeans.modules.form.RADConnectionPropertyEditor">
<Connection code="PROXY_TYPE_NONE" type="code"/>
<Connection code="Constants.PROXY_TYPE_NONE" type="code"/>
</Property>
</Properties>
</Component>
@ -600,7 +624,7 @@
<Connection code="tr(&quot;Auto-detect proxy settings&quot;)" type="code"/>
</Property>
<Property name="actionCommand" type="java.lang.String" editor="org.netbeans.modules.form.RADConnectionPropertyEditor">
<Connection code="PROXY_TYPE_AUTO" type="code"/>
<Connection code="Constants.PROXY_TYPE_AUTO" type="code"/>
</Property>
</Properties>
<Events>
@ -616,7 +640,7 @@
<Connection code="tr(&quot;Manual proxy configuration&quot;)" type="code"/>
</Property>
<Property name="actionCommand" type="java.lang.String" editor="org.netbeans.modules.form.RADConnectionPropertyEditor">
<Connection code="PROXY_TYPE_MANUAL" type="code"/>
<Connection code="Constants.PROXY_TYPE_MANUAL" type="code"/>
</Property>
</Properties>
<Events>
@ -642,7 +666,7 @@
</Property>
<Property name="text" type="java.lang.String" value="HTTP"/>
<Property name="actionCommand" type="java.lang.String" editor="org.netbeans.modules.form.RADConnectionPropertyEditor">
<Connection code="PROXY_MANUAL_TYPE_HTTP" type="code"/>
<Connection code="Constants.PROXY_MANUAL_TYPE_HTTP" type="code"/>
</Property>
</Properties>
</Component>
@ -653,7 +677,7 @@
</Property>
<Property name="text" type="java.lang.String" value="SOCKS"/>
<Property name="actionCommand" type="java.lang.String" editor="org.netbeans.modules.form.RADConnectionPropertyEditor">
<Connection code="PROXY_MANUAL_TYPE_SOCKS" type="code"/>
<Connection code="Constants.PROXY_MANUAL_TYPE_SOCKS" type="code"/>
</Property>
</Properties>
</Component>
@ -696,6 +720,27 @@
<Property name="toolTipText" type="java.lang.String" value=""/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="autoProxyUsernameLabel">
<Properties>
<Property name="text" type="java.lang.String" editor="org.netbeans.modules.form.RADConnectionPropertyEditor">
<Connection code="tr(&quot;Username:&quot;)" type="code"/>
</Property>
</Properties>
</Component>
<Component class="javax.swing.JTextField" name="autoProxyUsername">
</Component>
<Component class="javax.swing.JPasswordField" name="autoProxyPassword">
<Properties>
<Property name="toolTipText" type="java.lang.String" value=""/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="autoProxyPasswordLabel">
<Properties>
<Property name="text" type="java.lang.String" editor="org.netbeans.modules.form.RADConnectionPropertyEditor">
<Connection code="tr(&quot;Password:&quot;)" type="code"/>
</Property>
</Properties>
</Component>
</SubComponents>
</Container>
</SubComponents>

View File

@ -252,6 +252,10 @@ public class Preferences extends javax.swing.JDialog {
manualProxyUsername = new javax.swing.JTextField();
manualProxyPasswordLabel = new javax.swing.JLabel();
manualProxyPassword = new javax.swing.JPasswordField();
autoProxyUsernameLabel = new javax.swing.JLabel();
autoProxyUsername = new javax.swing.JTextField();
autoProxyPassword = new javax.swing.JPasswordField();
autoProxyPasswordLabel = new javax.swing.JLabel();
javax.swing.JPanel jPanel3 = new javax.swing.JPanel();
javax.swing.JButton okButton = new javax.swing.JButton();
javax.swing.JButton cancelButton = new javax.swing.JButton();
@ -492,6 +496,12 @@ public class Preferences extends javax.swing.JDialog {
manualProxyPassword.setToolTipText("");
autoProxyUsernameLabel.setText(tr("Username:"));
autoProxyPassword.setToolTipText("");
autoProxyPasswordLabel.setText(tr("Password:"));
javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
jPanel4.setLayout(jPanel4Layout);
jPanel4Layout.setHorizontalGroup(
@ -499,20 +509,30 @@ public class Preferences extends javax.swing.JDialog {
.addGroup(jPanel4Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addGap(12, 12, 12)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(autoProxyUsePAC)
.addGroup(jPanel4Layout.createSequentialGroup()
.addGap(12, 12, 12)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(autoProxyUsernameLabel)
.addComponent(autoProxyPasswordLabel))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(autoProxyPACURL)
.addGroup(jPanel4Layout.createSequentialGroup()
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(autoProxyUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(autoProxyPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 0, Short.MAX_VALUE))))
.addGroup(jPanel4Layout.createSequentialGroup()
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(noProxy)
.addComponent(autoProxy)
.addComponent(manualProxy))
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(jPanel4Layout.createSequentialGroup()
.addGap(12, 12, 12)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addComponent(autoProxyUsePAC)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(autoProxyPACURL))
.addComponent(manualProxy)
.addGroup(jPanel4Layout.createSequentialGroup()
.addGap(12, 12, 12)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addComponent(manualProxyHTTP)
@ -529,9 +549,9 @@ public class Preferences extends javax.swing.JDialog {
.addComponent(manualProxyHostName, javax.swing.GroupLayout.PREFERRED_SIZE, 541, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(manualProxyPort, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(manualProxyPassword, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
.addComponent(manualProxyUsername, javax.swing.GroupLayout.Alignment.LEADING)))))
.addGap(0, 0, Short.MAX_VALUE)))))
.addComponent(manualProxyPassword, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(manualProxyUsername, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)))))))
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
jPanel4Layout.setVerticalGroup(
@ -546,6 +566,14 @@ public class Preferences extends javax.swing.JDialog {
.addComponent(autoProxyUsePAC)
.addComponent(autoProxyPACURL, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(autoProxyUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(autoProxyUsernameLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(autoProxyPasswordLabel)
.addComponent(autoProxyPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(manualProxy)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
@ -567,7 +595,7 @@ public class Preferences extends javax.swing.JDialog {
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(manualProxyPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(manualProxyPasswordLabel))
.addContainerGap(225, Short.MAX_VALUE))
.addContainerGap(163, Short.MAX_VALUE))
);
jTabbedPane1.addTab(tr("Network"), jPanel4);
@ -690,14 +718,18 @@ public class Preferences extends javax.swing.JDialog {
}//GEN-LAST:event_manualProxyItemStateChanged
private void autoProxyUsePACItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_autoProxyUsePACItemStateChanged
autoProxyPACURL.setEnabled(autoProxyUsePAC.isSelected());
autoProxyPACFieldsSetEnabled(autoProxyUsePAC.isSelected());
}//GEN-LAST:event_autoProxyUsePACItemStateChanged
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTextField additionalBoardsManagerField;
private javax.swing.JRadioButton autoProxy;
private javax.swing.JTextField autoProxyPACURL;
private javax.swing.JPasswordField autoProxyPassword;
private javax.swing.JLabel autoProxyPasswordLabel;
private javax.swing.JCheckBox autoProxyUsePAC;
private javax.swing.JTextField autoProxyUsername;
private javax.swing.JLabel autoProxyUsernameLabel;
private javax.swing.JCheckBox checkUpdatesBox;
private javax.swing.JComboBox comboLanguage;
private javax.swing.JComboBox comboWarnings;
@ -797,6 +829,8 @@ public class Preferences extends javax.swing.JDialog {
PreferencesData.set(Constants.PREF_PROXY_MANUAL_PORT, manualProxyPort.getText());
PreferencesData.set(Constants.PREF_PROXY_MANUAL_USERNAME, manualProxyUsername.getText());
PreferencesData.set(Constants.PREF_PROXY_MANUAL_PASSWORD, String.valueOf(manualProxyPassword.getPassword()));
PreferencesData.set(Constants.PREF_PROXY_AUTO_USERNAME, autoProxyUsername.getText());
PreferencesData.set(Constants.PREF_PROXY_AUTO_PASSWORD, String.valueOf(autoProxyPassword.getPassword()));
}
private void showPrerefencesData() {
@ -849,6 +883,8 @@ public class Preferences extends javax.swing.JDialog {
if (!PreferencesData.get(Constants.PREF_PROXY_PAC_URL, "").isEmpty()) {
autoProxyUsePAC.setSelected(true);
autoProxyPACURL.setText(PreferencesData.get(Constants.PREF_PROXY_PAC_URL));
autoProxyUsername.setText(PreferencesData.get(Constants.PREF_PROXY_AUTO_USERNAME));
autoProxyPassword.setText(PreferencesData.get(Constants.PREF_PROXY_AUTO_PASSWORD));
}
} else {
manualProxy.setSelected(true);
@ -879,7 +915,15 @@ public class Preferences extends javax.swing.JDialog {
private void autoProxyFieldsSetEnabled(boolean enabled) {
autoProxyUsePAC.setEnabled(enabled);
autoProxyPACURL.setEnabled(enabled && autoProxyUsePAC.isSelected());
autoProxyPACFieldsSetEnabled(enabled && autoProxyUsePAC.isSelected());
}
private void autoProxyPACFieldsSetEnabled(boolean enabled) {
autoProxyPACURL.setEnabled(enabled);
autoProxyUsername.setEnabled(enabled);
autoProxyUsernameLabel.setEnabled(enabled);
autoProxyPassword.setEnabled(enabled);
autoProxyPasswordLabel.setEnabled(enabled);
}
private void disableAllProxyFields() {

View File

@ -50,6 +50,22 @@ public class CustomProxySelectorTest {
assertEquals(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 8080)), proxy);
}
@Test
public void testProxyPACHTTPWithLogin() throws Exception {
preferences.put(Constants.PREF_PROXY_TYPE, Constants.PROXY_TYPE_AUTO);
preferences.put(Constants.PREF_PROXY_PAC_URL, CustomProxySelectorTest.class.getResource("proxy_http.pac").toExternalForm());
preferences.put(Constants.PREF_PROXY_AUTO_USERNAME, "auto");
preferences.put(Constants.PREF_PROXY_AUTO_PASSWORD, "autopassword");
CustomProxySelector proxySelector = new CustomProxySelector(preferences);
Proxy proxy = proxySelector.getProxyFor(uri);
assertEquals(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 8080)), proxy);
PasswordAuthentication authentication = Authenticator.requestPasswordAuthentication(null, 8080, uri.toURL().getProtocol(), "ciao", "");
assertEquals(authentication.getUserName(), "auto");
assertEquals(String.valueOf(authentication.getPassword()), "autopassword");
}
@Test
public void testProxyPACSOCKS() throws Exception {
preferences.put(Constants.PREF_PROXY_TYPE, Constants.PROXY_TYPE_AUTO);

View File

@ -58,6 +58,8 @@ public class Constants {
public static final String PREF_PROXY_MANUAL_PORT = "proxy.manual.port";
public static final String PREF_PROXY_MANUAL_USERNAME = "proxy.manual.username";
public static final String PREF_PROXY_MANUAL_PASSWORD = "proxy.manual.password";
public static final String PREF_PROXY_AUTO_USERNAME = "proxy.manual.username";
public static final String PREF_PROXY_AUTO_PASSWORD = "proxy.manual.password";
public static final String PACKAGE_INDEX_URL;
public static final String LIBRARY_INDEX_URL;

View File

@ -43,6 +43,8 @@ public class CustomProxySelector {
}
private Proxy pacProxy(String pac, URI uri) throws IOException, ScriptException, NoSuchMethodException {
setAuthenticator(preferences.get(Constants.PREF_PROXY_AUTO_USERNAME), preferences.get(Constants.PREF_PROXY_AUTO_PASSWORD));
URLConnection urlConnection = new URL(pac).openConnection();
urlConnection.connect();
if (urlConnection instanceof HttpURLConnection) {
@ -90,7 +92,7 @@ public class CustomProxySelector {
}
private Proxy manualProxy() {
setAuthenticator();
setAuthenticator(preferences.get(Constants.PREF_PROXY_MANUAL_USERNAME), preferences.get(Constants.PREF_PROXY_MANUAL_PASSWORD));
Proxy.Type type = Proxy.Type.valueOf(preferences.get(Constants.PREF_PROXY_MANUAL_TYPE));
return new Proxy(type, new InetSocketAddress(preferences.get(Constants.PREF_PROXY_MANUAL_HOSTNAME), Integer.valueOf(preferences.get(Constants.PREF_PROXY_MANUAL_PORT))));
}
@ -99,12 +101,20 @@ public class CustomProxySelector {
Authenticator.setDefault(null);
}
private void setAuthenticator() {
private void setAuthenticator(String username, String password) {
if (username == null || username.isEmpty()) {
return;
}
String actualPassword;
if (password == null) {
actualPassword = "";
} else {
actualPassword = password;
}
Authenticator.setDefault(
new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
preferences.get(Constants.PREF_PROXY_MANUAL_USERNAME), preferences.get(Constants.PREF_PROXY_MANUAL_PASSWORD).toCharArray());
return new PasswordAuthentication(username, actualPassword.toCharArray());
}
}
);