Changeset 17655 in josm


Ignore:
Timestamp:
2021-03-24T17:56:01+01:00 (10 months ago)
Author:
simon04
Message:

fix #20660 - Remote control /imagery: add imagery by id

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/ImageryHandler.java

    r16735 r17655  
    55
    66import java.util.Arrays;
     7import java.util.LinkedHashSet;
     8import java.util.Map;
     9import java.util.Objects;
     10import java.util.Optional;
     11import java.util.Set;
    712
    813import org.openstreetmap.josm.data.StructUtils;
     
    1015import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryPreferenceEntry;
    1116import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
     17import org.openstreetmap.josm.data.imagery.ImageryLayerInfo;
    1218import org.openstreetmap.josm.gui.MainApplication;
    1319import org.openstreetmap.josm.gui.layer.ImageryLayer;
     
    3238    public String getPermissionMessage() {
    3339        return tr("Remote Control has been asked to load an imagery layer from the following URL:")
    34                 + "<br>" + args.get("url");
     40                + "<br>" + args.getOrDefault("url", args.get("id"));
    3541    }
    3642
    3743    @Override
    3844    public String[] getMandatoryParams() {
    39         return new String[]{"url"};
     45        return new String[0];
    4046    }
    4147
    4248    @Override
    4349    public String[] getOptionalParams() {
    44         return StructUtils.serializeStruct(new ImageryPreferenceEntry(), ImageryPreferenceEntry.class,
    45                 StructUtils.SerializeOptions.INCLUDE_NULL, StructUtils.SerializeOptions.INCLUDE_DEFAULT
    46         ).keySet().toArray(new String[0]);
     50        Set<String> params = new LinkedHashSet<>();
     51        params.add("url");
     52        params.add("id");
     53        Map<String, String> struct = StructUtils.serializeStruct(new ImageryPreferenceEntry(), ImageryPreferenceEntry.class,
     54                StructUtils.SerializeOptions.INCLUDE_NULL, StructUtils.SerializeOptions.INCLUDE_DEFAULT);
     55        params.addAll(struct.keySet());
     56        return params.toArray(new String[0]);
    4757    }
    4858
     
    5363
    5464    protected ImageryInfo buildImageryInfo() {
     65        String id = args.get("id");
     66        if (id != null) {
     67            Optional<ImageryInfo> byId = ImageryLayerInfo.instance.getLayers().stream()
     68                    .filter(l -> Objects.equals(l.getId(), id))
     69                    .findFirst();
     70            if (byId.isPresent()) {
     71                return byId.get();
     72            }
     73        }
    5574        args.computeIfAbsent("type", ignore -> ImageryType.WMS.getDefault().getTypeString());
    5675        args.computeIfAbsent("name", ignore -> args.getOrDefault("title", tr("Remote imagery")));
     
    83102        try {
    84103            CheckParameterUtil.ensureParameterNotNull(args);
    85             CheckParameterUtil.ensureParameterNotNull(args.get("url"));
     104            CheckParameterUtil.ensureThat(args.containsKey("url") || args.containsKey("id"),
     105                    tr("The following keys are mandatory, but have not been provided: {0}", "url/id"));
    86106            ImageryLayer.create(buildImageryInfo());
    87107        } catch (IllegalArgumentException e) {
     
    100120                ImageryType::getTypeString));
    101121        return new String[] {
     122            "/imagery?id=Bing",
    102123            "/imagery?title=osm&type=tms&url=https://a.tile.openstreetmap.org/%7Bzoom%7D/%7Bx%7D/%7By%7D.png",
    103124            "/imagery?title=landsat&type=wms&url=http://irs.gis-lab.info/?" +
  • trunk/test/unit/org/openstreetmap/josm/io/remotecontrol/handler/ImageryHandlerTest.java

    r17275 r17655  
    5353    void testBadRequestInvalidUrl() {
    5454        Exception e = assertThrows(RequestHandlerBadRequestException.class, () -> newHandler("invalid_url").handle());
    55         assertEquals("The following keys are mandatory, but have not been provided: url", e.getMessage());
     55        assertEquals("The following keys are mandatory, but have not been provided: url/id", e.getMessage());
    5656    }
    5757
     
    6262    void testBadRequestIncompleteUrl() {
    6363        Exception e = assertThrows(RequestHandlerBadRequestException.class, () -> newHandler("https://localhost").handle());
    64         assertEquals("The following keys are mandatory, but have not been provided: url", e.getMessage());
     64        assertEquals("The following keys are mandatory, but have not been provided: url/id", e.getMessage());
    6565    }
    6666
Note: See TracChangeset for help on using the changeset viewer.