Skip to content

Commit ece765b

Browse files
committed
add checking connection function
as JabRef#6560 suggested
1 parent bbe3056 commit ece765b

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

Diff for: 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>

Diff for: src/main/java/org/jabref/gui/preferences/NetworkTabView.java

+8
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;
@@ -91,6 +92,8 @@ public void initialize() {
9192
proxyPassword.getRight().addEventFilter(MouseEvent.MOUSE_RELEASED, this::proxyPasswordMask);
9293
proxyPassword.getRight().addEventFilter(MouseEvent.MOUSE_EXITED, this::proxyPasswordMask);
9394

95+
checkConnectionButton.disableProperty().bind(proxyUse.selectedProperty().not());
96+
9497
ActionFactory actionFactory = new ActionFactory(Globals.getKeyPrefs());
9598
actionFactory.configureIconButton(StandardActions.HELP, new HelpAction(HelpFile.REMOTE), remoteHelp);
9699

@@ -120,4 +123,9 @@ private void proxyPasswordMask(MouseEvent event) {
120123
proxyPasswordCaretPosition = 0;
121124
}
122125
}
126+
127+
@FXML
128+
void checkConnection() {
129+
viewModel.checkConnection();
130+
}
123131
}

Diff for: src/main/java/org/jabref/gui/preferences/NetworkTabViewModel.java

+54
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;
@@ -9,12 +10,19 @@
910
import javafx.beans.property.SimpleStringProperty;
1011
import javafx.beans.property.StringProperty;
1112

13+
import javafx.scene.control.ButtonType;
14+
import javafx.scene.control.DialogPane;
15+
import javafx.scene.control.Label;
16+
import javafx.scene.control.TextField;
17+
import javafx.scene.layout.GridPane;
18+
import kong.unirest.UnirestException;
1219
import org.jabref.Globals;
1320
import org.jabref.gui.DialogService;
1421
import org.jabref.gui.remote.JabRefMessageHandler;
1522
import org.jabref.logic.l10n.Localization;
1623
import org.jabref.logic.net.ProxyPreferences;
1724
import org.jabref.logic.net.ProxyRegisterer;
25+
import org.jabref.logic.net.URLDownload;
1826
import org.jabref.logic.remote.RemotePreferences;
1927
import org.jabref.logic.remote.RemoteUtil;
2028
import org.jabref.model.strings.StringUtil;
@@ -215,6 +223,52 @@ public boolean validateSettings() {
215223
return true;
216224
}
217225

226+
public void checkConnection() {
227+
DialogPane dialogPane = new DialogPane();
228+
GridPane settingsPane = new GridPane();
229+
settingsPane.setHgap(4.0);
230+
settingsPane.setVgap(4.0);
231+
232+
Label messageLabel = new Label();
233+
messageLabel.setText(Localization.lang("Warning: your settings will be saved.\nEnter any URL to check connection to:"));
234+
235+
TextField url = new TextField();
236+
url.setText("http://");
237+
238+
settingsPane.add(messageLabel, 1, 0);
239+
240+
settingsPane.add(url, 1, 1);
241+
dialogPane.setContent(settingsPane);
242+
String title = Localization.lang("Check Proxy Setting");
243+
dialogService.showCustomDialogAndWait(
244+
title,
245+
dialogPane,
246+
ButtonType.OK, ButtonType.CANCEL)
247+
.ifPresent(btn -> {
248+
if (btn == ButtonType.OK) {
249+
String connectionProblemText = Localization.lang("Problem with connection: ");
250+
String connectionSuccessText = Localization.lang("Connection Successful!");
251+
storeProxySettings();
252+
System.out.println(url.getText());
253+
URLDownload dl;
254+
try {
255+
dl = new URLDownload(url.getText());
256+
int connectionStatus = dl.checkConnection();
257+
if (connectionStatus == 200) {
258+
dialogService.showInformationDialogAndWait(title, connectionSuccessText);
259+
} else {
260+
dialogService.showErrorDialogAndWait(title, connectionProblemText + Localization.lang("Request failed with status code ") + connectionStatus);
261+
}
262+
} catch (MalformedURLException e) {
263+
dialogService.showErrorDialogAndWait(title, connectionProblemText + e.getMessage());
264+
} catch (UnirestException e) {
265+
dialogService.showErrorDialogAndWait(title, connectionProblemText + e.getMessage());
266+
}
267+
}
268+
}
269+
);
270+
}
271+
218272
@Override
219273
public List<String> getRestartWarnings() {
220274
return restartWarning;

Diff for: src/main/java/org/jabref/logic/net/URLDownload.java

+12
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import javax.net.ssl.TrustManager;
3939
import javax.net.ssl.X509TrustManager;
4040

41+
import kong.unirest.UnirestException;
4142
import org.jabref.logic.util.io.FileUtil;
4243
import org.jabref.model.util.FileHelper;
4344

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

175+
public int checkConnection() {
176+
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");
177+
// Try to use HEAD request to avoid downloading the whole file
178+
try {
179+
int statusCode = Unirest.head(source.toString()).asString().getStatus();
180+
return statusCode;
181+
} catch (UnirestException e){
182+
throw e;
183+
}
184+
}
185+
174186
public boolean isMimeType(String type) {
175187
String mime = getMimeType();
176188

0 commit comments

Comments
 (0)