use of de.ii.ogcapi.features.core.domain.FeatureFormatExtension in project ldproxy by interactive-instruments.
the class FeaturesCoreQueriesHandlerImpl method getItemResponse.
private Response getItemResponse(QueryInputFeature queryInput, ApiRequestContext requestContext) {
OgcApi api = requestContext.getApi();
OgcApiDataV2 apiData = api.getData();
String collectionId = queryInput.getCollectionId();
String featureId = queryInput.getFeatureId();
FeatureQuery query = queryInput.getQuery();
FeatureFormatExtension outputFormat = api.getOutputFormat(FeatureFormatExtension.class, requestContext.getMediaType(), "/collections/" + collectionId + "/items/" + featureId, Optional.of(collectionId)).orElseThrow(() -> new NotAcceptableException(MessageFormat.format("The requested media type ''{0}'' is not supported for this resource.", requestContext.getMediaType())));
String persistentUri = null;
Optional<String> template = api.getData().getCollections().get(collectionId).getPersistentUriTemplate();
if (template.isPresent()) {
persistentUri = StringTemplateFilters.applyTemplate(template.get(), featureId);
}
return getItemsResponse(api, requestContext, collectionId, featureId, queryInput, query, queryInput.getFeatureProvider(), persistentUri, outputFormat, false, Optional.empty(), false, queryInput.getIncludeLinkHeader(), queryInput.getDefaultCrs());
}
use of de.ii.ogcapi.features.core.domain.FeatureFormatExtension in project ldproxy by interactive-instruments.
the class FeaturesCoreQueriesHandlerImpl method getItemsResponse.
private Response getItemsResponse(OgcApi api, ApiRequestContext requestContext, String collectionId, String featureId, QueryInput queryInput, FeatureQuery query, FeatureProvider2 featureProvider, String canonicalUri, FeatureFormatExtension outputFormat, boolean onlyHitsIfMore, Optional<Integer> defaultPageSize, boolean showsFeatureSelfLink, boolean includeLinkHeader, EpsgCrs defaultCrs) {
ensureCollectionIdExists(api.getData(), collectionId);
ensureFeatureProviderSupportsQueries(featureProvider);
Optional<CrsTransformer> crsTransformer = Optional.empty();
EpsgCrs sourceCrs = null;
EpsgCrs targetCrs = query.getCrs().orElse(defaultCrs);
if (featureProvider.supportsCrs()) {
sourceCrs = featureProvider.crs().getNativeCrs();
crsTransformer = crsTransformerFactory.getTransformer(sourceCrs, targetCrs);
}
List<ApiMediaType> alternateMediaTypes = requestContext.getAlternateMediaTypes();
List<Link> links = Objects.isNull(featureId) ? new FeaturesLinksGenerator().generateLinks(requestContext.getUriCustomizer(), query.getOffset(), query.getLimit(), defaultPageSize.orElse(0), requestContext.getMediaType(), alternateMediaTypes, i18n, requestContext.getLanguage()) : new FeatureLinksGenerator().generateLinks(requestContext.getUriCustomizer(), requestContext.getMediaType(), alternateMediaTypes, outputFormat.getCollectionMediaType(), canonicalUri, i18n, requestContext.getLanguage());
String featureTypeId = api.getData().getCollections().get(collectionId).getExtension(FeaturesCoreConfiguration.class).map(cfg -> cfg.getFeatureType().orElse(collectionId)).orElse(collectionId);
ImmutableFeatureTransformationContextGeneric.Builder transformationContext = new ImmutableFeatureTransformationContextGeneric.Builder().api(api).apiData(api.getData()).featureSchema(featureProvider.getData().getTypes().get(featureTypeId)).collectionId(collectionId).ogcApiRequest(requestContext).crsTransformer(crsTransformer).codelists(entityRegistry.getEntitiesForType(Codelist.class).stream().collect(Collectors.toMap(PersistentEntity::getId, c -> c))).defaultCrs(defaultCrs).sourceCrs(Optional.ofNullable(sourceCrs)).links(links).isFeatureCollection(Objects.isNull(featureId)).isHitsOnly(query.hitsOnly()).isPropertyOnly(query.propertyOnly()).fields(query.getFields()).limit(query.getLimit()).offset(query.getOffset()).maxAllowableOffset(query.getMaxAllowableOffset()).geometryPrecision(query.getGeometryPrecision()).isHitsOnlyIfMore(onlyHitsIfMore).showsFeatureSelfLink(showsFeatureSelfLink);
StreamingOutput streamingOutput;
if (outputFormat.canPassThroughFeatures() && featureProvider.supportsPassThrough() && outputFormat.getMediaType().matches(featureProvider.passThrough().getMediaType())) {
FeatureStream featureStream = featureProvider.passThrough().getFeatureStreamPassThrough(query);
ImmutableFeatureTransformationContextGeneric transformationContextGeneric = transformationContext.outputStream(new OutputStreamToByteConsumer()).build();
FeatureTokenEncoder<?> encoder = outputFormat.getFeatureEncoderPassThrough(transformationContextGeneric, requestContext.getLanguage()).get();
streamingOutput = stream(featureStream, Objects.nonNull(featureId), encoder, Optional.empty());
} else if (outputFormat.canEncodeFeatures()) {
FeatureStream featureStream = featureProvider.queries().getFeatureStream(query);
ImmutableFeatureTransformationContextGeneric transformationContextGeneric = transformationContext.outputStream(new OutputStreamToByteConsumer()).build();
FeatureTokenEncoder<?> encoder = outputFormat.getFeatureEncoder(transformationContextGeneric, requestContext.getLanguage()).get();
Optional<PropertyTransformations> propertyTransformations = outputFormat.getPropertyTransformations(api.getData().getCollections().get(collectionId)).map(pt -> pt.withSubstitutions(ImmutableMap.of("serviceUrl", transformationContextGeneric.getServiceUrl())));
streamingOutput = stream(featureStream, Objects.nonNull(featureId), encoder, propertyTransformations);
} else {
throw new NotAcceptableException(MessageFormat.format("The requested media type {0} cannot be generated, because it does not support streaming.", requestContext.getMediaType().type()));
}
Date lastModified = null;
EntityTag etag = null;
byte[] result = null;
if (Objects.nonNull(featureId)) {
// support etag from the content for a single feature
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
streamingOutput.write(baos);
} catch (IOException e) {
throw new IllegalStateException("Feature stream error.", e);
}
result = baos.toByteArray();
etag = !outputFormat.getMediaType().type().equals(MediaType.TEXT_HTML_TYPE) || api.getData().getExtension(HtmlConfiguration.class, collectionId).map(HtmlConfiguration::getSendEtags).orElse(false) ? getEtag(result) : null;
} else {
lastModified = getLastModified(queryInput, requestContext.getApi(), featureProvider);
}
Response.ResponseBuilder response = evaluatePreconditions(requestContext, lastModified, etag);
if (Objects.nonNull(response))
return response.build();
return prepareSuccessResponse(requestContext, includeLinkHeader ? links.stream().filter(link -> !"next".equalsIgnoreCase(link.getRel())).collect(ImmutableList.toImmutableList()) : null, lastModified, etag, queryInput.getCacheControl().orElse(null), queryInput.getExpires().orElse(null), targetCrs, true, String.format("%s.%s", Objects.isNull(featureId) ? collectionId : featureId, outputFormat.getMediaType().fileExtension())).entity(Objects.nonNull(result) ? result : streamingOutput).build();
}
use of de.ii.ogcapi.features.core.domain.FeatureFormatExtension in project ldproxy by interactive-instruments.
the class FeaturesCoreQueriesHandlerImpl method getItemsResponse.
private Response getItemsResponse(QueryInputFeatures queryInput, ApiRequestContext requestContext) {
OgcApi api = requestContext.getApi();
String collectionId = queryInput.getCollectionId();
FeatureQuery query = queryInput.getQuery();
Optional<Integer> defaultPageSize = queryInput.getDefaultPageSize();
// TODO check
boolean onlyHitsIfMore = false;
FeatureFormatExtension outputFormat = api.getOutputFormat(FeatureFormatExtension.class, requestContext.getMediaType(), "/collections/" + collectionId + "/items", Optional.of(collectionId)).orElseThrow(() -> new NotAcceptableException(MessageFormat.format("The requested media type ''{0}'' is not supported for this resource.", requestContext.getMediaType())));
return getItemsResponse(api, requestContext, collectionId, null, queryInput, query, queryInput.getFeatureProvider(), null, outputFormat, onlyHitsIfMore, defaultPageSize, queryInput.getShowsFeatureSelfLink(), queryInput.getIncludeLinkHeader(), queryInput.getDefaultCrs());
}
Aggregations