Search in sources :

Example 1 with ApiMediaType

use of de.ii.ogcapi.foundation.domain.ApiMediaType in project ldproxy by interactive-instruments.

the class TilesQueriesHandlerImpl method getTileServerTileResponse.

private Response getTileServerTileResponse(QueryInputTileTileServerTile queryInput, ApiRequestContext requestContext) {
    Tile tile = queryInput.getTile();
    final String urlTemplate = tile.isDatasetTile() ? queryInput.getProvider().getUrlTemplate() : queryInput.getProvider().getUrlTemplateSingleCollection();
    if (Objects.isNull(urlTemplate))
        throw new IllegalStateException("The MAP_TILES configuration is invalid, no 'urlTemplate' was found.");
    ApiMediaType mediaType = tile.getOutputFormat().getMediaType();
    WebTarget client = ClientBuilder.newClient().target(urlTemplate).resolveTemplate("tileMatrix", tile.getTileLevel()).resolveTemplate("tileRow", tile.getTileRow()).resolveTemplate("tileCol", tile.getTileCol()).resolveTemplate("fileExtension", mediaType.fileExtension());
    if (Objects.nonNull(tile.getCollectionId()))
        client = client.resolveTemplate("collectionId", tile.getCollectionId());
    Response response = client.request(mediaType.type()).get();
    // unsuccessful? just forward the error response
    if (response.getStatus() != 200)
        return response;
    List<Link> links = new DefaultLinksGenerator().generateLinks(requestContext.getUriCustomizer(), requestContext.getMediaType(), ImmutableList.of(), i18n, requestContext.getLanguage());
    byte[] content;
    try {
        content = response.readEntity(InputStream.class).readAllBytes();
    } catch (IOException e) {
        throw new RuntimeException("Could not read map tile from TileServer.", e);
    }
    Date lastModified = null;
    EntityTag etag = getEtag(content);
    Response.ResponseBuilder responseBuilder = evaluatePreconditions(requestContext, lastModified, etag);
    if (Objects.nonNull(responseBuilder))
        return responseBuilder.build();
    return prepareSuccessResponse(requestContext, queryInput.getIncludeLinkHeader() ? links : null, lastModified, etag, queryInput.getCacheControl().orElse(null), queryInput.getExpires().orElse(null), null, true, String.format("%s_%d_%d_%d.%s", tile.getTileMatrixSet().getId(), tile.getTileLevel(), tile.getTileRow(), tile.getTileCol(), tile.getOutputFormat().getMediaType().fileExtension())).entity(content).build();
}
Also used : Tile(de.ii.ogcapi.tiles.domain.Tile) ApiMediaType(de.ii.ogcapi.foundation.domain.ApiMediaType) IOException(java.io.IOException) Date(java.util.Date) Response(javax.ws.rs.core.Response) DefaultLinksGenerator(de.ii.ogcapi.foundation.domain.DefaultLinksGenerator) EntityTag(javax.ws.rs.core.EntityTag) WebTarget(javax.ws.rs.client.WebTarget) Link(de.ii.ogcapi.foundation.domain.Link)

Example 2 with ApiMediaType

use of de.ii.ogcapi.foundation.domain.ApiMediaType in project ldproxy by interactive-instruments.

the class QueriesHandlerStylesImpl method getStyleResponse.

private Response getStyleResponse(QueryInputStyle queryInput, ApiRequestContext requestContext) {
    OgcApi api = requestContext.getApi();
    OgcApiDataV2 apiData = api.getData();
    Optional<String> collectionId = queryInput.getCollectionId();
    String styleId = queryInput.getStyleId();
    StyleFormatExtension format = styleRepository.getStyleFormatStream(apiData, collectionId).filter(f -> requestContext.getMediaType().matches(f.getMediaType().type())).findAny().orElseThrow(() -> new NotAcceptableException(MessageFormat.format("The requested media type ''{0}'' is not supported, the following media types are available: {1}", requestContext.getMediaType(), String.join(", ", styleRepository.getStyleFormatStream(apiData, collectionId).map(f -> f.getMediaType().type().toString()).collect(Collectors.toUnmodifiableList())))));
    StylesheetContent stylesheetContent = styleRepository.getStylesheet(apiData, collectionId, styleId, format, requestContext, true);
    // collect self/alternate links, but only, if we need to return them in the headers
    List<Link> links = null;
    if (queryInput.getIncludeLinkHeader()) {
        final DefaultLinksGenerator defaultLinkGenerator = new DefaultLinksGenerator();
        List<ApiMediaType> alternateMediaTypes = styleRepository.getStylesheetMediaTypes(apiData, collectionId, styleId, true, true).stream().filter(apiMediaType -> !apiMediaType.type().equals(format.getMediaType().type())).collect(Collectors.toUnmodifiableList());
        links = defaultLinkGenerator.generateLinks(requestContext.getUriCustomizer(), format.getMediaType(), alternateMediaTypes, i18n, requestContext.getLanguage());
    }
    Date lastModified = styleRepository.getStylesheetLastModified(apiData, collectionId, styleId, format, true);
    EntityTag etag = !format.getMediaType().type().equals(MediaType.TEXT_HTML_TYPE) || (collectionId.isEmpty() ? apiData.getExtension(HtmlConfiguration.class) : apiData.getExtension(HtmlConfiguration.class, collectionId.get())).map(HtmlConfiguration::getSendEtags).orElse(false) ? getEtag(stylesheetContent.getContent()) : null;
    Response.ResponseBuilder response = evaluatePreconditions(requestContext, lastModified, etag);
    if (Objects.nonNull(response))
        return response.build();
    return prepareSuccessResponse(requestContext, links, lastModified, etag, queryInput.getCacheControl().orElse(null), queryInput.getExpires().orElse(null), null, true, String.format("%s.%s", styleId, format.getFileExtension())).entity(format.getStyleEntity(stylesheetContent, api, collectionId, styleId, requestContext)).build();
}
Also used : QueryHandler(de.ii.ogcapi.foundation.domain.QueryHandler) Date(java.util.Date) StyleRepository(de.ii.ogcapi.styles.domain.StyleRepository) Link(de.ii.ogcapi.foundation.domain.Link) QueryInput(de.ii.ogcapi.foundation.domain.QueryInput) Singleton(javax.inject.Singleton) AutoBind(com.github.azahnen.dagger.annotations.AutoBind) ApiMediaType(de.ii.ogcapi.foundation.domain.ApiMediaType) MessageFormat(java.text.MessageFormat) Inject(javax.inject.Inject) MediaType(javax.ws.rs.core.MediaType) Map(java.util.Map) ExtensionRegistry(de.ii.ogcapi.foundation.domain.ExtensionRegistry) HtmlConfiguration(de.ii.ogcapi.html.domain.HtmlConfiguration) ApiRequestContext(de.ii.ogcapi.foundation.domain.ApiRequestContext) NotAcceptableException(javax.ws.rs.NotAcceptableException) ImmutableMap(com.google.common.collect.ImmutableMap) I18n(de.ii.ogcapi.foundation.domain.I18n) OgcApi(de.ii.ogcapi.foundation.domain.OgcApi) DefaultLinksGenerator(de.ii.ogcapi.foundation.domain.DefaultLinksGenerator) Instant(java.time.Instant) EntityTag(javax.ws.rs.core.EntityTag) Collectors(java.util.stream.Collectors) StyleMetadataFormatExtension(de.ii.ogcapi.styles.domain.StyleMetadataFormatExtension) QueriesHandlerStyles(de.ii.ogcapi.styles.domain.QueriesHandlerStyles) Objects(java.util.Objects) StylesheetContent(de.ii.ogcapi.styles.domain.StylesheetContent) List(java.util.List) StyleMetadata(de.ii.ogcapi.styles.domain.StyleMetadata) StylesFormatExtension(de.ii.ogcapi.styles.domain.StylesFormatExtension) Response(javax.ws.rs.core.Response) OgcApiDataV2(de.ii.ogcapi.foundation.domain.OgcApiDataV2) Optional(java.util.Optional) StyleFormatExtension(de.ii.ogcapi.styles.domain.StyleFormatExtension) Styles(de.ii.ogcapi.styles.domain.Styles) StylesheetContent(de.ii.ogcapi.styles.domain.StylesheetContent) OgcApi(de.ii.ogcapi.foundation.domain.OgcApi) ApiMediaType(de.ii.ogcapi.foundation.domain.ApiMediaType) StyleFormatExtension(de.ii.ogcapi.styles.domain.StyleFormatExtension) Date(java.util.Date) OgcApiDataV2(de.ii.ogcapi.foundation.domain.OgcApiDataV2) Response(javax.ws.rs.core.Response) DefaultLinksGenerator(de.ii.ogcapi.foundation.domain.DefaultLinksGenerator) NotAcceptableException(javax.ws.rs.NotAcceptableException) EntityTag(javax.ws.rs.core.EntityTag) Link(de.ii.ogcapi.foundation.domain.Link)

Example 3 with ApiMediaType

use of de.ii.ogcapi.foundation.domain.ApiMediaType in project ldproxy by interactive-instruments.

the class StyleRepositoryFiles method getStyles.

@Override
public Styles getStyles(OgcApiDataV2 apiData, Optional<String> collectionId, ApiRequestContext requestContext) {
    File dir = getPathStyles(apiData, collectionId).toFile();
    if (!dir.exists())
        dir.getParentFile().mkdirs();
    final StylesLinkGenerator stylesLinkGenerator = new StylesLinkGenerator();
    Map<String, StyleFormatExtension> formatMap = getStyleFormatStream(apiData, collectionId).filter(format -> !format.getDerived()).collect(Collectors.toUnmodifiableMap(format -> format.getFileExtension(), format -> format));
    List<StyleEntry> styleEntries = Arrays.stream(Objects.requireNonNullElse(dir.listFiles(), ImmutableList.of().toArray(File[]::new))).filter(file -> !file.isHidden()).filter(file -> formatMap.containsKey(com.google.common.io.Files.getFileExtension(file.getName()))).map(file -> com.google.common.io.Files.getNameWithoutExtension(file.getName())).distinct().sorted().map(styleId -> {
        Builder builder = ImmutableStyleEntry.builder().id(styleId).title(getTitle(apiData, collectionId, styleId, requestContext).orElse(styleId));
        Date lastModified = getStyleLastModified(apiData, collectionId, styleId);
        if (Objects.nonNull(lastModified))
            builder.lastModified(lastModified);
        List<ApiMediaType> mediaTypes = getStylesheetMediaTypes(apiData, collectionId, styleId, true, false);
        builder.links(stylesLinkGenerator.generateStyleLinks(requestContext.getUriCustomizer(), styleId, mediaTypes, i18n, requestContext.getLanguage()));
        if (collectionId.isPresent()) {
            List<ApiMediaType> additionalMediaTypes = getStyleFormatStream(apiData, collectionId).filter(format -> format.canDeriveCollectionStyle() && !mediaTypes.contains(format.getMediaType()) && willDeriveStylesheet(apiData, collectionId, styleId, format)).map(StyleFormatExtension::getMediaType).collect(Collectors.toUnmodifiableList());
            if (!additionalMediaTypes.isEmpty()) {
                builder.addAllLinks(stylesLinkGenerator.generateStyleLinks(requestContext.getUriCustomizer(), styleId, additionalMediaTypes, i18n, requestContext.getLanguage()));
            }
        }
        builder.addLinks(stylesLinkGenerator.generateStyleMetadataLink(requestContext.getUriCustomizer(), styleId, i18n, requestContext.getLanguage()));
        return builder.build();
    }).collect(Collectors.toList());
    Styles styles = ImmutableStyles.builder().styles(styleEntries).lastModified(styleEntries.stream().map(StyleEntry::getLastModified).map(Optional::get).max(Comparator.naturalOrder())).links(new DefaultLinksGenerator().generateLinks(requestContext.getUriCustomizer(), requestContext.getMediaType(), requestContext.getAlternateMediaTypes(), i18n, requestContext.getLanguage())).build();
    return styles;
}
Also used : Arrays(java.util.Arrays) StylesheetMetadata(de.ii.ogcapi.styles.domain.StylesheetMetadata) API_RESOURCES_DIR(de.ii.ogcapi.foundation.domain.FoundationConfiguration.API_RESOURCES_DIR) Date(java.util.Date) Link(de.ii.ogcapi.foundation.domain.Link) LoggerFactory(org.slf4j.LoggerFactory) ImmutableValidationResult(de.ii.xtraplatform.store.domain.entities.ImmutableValidationResult) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) ApiMediaType(de.ii.ogcapi.foundation.domain.ApiMediaType) MediaType(javax.ws.rs.core.MediaType) Builder(de.ii.ogcapi.styles.domain.ImmutableStyleEntry.Builder) Map(java.util.Map) URI(java.net.URI) ExtensionRegistry(de.ii.ogcapi.foundation.domain.ExtensionRegistry) Path(java.nio.file.Path) HtmlConfiguration(de.ii.ogcapi.html.domain.HtmlConfiguration) JsonPatchException(com.github.fge.jsonpatch.JsonPatchException) ApiRequestContext(de.ii.ogcapi.foundation.domain.ApiRequestContext) ImmutableSet(com.google.common.collect.ImmutableSet) FeaturesCoreProviders(de.ii.ogcapi.features.core.domain.FeaturesCoreProviders) Set(java.util.Set) DefaultLinksGenerator(de.ii.ogcapi.foundation.domain.DefaultLinksGenerator) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) StyleMetadataFormatExtension(de.ii.ogcapi.styles.domain.StyleMetadataFormatExtension) NotFoundException(javax.ws.rs.NotFoundException) Objects(java.util.Objects) StylesheetContent(de.ii.ogcapi.styles.domain.StylesheetContent) List(java.util.List) Stream(java.util.stream.Stream) StylesFormatExtension(de.ii.ogcapi.styles.domain.StylesFormatExtension) OgcApiDataV2(de.ii.ogcapi.foundation.domain.OgcApiDataV2) StylesConfiguration(de.ii.ogcapi.styles.domain.StylesConfiguration) StylesLinkGenerator(de.ii.ogcapi.styles.domain.StylesLinkGenerator) Optional(java.util.Optional) StyleFormatExtension(de.ii.ogcapi.styles.domain.StyleFormatExtension) ImmutableStyles(de.ii.ogcapi.styles.domain.ImmutableStyles) StyleEntry(de.ii.ogcapi.styles.domain.StyleEntry) EntityRegistry(de.ii.xtraplatform.store.domain.entities.EntityRegistry) ServicesContext(de.ii.xtraplatform.services.domain.ServicesContext) FilenameFilter(java.io.FilenameFilter) StyleRepository(de.ii.ogcapi.styles.domain.StyleRepository) Singleton(javax.inject.Singleton) AutoBind(com.github.azahnen.dagger.annotations.AutoBind) TilesConfiguration(de.ii.ogcapi.tiles.domain.TilesConfiguration) MessageFormat(java.text.MessageFormat) Inject(javax.inject.Inject) AppLifeCycle(de.ii.xtraplatform.base.domain.AppLifeCycle) ImmutableList(com.google.common.collect.ImmutableList) AppContext(de.ii.xtraplatform.base.domain.AppContext) FeaturesHtmlConfiguration(de.ii.ogcapi.features.html.domain.FeaturesHtmlConfiguration) ImmutableStylesheetMetadata(de.ii.ogcapi.styles.domain.ImmutableStylesheetMetadata) NotAcceptableException(javax.ws.rs.NotAcceptableException) Logger(org.slf4j.Logger) Files(java.nio.file.Files) I18n(de.ii.ogcapi.foundation.domain.I18n) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonMergePatch(com.github.fge.jsonpatch.mergepatch.JsonMergePatch) URICustomizer(de.ii.ogcapi.foundation.domain.URICustomizer) IOException(java.io.IOException) File(java.io.File) GuavaModule(com.fasterxml.jackson.datatype.guava.GuavaModule) ImmutableStyleEntry(de.ii.ogcapi.styles.domain.ImmutableStyleEntry) StyleMetadata(de.ii.ogcapi.styles.domain.StyleMetadata) ImmutableStyleMetadata(de.ii.ogcapi.styles.domain.ImmutableStyleMetadata) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) Styles(de.ii.ogcapi.styles.domain.Styles) Comparator(java.util.Comparator) Optional(java.util.Optional) Builder(de.ii.ogcapi.styles.domain.ImmutableStyleEntry.Builder) StyleFormatExtension(de.ii.ogcapi.styles.domain.StyleFormatExtension) Date(java.util.Date) StylesLinkGenerator(de.ii.ogcapi.styles.domain.StylesLinkGenerator) DefaultLinksGenerator(de.ii.ogcapi.foundation.domain.DefaultLinksGenerator) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) StyleEntry(de.ii.ogcapi.styles.domain.StyleEntry) ImmutableStyleEntry(de.ii.ogcapi.styles.domain.ImmutableStyleEntry) File(java.io.File) ImmutableStyles(de.ii.ogcapi.styles.domain.ImmutableStyles) Styles(de.ii.ogcapi.styles.domain.Styles)

Example 4 with ApiMediaType

use of de.ii.ogcapi.foundation.domain.ApiMediaType in project ldproxy by interactive-instruments.

the class ApiRequestDispatcher method dispatch.

@Path("/{entrypoint: [^/]*}")
public EndpointExtension dispatch(@PathParam("entrypoint") String entrypoint, @Context OgcApi service, @Context ContainerRequestContext requestContext, @Context Request request) {
    String subPath = ((UriRoutingContext) requestContext.getUriInfo()).getFinalMatchingGroup();
    String method = requestContext.getMethod();
    EndpointExtension ogcApiEndpoint = findEndpoint(service.getData(), entrypoint, subPath, method).orElse(null);
    if (ogcApiEndpoint == null) {
        throwNotAllowedOrNotFound(getMethods(service.getData(), entrypoint, subPath));
    /* TODO should this belong here or should this be done by the resources?
            // check, if this may be an issue of special characters in the path, replace all non-Word characters with an underscore and test the sub path again
            String subPathReduced = subPath.replaceAll("\\W","_");
            if (findEndpoint(service.getData(), entrypoint, subPathReduced, null).isPresent())
                throw new BadRequestException("The sub path '"+subPath+"' includes characters that are not supported for a resource. Resource ids typically only support word characters (ASCII letters, digits, underscore) for the resource names.");
            throw new NotFoundException();
             */
    }
    Set<String> parameters = requestContext.getUriInfo().getQueryParameters().keySet();
    List<OgcApiQueryParameter> knownParameters = ogcApiEndpoint.getParameters(service.getData(), subPath, method);
    Set<String> unknownParameters = parameters.stream().filter(parameter -> !knownParameters.stream().filter(param -> param.getName().equalsIgnoreCase(parameter)).findAny().isPresent()).collect(Collectors.toSet());
    if (!unknownParameters.isEmpty()) {
        throw new BadRequestException("The following query parameters are rejected: " + String.join(", ", unknownParameters) + ". Valid parameters for this request are: " + String.join(", ", knownParameters.stream().map(ParameterExtension::getName).collect(Collectors.toList())));
    }
    ImmutableSet<ApiMediaType> supportedMediaTypes = method.equals("GET") || method.equals("HEAD") ? ogcApiEndpoint.getMediaTypes(service.getData(), subPath) : ogcApiEndpoint.getMediaTypes(service.getData(), subPath, method);
    ApiMediaType selectedMediaType;
    Set<ApiMediaType> alternateMediaTypes;
    if (supportedMediaTypes.isEmpty() && NOCONTENT_METHODS.contains(method)) {
        selectedMediaType = DEFAULT_MEDIA_TYPE;
        alternateMediaTypes = ImmutableSet.of();
    } else {
        selectedMediaType = contentNegotiation.negotiate(requestContext, supportedMediaTypes).orElseThrow(() -> new NotAcceptableException(MessageFormat.format("The Accept header ''{0}'' does not match any of the supported media types for this resource: {1}.", requestContext.getHeaderString("Accept"), supportedMediaTypes.stream().map(mediaType -> mediaType.type().toString()).collect(Collectors.toList()))));
        alternateMediaTypes = getAlternateMediaTypes(selectedMediaType, supportedMediaTypes);
    }
    Locale selectedLanguage = contentNegotiation.negotiate(requestContext).orElse(Locale.ENGLISH);
    ApiRequestContext apiRequestContext = new Builder().requestUri(requestContext.getUriInfo().getRequestUri()).request(request).externalUri(getExternalUri()).mediaType(selectedMediaType).alternateMediaTypes(alternateMediaTypes).language(selectedLanguage).api(service).build();
    // validate request
    ApiEndpointDefinition apiDef = ogcApiEndpoint.getDefinition(service.getData());
    if (!apiDef.getResources().isEmpty()) {
        // check that the subPath is valid
        OgcApiResource resource = apiDef.getResource("/" + entrypoint + subPath).orElse(null);
        if (resource == null)
            throw new NotFoundException("The requested path is not a resource in this API.");
        // no need to check the path parameters here, only the parent path parameters (service, endpoint) are available;
        // path parameters in the sub-path have to be checked later
        ApiOperation operation = apiDef.getOperation(resource, method).orElse(null);
        if (operation == null) {
            throwNotAllowedOrNotFound(getMethods(service.getData(), entrypoint, subPath));
        }
        Optional<String> collectionId = resource.getCollectionId(service.getData());
        // validate query parameters
        requestContext.getUriInfo().getQueryParameters().entrySet().stream().forEach(p -> {
            String name = p.getKey();
            List<String> values = p.getValue();
            operation.getQueryParameters().stream().filter(param -> param.getName().equalsIgnoreCase(name)).forEach(param -> {
                Optional<String> result = param.validate(service.getData(), collectionId, values);
                if (result.isPresent())
                    throw new BadRequestException(result.get());
            });
        });
    }
    // TODO check lang, too
    ogcApiInjectableContext.inject(requestContext, apiRequestContext);
    return ogcApiEndpoint;
}
Also used : SORT_PRIORITY_DUMMY(de.ii.ogcapi.foundation.domain.ApiEndpointDefinition.SORT_PRIORITY_DUMMY) PathParam(javax.ws.rs.PathParam) ServiceEndpoint(de.ii.xtraplatform.services.domain.ServiceEndpoint) Path(javax.ws.rs.Path) LoggerFactory(org.slf4j.LoggerFactory) Singleton(javax.inject.Singleton) NotAllowedException(javax.ws.rs.NotAllowedException) AutoBind(com.github.azahnen.dagger.annotations.AutoBind) ApiMediaType(de.ii.ogcapi.foundation.domain.ApiMediaType) MessageFormat(java.text.MessageFormat) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) Inject(javax.inject.Inject) ImmutableApiMediaType(de.ii.ogcapi.foundation.domain.ImmutableApiMediaType) MediaType(javax.ws.rs.core.MediaType) UriRoutingContext(org.glassfish.jersey.server.internal.routing.UriRoutingContext) Locale(java.util.Locale) BadRequestException(javax.ws.rs.BadRequestException) URI(java.net.URI) ExtensionRegistry(de.ii.ogcapi.foundation.domain.ExtensionRegistry) ApiOperation(de.ii.ogcapi.foundation.domain.ApiOperation) OgcApiQueryParameter(de.ii.ogcapi.foundation.domain.OgcApiQueryParameter) ApiRequestContext(de.ii.ogcapi.foundation.domain.ApiRequestContext) Builder(de.ii.ogcapi.foundation.domain.ImmutableRequestContext.Builder) NotAcceptableException(javax.ws.rs.NotAcceptableException) ImmutableSet(com.google.common.collect.ImmutableSet) OgcApiResource(de.ii.ogcapi.foundation.domain.OgcApiResource) Context(javax.ws.rs.core.Context) Logger(org.slf4j.Logger) PermitAll(javax.annotation.security.PermitAll) EndpointExtension(de.ii.ogcapi.foundation.domain.EndpointExtension) OgcApi(de.ii.ogcapi.foundation.domain.OgcApi) Set(java.util.Set) ParameterExtension(de.ii.ogcapi.foundation.domain.ParameterExtension) Collectors(java.util.stream.Collectors) RequestInjectableContext(de.ii.ogcapi.foundation.domain.RequestInjectableContext) NotFoundException(javax.ws.rs.NotFoundException) Objects(java.util.Objects) ApiEndpointDefinition(de.ii.ogcapi.foundation.domain.ApiEndpointDefinition) List(java.util.List) OgcApiDataV2(de.ii.ogcapi.foundation.domain.OgcApiDataV2) Optional(java.util.Optional) Request(javax.ws.rs.core.Request) ServicesContext(de.ii.xtraplatform.services.domain.ServicesContext) Locale(java.util.Locale) UriRoutingContext(org.glassfish.jersey.server.internal.routing.UriRoutingContext) ApiEndpointDefinition(de.ii.ogcapi.foundation.domain.ApiEndpointDefinition) Builder(de.ii.ogcapi.foundation.domain.ImmutableRequestContext.Builder) NotFoundException(javax.ws.rs.NotFoundException) ApiMediaType(de.ii.ogcapi.foundation.domain.ApiMediaType) ImmutableApiMediaType(de.ii.ogcapi.foundation.domain.ImmutableApiMediaType) OgcApiQueryParameter(de.ii.ogcapi.foundation.domain.OgcApiQueryParameter) EndpointExtension(de.ii.ogcapi.foundation.domain.EndpointExtension) NotAcceptableException(javax.ws.rs.NotAcceptableException) ApiRequestContext(de.ii.ogcapi.foundation.domain.ApiRequestContext) ApiOperation(de.ii.ogcapi.foundation.domain.ApiOperation) BadRequestException(javax.ws.rs.BadRequestException) OgcApiResource(de.ii.ogcapi.foundation.domain.OgcApiResource) ParameterExtension(de.ii.ogcapi.foundation.domain.ParameterExtension) Path(javax.ws.rs.Path)

Example 5 with ApiMediaType

use of de.ii.ogcapi.foundation.domain.ApiMediaType in project ldproxy by interactive-instruments.

the class CollectionsOnLandingPage method process.

@Override
public ImmutableLandingPage.Builder process(Builder landingPageBuilder, OgcApi api, URICustomizer uriCustomizer, ApiMediaType mediaType, List<ApiMediaType> alternateMediaTypes, Optional<Locale> language) {
    OgcApiDataV2 apiData = api.getData();
    if (!isEnabledForApi(apiData)) {
        return landingPageBuilder;
    }
    List<String> collectionNames = apiData.getCollections().values().stream().filter(featureType -> featureType.getEnabled()).map(featureType -> featureType.getLabel()).collect(Collectors.toList());
    String suffix = (collectionNames.size() > 0 && collectionNames.size() <= 4) ? " (" + String.join(", ", collectionNames) + ")" : "";
    landingPageBuilder.addLinks(new ImmutableLink.Builder().href(uriCustomizer.copy().ensureNoTrailingSlash().ensureLastPathSegment("collections").removeParameters("f").toString()).rel("data").title(i18n.get("dataLink", language) + suffix).build()).addLinks(new ImmutableLink.Builder().href(uriCustomizer.copy().ensureNoTrailingSlash().ensureLastPathSegment("collections").removeParameters("f").toString()).rel("http://www.opengis.net/def/rel/ogc/1.0/data").title(i18n.get("dataLink", language) + suffix).build());
    ImmutableList.Builder<Link> distributionLinks = new ImmutableList.Builder<Link>().addAll(apiData.getExtension(CollectionsConfiguration.class).map(CollectionsConfiguration::getAdditionalLinks).orElse(ImmutableList.<Link>of()).stream().filter(link -> Objects.equals(link.getRel(), "enclosure")).collect(Collectors.toUnmodifiableList()));
    // links to the features in the API)
    if (apiData.getCollections().size() == 1) {
        String collectionId = apiData.getCollections().keySet().iterator().next();
        FeatureTypeConfigurationOgcApi featureTypeConfiguration = apiData.getCollections().get(collectionId);
        distributionLinks.addAll(featureTypeConfiguration.getAdditionalLinks().stream().filter(link -> Objects.equals(link.getRel(), "enclosure")).collect(Collectors.toUnmodifiableList()));
        ImmutableOgcApiCollection.Builder ogcApiCollection = ImmutableOgcApiCollection.builder().id(collectionId);
        for (CollectionExtension ogcApiCollectionExtension : extensionRegistry.getExtensionsForType(CollectionExtension.class)) {
            ogcApiCollection = ogcApiCollectionExtension.process(ogcApiCollection, featureTypeConfiguration, api, uriCustomizer.copy().clearParameters().ensureLastPathSegments("collections", collectionId).ensureNoTrailingSlash(), false, mediaType, alternateMediaTypes, language);
        }
        distributionLinks.addAll(ogcApiCollection.build().getLinks().stream().filter(link -> Objects.equals(link.getRel(), "items") && !Objects.equals(link.getType(), "text/html")).collect(Collectors.toUnmodifiableList()));
    }
    landingPageBuilder.putExtensions("datasetDownloadLinks", distributionLinks.build());
    return landingPageBuilder;
}
Also used : ExtensionConfiguration(de.ii.ogcapi.foundation.domain.ExtensionConfiguration) LandingPageExtension(de.ii.ogcapi.common.domain.LandingPageExtension) ImmutableLandingPage(de.ii.ogcapi.common.domain.ImmutableLandingPage) Link(de.ii.ogcapi.foundation.domain.Link) Singleton(javax.inject.Singleton) CollectionsConfiguration(de.ii.ogcapi.collections.domain.CollectionsConfiguration) AutoBind(com.github.azahnen.dagger.annotations.AutoBind) ApiMediaType(de.ii.ogcapi.foundation.domain.ApiMediaType) Inject(javax.inject.Inject) ImmutableList(com.google.common.collect.ImmutableList) Locale(java.util.Locale) ImmutableLink(de.ii.ogcapi.foundation.domain.ImmutableLink) ImmutableOgcApiCollection(de.ii.ogcapi.collections.domain.ImmutableOgcApiCollection) ExtensionRegistry(de.ii.ogcapi.foundation.domain.ExtensionRegistry) Builder(de.ii.ogcapi.common.domain.ImmutableLandingPage.Builder) I18n(de.ii.ogcapi.foundation.domain.I18n) OgcApi(de.ii.ogcapi.foundation.domain.OgcApi) URICustomizer(de.ii.ogcapi.foundation.domain.URICustomizer) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) OgcApiDataV2(de.ii.ogcapi.foundation.domain.OgcApiDataV2) Optional(java.util.Optional) CollectionExtension(de.ii.ogcapi.collections.domain.CollectionExtension) FeatureTypeConfigurationOgcApi(de.ii.ogcapi.foundation.domain.FeatureTypeConfigurationOgcApi) CollectionExtension(de.ii.ogcapi.collections.domain.CollectionExtension) ImmutableList(com.google.common.collect.ImmutableList) Builder(de.ii.ogcapi.common.domain.ImmutableLandingPage.Builder) OgcApiDataV2(de.ii.ogcapi.foundation.domain.OgcApiDataV2) FeatureTypeConfigurationOgcApi(de.ii.ogcapi.foundation.domain.FeatureTypeConfigurationOgcApi) ImmutableOgcApiCollection(de.ii.ogcapi.collections.domain.ImmutableOgcApiCollection) ImmutableLink(de.ii.ogcapi.foundation.domain.ImmutableLink) Link(de.ii.ogcapi.foundation.domain.Link) ImmutableLink(de.ii.ogcapi.foundation.domain.ImmutableLink)

Aggregations

ApiMediaType (de.ii.ogcapi.foundation.domain.ApiMediaType)13 AutoBind (com.github.azahnen.dagger.annotations.AutoBind)11 Optional (java.util.Optional)11 Inject (javax.inject.Inject)11 Singleton (javax.inject.Singleton)11 OgcApi (de.ii.ogcapi.foundation.domain.OgcApi)10 OgcApiDataV2 (de.ii.ogcapi.foundation.domain.OgcApiDataV2)10 List (java.util.List)10 Objects (java.util.Objects)10 I18n (de.ii.ogcapi.foundation.domain.I18n)9 Link (de.ii.ogcapi.foundation.domain.Link)9 MediaType (javax.ws.rs.core.MediaType)8 HtmlConfiguration (de.ii.ogcapi.html.domain.HtmlConfiguration)7 Date (java.util.Date)7 Collectors (java.util.stream.Collectors)7 NotAcceptableException (javax.ws.rs.NotAcceptableException)7 Response (javax.ws.rs.core.Response)7 ApiRequestContext (de.ii.ogcapi.foundation.domain.ApiRequestContext)6 ExtensionRegistry (de.ii.ogcapi.foundation.domain.ExtensionRegistry)6 MessageFormat (java.text.MessageFormat)6