Skip to content

Commit 1b3c270

Browse files
calixtuschenyuheng
andauthored
Add connection check to NetworkTab in preferences (#6838)
* add checking connection function as #6560 suggested * checkstyle fix * add javadoc * add tests * trivial fix * Update CHANGELOG.md * Removed dialog * Refactored according to remarks in PR * Fixed CHANGELOG.md Co-authored-by: chenyuheng <[email protected]>
1 parent d3b584a commit 1b3c270

File tree

6 files changed

+98
-18
lines changed

6 files changed

+98
-18
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
1414

1515
- We added a query parser and mapping layer to enable conversion of queries formulated in simplified lucene syntax by the user into api queries. [#6799](https://github.com/JabRef/jabref/pull/6799)
1616
- We added some basic functionality to customise the look of JabRef by importing a css theme file. [#5790](https://github.com/JabRef/jabref/issues/5790)
17+
- We added connection check function in network preference setting [#6560](https://github.com/JabRef/jabref/issues/6560)
1718

1819
### Changed
1920

src/main/java/org/jabref/gui/preferences/NetworkTab.fxml

+2
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,7 @@
5353
GridPane.rowIndex="5"/>
5454
<Label fx:id="proxyAttentionLabel" styleClass="warning-message"
5555
text="%Attention: Password is stored in plain text!" GridPane.columnIndex="2" GridPane.rowIndex="5"/>
56+
<Button fx:id="checkConnectionButton" text="%Check connection" onAction="#checkConnection"
57+
prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="6"/>
5658
</GridPane>
5759
</fx:root>

src/main/java/org/jabref/gui/preferences/NetworkTabView.java

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class NetworkTabView extends AbstractPreferenceTabView<NetworkTabViewMode
4040
@FXML private Label proxyPasswordLabel;
4141
@FXML private CustomPasswordField proxyPassword;
4242
@FXML private Label proxyAttentionLabel;
43+
@FXML private Button checkConnectionButton;
4344

4445
private String proxyPasswordText = "";
4546
private int proxyPasswordCaretPosition = 0;
@@ -120,4 +121,9 @@ private void proxyPasswordMask(MouseEvent event) {
120121
proxyPasswordCaretPosition = 0;
121122
}
122123
}
124+
125+
@FXML
126+
void checkConnection() {
127+
viewModel.checkConnection();
128+
}
123129
}

src/main/java/org/jabref/gui/preferences/NetworkTabViewModel.java

+56-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.jabref.gui.preferences;
22

3+
import java.net.MalformedURLException;
34
import java.util.ArrayList;
45
import java.util.List;
56
import java.util.Optional;
@@ -15,6 +16,7 @@
1516
import org.jabref.logic.l10n.Localization;
1617
import org.jabref.logic.net.ProxyPreferences;
1718
import org.jabref.logic.net.ProxyRegisterer;
19+
import org.jabref.logic.net.URLDownload;
1820
import org.jabref.logic.remote.RemotePreferences;
1921
import org.jabref.logic.remote.RemoteUtil;
2022
import org.jabref.model.strings.StringUtil;
@@ -25,6 +27,7 @@
2527
import de.saxsys.mvvmfx.utils.validation.ValidationMessage;
2628
import de.saxsys.mvvmfx.utils.validation.ValidationStatus;
2729
import de.saxsys.mvvmfx.utils.validation.Validator;
30+
import kong.unirest.UnirestException;
2831

2932
public class NetworkTabViewModel implements PreferenceTabViewModel {
3033
private final BooleanProperty remoteServerProperty = new SimpleBooleanProperty();
@@ -44,16 +47,16 @@ public class NetworkTabViewModel implements PreferenceTabViewModel {
4447

4548
private final DialogService dialogService;
4649
private final PreferencesService preferences;
47-
private final RemotePreferences remotePreferences;
48-
private final ProxyPreferences proxyPreferences;
50+
private final RemotePreferences initialRemotePreferences;
51+
private final ProxyPreferences initialProxyPreferences;
4952

5053
private final List<String> restartWarning = new ArrayList<>();
5154

5255
public NetworkTabViewModel(DialogService dialogService, PreferencesService preferences) {
5356
this.dialogService = dialogService;
5457
this.preferences = preferences;
55-
this.remotePreferences = preferences.getRemotePreferences();
56-
this.proxyPreferences = preferences.getProxyPreferences();
58+
this.initialRemotePreferences = preferences.getRemotePreferences();
59+
this.initialProxyPreferences = preferences.getProxyPreferences();
5760

5861
remotePortValidator = new FunctionBasedValidator<>(
5962
remotePortProperty,
@@ -104,15 +107,19 @@ public NetworkTabViewModel(DialogService dialogService, PreferencesService prefe
104107
}
105108

106109
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()));
109112

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());
116123
}
117124

118125
public void storeSettings() {
@@ -122,12 +129,12 @@ public void storeSettings() {
122129

123130
private void storeRemoteSettings() {
124131
RemotePreferences newRemotePreferences = new RemotePreferences(
125-
remotePreferences.getPort(),
132+
initialRemotePreferences.getPort(),
126133
remoteServerProperty.getValue()
127134
);
128135

129136
getPortAsInt(remotePortProperty.getValue()).ifPresent(newPort -> {
130-
if (remotePreferences.isDifferentPort(newPort)) {
137+
if (initialRemotePreferences.isDifferentPort(newPort)) {
131138
newRemotePreferences.setPort(newPort);
132139

133140
if (newRemotePreferences.useRemoteServer()) {
@@ -137,7 +144,7 @@ private void storeRemoteSettings() {
137144
});
138145

139146
if (newRemotePreferences.useRemoteServer()) {
140-
Globals.REMOTE_LISTENER.openAndStart(new JabRefMessageHandler(), remotePreferences.getPort());
147+
Globals.REMOTE_LISTENER.openAndStart(new JabRefMessageHandler(), initialRemotePreferences.getPort());
141148
} else {
142149
Globals.REMOTE_LISTENER.stop();
143150
}
@@ -155,7 +162,7 @@ private void storeProxySettings() {
155162
proxyPasswordProperty.getValue()
156163
);
157164

158-
if (!newProxyPreferences.equals(proxyPreferences)) {
165+
if (!newProxyPreferences.equals(initialProxyPreferences)) {
159166
ProxyRegisterer.register(newProxyPreferences);
160167
}
161168
preferences.storeProxyPreferences(newProxyPreferences);
@@ -215,6 +222,39 @@ public boolean validateSettings() {
215222
return true;
216223
}
217224

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+
218258
@Override
219259
public List<String> getRestartWarnings() {
220260
return restartWarning;

src/main/java/org/jabref/logic/net/URLDownload.java

+15-2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.jabref.model.util.FileHelper;
4343

4444
import kong.unirest.Unirest;
45+
import kong.unirest.UnirestException;
4546
import org.slf4j.Logger;
4647
import org.slf4j.LoggerFactory;
4748

@@ -171,6 +172,19 @@ public String getMimeType() {
171172
return "";
172173
}
173174

175+
/**
176+
* Check the connection by using the HEAD request.
177+
* UnirestException can be thrown for invalid request.
178+
*
179+
* @return the status code of the response
180+
*/
181+
public boolean canBeReached() throws UnirestException {
182+
Unirest.config().setDefaultHeader("User-Agent", "Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
183+
184+
int statusCode = Unirest.head(source.toString()).asString().getStatus();
185+
return statusCode >= 200 && statusCode < 300;
186+
}
187+
174188
public boolean isMimeType(String type) {
175189
String mime = getMimeType();
176190

@@ -289,8 +303,7 @@ public String toString() {
289303
}
290304

291305
private void copy(InputStream in, Writer out, Charset encoding) throws IOException {
292-
InputStream monitoredInputStream = in;
293-
Reader r = new InputStreamReader(monitoredInputStream, encoding);
306+
Reader r = new InputStreamReader(in, encoding);
294307
try (BufferedReader read = new BufferedReader(r)) {
295308

296309
String line;

src/test/java/org/jabref/logic/net/URLDownloadTest.java

+18
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22

33
import java.io.File;
44
import java.io.IOException;
5+
import java.net.MalformedURLException;
56
import java.net.URL;
67
import java.nio.charset.StandardCharsets;
78
import java.nio.file.Path;
89

910
import org.jabref.support.DisabledOnCIServer;
1011

12+
import kong.unirest.UnirestException;
1113
import org.junit.jupiter.api.Test;
1214

1315
import static org.junit.jupiter.api.Assertions.assertNotNull;
16+
import static org.junit.jupiter.api.Assertions.assertThrows;
1417
import static org.junit.jupiter.api.Assertions.assertTrue;
1518

1619
public class URLDownloadTest {
@@ -91,4 +94,19 @@ public void downloadOfHttpsSucceeds() throws IOException {
9194
Path path = ftp.toTemporaryFile();
9295
assertNotNull(path);
9396
}
97+
98+
@Test
99+
public void testCheckConnectionSuccess() throws MalformedURLException {
100+
URLDownload google = new URLDownload(new URL("http://www.google.com"));
101+
102+
assertTrue(google.canBeReached());
103+
}
104+
105+
@Test
106+
public void testCheckConnectionFail() throws MalformedURLException {
107+
URLDownload nonsense = new URLDownload(new URL("http://nonsenseadddress"));
108+
109+
assertThrows(UnirestException.class, nonsense::canBeReached);
110+
}
111+
94112
}

0 commit comments

Comments
 (0)