From 4c3c5091a2861c79d72ff8e1c0e49818ecac1048 Mon Sep 17 00:00:00 2001
From: Brandon Clayton <bclayton@usgs.gov>
Date: Tue, 22 Nov 2022 15:54:52 -0700
Subject: [PATCH] add common service

---
 .../netcdf/www/FeatureCollectionService.java  | 50 +++++++++++++++++++
 1 file changed, 50 insertions(+)
 create mode 100644 src/hazard/src/main/java/gov/usgs/earthquake/nshmp/netcdf/www/FeatureCollectionService.java

diff --git a/src/hazard/src/main/java/gov/usgs/earthquake/nshmp/netcdf/www/FeatureCollectionService.java b/src/hazard/src/main/java/gov/usgs/earthquake/nshmp/netcdf/www/FeatureCollectionService.java
new file mode 100644
index 0000000..e5cb9d5
--- /dev/null
+++ b/src/hazard/src/main/java/gov/usgs/earthquake/nshmp/netcdf/www/FeatureCollectionService.java
@@ -0,0 +1,50 @@
+package gov.usgs.earthquake.nshmp.netcdf.www;
+
+import com.google.gson.JsonElement;
+
+import gov.usgs.earthquake.nshmp.geo.json.FeatureCollection;
+import gov.usgs.earthquake.nshmp.netcdf.NetcdfVersion;
+import gov.usgs.earthquake.nshmp.www.ResponseBody;
+import gov.usgs.earthquake.nshmp.www.ResponseMetadata;
+
+import io.micronaut.http.HttpRequest;
+import jakarta.inject.Singleton;
+
+/**
+ * Handle feature collection services.
+ *
+ * @author U.S. Geological Survey
+ */
+@Singleton
+public class FeatureCollectionService {
+
+  static String handleFeatureCollection(
+      HttpRequest<?> http,
+      String name,
+      FeatureCollection fc,
+      Boolean raw) {
+    RequestData requestData = new RequestData(raw);
+
+    if (requestData.raw) {
+      return fc.toJson();
+    } else {
+      var response = ResponseBody.<RequestData, JsonElement> success()
+          .name(name)
+          .url(http.getUri().toString())
+          .metadata(new ResponseMetadata(NetcdfVersion.appVersions()))
+          .request(requestData)
+          .response(fc.toJsonTree())
+          .build();
+
+      return NetcdfWsUtils.GSON.toJson(response);
+    }
+  }
+
+  static class RequestData {
+    public boolean raw;
+
+    RequestData(Boolean raw) {
+      this.raw = raw == null ? false : raw;
+    }
+  }
+}
-- 
GitLab