summaryrefslogtreecommitdiff
path: root/platform/platform-api/src/com/intellij/util/net/HTTPProxySettingsPanel.java
blob: 46ea422a3f69e4b504a75bae673810c195e6afd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.util.net;

import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.SearchableConfigurable;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.MultiLineLabelUI;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.wm.IdeFocusManager;
import com.intellij.openapi.wm.IdeFrame;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBRadioButton;
import com.intellij.util.proxy.CommonProxy;
import com.intellij.util.proxy.JavaProxyProperty;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.concurrent.atomic.AtomicReference;

/**
 * Created by IntelliJ IDEA.
 * User: stathik
 * Date: Aug 28, 2003
 * Time: 3:52:47 PM
 * To change this template use Options | File Templates.
 */
public class HTTPProxySettingsPanel implements SearchableConfigurable, Configurable.NoScroll {
  public static final String NAME = "Proxy";
  private JPanel myMainPanel;

  private JTextField myProxyLoginTextField;
  private JPasswordField myProxyPasswordTextField;
  private JCheckBox myProxyAuthCheckBox;
  private JTextField myProxyPortTextField;
  private JTextField myProxyHostTextField;
  private JCheckBox myRememberProxyPasswordCheckBox;

  private JLabel myProxyLoginLabel;
  private JLabel myProxyPasswordLabel;
  private JLabel myHostNameLabel;
  private JLabel myPortNumberLabel;
  private JBRadioButton myAutoDetectProxyRb;
  private JBRadioButton myUseHTTPProxyRb;
  private JBLabel mySystemProxyDefined;
  private JBRadioButton myNoProxyRb;
  private JBRadioButton myHTTP;
  private JBRadioButton mySocks;
  private JButton myClearPasswordsButton;
  private JLabel myErrorLabel;
  private JButton myCheckButton;
  private JBLabel myOtherWarning;
  private JLabel myProxyExceptionsLabel;
  private JTextArea myProxyExceptions;
  private JLabel myNoProxyForLabel;
  private JCheckBox myPacUrlCheckBox;
  private JTextField myPacUrlTextField;
  private final HttpConfigurable myHttpConfigurable;
  private volatile boolean myConnectionCheckInProgress;

  public boolean isModified() {
    boolean isModified = false;
    HttpConfigurable httpConfigurable = myHttpConfigurable;
    if (! Comparing.equal(myProxyExceptions.getText().trim(), httpConfigurable.PROXY_EXCEPTIONS)) return true;
    isModified |= httpConfigurable.USE_PROXY_PAC != myAutoDetectProxyRb.isSelected();
    isModified |= httpConfigurable.USE_PAC_URL != myPacUrlCheckBox.isSelected();
    isModified |= !Comparing.strEqual(httpConfigurable.PAC_URL, myPacUrlTextField.getText());
    isModified |= httpConfigurable.USE_HTTP_PROXY != myUseHTTPProxyRb.isSelected();
    isModified |= httpConfigurable.PROXY_AUTHENTICATION != myProxyAuthCheckBox.isSelected();
    isModified |= httpConfigurable.KEEP_PROXY_PASSWORD != myRememberProxyPasswordCheckBox.isSelected();
    isModified |= httpConfigurable.PROXY_TYPE_IS_SOCKS != mySocks.isSelected();

    isModified |= !Comparing.strEqual(httpConfigurable.PROXY_LOGIN, myProxyLoginTextField.getText());
    isModified |= !Comparing.strEqual(httpConfigurable.getPlainProxyPassword(),new String (myProxyPasswordTextField.getPassword()));

    try {
      isModified |= httpConfigurable.PROXY_PORT != Integer.valueOf(myProxyPortTextField.getText()).intValue();
    } catch (NumberFormatException e) {
      isModified = true;
    }
    isModified |= !Comparing.strEqual(httpConfigurable.PROXY_HOST, myProxyHostTextField.getText());
    return isModified;
  }

  public HTTPProxySettingsPanel(final HttpConfigurable httpConfigurable) {
    final ButtonGroup group = new ButtonGroup();
    group.add(myUseHTTPProxyRb);
    group.add(myAutoDetectProxyRb);
    group.add(myNoProxyRb);
    myNoProxyRb.setSelected(true);

    final ButtonGroup proxyTypeGroup = new ButtonGroup();
    proxyTypeGroup.add(myHTTP);
    proxyTypeGroup.add(mySocks);
    myHTTP.setSelected(true);

    myProxyExceptions.setBorder(UIUtil.getTextFieldBorder());

    final Boolean property = Boolean.getBoolean(JavaProxyProperty.USE_SYSTEM_PROXY);
    mySystemProxyDefined.setVisible(Boolean.TRUE.equals(property));
    if (Boolean.TRUE.equals(property)) {
      mySystemProxyDefined.setIcon(Messages.getWarningIcon());
      mySystemProxyDefined.setFont(mySystemProxyDefined.getFont().deriveFont(Font.BOLD));
      mySystemProxyDefined.setUI(new MultiLineLabelUI());
    }

    myProxyAuthCheckBox.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        enableProxyAuthentication(myProxyAuthCheckBox.isSelected());
      }
    });
    myPacUrlCheckBox.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        myPacUrlTextField.setEnabled(myPacUrlCheckBox.isSelected());
      }
    });

    final ActionListener listener = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        enableProxy(myUseHTTPProxyRb.isSelected());
      }
    };
    myUseHTTPProxyRb.addActionListener(listener);
    myAutoDetectProxyRb.addActionListener(listener);
    myNoProxyRb.addActionListener(listener);
    myHttpConfigurable = httpConfigurable;

    myClearPasswordsButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        myHttpConfigurable.clearGenericPasswords();
        Messages.showMessageDialog(myMainPanel, "Proxy passwords were cleared.", "Auto-detected proxy", Messages.getInformationIcon());
      }
    });

    if (HttpConfigurable.getInstance() != null) {
      myCheckButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
          final String title = "Check Proxy Settings";
          final String answer = Messages
            .showInputDialog(myMainPanel, "Warning: your settings will be saved.\n\nEnter any URL to check connection to:",
                             title, Messages.getQuestionIcon(), "http://", null);
          if (! StringUtil.isEmptyOrSpaces(answer)) {
            apply();
            final HttpConfigurable instance = HttpConfigurable.getInstance();
            final AtomicReference<IOException> exc = new AtomicReference<IOException>();
            myCheckButton.setEnabled(false);
            myCheckButton.setText("Check connection (in progress...)");
            myConnectionCheckInProgress = true;
            final Application application = ApplicationManager.getApplication();
            application.executeOnPooledThread(new Runnable() {
              @Override
              public void run() {
                HttpURLConnection connection = null;
                try {
                  //already checked for null above
                  //noinspection ConstantConditions
                  connection = instance.openHttpConnection(answer);
                  connection.setReadTimeout(3 * 1000);
                  connection.setConnectTimeout(3 * 1000);
                  connection.connect();
                  final int code = connection.getResponseCode();
                  if (HttpURLConnection.HTTP_OK != code) {
                    exc.set(new IOException("Error code: " + code));
                  }
                }
                catch (IOException e1) {
                  exc.set(e1);
                }
                finally {
                  if (connection != null) {
                    connection.disconnect();
                  }
                }
                //noinspection SSBasedInspection
                SwingUtilities.invokeLater(new Runnable() {
                  @Override
                  public void run() {
                    myConnectionCheckInProgress = false;
                    reset();  // since password might have been set
                    Component parent = null;
                    if (myMainPanel.isShowing()) {
                      parent = myMainPanel;
                      myCheckButton.setText("Check connection");
                      myCheckButton.setEnabled(canEnableConnectionCheck());
                    } else {
                      final IdeFrame frame = IdeFocusManager.findInstance().getLastFocusedFrame();
                      if (frame == null) {
                        return;
                      }
                      parent = frame.getComponent();
                    }
                    //noinspection ThrowableResultOfMethodCallIgnored
                    final IOException exception = exc.get();
                    if (exception == null) {
                      Messages.showMessageDialog(parent, "Connection successful", title, Messages.getInformationIcon());
                    }
                    else {
                      final String message = exception.getMessage();
                      if (instance.USE_HTTP_PROXY) {
                        instance.LAST_ERROR = message;
                      }
                      Messages.showErrorDialog(parent, errorText(message));
                    }
                  }
                });
              }
            });
          }
        }
      });
    } else {
      myCheckButton.setVisible(false);
    }
  }

  private boolean canEnableConnectionCheck() {
    return ! myNoProxyRb.isSelected() && ! myConnectionCheckInProgress;
  }

  public void reset() {
    myNoProxyRb.setSelected(true);  // default
    HttpConfigurable httpConfigurable = myHttpConfigurable;
    myAutoDetectProxyRb.setSelected(httpConfigurable.USE_PROXY_PAC);
    myPacUrlCheckBox.setSelected(httpConfigurable.USE_PAC_URL);
    myPacUrlTextField.setText(httpConfigurable.PAC_URL);
    myUseHTTPProxyRb.setSelected(httpConfigurable.USE_HTTP_PROXY);
    myProxyAuthCheckBox.setSelected(httpConfigurable.PROXY_AUTHENTICATION);

    enableProxy(httpConfigurable.USE_HTTP_PROXY);

    myProxyLoginTextField.setText(httpConfigurable.PROXY_LOGIN);
    myProxyPasswordTextField.setText(httpConfigurable.getPlainProxyPassword());

    myProxyPortTextField.setText(Integer.toString(httpConfigurable.PROXY_PORT));
    myProxyHostTextField.setText(httpConfigurable.PROXY_HOST);
    myProxyExceptions.setText(httpConfigurable.PROXY_EXCEPTIONS);

    myRememberProxyPasswordCheckBox.setSelected(httpConfigurable.KEEP_PROXY_PASSWORD);
    mySocks.setSelected(httpConfigurable.PROXY_TYPE_IS_SOCKS);
    myHTTP.setSelected(!httpConfigurable.PROXY_TYPE_IS_SOCKS);

    final boolean showError = !StringUtil.isEmptyOrSpaces(httpConfigurable.LAST_ERROR);
    myErrorLabel.setVisible(showError);
    myErrorLabel.setText(showError ? errorText(httpConfigurable.LAST_ERROR) : "");

    final String oldStyleText = CommonProxy.getMessageFromProps(CommonProxy.getOldStyleProperties());
    myOtherWarning.setVisible(oldStyleText != null);
    if (oldStyleText != null) {
      myOtherWarning.setText(oldStyleText);
      myOtherWarning.setUI(new MultiLineLabelUI());
      myOtherWarning.setIcon(Messages.getWarningIcon());
    }
  }

  private String errorText(final String s) {
    return "Problem with connection: " + s;
  }

  public void apply () {
    HttpConfigurable httpConfigurable = myHttpConfigurable;
    if (isModified()){
      httpConfigurable.AUTHENTICATION_CANCELLED = false;
    }
    httpConfigurable.USE_PROXY_PAC = myAutoDetectProxyRb.isSelected();
    httpConfigurable.USE_PAC_URL = myPacUrlCheckBox.isSelected();
    httpConfigurable.PAC_URL = trimFieldText(myPacUrlTextField);
    httpConfigurable.USE_HTTP_PROXY = myUseHTTPProxyRb.isSelected();
    httpConfigurable.PROXY_TYPE_IS_SOCKS = mySocks.isSelected();
    httpConfigurable.PROXY_AUTHENTICATION = myProxyAuthCheckBox.isSelected();
    httpConfigurable.KEEP_PROXY_PASSWORD = myRememberProxyPasswordCheckBox.isSelected();

    httpConfigurable.PROXY_LOGIN = trimFieldText(myProxyLoginTextField);
    httpConfigurable.setPlainProxyPassword(new String(myProxyPasswordTextField.getPassword()));
    httpConfigurable.PROXY_EXCEPTIONS = myProxyExceptions.getText();

    try {
      httpConfigurable.PROXY_PORT = Integer.valueOf(trimFieldText(myProxyPortTextField)).intValue();
    } catch (NumberFormatException e) {
      httpConfigurable.PROXY_PORT = 80;
    }
    httpConfigurable.PROXY_HOST = trimFieldText(myProxyHostTextField);
  }

  private static String trimFieldText(JTextField field) {
    String trimmed = field.getText().trim();
    field.setText(trimmed);
    return trimmed;
  }

  private void enableProxy (boolean enabled) {
    myHostNameLabel.setEnabled(enabled);
    myPortNumberLabel.setEnabled(enabled);
    myProxyHostTextField.setEnabled(enabled);
    myProxyPortTextField.setEnabled(enabled);
    mySocks.setEnabled(enabled);
    myHTTP.setEnabled(enabled);
    myProxyExceptions.setEnabled(enabled);
    myProxyExceptions.setBackground(myProxyPortTextField.getBackground());
    myProxyExceptionsLabel.setEnabled(enabled);
    myNoProxyForLabel.setEnabled(enabled);

    myProxyAuthCheckBox.setEnabled(enabled);
    enableProxyAuthentication(enabled && myProxyAuthCheckBox.isSelected());
    myCheckButton.setEnabled(canEnableConnectionCheck());

    final boolean autoDetectProxy = myAutoDetectProxyRb.isSelected();
    myPacUrlCheckBox.setEnabled(autoDetectProxy);
    myClearPasswordsButton.setEnabled(autoDetectProxy);
    myPacUrlTextField.setEnabled(autoDetectProxy && myPacUrlCheckBox.isSelected());
  }

  private void enableProxyAuthentication (boolean enabled) {
    myProxyPasswordLabel.setEnabled(enabled);
    myProxyLoginLabel.setEnabled(enabled);

    myProxyLoginTextField.setEnabled(enabled);
    myProxyPasswordTextField.setEnabled(enabled);

    myRememberProxyPasswordCheckBox.setEnabled(enabled);
  }

  public JComponent getComponent() {
    return myMainPanel;
  }

  public JComponent createComponent() {
    return myMainPanel;
  }

  @NotNull
  public String getId() {
    return getHelpTopic();
  }

  public Runnable enableSearch(final String option) {
    return null;
  }

  @Nls
  public String getDisplayName() {
    return NAME;
  }

  public String getHelpTopic() {
    return "http.proxy";
  }

  public void addActionListener(final ActionListener actionListener) {
    myProxyLoginTextField.addActionListener(actionListener);
    DocumentListener docListener = new DocumentListener() {
      public void insertUpdate(DocumentEvent e) {
        actionListener.actionPerformed(null);
      }

      public void removeUpdate(DocumentEvent e) {
        actionListener.actionPerformed(null);
      }

      public void changedUpdate(DocumentEvent e) {
        actionListener.actionPerformed(null);
      }
    };
    myProxyPasswordTextField.getDocument().addDocumentListener(docListener);
    myProxyAuthCheckBox.addActionListener(actionListener);
    myProxyPortTextField.getDocument().addDocumentListener(docListener);
    myProxyHostTextField.getDocument().addDocumentListener(docListener);
    myUseHTTPProxyRb.addActionListener(actionListener);
    myRememberProxyPasswordCheckBox.addActionListener(actionListener);

  }

  @Override
  public void disposeUIResources() {
  }
}