Skip to content

Commit

Permalink
feat: add items type to paginable formats
Browse files Browse the repository at this point in the history
  • Loading branch information
f-necas committed May 15, 2024
1 parent 4104742 commit a423d59
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@

public enum MimeTypes {

JSON(new MimeType("application", "json"), "json", "JSON") {
JSON(new MimeType("application", "json"), "json", "JSON", true) {
public @Override void addHeaders(String collectionId, HttpHeaders headers) {
}
},
GeoJSON(new MimeType("application", "geo+json"), "geojson", "GeoJSON") {
GeoJSON(new MimeType("application", "geo+json"), "geojson", "GeoJSON", true) {
public @Override void addHeaders(String collectionId, HttpHeaders headers) {
}
},
SHAPEFILE(new MimeType("application", "x-shapefile"), "shapefile", "Esri Shapefile") {
SHAPEFILE(new MimeType("application", "x-shapefile"), "shapefile", "Esri Shapefile", false) {
public @Override void addHeaders(String collectionId, HttpHeaders headers) {
contentDisposition(collectionId, "shp.zip", headers);
}
Expand All @@ -29,13 +29,13 @@ public enum MimeTypes {
return "feature".equals(itemType);
}
},
CSV(new MimeType("text", "csv", StandardCharsets.UTF_8), "csv", "Comma Separated Values") {
CSV(new MimeType("text", "csv", StandardCharsets.UTF_8), "csv", "Comma Separated Values", true) {
public @Override void addHeaders(String collectionId, HttpHeaders headers) {
contentDisposition(collectionId, "csv", headers);
}
},
OOXML(new MimeType("application", "vnd.openxmlformats-officedocument.spreadsheetml.sheet"), "ooxml",
"Excel 2007 / OOXML") {
"Excel 2007 / OOXML", false) {
public @Override void addHeaders(String collectionId, HttpHeaders headers) {
contentDisposition(collectionId, "xlsx", headers);
}
Expand All @@ -44,11 +44,13 @@ public enum MimeTypes {
private final @Getter @NonNull MimeType mimeType;
private final @Getter @NonNull String shortName;
private final @Getter @NonNull String displayName;
private final @Getter @NonNull boolean paginable;

private MimeTypes(MimeType type, String shortName, String displayName) {
private MimeTypes(MimeType type, String shortName, String displayName, boolean paginable) {
this.mimeType = type;
this.shortName = shortName;
this.displayName = displayName;
this.paginable = paginable;
}

public static Optional<MimeTypes> find(@NonNull MimeType contentType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.camptocamp.opendata.ogc.features.server.impl;

import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

import com.camptocamp.opendata.ogc.features.model.*;
import com.camptocamp.opendata.ogc.features.model.Collection;
Expand Down Expand Up @@ -117,10 +118,11 @@ private Collection addLinks(Collection collection, String baseUrl) {
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(baseUrl);
builder.pathSegment("items");

MimeTypes defFormat = MimeTypes.GeoJSON;
UriComponents itemsc = builder.replaceQueryParam("f", defFormat.getShortName()).build();
Link items = link(itemsc.toString(), "items", defFormat.getMimeType().toString(), collection.getId());
collection.addLinksItem(items);
Arrays.stream(MimeTypes.values()).filter(MimeTypes::isPaginable).forEach(m -> {
if (m.supportsItemType(collection.getItemType())) {
collection.addLinksItem(createItem(builder, m, collection.getId()));
}
});

Arrays.stream(MimeTypes.values()).forEach(m -> {
if (m.supportsItemType(collection.getItemType())) {
Expand All @@ -135,6 +137,11 @@ private Collection addLinks(Collection collection, String baseUrl) {
return collection;
}

private Link createItem(UriComponentsBuilder builder, MimeTypes defFormat, String collectionId) {
UriComponents itemsc = builder.replaceQueryParam("f", defFormat.getShortName()).build();
return link(itemsc.toString(), "items", defFormat.getMimeType().toString(), collectionId);
}

private Link link(String href, String rel, String type, String title) {
Link link = new Link(href);
link.setRel(rel);
Expand Down

0 comments on commit a423d59

Please sign in to comment.