Ignore:
Timestamp:
2016-11-18T20:00:52+01:00 (8 years ago)
Author:
simon04
Message:

JOSM/wikipedia: Refactoring (HTTP)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/wikipedia/src/org/wikipedia/WikipediaApp.java

    r33075 r33076  
    7070    }
    7171
     72    private static HttpClient.Response connect(String url) throws IOException {
     73        final HttpClient.Response response = HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect();
     74        if (response.getResponseCode() != 200) {
     75            throw new IOException("Server responded with HTTP " + response.getResponseCode());
     76        }
     77        return response;
     78    }
     79
    7280    public List<WikipediaEntry> getEntriesFromCoordinates(LatLon min, LatLon max) {
    7381        try {
     
    8088                    + "&gsbbox=" + max.lat() + "|" + min.lon() + "|" + min.lat() + "|" + max.lon();
    8189            // parse XML document
    82             try (final InputStream in = HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContent()) {
     90            try (final InputStream in = connect(url).getContent()) {
    8391                final Document doc = newDocumentBuilder().parse(in);
    8492                final List<WikipediaEntry> entries = X_PATH.evaluateNodes("//gs", doc).stream()
     
    114122                    "&limit=50" +
    115123                    "&format=xml";
    116             try (final InputStream in = HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContent()) {
     124            try (final InputStream in = connect(url).getContent()) {
    117125                final Document xml = newDocumentBuilder().parse(in);
    118126                final List<WikidataEntry> r = X_PATH.evaluateNodes("//entity", xml).stream()
     
    133141                    + "&cat=" + Utils.encodeUrl(category);
    134142
    135             try (final BufferedReader reader = HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia")
    136                     .connect().getContentReader()) {
     143            try (final BufferedReader reader = connect(url).getContentReader()) {
    137144                return reader.lines()
    138145                        .map(line -> new WikipediaEntry(wikipediaLang, line.trim().replace("_", " ")))
     
    239246                    "&titles=" + articles.stream().map(Utils::encodeUrl).collect(Collectors.joining("|"));
    240247            final Map<String, String> r = new TreeMap<>();
    241             try (final InputStream in = HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContent()) {
     248            try (final InputStream in = connect(url).getContent()) {
    242249                final Document xml = newDocumentBuilder().parse(in);
    243250                X_PATH.evaluateNodes("//entity", xml).forEach(node -> {
     
    265272                    + "&pssearch=" + Utils.encodeUrl(prefix);
    266273            // parse XML document
    267             try (final InputStream in = HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContent()) {
     274            try (final InputStream in = connect(url).getContent()) {
    268275                final Document doc = newDocumentBuilder().parse(in);
    269276                return X_PATH.evaluateNodes("//ps/@title", doc).stream()
     
    309316            languages.add(null);
    310317            final List<WikidataEntry> r = new ArrayList<>(entries.size());
    311             try (final InputStream in = HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContent()) {
     318            try (final InputStream in = connect(url).getContent()) {
    312319                final Document xml = newDocumentBuilder().parse(in);
    313320                for (final WikipediaEntry entry : entries) {
     
    348355                    "&lllimit=500" +
    349356                    "&format=xml";
    350             try (final InputStream in = HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContent()) {
     357            try (final InputStream in = connect(url).getContent()) {
    351358                final Document xml = newDocumentBuilder().parse(in);
    352359                return X_PATH.evaluateNodes("//ll", xml).stream()
     
    369376                    "&titles=" + Utils.encodeUrl(article) +
    370377                    "&format=xml";
    371             try (final InputStream in = HttpClient.create(new URL(url)).setReasonForRequest("Wikipedia").connect().getContent()) {
     378            try (final InputStream in = connect(url).getContent()) {
    372379                final Document xml = newDocumentBuilder().parse(in);
    373380                final Node node = X_PATH.evaluateNode("//coordinates/co", xml);
Note: See TracChangeset for help on using the changeset viewer.