Changeset 16589 in josm
- Timestamp:
- 2020-06-09T23:44:55+02:00 (4 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/StructUtils.java
r16545 r16589 8 8 import java.lang.reflect.Field; 9 9 import java.util.ArrayList; 10 import java.util.Arrays; 10 11 import java.util.Collection; 11 12 import java.util.Collections; … … 53 54 * Indicates that a certain field should be considered in the conversion process. Otherwise it is ignored. 54 55 * 55 * @see #serializeStruct (java.lang.Object, java.lang.Class)56 * @see #serializeStruct 56 57 * @see #deserializeStruct(java.util.Map, java.lang.Class) 57 58 */ … … 63 64 * Indicates that a certain field should be written to the map, even if the value is the same as the default value. 64 65 * 65 * @see #serializeStruct (java.lang.Object, java.lang.Class)66 * @see #serializeStruct 66 67 */ 67 68 @Retention(RetentionPolicy.RUNTIME) // keep annotation at runtime … … 128 129 129 130 /** 131 * Options for {@link #serializeStruct} 132 */ 133 public enum SerializeOptions { 134 /** 135 * Serialize {@code null} values 136 */ 137 INCLUDE_NULL, 138 /** 139 * Serialize default values 140 */ 141 INCLUDE_DEFAULT 142 } 143 144 /** 130 145 * Convert an object to a String Map, by using field names and values as map key and value. 131 146 * … … 141 156 * @param struct the object to be converted 142 157 * @param klass the class T 158 * @param options optional serialization options 143 159 * @return the resulting map (same data content as <code>struct</code>) 144 160 */ 145 public static <T> HashMap<String, String> serializeStruct(T struct, Class<T> klass) { 161 public static <T> HashMap<String, String> serializeStruct(T struct, Class<T> klass, SerializeOptions... options) { 162 List<SerializeOptions> optionsList = Arrays.asList(options); 146 163 T structPrototype; 147 164 try { … … 160 177 Object fieldValue = f.get(struct); 161 178 Object defaultFieldValue = f.get(structPrototype); 162 if (fieldValue != null && ( 163 f.getAnnotation(WriteExplicitly.class) != null || 164 !Objects.equals(fieldValue, defaultFieldValue))) { 179 boolean serializeNull = optionsList.contains(SerializeOptions.INCLUDE_NULL) || fieldValue != null; 180 boolean serializeDefault = optionsList.contains(SerializeOptions.INCLUDE_DEFAULT) 181 || f.getAnnotation(WriteExplicitly.class) != null 182 || !Objects.equals(fieldValue, defaultFieldValue); 183 if (serializeNull && serializeDefault) { 165 184 String key = f.getName().replace('_', '-'); 166 185 if (fieldValue instanceof Map) { … … 168 187 } else if (fieldValue instanceof MultiMap) { 169 188 hash.put(key, multiMapToJson((MultiMap<?, ?>) fieldValue)); 189 } else if (fieldValue == null) { 190 hash.put(key, null); 170 191 } else { 171 192 hash.put(key, fieldValue.toString()); -
trunk/src/org/openstreetmap/josm/io/remotecontrol/RemoteControl.java
r16324 r16589 32 32 */ 33 33 static final int protocolMajorVersion = 1; 34 static final int protocolMinorVersion = 9;34 static final int protocolMinorVersion = 10; 35 35 36 36 /** -
trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/ImageryHandler.java
r16436 r16589 6 6 import java.util.Arrays; 7 7 8 import org.openstreetmap.josm.data.StructUtils; 8 9 import org.openstreetmap.josm.data.imagery.ImageryInfo; 10 import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryPreferenceEntry; 9 11 import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType; 10 import org.openstreetmap.josm.data.imagery.ImageryLayerInfo;11 12 import org.openstreetmap.josm.gui.MainApplication; 12 13 import org.openstreetmap.josm.gui.layer.ImageryLayer; 13 14 import org.openstreetmap.josm.gui.util.GuiHelper; 14 15 import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault; 16 import org.openstreetmap.josm.tools.CheckParameterUtil; 15 17 import org.openstreetmap.josm.tools.Logging; 16 18 import org.openstreetmap.josm.tools.Utils; … … 40 42 @Override 41 43 public String[] getOptionalParams() { 42 return new String[] {"title", "type", "cookies", "min_zoom", "max_zoom"}; 44 return StructUtils.serializeStruct(new ImageryPreferenceEntry(), ImageryPreferenceEntry.class, 45 StructUtils.SerializeOptions.INCLUDE_NULL, StructUtils.SerializeOptions.INCLUDE_DEFAULT 46 ).keySet().toArray(new String[0]); 43 47 } 44 48 … … 48 52 } 49 53 50 protected static ImageryInfo findBingEntry() {51 return ImageryLayerInfo.instance.getDefaultLayers().stream()52 .filter(i -> ImageryType.BING == i.getImageryType())53 .findFirst().orElse(null);54 }55 56 54 protected ImageryInfo buildImageryInfo() { 57 String url = args.get("url"); 58 String title = args.get("title"); 59 String type = args.get("type"); 60 final ImageryInfo bing = ImageryType.BING.getTypeString().equals(type) ? findBingEntry() : null; 61 if ((title == null || title.isEmpty()) && bing != null) { 62 title = bing.getName(); 63 } 64 if (title == null || title.isEmpty()) { 65 title = tr("Remote imagery"); 66 } 67 String cookies = args.get("cookies"); 68 final ImageryInfo imgInfo = new ImageryInfo(title, url, type, null, cookies); 69 if (bing != null) { 70 imgInfo.setIcon(bing.getIcon()); 71 } 72 String minZoom = args.get("min_zoom"); 73 if (minZoom != null && !minZoom.isEmpty()) { 74 try { 75 imgInfo.setDefaultMinZoom(Integer.parseInt(minZoom)); 76 } catch (NumberFormatException e) { 77 Logging.error(e); 78 } 79 } 80 String maxZoom = args.get("max_zoom"); 81 if (maxZoom != null && !maxZoom.isEmpty()) { 82 try { 83 imgInfo.setDefaultMaxZoom(Integer.parseInt(maxZoom)); 84 } catch (NumberFormatException e) { 85 Logging.error(e); 86 } 87 } 88 return imgInfo; 55 args.computeIfAbsent("type", ignore -> ImageryType.WMS.getDefault().getTypeString()); 56 args.computeIfAbsent("name", ignore -> args.getOrDefault("title", tr("Remote imagery"))); 57 ImageryPreferenceEntry imageryPreferenceEntry = StructUtils.deserializeStruct(args, ImageryPreferenceEntry.class); 58 return new ImageryInfo(imageryPreferenceEntry); 89 59 } 90 60 … … 111 81 @Override 112 82 protected void validateRequest() throws RequestHandlerBadRequestException { 113 String url = args != null ? args.get("url") : null;114 String type = args != null ? args.get("type") : null;115 String cookies = args != null ? args.get("cookies") : null;116 83 try { 117 ImageryLayer.create(new ImageryInfo(null, url, type, null, cookies)); 84 CheckParameterUtil.ensureParameterNotNull(args); 85 CheckParameterUtil.ensureParameterNotNull(args.get("url")); 86 ImageryLayer.create(buildImageryInfo()); 118 87 } catch (IllegalArgumentException e) { 119 88 throw new RequestHandlerBadRequestException(e.getMessage(), e); -
trunk/test/unit/org/openstreetmap/josm/io/remotecontrol/handler/ImageryHandlerTest.java
r16588 r16589 2 2 package org.openstreetmap.josm.io.remotecontrol.handler; 3 3 4 import static org.hamcrest.CoreMatchers.hasItem; 4 5 import static org.junit.Assert.assertEquals; 6 import static org.junit.Assert.assertThat; 7 8 import java.util.Arrays; 9 import java.util.List; 5 10 6 11 import org.junit.Rule; … … 81 86 82 87 /** 88 * Unit test for {@link ImageryHandler#getOptionalParams()} 89 * @throws Exception if any error occurs 90 */ 91 @Test 92 public void testOptionalParams() throws Exception { 93 List<String> optionalParams = Arrays.asList(newHandler("").getOptionalParams()); 94 assertThat(optionalParams, hasItem("type")); 95 assertThat(optionalParams, hasItem("min-zoom")); 96 assertThat(optionalParams, hasItem("max-zoom")); 97 assertThat(optionalParams, hasItem("category")); 98 } 99 100 /** 83 101 * Unit test for {@link ImageryHandler#buildImageryInfo()} 84 102 * @throws Exception if any error occurs … … 87 105 public void testBuildImageryInfo() throws Exception { 88 106 String url = "https://localhost/imagery?title=osm" 89 + "&type=tms&min_zoom=3&max_zoom=23 "107 + "&type=tms&min_zoom=3&max_zoom=23&category=osmbasedmap&country_code=XA" 90 108 + "&url=https://a.tile.openstreetmap.org/%7Bzoom%7D/%7Bx%7D/%7By%7D.png"; 91 109 ImageryInfo imageryInfo = newHandler(url).buildImageryInfo(); … … 95 113 assertEquals(3, imageryInfo.getMinZoom()); 96 114 assertEquals(23, imageryInfo.getMaxZoom()); 115 assertEquals(ImageryInfo.ImageryCategory.OSMBASEDMAP, imageryInfo.getImageryCategory()); 116 assertEquals("XA", imageryInfo.getCountryCode()); 97 117 } 98 118 }
Note:
See TracChangeset
for help on using the changeset viewer.