|
1 | 1 | package org.openstreetmap.josm.plugins.fieldpapers;
|
2 | 2 |
|
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; |
12 | 10 |
|
13 | 11 | import javax.json.Json;
|
14 | 12 | import javax.json.JsonArray;
|
15 | 13 | import javax.json.JsonObject;
|
16 | 14 | import javax.json.JsonReader;
|
| 15 | +import javax.json.stream.JsonParsingException; |
17 | 16 | import javax.swing.JLabel;
|
18 | 17 | import javax.swing.JOptionPane;
|
19 | 18 | import javax.swing.JPanel;
|
20 | 19 |
|
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; |
29 | 31 |
|
30 | 32 | @SuppressWarnings("serial")
|
31 | 33 | public class FieldPapersAddLayerAction extends JosmAction {
|
@@ -94,36 +96,19 @@ public void openUrl(String url) {
|
94 | 96 | MainApplication.getLayerManager().addLayer(wpl);
|
95 | 97 |
|
96 | 98 | } catch (IOException ex) {
|
97 |
| - ex.printStackTrace(); |
| 99 | + Logging.error(ex); |
98 | 100 | JOptionPane.showMessageDialog(Main.parent,tr("Could not read information for the id \"{0}\" from fieldpapers.org", url));
|
99 | 101 | }
|
100 | 102 | }
|
101 | 103 |
|
102 | 104 | 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 | + ) { |
115 | 109 | 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); |
127 | 112 | }
|
128 | 113 | }
|
129 | 114 |
|
|
0 commit comments