Skip to content

Commit

Permalink
Merge pull request #10 from don-vip/master
Browse files Browse the repository at this point in the history
use JOSM HTTPClient + catch JsonParsingException
  • Loading branch information
iandees authored Feb 12, 2018
2 parents 22c7b9f + a1776d5 commit 999703f
Showing 1 changed file with 26 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
package org.openstreetmap.josm.plugins.fieldpapers;

import org.openstreetmap.josm.Main;
import org.openstreetmap.josm.actions.JosmAction;
import org.openstreetmap.josm.data.Bounds;
import org.openstreetmap.josm.data.coor.LatLon;
import org.openstreetmap.josm.gui.ExtendedDialog;
import org.openstreetmap.josm.gui.MainApplication;
import org.openstreetmap.josm.gui.widgets.JosmTextField;
import org.openstreetmap.josm.tools.GBC;
import org.openstreetmap.josm.tools.Utils;
import static org.openstreetmap.josm.tools.I18n.tr;

import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.stream.JsonParsingException;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import static org.openstreetmap.josm.tools.I18n.tr;
import org.openstreetmap.josm.Main;
import org.openstreetmap.josm.actions.JosmAction;
import org.openstreetmap.josm.data.Bounds;
import org.openstreetmap.josm.data.coor.LatLon;
import org.openstreetmap.josm.gui.ExtendedDialog;
import org.openstreetmap.josm.gui.MainApplication;
import org.openstreetmap.josm.gui.widgets.JosmTextField;
import org.openstreetmap.josm.tools.GBC;
import org.openstreetmap.josm.tools.HttpClient;
import org.openstreetmap.josm.tools.Logging;
import org.openstreetmap.josm.tools.Utils;

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

} catch (IOException ex) {
ex.printStackTrace();
Logging.error(ex);
JOptionPane.showMessageDialog(Main.parent,tr("Could not read information for the id \"{0}\" from fieldpapers.org", url));
}
}

private JsonObject getMetadata(String snapshotUrl) throws IOException {
InputStream is = null;
JsonReader reader = null;

try {
URL url = new URL(snapshotUrl);

URLConnection connection = url.openConnection();
connection.setRequestProperty("Accept", "application/json");

is = connection.getInputStream();
reader = Json.createReader(is);

try (
InputStream is = HttpClient.create(new URL(snapshotUrl)).setAccept("application/json").connect().getContent();
JsonReader reader = Json.createReader(is)
) {
return reader.readObject();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException err) {
// Ignore
}
}
if (reader != null) {
reader.close();
}
} catch (JsonParsingException e) {
throw new IOException(e);
}
}

Expand Down

0 comments on commit 999703f

Please sign in to comment.