Changeset 15423 in josm


Ignore:
Timestamp:
2019-10-05T19:23:20+02:00 (5 years ago)
Author:
Don-vip
Message:

fix #17240 - Presets: verify that wiki links are no redirect

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/tagging/presets/items/Link.java

    r14649 r15423  
    77import java.util.Arrays;
    88import java.util.Collection;
    9 import java.util.List;
    109import java.util.Optional;
    1110
     
    2221/**
    2322 * Hyperlink type.
     23 * @since 8863
    2424 */
    2525public class Link extends TextItem {
     
    3737    public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches) {
    3838        initializeLocaleText(tr("More information about this feature"));
     39        Optional.ofNullable(buildUrlLabel()).ifPresent(label -> p.add(label, GBC.eol().insets(0, 10, 0, 0).fill(GBC.HORIZONTAL)));
     40        return false;
     41    }
     42
     43    protected UrlLabel buildUrlLabel() {
     44        final String url = getUrl();
    3945        if (wiki != null) {
    40             final String url = Config.getUrls().getOSMWiki() + "/wiki/" + wiki;
    41             final UrlLabel label = new UrlLabel(url, locale_text, 2) {
     46            return new UrlLabel(url, locale_text, 2) {
    4247                @Override
    4348                public void mouseClicked(MouseEvent e) {
    4449                    if (SwingUtilities.isLeftMouseButton(e)) {
    4550                        // Open localized page if exists
    46                         final List<String> pages = Arrays.asList(
     51                        HelpAction.displayHelp(Arrays.asList(
    4752                                LanguageInfo.getWikiLanguagePrefix(LanguageInfo.LocaleType.OSM_WIKI) + wiki,
    48                                 wiki);
    49                         HelpAction.displayHelp(pages);
     53                                wiki));
    5054                    } else {
    5155                        super.mouseClicked(e);
     
    5357                }
    5458            };
    55             p.add(label, GBC.eol().insets(0, 10, 0, 0).fill(GBC.HORIZONTAL));
    5659        } else if (href != null || locale_href != null) {
    57             final String url = Optional.ofNullable(locale_href).orElse(href);
    58             final UrlLabel label = new UrlLabel(url, locale_text, 2);
    59             p.add(label, GBC.eol().insets(0, 10, 0, 0).fill(GBC.HORIZONTAL));
     60            return new UrlLabel(url, locale_text, 2);
    6061        }
    61         return false;
     62        return null;
     63    }
     64
     65    /**
     66     * Returns the link URL.
     67     * @return the link URL
     68     * @since 15423
     69     */
     70    public String getUrl() {
     71        if (wiki != null) {
     72            return Config.getUrls().getOSMWiki() + "/wiki/" + wiki;
     73        } else if (href != null || locale_href != null) {
     74            return Optional.ofNullable(locale_href).orElse(href);
     75        }
     76        return null;
    6277    }
    6378
  • trunk/src/org/openstreetmap/josm/tools/HttpClient.java

    r15389 r15423  
    821821    }
    822822
    823     private static boolean isRedirect(final int statusCode) {
     823    /**
     824     * Determines if the given status code is an HTTP redirection.
     825     * @param statusCode HTTP status code
     826     * @return {@code true} if the given status code is an HTTP redirection
     827     * @since 15423
     828     */
     829    public static boolean isRedirect(final int statusCode) {
    824830        switch (statusCode) {
    825831            case HttpURLConnection.HTTP_MOVED_PERM: // 301
  • trunk/test/unit/org/openstreetmap/josm/gui/preferences/map/TaggingPresetPreferenceTestIT.java

    r15101 r15423  
    88
    99import java.io.IOException;
     10import java.net.URL;
    1011import java.util.Collection;
    1112import java.util.HashSet;
     
    2829import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
    2930import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetReader;
     31import org.openstreetmap.josm.gui.tagging.presets.items.Link;
    3032import org.openstreetmap.josm.spi.preferences.Config;
    3133import org.openstreetmap.josm.testutils.JOSMTestRules;
     34import org.openstreetmap.josm.tools.HttpClient;
     35import org.openstreetmap.josm.tools.HttpClient.Response;
    3236import org.openstreetmap.josm.tools.ImageProvider;
    3337import org.openstreetmap.josm.tools.Logging;
     
    113117        assertFalse(presets.isEmpty());
    114118        // wait for asynchronous icon loading
    115         presets.stream().map(TaggingPreset::getIconLoadingTask).filter(Objects::nonNull).forEach(t -> {
     119        presets.parallelStream().map(TaggingPreset::getIconLoadingTask).filter(Objects::nonNull).forEach(t -> {
    116120            try {
    117121                t.get(30, TimeUnit.SECONDS);
    118122            } catch (InterruptedException | ExecutionException | TimeoutException e) {
     123                Logging.error(e);
     124            }
     125        });
     126        // check that links are correct and not redirections
     127        presets.parallelStream().flatMap(x -> x.data.stream().filter(i -> i instanceof Link).map(i -> ((Link) i).getUrl())).forEach(u -> {
     128            try {
     129                Response cr = HttpClient.create(new URL(u)).setMaxRedirects(-1).connect();
     130                final int code = cr.getResponseCode();
     131                if (HttpClient.isRedirect(code)) {
     132                    addOrIgnoreError(messages, "Found HTTP redirection for " + u + " -> " + code + " -> " + cr.getHeaderField("Location"));
     133                } else if (code >= 400) {
     134                    addOrIgnoreError(messages, "Found HTTP error for " + u + " -> " + code);
     135                }
     136            } catch (IOException e) {
    119137                Logging.error(e);
    120138            }
     
    125143            if (message.contains(TaggingPreset.PRESET_ICON_ERROR_MSG_PREFIX)) {
    126144                error = true;
    127                 if (isIgnoredSubstring(message)) {
    128                     ignoredErrors.add(message);
    129                 } else {
    130                     messages.add(message);
    131                 }
     145                addOrIgnoreError(messages, message);
    132146            }
    133147        }
     
    136150        }
    137151    }
     152
     153    void addOrIgnoreError(Set<String> messages, String message) {
     154        if (isIgnoredSubstring(message)) {
     155            ignoredErrors.add(message);
     156        } else {
     157            messages.add(message);
     158        }
     159    }
    138160}
Note: See TracChangeset for help on using the changeset viewer.