package org.oskari.maplayer.admin;

import fi.nls.oskari.service.ServiceRuntimeException;
import fi.nls.oskari.util.PropertyUtil;
import org.oskari.util.HtmlHelper;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LayerValidator {

    public static Map<String, Map<String, String>> validateLocale(Map<String, Map<String, String>> locale) throws ServiceRuntimeException {
        String lang = PropertyUtil.getDefaultLanguage();
        Map<String, String> langLocale = locale.getOrDefault(lang, Collections.emptyMap());
        if (langLocale.get("name") == null) {
            // name for default language is required
            throw new ServiceRuntimeException("Name missing for default language: " + lang);
        }
        return locale;
    }

    public static String validateUrl(final String url) throws ServiceRuntimeException {
        // TODO remove query part with ? check or with URL object
        // TODO: can we remove the whole querystring or should we just remove problematic params like service and request?
        String baseUrl = url.contains("?") ? url.substring(0, url.indexOf("?")) : url;
        baseUrl = baseUrl.endsWith("/") ? baseUrl.substring(0, baseUrl.length() - 1) : baseUrl;
        try {
            // check that it's a valid url by creating an URL object...
            new URL(url);
        } catch (MalformedURLException e) {
            throw new ServiceRuntimeException("Invalid url: " + url, "invalid_field_value");
        }
        return baseUrl;
    }

    public static String cleanGFIContent(String gfiContent) {
        if (gfiContent == null) {
            return null;
        }
        // Clean GFI content
        final String[] tags = PropertyUtil.getCommaSeparatedList("gficontent.whitelist");
        HashMap<String, String[]> attributes = new HashMap<>();
        HashMap<String[], String[]> protocols = new HashMap<>();
        String[] allAttributes = PropertyUtil.getCommaSeparatedList("gficontent.whitelist.attr");
        if (allAttributes.length > 0) {
            attributes.put(":all", allAttributes);
        }
        List<String> attrProps = PropertyUtil.getPropertyNamesStartingWith("gficontent.whitelist.attr.");
        for (String attrProp : attrProps) {
            String[] parts = attrProp.split("\\.");
            if (parts[parts.length - 2].equals("protocol")) {
                protocols.put(new String[]{parts[parts.length - 3], parts[parts.length - 1]}, PropertyUtil.getCommaSeparatedList(attrProp));
            } else {
                attributes.put(parts[parts.length - 1], PropertyUtil.getCommaSeparatedList(attrProp));
            }
        }
        return HtmlHelper.cleanHTMLString(gfiContent, tags, attributes, protocols);
    }

}

@@ -79,24 +79,104 @@
 eException;%0A
+import fi.nls.oskari.util.ConversionHelper;%0Aimport fi.nls.oskari.util.IOHelper;%0A
 import fi.nl
@@ -316,27 +316,17 @@
 va.util.
-Collections
+*
 ;%0Aimport
@@ -340,92 +340,170 @@
 til.
-HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0Apublic class LayerValidator %7B
+stream.Collectors;%0A%0Apublic class LayerValidator %7B%0A%0A    private static final Set%3CString%3E RESERVED_PARAMS = ConversionHelper.asSet(%22service%22, %22request%22, %22version%22);
 %0A%0A  
@@ -1121,136 +1121,90 @@
     
-// TODO remove query part with ? check or with URL object%0A        // TODO: can we remove the whole q
+try %7B%0A            String baseURL = IOHelper.removeQ
 uery
-s
+S
 tring
- or should we just
+(url);%0A            //
  rem
@@ -1228,217 +1228,485 @@
 aram
-s like service and request?%0A        String baseUrl = url.contains(%22?%22) ? url.substring(0, url.indexOf(%22?%22)) : url;%0A        baseUrl = baseUrl.endsWith(%22/%22) ? baseUrl.substring(0, baseUrl.length() - 1) : baseUrl
+eters from URL%0A            Map%3CString, List%3CString%3E%3E params = IOHelper.parseQuerystring(url);%0A            List%3CString%3E problematicParams = params.keySet()%0A                    .stream().filter(key -%3E RESERVED_PARAMS.contains(key.toLowerCase())).collect(Collectors.toList());%0A            // TODO: these are only problematic for OGC urls. Maybe we should skip validation for non-OGC layers?%0A            for (String param : problematicParams) %7B%0A                params.remove(param)
 ;%0A  
@@ -1703,37 +1703,37 @@
 param);%0A        
-try %7B
+    %7D
 %0A            // 
@@ -1733,91 +1733,182 @@
     
-// check that it's a valid url by creating an URL object...%0A            new URL(url
+String querystring = IOHelper.createQuerystring(params);%0A            // return whitelisted params and base url%0A            return IOHelper.addQueryString(baseURL, querystring
 );%0A 
@@ -1923,28 +1923,16 @@
  catch (
-MalformedURL
 Exceptio
@@ -2045,32 +2045,8 @@
   %7D%0A
-        return baseUrl;%0A
     

Allow URLs with non-problematic params