1
1
package org .jabref .gui .preferences ;
2
2
3
+ import java .net .MalformedURLException ;
3
4
import java .util .ArrayList ;
4
5
import java .util .List ;
5
6
import java .util .Optional ;
15
16
import org .jabref .logic .l10n .Localization ;
16
17
import org .jabref .logic .net .ProxyPreferences ;
17
18
import org .jabref .logic .net .ProxyRegisterer ;
19
+ import org .jabref .logic .net .URLDownload ;
18
20
import org .jabref .logic .remote .RemotePreferences ;
19
21
import org .jabref .logic .remote .RemoteUtil ;
20
22
import org .jabref .model .strings .StringUtil ;
25
27
import de .saxsys .mvvmfx .utils .validation .ValidationMessage ;
26
28
import de .saxsys .mvvmfx .utils .validation .ValidationStatus ;
27
29
import de .saxsys .mvvmfx .utils .validation .Validator ;
30
+ import kong .unirest .UnirestException ;
28
31
29
32
public class NetworkTabViewModel implements PreferenceTabViewModel {
30
33
private final BooleanProperty remoteServerProperty = new SimpleBooleanProperty ();
@@ -44,16 +47,16 @@ public class NetworkTabViewModel implements PreferenceTabViewModel {
44
47
45
48
private final DialogService dialogService ;
46
49
private final PreferencesService preferences ;
47
- private final RemotePreferences remotePreferences ;
48
- private final ProxyPreferences proxyPreferences ;
50
+ private final RemotePreferences initialRemotePreferences ;
51
+ private final ProxyPreferences initialProxyPreferences ;
49
52
50
53
private final List <String > restartWarning = new ArrayList <>();
51
54
52
55
public NetworkTabViewModel (DialogService dialogService , PreferencesService preferences ) {
53
56
this .dialogService = dialogService ;
54
57
this .preferences = preferences ;
55
- this .remotePreferences = preferences .getRemotePreferences ();
56
- this .proxyPreferences = preferences .getProxyPreferences ();
58
+ this .initialRemotePreferences = preferences .getRemotePreferences ();
59
+ this .initialProxyPreferences = preferences .getProxyPreferences ();
57
60
58
61
remotePortValidator = new FunctionBasedValidator <>(
59
62
remotePortProperty ,
@@ -104,15 +107,19 @@ public NetworkTabViewModel(DialogService dialogService, PreferencesService prefe
104
107
}
105
108
106
109
public void setValues () {
107
- remoteServerProperty .setValue (remotePreferences .useRemoteServer ());
108
- remotePortProperty .setValue (String .valueOf (remotePreferences .getPort ()));
110
+ remoteServerProperty .setValue (initialRemotePreferences .useRemoteServer ());
111
+ remotePortProperty .setValue (String .valueOf (initialRemotePreferences .getPort ()));
109
112
110
- proxyUseProperty .setValue (proxyPreferences .isUseProxy ());
111
- proxyHostnameProperty .setValue (proxyPreferences .getHostname ());
112
- proxyPortProperty .setValue (proxyPreferences .getPort ());
113
- proxyUseAuthenticationProperty .setValue (proxyPreferences .isUseAuthentication ());
114
- proxyUsernameProperty .setValue (proxyPreferences .getUsername ());
115
- proxyPasswordProperty .setValue (proxyPreferences .getPassword ());
113
+ setProxyValues ();
114
+ }
115
+
116
+ private void setProxyValues () {
117
+ proxyUseProperty .setValue (initialProxyPreferences .isUseProxy ());
118
+ proxyHostnameProperty .setValue (initialProxyPreferences .getHostname ());
119
+ proxyPortProperty .setValue (initialProxyPreferences .getPort ());
120
+ proxyUseAuthenticationProperty .setValue (initialProxyPreferences .isUseAuthentication ());
121
+ proxyUsernameProperty .setValue (initialProxyPreferences .getUsername ());
122
+ proxyPasswordProperty .setValue (initialProxyPreferences .getPassword ());
116
123
}
117
124
118
125
public void storeSettings () {
@@ -122,12 +129,12 @@ public void storeSettings() {
122
129
123
130
private void storeRemoteSettings () {
124
131
RemotePreferences newRemotePreferences = new RemotePreferences (
125
- remotePreferences .getPort (),
132
+ initialRemotePreferences .getPort (),
126
133
remoteServerProperty .getValue ()
127
134
);
128
135
129
136
getPortAsInt (remotePortProperty .getValue ()).ifPresent (newPort -> {
130
- if (remotePreferences .isDifferentPort (newPort )) {
137
+ if (initialRemotePreferences .isDifferentPort (newPort )) {
131
138
newRemotePreferences .setPort (newPort );
132
139
133
140
if (newRemotePreferences .useRemoteServer ()) {
@@ -137,7 +144,7 @@ private void storeRemoteSettings() {
137
144
});
138
145
139
146
if (newRemotePreferences .useRemoteServer ()) {
140
- Globals .REMOTE_LISTENER .openAndStart (new JabRefMessageHandler (), remotePreferences .getPort ());
147
+ Globals .REMOTE_LISTENER .openAndStart (new JabRefMessageHandler (), initialRemotePreferences .getPort ());
141
148
} else {
142
149
Globals .REMOTE_LISTENER .stop ();
143
150
}
@@ -155,7 +162,7 @@ private void storeProxySettings() {
155
162
proxyPasswordProperty .getValue ()
156
163
);
157
164
158
- if (!newProxyPreferences .equals (proxyPreferences )) {
165
+ if (!newProxyPreferences .equals (initialProxyPreferences )) {
159
166
ProxyRegisterer .register (newProxyPreferences );
160
167
}
161
168
preferences .storeProxyPreferences (newProxyPreferences );
@@ -215,6 +222,39 @@ public boolean validateSettings() {
215
222
return true ;
216
223
}
217
224
225
+ /**
226
+ * Check the connection by using the given url. Used for validating the http proxy.
227
+ * The checking result will be appear when request finished.
228
+ * The checking result could be either success or fail, if fail, the cause will be displayed.
229
+ */
230
+ public void checkConnection () {
231
+ final String connectionSuccessText = Localization .lang ("Connection successful!" );
232
+ final String connectionFailedText = Localization .lang ("Connection failed!" );
233
+ final String dialogTitle = Localization .lang ("Check Proxy Setting" );
234
+
235
+ final String testUrl = "http://jabref.org" ;
236
+
237
+ // Workaround for testing, since the URLDownload uses stored proxy settings, see
238
+ // preferences.storeProxyPreferences(...) below.
239
+ storeProxySettings ();
240
+
241
+ URLDownload urlDownload ;
242
+ try {
243
+ urlDownload = new URLDownload (testUrl );
244
+ if (urlDownload .canBeReached ()) {
245
+ dialogService .showInformationDialogAndWait (dialogTitle , connectionSuccessText );
246
+ } else {
247
+ dialogService .showErrorDialogAndWait (dialogTitle , connectionFailedText );
248
+ }
249
+ } catch (MalformedURLException e ) {
250
+ // Why would that happen? Because one of developers inserted a failing url in testUrl...
251
+ } catch (UnirestException e ) {
252
+ dialogService .showErrorDialogAndWait (dialogTitle , connectionFailedText );
253
+ }
254
+
255
+ preferences .storeProxyPreferences (initialProxyPreferences );
256
+ }
257
+
218
258
@ Override
219
259
public List <String > getRestartWarnings () {
220
260
return restartWarning ;
0 commit comments