Skip to content

Commit 999703f

Browse files
authored
Merge pull request #10 from don-vip/master
use JOSM HTTPClient + catch JsonParsingException
2 parents 22c7b9f + a1776d5 commit 999703f

File tree

1 file changed

+26
-41
lines changed

1 file changed

+26
-41
lines changed

src/org/openstreetmap/josm/plugins/fieldpapers/FieldPapersAddLayerAction.java

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
11
package org.openstreetmap.josm.plugins.fieldpapers;
22

3-
import org.openstreetmap.josm.Main;
4-
import org.openstreetmap.josm.actions.JosmAction;
5-
import org.openstreetmap.josm.data.Bounds;
6-
import org.openstreetmap.josm.data.coor.LatLon;
7-
import org.openstreetmap.josm.gui.ExtendedDialog;
8-
import org.openstreetmap.josm.gui.MainApplication;
9-
import org.openstreetmap.josm.gui.widgets.JosmTextField;
10-
import org.openstreetmap.josm.tools.GBC;
11-
import org.openstreetmap.josm.tools.Utils;
3+
import static org.openstreetmap.josm.tools.I18n.tr;
4+
5+
import java.awt.GridBagLayout;
6+
import java.awt.event.ActionEvent;
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
import java.net.URL;
1210

1311
import javax.json.Json;
1412
import javax.json.JsonArray;
1513
import javax.json.JsonObject;
1614
import javax.json.JsonReader;
15+
import javax.json.stream.JsonParsingException;
1716
import javax.swing.JLabel;
1817
import javax.swing.JOptionPane;
1918
import javax.swing.JPanel;
2019

21-
import java.awt.GridBagLayout;
22-
import java.awt.event.ActionEvent;
23-
import java.io.IOException;
24-
import java.io.InputStream;
25-
import java.net.URL;
26-
import java.net.URLConnection;
27-
28-
import static org.openstreetmap.josm.tools.I18n.tr;
20+
import org.openstreetmap.josm.Main;
21+
import org.openstreetmap.josm.actions.JosmAction;
22+
import org.openstreetmap.josm.data.Bounds;
23+
import org.openstreetmap.josm.data.coor.LatLon;
24+
import org.openstreetmap.josm.gui.ExtendedDialog;
25+
import org.openstreetmap.josm.gui.MainApplication;
26+
import org.openstreetmap.josm.gui.widgets.JosmTextField;
27+
import org.openstreetmap.josm.tools.GBC;
28+
import org.openstreetmap.josm.tools.HttpClient;
29+
import org.openstreetmap.josm.tools.Logging;
30+
import org.openstreetmap.josm.tools.Utils;
2931

3032
@SuppressWarnings("serial")
3133
public class FieldPapersAddLayerAction extends JosmAction {
@@ -94,36 +96,19 @@ public void openUrl(String url) {
9496
MainApplication.getLayerManager().addLayer(wpl);
9597

9698
} catch (IOException ex) {
97-
ex.printStackTrace();
99+
Logging.error(ex);
98100
JOptionPane.showMessageDialog(Main.parent,tr("Could not read information for the id \"{0}\" from fieldpapers.org", url));
99101
}
100102
}
101103

102104
private JsonObject getMetadata(String snapshotUrl) throws IOException {
103-
InputStream is = null;
104-
JsonReader reader = null;
105-
106-
try {
107-
URL url = new URL(snapshotUrl);
108-
109-
URLConnection connection = url.openConnection();
110-
connection.setRequestProperty("Accept", "application/json");
111-
112-
is = connection.getInputStream();
113-
reader = Json.createReader(is);
114-
105+
try (
106+
InputStream is = HttpClient.create(new URL(snapshotUrl)).setAccept("application/json").connect().getContent();
107+
JsonReader reader = Json.createReader(is)
108+
) {
115109
return reader.readObject();
116-
} finally {
117-
if (is != null) {
118-
try {
119-
is.close();
120-
} catch (IOException err) {
121-
// Ignore
122-
}
123-
}
124-
if (reader != null) {
125-
reader.close();
126-
}
110+
} catch (JsonParsingException e) {
111+
throw new IOException(e);
127112
}
128113
}
129114

0 commit comments

Comments
 (0)