<commit_before>package fi.nls.oskari.control.layer;

import fi.nls.oskari.annotation.OskariActionRoute;
import fi.nls.oskari.control.*;
import fi.nls.oskari.domain.map.OskariLayer;
import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;
import fi.nls.oskari.service.OskariComponentManager;
import fi.nls.oskari.service.capabilities.CapabilitiesCacheService;
import fi.nls.oskari.service.capabilities.OskariLayerCapabilities;
import fi.nls.oskari.util.JSONHelper;
import fi.nls.oskari.util.PropertyUtil;
import fi.nls.oskari.util.ResponseHelper;
import fi.nls.oskari.wfs.GetGtWFSCapabilities;
import fi.nls.oskari.wms.GetGtWMSCapabilities;
import fi.nls.oskari.wmts.WMTSCapabilitiesParser;
import fi.nls.oskari.wmts.domain.WMTSCapabilities;
import org.json.JSONObject;

/**
 * Get capabilites for layer and returns JSON formatted as Oskari layers
 */
@OskariActionRoute("GetWSCapabilities")
public class GetWSCapabilitiesHandler extends ActionHandler {

    private static final Logger log = LogFactory.getLogger(GetWSCapabilitiesHandler.class);
    private static final String PARM_URL = "url";
    private static final String PARM_TYPE = "type";
    private static final String PARM_VERSION = "version";
    private static final String PARM_USER = "user";
    private static final String PARM_PW = "pw";
    private static final String PARM_CRS = "crs";

    private final CapabilitiesCacheService capabilitiesService;
    private String[] permittedRoles;

    public GetWSCapabilitiesHandler() {
        this(OskariComponentManager.getComponentOfType(CapabilitiesCacheService.class));
    }

    public GetWSCapabilitiesHandler(CapabilitiesCacheService capabilitiesService) {
        this.capabilitiesService = capabilitiesService;
    }

    @Override
    public void init() {
        permittedRoles = PropertyUtil.getCommaSeparatedList("actionhandler.GetWSCapabilitiesHandler.roles");
    }

    public void handleAction(ActionParameters params) throws ActionException {
        if (!params.getUser().hasAnyRoleIn(permittedRoles)) {
            throw new ActionDeniedException("Unauthorized user tried to proxy via capabilities");
        }

        final String url = params.getRequiredParam(PARM_URL).trim();
        final String type = params.getHttpParam(PARM_TYPE, OskariLayer.TYPE_WMS);
        final String version = params.getHttpParam(PARM_VERSION, "");
        final String user = params.getHttpParam(PARM_USER, "");
        final String pw = params.getHttpParam(PARM_PW, "");
        final String currentCrs = params.getHttpParam(PARM_CRS, "EPSG:3067");

        log.debug("Trying to get capabilities for type:", type, "with url:", url);
        JSONObject capabilities = getCapabilities(url, type, version, user, pw, currentCrs);
        ResponseHelper.writeResponse(params, capabilities);
    }

    protected JSONObject getCapabilities(String url, String type, String version,
            String user, String pw, String currentCrs) throws ActionException {
        try {
            switch (type) {
            case OskariLayer.TYPE_WMS:
                return GetGtWMSCapabilities.getWMSCapabilities(capabilitiesService, url, user, pw, version, currentCrs);
            case OskariLayer.TYPE_WFS:
                return GetGtWFSCapabilities.getWFSCapabilities(url, version, user, pw, currentCrs);
            case OskariLayer.TYPE_WMTS:
                OskariLayerCapabilities caps = capabilitiesService.getCapabilities(url, OskariLayer.TYPE_WMTS, version, user, pw);
                String capabilitiesXML = caps.getData();
                WMTSCapabilities wmtsCaps = WMTSCapabilitiesParser.parseCapabilities(capabilitiesXML);
                if (caps.getId() == null) {
                    capabilitiesService.save(caps);
                }
                JSONObject resultJSON = WMTSCapabilitiesParser.asJSON(wmtsCaps, url, currentCrs);
                JSONHelper.putValue(resultJSON, "xml", capabilitiesXML);
                return resultJSON;
            default:
                throw new ActionParamsException("Couldn't determine operation based on parameters");
            }
        } catch (Exception e) {
            throw new ActionException("WMS Capabilities parsing failed: ", e);
        }
    }

}
<commit_msg>Add support for WFS3 layers<commit_after>package fi.nls.oskari.control.layer;

import java.util.List;
import java.util.stream.Collectors;

import org.json.JSONArray;
import org.json.JSONObject;
import org.oskari.service.wfs3.WFS3Service;
import org.oskari.service.wfs3.model.WFS3CollectionInfo;

import fi.nls.oskari.annotation.OskariActionRoute;
import fi.nls.oskari.control.*;
import fi.nls.oskari.domain.map.OskariLayer;
import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;
import fi.nls.oskari.map.layer.formatters.LayerJSONFormatterWFS;
import fi.nls.oskari.service.OskariComponentManager;
import fi.nls.oskari.service.capabilities.CapabilitiesCacheService;
import fi.nls.oskari.service.capabilities.OskariLayerCapabilities;
import fi.nls.oskari.util.JSONHelper;
import fi.nls.oskari.util.PropertyUtil;
import fi.nls.oskari.util.ResponseHelper;
import fi.nls.oskari.wfs.GetGtWFSCapabilities;
import fi.nls.oskari.wms.GetGtWMSCapabilities;
import fi.nls.oskari.wmts.WMTSCapabilitiesParser;
import fi.nls.oskari.wmts.domain.WMTSCapabilities;

/**
 * Get capabilites for layer and returns JSON formatted as Oskari layers
 */
@OskariActionRoute("GetWSCapabilities")
public class GetWSCapabilitiesHandler extends ActionHandler {

    private static final Logger log = LogFactory.getLogger(GetWSCapabilitiesHandler.class);
    private static final String PARM_URL = "url";
    private static final String PARM_TYPE = "type";
    private static final String PARM_VERSION = "version";
    private static final String PARM_USER = "user";
    private static final String PARM_PW = "pw";
    private static final String PARM_CRS = "crs";

    private static final String VERSION_WFS3 = "3.0.0";

    private final CapabilitiesCacheService capabilitiesService;
    private String[] permittedRoles;

    public GetWSCapabilitiesHandler() {
        this(OskariComponentManager.getComponentOfType(CapabilitiesCacheService.class));
    }

    public GetWSCapabilitiesHandler(CapabilitiesCacheService capabilitiesService) {
        this.capabilitiesService = capabilitiesService;
    }

    @Override
    public void init() {
        permittedRoles = PropertyUtil.getCommaSeparatedList("actionhandler.GetWSCapabilitiesHandler.roles");
    }

    public void handleAction(ActionParameters params) throws ActionException {
        if (!params.getUser().hasAnyRoleIn(permittedRoles)) {
            throw new ActionDeniedException("Unauthorized user tried to proxy via capabilities");
        }

        final String url = params.getRequiredParam(PARM_URL).trim();
        final String type = params.getHttpParam(PARM_TYPE, OskariLayer.TYPE_WMS);
        final String version = params.getHttpParam(PARM_VERSION, "");
        final String user = params.getHttpParam(PARM_USER, "");
        final String pw = params.getHttpParam(PARM_PW, "");
        final String currentCrs = params.getHttpParam(PARM_CRS, "EPSG:3067");

        log.debug("Trying to get capabilities for type:", type, "with url:", url);
        JSONObject capabilities = getCapabilities(url, type, version, user, pw, currentCrs);
        ResponseHelper.writeResponse(params, capabilities);
    }

    protected JSONObject getCapabilities(String url, String type, String version,
            String user, String pw, String currentCrs) throws ActionException {
        try {
            switch (type) {
            case OskariLayer.TYPE_WMS:
                return GetGtWMSCapabilities.getWMSCapabilities(capabilitiesService, url, user, pw, version, currentCrs);
            case OskariLayer.TYPE_WFS:
                if (VERSION_WFS3.equals(version)) {
                    WFS3Service service = WFS3Service.fromURL(url, type, version);
                    List<JSONObject> layers = service.getCollections().stream()
                            .map(collectionInfo -> toOskariLayer(url, collectionInfo))
                            .map(layer -> wfsLayerToJSON(layer, currentCrs))
                            .collect(Collectors.toList());
                    return JSONHelper.createJSONObject("features", new JSONArray(layers));
                } else {
                    return GetGtWFSCapabilities.getWFSCapabilities(url, version, user, pw, currentCrs);
                }
            case OskariLayer.TYPE_WMTS:
                OskariLayerCapabilities caps = capabilitiesService.getCapabilities(url, OskariLayer.TYPE_WMTS, version, user, pw);
                String capabilitiesXML = caps.getData();
                WMTSCapabilities wmtsCaps = WMTSCapabilitiesParser.parseCapabilities(capabilitiesXML);
                if (caps.getId() == null) {
                    capabilitiesService.save(caps);
                }
                JSONObject resultJSON = WMTSCapabilitiesParser.asJSON(wmtsCaps, url, currentCrs);
                JSONHelper.putValue(resultJSON, "xml", capabilitiesXML);
                return resultJSON;
            default:
                throw new ActionParamsException("Couldn't determine operation based on parameters");
            }
        } catch (Exception e) {
            throw new ActionException("WMS Capabilities parsing failed: ", e);
        }
    }

    private OskariLayer toOskariLayer(String url, WFS3CollectionInfo collection) {
        OskariLayer layer = new OskariLayer();
        layer.setType(OskariLayer.TYPE_WFS);
        layer.setVersion(VERSION_WFS3);
        layer.setUrl(url);
        layer.setName(collection.getId());
        layer.setMaxScale(1d);
        layer.setMinScale(1500000d);

        String title = collection.getTitle() != null ? collection.getTitle() : collection.getId();
        for (String lang : PropertyUtil.getSupportedLanguages()) {
            layer.setName(lang, title);
        }

        return layer;
    }

    private JSONObject wfsLayerToJSON(OskariLayer layer, String crs) {
        LayerJSONFormatterWFS formatter = new LayerJSONFormatterWFS();
        String lang = PropertyUtil.getDefaultLanguage();
        JSONObject obj = formatter.getJSON(layer, lang, false, crs);
        // NOTE! Important to remove id since this is at template
        obj.remove("id");
        return obj;
    }

}

{'fraction_non_alphanumeric': 0.08927541802806073, 'fraction_numerical': 0.0028829521429944265, 'mean_word_length': 3.2373778501628663, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5221095', 'n_tokens_mistral': 3023, 'n_tokens_neox': 2963, 'n_words': 628}