@Test public void validateBingLayer() { MapLayer input = new MapLayer(); input.setType(OskariLayer.TYPE_BINGLAYER); try { LayerValidator.validateAndSanitizeLayerInput(input); } catch (IllegalArgumentException e) { assertEquals("Required field missing 'options.apiKey'", e.getMessage()); } input.setOptions(new HashMap<>()); try { LayerValidator.validateAndSanitizeLayerInput(input); } catch (IllegalArgumentException e) { assertEquals("Required field missing 'options.apiKey'", e.getMessage()); } input.setLocale(createValidLocaleForLayer()); HashMap jee = new HashMap<>(); jee.put("apiKey", "testing"); input.setOptions(jee); LayerValidator.validateAndSanitizeLayerInput(input); }
public static void validateAndSanitizeLayerInput(MapLayer input) throws IllegalArgumentException { if (input == null) { throw new IllegalArgumentException("Layer data missing"); } if (!hasValue(input.getType())) { throw new IllegalArgumentException("Required field missing 'type'"); } Set<String> recognizedTypes = getRecognizedLayerTypes(); if (!recognizedTypes.contains(input.getType())) { throw new IllegalArgumentException("Layer type is not recognized: " + input.getType() + ". Recognized types are: " + LOG.getAsString(recognizedTypes)); } Set<String> mandatoryFields = getMandatoryFields(input.getType()); for (String field: mandatoryFields) { if (!hasValue(input, field)) { throw new IllegalArgumentException("Required field missing '" + field + "'"); } } if (mandatoryFields.contains("url")) { input.setUrl(LayerValidator.sanitizeUrl(input.getUrl())); } if (input.getVersion() == null) { input.setVersion(""); } input.setLocale(LayerValidator.validateLocale(input.getLocale())); input.setGfi_content(LayerValidator.sanitizeGFIContent(input.getGfi_content())); }
LayerValidator { public static void validateAndSanitizeLayerInput(MapLayer input) throws IllegalArgumentException { if (input == null) { throw new IllegalArgumentException("Layer data missing"); } if (!hasValue(input.getType())) { throw new IllegalArgumentException("Required field missing 'type'"); } Set<String> recognizedTypes = getRecognizedLayerTypes(); if (!recognizedTypes.contains(input.getType())) { throw new IllegalArgumentException("Layer type is not recognized: " + input.getType() + ". Recognized types are: " + LOG.getAsString(recognizedTypes)); } Set<String> mandatoryFields = getMandatoryFields(input.getType()); for (String field: mandatoryFields) { if (!hasValue(input, field)) { throw new IllegalArgumentException("Required field missing '" + field + "'"); } } if (mandatoryFields.contains("url")) { input.setUrl(LayerValidator.sanitizeUrl(input.getUrl())); } if (input.getVersion() == null) { input.setVersion(""); } input.setLocale(LayerValidator.validateLocale(input.getLocale())); input.setGfi_content(LayerValidator.sanitizeGFIContent(input.getGfi_content())); } }
LayerValidator { public static void validateAndSanitizeLayerInput(MapLayer input) throws IllegalArgumentException { if (input == null) { throw new IllegalArgumentException("Layer data missing"); } if (!hasValue(input.getType())) { throw new IllegalArgumentException("Required field missing 'type'"); } Set<String> recognizedTypes = getRecognizedLayerTypes(); if (!recognizedTypes.contains(input.getType())) { throw new IllegalArgumentException("Layer type is not recognized: " + input.getType() + ". Recognized types are: " + LOG.getAsString(recognizedTypes)); } Set<String> mandatoryFields = getMandatoryFields(input.getType()); for (String field: mandatoryFields) { if (!hasValue(input, field)) { throw new IllegalArgumentException("Required field missing '" + field + "'"); } } if (mandatoryFields.contains("url")) { input.setUrl(LayerValidator.sanitizeUrl(input.getUrl())); } if (input.getVersion() == null) { input.setVersion(""); } input.setLocale(LayerValidator.validateLocale(input.getLocale())); input.setGfi_content(LayerValidator.sanitizeGFIContent(input.getGfi_content())); }  }
LayerValidator { public static void validateAndSanitizeLayerInput(MapLayer input) throws IllegalArgumentException { if (input == null) { throw new IllegalArgumentException("Layer data missing"); } if (!hasValue(input.getType())) { throw new IllegalArgumentException("Required field missing 'type'"); } Set<String> recognizedTypes = getRecognizedLayerTypes(); if (!recognizedTypes.contains(input.getType())) { throw new IllegalArgumentException("Layer type is not recognized: " + input.getType() + ". Recognized types are: " + LOG.getAsString(recognizedTypes)); } Set<String> mandatoryFields = getMandatoryFields(input.getType()); for (String field: mandatoryFields) { if (!hasValue(input, field)) { throw new IllegalArgumentException("Required field missing '" + field + "'"); } } if (mandatoryFields.contains("url")) { input.setUrl(LayerValidator.sanitizeUrl(input.getUrl())); } if (input.getVersion() == null) { input.setVersion(""); } input.setLocale(LayerValidator.validateLocale(input.getLocale())); input.setGfi_content(LayerValidator.sanitizeGFIContent(input.getGfi_content())); }  static Map<String, Map<String, String>> validateLocale(Map<String, Map<String, String>> locale); static String sanitizeUrl(final String url); static String sanitizeUrl(final String url, Set<String> paramsToRemove); static String sanitizeGFIContent(String gfiContent); static Set<String> getMandatoryFields(String type); static Set<String> getRecognizedLayerTypes(); static void validateAndSanitizeLayerInput(MapLayer input); }
LayerValidator { public static void validateAndSanitizeLayerInput(MapLayer input) throws IllegalArgumentException { if (input == null) { throw new IllegalArgumentException("Layer data missing"); } if (!hasValue(input.getType())) { throw new IllegalArgumentException("Required field missing 'type'"); } Set<String> recognizedTypes = getRecognizedLayerTypes(); if (!recognizedTypes.contains(input.getType())) { throw new IllegalArgumentException("Layer type is not recognized: " + input.getType() + ". Recognized types are: " + LOG.getAsString(recognizedTypes)); } Set<String> mandatoryFields = getMandatoryFields(input.getType()); for (String field: mandatoryFields) { if (!hasValue(input, field)) { throw new IllegalArgumentException("Required field missing '" + field + "'"); } } if (mandatoryFields.contains("url")) { input.setUrl(LayerValidator.sanitizeUrl(input.getUrl())); } if (input.getVersion() == null) { input.setVersion(""); } input.setLocale(LayerValidator.validateLocale(input.getLocale())); input.setGfi_content(LayerValidator.sanitizeGFIContent(input.getGfi_content())); }  static Map<String, Map<String, String>> validateLocale(Map<String, Map<String, String>> locale); static String sanitizeUrl(final String url); static String sanitizeUrl(final String url, Set<String> paramsToRemove); static String sanitizeGFIContent(String gfiContent); static Set<String> getMandatoryFields(String type); static Set<String> getRecognizedLayerTypes(); static void validateAndSanitizeLayerInput(MapLayer input);  }