<commit_before>/**
 * Copyright 2019 interactive instruments GmbH
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package de.ii.ldproxy.wfs3.core;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import de.ii.ldproxy.wfs3.api.Wfs3Collection;
import de.ii.ldproxy.wfs3.api.Wfs3Collections;
import de.ii.ldproxy.wfs3.api.Wfs3EndpointExtension;
import de.ii.ldproxy.wfs3.api.Wfs3ExtensionRegistry;
import de.ii.ldproxy.wfs3.api.Wfs3LinksGenerator;
import de.ii.ldproxy.wfs3.api.Wfs3MediaType;
import de.ii.ldproxy.wfs3.api.Wfs3OutputFormatExtension;
import de.ii.ldproxy.wfs3.api.Wfs3ParameterExtension;
import de.ii.ldproxy.wfs3.api.Wfs3RequestContext;
import de.ii.ldproxy.wfs3.api.Wfs3Service;
import de.ii.xtraplatform.auth.api.User;
import de.ii.xtraplatform.feature.provider.api.FeatureQuery;
import de.ii.xtraplatform.service.api.Service;
import io.dropwizard.auth.Auth;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.Requires;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.util.AbstractMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author zahnen
 */
@Component
@Provides
@Instantiate
public class Wfs3EndpointCore implements Wfs3EndpointExtension {

    private static final Logger LOGGER = LoggerFactory.getLogger(Wfs3EndpointCore.class);

    @Requires
    private Wfs3Core wfs3Core;

    @Requires
    private Wfs3Query wfs3Query;

    private final Map<Wfs3MediaType, Wfs3OutputFormatExtension> wfs3OutputFormats;
    private final List<Wfs3ParameterExtension> wfs3ParameterExtensions;
    private final Wfs3ExtensionRegistry wfs3ExtensionRegistry;

    public Wfs3EndpointCore(@Requires Wfs3ExtensionRegistry wfs3ExtensionRegistry) {
        this.wfs3ExtensionRegistry = wfs3ExtensionRegistry;
        this.wfs3OutputFormats = wfs3ExtensionRegistry.getOutputFormats();
        this.wfs3ParameterExtensions = wfs3ExtensionRegistry.getWfs3Parameters();
    }


    @Override
    public String getPath() {
        return "collections";
    }

    @Override
    public String getSubPathRegex() {
        return "^\\/?(?:\\/\\w+\\/?(?:\\/items\\/?.*)?)?$";
    }

    @Override
    public List<String> getMethods() {
        return ImmutableList.of("GET");
    }

    @Path("/")
    @GET
    public Response getCollections(@Auth Optional<User> optionalUser, @Context Service service, @Context Wfs3RequestContext wfs3Request) {
        Wfs3Service wfs3Service = (Wfs3Service) service;

        checkAuthorization(wfs3Service.getData(), optionalUser);

        Wfs3Collections collections = wfs3Core.createCollections(wfs3Service.getData(), wfs3Request.getMediaType(), getAlternativeMediaTypes(wfs3Request.getMediaType()), wfs3Request.getUriCustomizer());

        return wfs3OutputFormats.get(wfs3Request.getMediaType())
                                .getDatasetResponse(collections, wfs3Service.getData(), wfs3Request.getMediaType(), getAlternativeMediaTypes(wfs3Request.getMediaType()), wfs3Request.getUriCustomizer(), wfs3Request.getStaticUrlPrefix(), true);
    }

    @Path("/{id}")
    @GET
    public Response getCollectionInfo(@Auth Optional<User> optionalUser, @PathParam("id") String id, @Context Service service, @Context Wfs3RequestContext wfs3Request) {
        Wfs3Service wfs3Service = (Wfs3Service) service;

        checkAuthorization(wfs3Service.getData(), optionalUser);

        wfs3Core.checkCollectionName(wfs3Service.getData(), id);

        Wfs3Collection wfs3Collection = wfs3Core.createCollection(wfs3Service.getData()
                                                                             .getFeatureTypes()
                                                                             .get(id), new Wfs3LinksGenerator(), wfs3Service.getData(), wfs3Request.getMediaType(), getAlternativeMediaTypes(wfs3Request.getMediaType()), wfs3Request.getUriCustomizer(), false);

        return wfs3OutputFormats.get(wfs3Request.getMediaType())
                                .getCollectionResponse(wfs3Collection, wfs3Service.getData(), wfs3Request.getMediaType(), getAlternativeMediaTypes(wfs3Request.getMediaType()), wfs3Request.getUriCustomizer(), id);


    }

    @Path("/{id}/items")
    @GET
    public Response getItems(@Auth Optional<User> optionalUser, @PathParam("id") String id, @HeaderParam("Range") String range, @Context Service service, @Context UriInfo uriInfo, @Context Wfs3RequestContext wfs3Request) {
        checkAuthorization(((Wfs3Service) service).getData(), optionalUser);

        FeatureQuery query = wfs3Query.requestToFeatureQuery(((Wfs3Service) service), id, range, toFlatMap(uriInfo.getQueryParameters()));

        return ((Wfs3Service) service).getItemsResponse(wfs3Request, id, query);
    }

    @Path("/{id}/items/{featureid}")
    @GET
    public Response getItem(@Auth Optional<User> optionalUser, @PathParam("id") String id, @PathParam("featureid") final String featureId, @Context Service service, @Context Wfs3RequestContext wfs3Request, @Context UriInfo uriInfo) {
        checkAuthorization(((Wfs3Service) service).getData(), optionalUser);

        FeatureQuery query = wfs3Query.requestToFeatureQuery(((Wfs3Service) service), id, toFlatMap(uriInfo.getQueryParameters()), featureId);

        return ((Wfs3Service) service).getItemsResponse(wfs3Request, id, query);
    }

    public static Map<String, String> toFlatMap(MultivaluedMap<String, String> queryParameters) {
        return queryParameters.entrySet()
                                                              .stream()
                                                              .map(entry -> {
                                                                  return new AbstractMap.SimpleImmutableEntry<>(entry.getKey(), entry.getValue().isEmpty() ? "" : entry.getValue().get(0));
                                                              })
                                                              .collect(ImmutableMap.toImmutableMap(Map.Entry::getKey, Map.Entry::getValue));
    }

    public static Map<String, String> getFiltersFromQuery(Map<String, String> query, Map<String, String> filterableFields) {

        Map<String, String> filters = new LinkedHashMap<>();

        for (String filterKey : query.keySet()) {
            if (filterableFields.containsKey(filterKey.toLowerCase())) {
                String filterValue = query.get(filterKey);
                filters.put(filterKey.toLowerCase(), filterValue);
            }
        }

        return filters;
    }

    public Wfs3MediaType[] getAlternativeMediaTypes(Wfs3MediaType mediaType) {
        return wfs3OutputFormats.keySet()
                                .stream()
                                .filter(wfs3MediaType -> !wfs3MediaType.equals(mediaType))
                                .toArray(Wfs3MediaType[]::new);
    }


}<commit_msg>remove alternate links for inactive formats<commit_after>/**
 * Copyright 2019 interactive instruments GmbH
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package de.ii.ldproxy.wfs3.core;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import de.ii.ldproxy.wfs3.api.*;
import de.ii.xtraplatform.auth.api.User;
import de.ii.xtraplatform.feature.provider.api.FeatureQuery;
import de.ii.xtraplatform.service.api.Service;
import io.dropwizard.auth.Auth;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.Requires;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.util.AbstractMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author zahnen
 */
@Component
@Provides
@Instantiate
public class Wfs3EndpointCore implements Wfs3EndpointExtension {

    private static final Logger LOGGER = LoggerFactory.getLogger(Wfs3EndpointCore.class);

    @Requires
    private Wfs3Core wfs3Core;

    @Requires
    private Wfs3Query wfs3Query;

    private final Map<Wfs3MediaType, Wfs3OutputFormatExtension> wfs3OutputFormats;
    private final List<Wfs3ParameterExtension> wfs3ParameterExtensions;
    private final Wfs3ExtensionRegistry wfs3ExtensionRegistry;

    public Wfs3EndpointCore(@Requires Wfs3ExtensionRegistry wfs3ExtensionRegistry) {
        this.wfs3ExtensionRegistry = wfs3ExtensionRegistry;
        this.wfs3OutputFormats = wfs3ExtensionRegistry.getOutputFormats();
        this.wfs3ParameterExtensions = wfs3ExtensionRegistry.getWfs3Parameters();
    }


    @Override
    public String getPath() {
        return "collections";
    }

    @Override
    public String getSubPathRegex() {
        return "^\\/?(?:\\/\\w+\\/?(?:\\/items\\/?.*)?)?$";
    }

    @Override
    public List<String> getMethods() {
        return ImmutableList.of("GET");
    }

    @Path("/")
    @GET
    public Response getCollections(@Auth Optional<User> optionalUser, @Context Service service, @Context Wfs3RequestContext wfs3Request) {
        Wfs3Service wfs3Service = (Wfs3Service) service;

        checkAuthorization(wfs3Service.getData(), optionalUser);

        Wfs3Collections collections = wfs3Core.createCollections(wfs3Service.getData(), wfs3Request.getMediaType(), getAlternativeMediaTypes(wfs3Request.getMediaType(),wfs3Service.getData()), wfs3Request.getUriCustomizer());

        return wfs3OutputFormats.get(wfs3Request.getMediaType())
                                .getDatasetResponse(collections, wfs3Service.getData(), wfs3Request.getMediaType(), getAlternativeMediaTypes(wfs3Request.getMediaType(),wfs3Service.getData()), wfs3Request.getUriCustomizer(), wfs3Request.getStaticUrlPrefix(), true);
    }

    @Path("/{id}")
    @GET
    public Response getCollectionInfo(@Auth Optional<User> optionalUser, @PathParam("id") String id, @Context Service service, @Context Wfs3RequestContext wfs3Request) {
        Wfs3Service wfs3Service = (Wfs3Service) service;

        checkAuthorization(wfs3Service.getData(), optionalUser);

        wfs3Core.checkCollectionName(wfs3Service.getData(), id);

        Wfs3Collection wfs3Collection = wfs3Core.createCollection(wfs3Service.getData()
                                                                             .getFeatureTypes()
                                                                             .get(id), new Wfs3LinksGenerator(), wfs3Service.getData(), wfs3Request.getMediaType(), getAlternativeMediaTypes(wfs3Request.getMediaType(),wfs3Service.getData()), wfs3Request.getUriCustomizer(), false);

        return wfs3OutputFormats.get(wfs3Request.getMediaType())
                                .getCollectionResponse(wfs3Collection, wfs3Service.getData(), wfs3Request.getMediaType(), getAlternativeMediaTypes(wfs3Request.getMediaType(),wfs3Service.getData()), wfs3Request.getUriCustomizer(), id);


    }

    @Path("/{id}/items")
    @GET
    public Response getItems(@Auth Optional<User> optionalUser, @PathParam("id") String id, @HeaderParam("Range") String range, @Context Service service, @Context UriInfo uriInfo, @Context Wfs3RequestContext wfs3Request) {
        checkAuthorization(((Wfs3Service) service).getData(), optionalUser);

        FeatureQuery query = wfs3Query.requestToFeatureQuery(((Wfs3Service) service), id, range, toFlatMap(uriInfo.getQueryParameters()));

        return ((Wfs3Service) service).getItemsResponse(wfs3Request, id, query);
    }

    @Path("/{id}/items/{featureid}")
    @GET
    public Response getItem(@Auth Optional<User> optionalUser, @PathParam("id") String id, @PathParam("featureid") final String featureId, @Context Service service, @Context Wfs3RequestContext wfs3Request, @Context UriInfo uriInfo) {
        checkAuthorization(((Wfs3Service) service).getData(), optionalUser);

        FeatureQuery query = wfs3Query.requestToFeatureQuery(((Wfs3Service) service), id, toFlatMap(uriInfo.getQueryParameters()), featureId);

        return ((Wfs3Service) service).getItemsResponse(wfs3Request, id, query);
    }

    public static Map<String, String> toFlatMap(MultivaluedMap<String, String> queryParameters) {
        return queryParameters.entrySet()
                                                              .stream()
                                                              .map(entry -> {
                                                                  return new AbstractMap.SimpleImmutableEntry<>(entry.getKey(), entry.getValue().isEmpty() ? "" : entry.getValue().get(0));
                                                              })
                                                              .collect(ImmutableMap.toImmutableMap(Map.Entry::getKey, Map.Entry::getValue));
    }

    public static Map<String, String> getFiltersFromQuery(Map<String, String> query, Map<String, String> filterableFields) {

        Map<String, String> filters = new LinkedHashMap<>();

        for (String filterKey : query.keySet()) {
            if (filterableFields.containsKey(filterKey.toLowerCase())) {
                String filterValue = query.get(filterKey);
                filters.put(filterKey.toLowerCase(), filterValue);
            }
        }

        return filters;
    }

    public Wfs3MediaType[] getAlternativeMediaTypes(Wfs3MediaType mediaType, Wfs3ServiceData serviceData) {
        return wfs3OutputFormats.keySet()
                                .stream()
                                .filter(wfs3MediaType -> !wfs3MediaType.equals(mediaType))
                                .filter(wfs3MediaType -> wfs3OutputFormats.get(wfs3MediaType).isEnabledForService(serviceData))
                                .toArray(Wfs3MediaType[]::new);
    }


}
{'fraction_non_alphanumeric': 0.10800379198266523, 'fraction_numerical': 0.015371072589382449, 'mean_word_length': 3.0979467258601554, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3023356', 'n_tokens_mistral': 4208, 'n_tokens_neox': 4134, 'n_words': 763}