-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectionsApiController.java
772 lines (685 loc) · 41.6 KB
/
CollectionsApiController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
package org.openeo.spring.api;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.security.Principal;
import java.time.Duration;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import javax.annotation.PostConstruct;
import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.Pattern;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.keycloak.representations.AccessToken;
import org.openeo.spring.bearer.ITokenService;
import org.openeo.spring.bearer.TokenUtil;
import org.openeo.spring.components.CollectionMap;
import org.openeo.spring.components.CollectionsMap;
import org.openeo.spring.json.OffsetDateTimeSerializer;
import org.openeo.spring.loaders.ICollectionsLoader;
import org.openeo.spring.loaders.ODCCollectionsLoader;
import org.openeo.spring.loaders.STACFileCollectionsLoader;
import org.openeo.spring.loaders.WCSCollectionsLoader;
import org.openeo.spring.model.Asset;
import org.openeo.spring.model.AssetType;
import org.openeo.spring.model.BatchJobResult;
import org.openeo.spring.model.Collection;
import org.openeo.spring.model.Collections;
import org.openeo.spring.model.EngineTypes;
import org.openeo.spring.model.Job;
import org.openeo.spring.model.JobStates;
import org.openeo.spring.model.Process;
import org.openeo.spring.model.Providers;
import org.openeo.wcps.ConvenienceHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.NativeWebRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2020-07-02T08:45:00.334+02:00[Europe/Rome]")
@RestController
@RequestMapping("${openapi.openEO.base-path:}")
public class CollectionsApiController implements CollectionsApi {
/** Home directory used as root for cached catalogs. */
public static final String CACHE_ROOT_DIR = System.getProperty("user.dir");
@Autowired
private JobsApiController jobsApiController;
/** Whether to run both inter- and intra-catalogue parallelized loading. */
@Value("${org.openeo.parallelizedHarvest}")
private boolean parallelizedHarvest;
@Value("${org.openeo.wcps.endpoint}")
private String wcpsEndpoint;
@Value("${org.openeo.wcps.endpoint.version}")
private String wcpsVersion;
@Value("${org.openeo.wcps.provider.name}")
private String wcpsProviderName;
@Value("${org.openeo.wcps.provider.type}")
private String wcpsProviderType;
@Value("${org.openeo.wcps.provider.url}")
private String wcpsProviderUrl;
@Value("${org.openeo.odc.collectionsEndpoint}")
private String odcCollEndpoint;
@Value("${org.openeo.odc.provider.name}")
private String odcProviderName;
@Value("${org.openeo.odc.provider.type}")
private String odcProviderType;
@Value("${org.openeo.odc.provider.url}")
private String odcProviderUrl;
@Value("${org.openeo.querycollectionsonstartup}")
private boolean queryCollectionsOnStartup;
@Value("${org.openeo.wcps.collections.list}")
Resource collectionsFileWCPS;
@Value("${org.openeo.odc.collections.list}")
Resource collectionsFileODC;
@Autowired
private CollectionsMap collectionsMap;
@Autowired
private CollectionMap collectionMap;
@Autowired(required = false)
private ITokenService tokenService;
private final NativeWebRequest request;
private static final Logger log = LogManager.getLogger(CollectionsApiController.class);
@org.springframework.beans.factory.annotation.Autowired
public CollectionsApiController(NativeWebRequest request) {
this.request = request;
}
@PostConstruct
public void init() {
Instant start = Instant.now();
ExecutorService exec = Executors.newFixedThreadPool(
parallelizedHarvest ? 2 : 1,
new CustomizableThreadFactory("CollectionsLoaders-"));
ICollectionsLoader wcsLoader = null;
Future<Collections> wcsCatalog = null;
ICollectionsLoader odcLoader = null;
Future<Collections> odcCatalog = null;
try {
if (queryCollectionsOnStartup) {
boolean hasWcpsEndpoint = !wcpsEndpoint.isEmpty();
boolean hasOdcEndpoint = !odcCollEndpoint.isEmpty();
// WC(P)S collections loader
if (hasWcpsEndpoint) {
wcsLoader = WCSCollectionsLoader.Builder
.of(wcpsEndpoint)
.version(wcpsVersion)
.provider(new Providers()
.name(wcpsProviderName)
.roles(wcpsProviderType)
.url(new URI(wcpsProviderUrl)))
.cache(collectionsFileWCPS)
.parallelism(parallelizedHarvest ? -1 : 1)
.build();
}
// ODC collections loader
if (hasOdcEndpoint) {
odcLoader = ODCCollectionsLoader.Builder
.of(odcCollEndpoint)
.cache(collectionsFileODC)
.build();
}
if (!hasWcpsEndpoint && !hasOdcEndpoint) {
log.error("No STAC endpoint was specified.");
// TODO: what to do here? Throw exception?
}
} else {
// cached STAC-WCS catalogue loader
wcsLoader = STACFileCollectionsLoader.Builder
.of(collectionsFileWCPS)
.engine(EngineTypes.WCPS)
.build();
// cached STAC-ODC catalogue loader
odcLoader = STACFileCollectionsLoader.Builder
.of(collectionsFileODC)
.engine(EngineTypes.ODC_DASK)
.build();
}
} catch (URISyntaxException e) {
log.error("Invalid URI provided.", e);
}
if (null != wcsLoader) {
wcsCatalog = exec.submit(wcsLoader);
}
if (null != odcLoader) {
odcCatalog = exec.submit(odcLoader);
}
// collect harvested collections
try {
if (null != wcsCatalog) {
collectionsMap.put(wcsLoader.getEngineType(), wcsCatalog.get());
}
if (null != odcCatalog) {
collectionsMap.put(odcLoader.getEngineType(), odcCatalog.get());
}
} catch (InterruptedException | ExecutionException e) {
log.error("Error while loading catalog.", e);
}
log.debug(collectionsMap.keySet());
// { collID -> coll } mapping
for (EngineTypes type : collectionsMap.keySet()) {
collectionsMap.get(type).getCollections()
.forEach(coll -> collectionMap.put(coll.getId(), coll));
}
// profiling
Instant end = Instant.now();
long ds = Duration.between(start, end).toMillis() / 1000; // TODO from Java 9: .toSeconds()
long dms = Duration.between(start, end).minusSeconds(ds).toMillis();
log.printf(Level.INFO, "Catalogues harvesting done (%d.%03d s).", ds, dms);
}
@Override
public Optional<NativeWebRequest> getRequest() {
return Optional.ofNullable(request);
}
/**
* GET /collections : Basic metadata for all datasets Lists available
* collections with at least the required information. It is **strongly
* RECOMMENDED** to keep the response size small by omitting larger optional
* values from the objects in `collections` (e.g. the
* `summaries` and `cube:dimensions` properties). To get the
* full metadata for a collection clients MUST request `GET
* /collections/{collection_id}`. This endpoint is compatible with [STAC
* 0.9.0](https://stacspec.org) and [OGC API -
* Features](http://docs.opengeospatial.org/is/17-069r3/17-069r3.html). [STAC
* API](https://github.com/radiantearth/stac-spec/tree/v0.9.0/api-spec) features
* / extensions and [STAC
* extensions](https://github.com/radiantearth/stac-spec/tree/v0.9.0/extensions)
* can be implemented in addition to what is documented here.
*
* @param limit This parameter enables pagination for the endpoint and specifies
* the maximum number of elements that arrays in the top-level
* object (e.g. jobs or log entries) are allowed to contain. The
* only exception is the `links` array, which MUST NOT be
* paginated as otherwise the pagination links may be missing ins
* responses. If the parameter is not provided or empty, all
* elements are returned. Pagination is OPTIONAL and back-ends and
* clients may not support it. Therefore it MUST be implemented in
* a way that clients not supporting pagination get all resources
* regardless. Back-ends not supporting pagination will return all
* resources. If the response is paginated, the links array MUST be
* used to propagate the links for pagination with pre-defined
* `rel` types. See the links array schema for supported
* `rel` types. *Note:* Implementations can use all kind
* of pagination techniques, depending on what is supported best by
* their infrastructure. So it doesn't care whether it is
* page-based, offset-based or uses tokens for pagination. The
* clients will use whatever is specified in the links with the
* corresponding `rel` types. (optional)
* @return Lists of collections and related links. (status code 200) or The
* request can't be fulfilled due to an error on client-side, i.e.
* the request is invalid. The client should not repeat the request
* without modifications. The response body SHOULD contain a JSON error
* object. MUST be any HTTP status code specified in [RFC
* 7231](https://tools.ietf.org/html/rfc7231#section-6.6). This request
* MUST respond with HTTP status codes 401 if authorization is required
* or 403 if the authorization failed or access is forbidden in general
* to the authenticated user. HTTP status code 404 should be used if the
* value of a path parameter is invalid. See also: * [Error
* Handling](#section/API-Principles/Error-Handling) in the API in
* general. * [Common Error Codes](errors.json) (status code 400) or The
* request can't be fulfilled due to an error at the back-end. The
* error is never the client’s fault and therefore it is reasonable for
* the client to retry the exact same request that triggered this
* response. The response body SHOULD contain a JSON error object. MUST
* be any HTTP status code specified in [RFC
* 7231](https://tools.ietf.org/html/rfc7231#section-6.6). See also: *
* [Error Handling](#section/API-Principles/Error-Handling) in the API
* in general. * [Common Error Codes](errors.json) (status code 500)
*/
@Operation(summary = "Basic metadata for all datasets", operationId = "listCollections", description = "Lists available collections with at least the required information. It is **strongly RECOMMENDED** to keep the response size small by omitting larger optional values from the objects in `collections` (e.g. the `summaries` and `cube:dimensions` properties). To get the full metadata for a collection clients MUST request `GET /collections/{collection_id}`. This endpoint is compatible with [STAC 0.9.0](https://stacspec.org) and [OGC API - Features](http://docs.opengeospatial.org/is/17-069r3/17-069r3.html). [STAC API](https://github.com/radiantearth/stac-spec/tree/v0.9.0/api-spec) features / extensions and [STAC extensions](https://github.com/radiantearth/stac-spec/tree/v0.9.0/extensions) can be implemented in addition to what is documented here.")
@ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Lists of collections and related links."),
@ApiResponse(responseCode = "400", description = "The request can't be fulfilled due to an error on client-side, i.e. the request is invalid. The client should not repeat the request without modifications. The response body SHOULD contain a JSON error object. MUST be any HTTP status code specified in [RFC 7231](https://tools.ietf.org/html/rfc7231#section-6.6). This request MUST respond with HTTP status codes 401 if authorization is required or 403 if the authorization failed or access is forbidden in general to the authenticated user. HTTP status code 404 should be used if the value of a path parameter is invalid. See also: * [Error Handling](#section/API-Principles/Error-Handling) in the API in general. * [Common Error Codes](errors.json)"),
@ApiResponse(responseCode = "500", description = "The request can't be fulfilled due to an error at the back-end. The error is never the client’s fault and therefore it is reasonable for the client to retry the exact same request that triggered this response. The response body SHOULD contain a JSON error object. MUST be any HTTP status code specified in [RFC 7231](https://tools.ietf.org/html/rfc7231#section-6.6). See also: * [Error Handling](#section/API-Principles/Error-Handling) in the API in general. * [Common Error Codes](errors.json)") })
@GetMapping(value = "/collections", produces = { "application/json" })
@Override
public ResponseEntity<Collections> listCollections(
@Min(1) @Parameter(name = "This parameter enables pagination for the endpoint and specifies the maximum number of elements that arrays in the top-level object (e.g. jobs or log entries) are allowed to contain. The only exception is the `links` array, which MUST NOT be paginated as otherwise the pagination links may be missing ins responses. If the parameter is not provided or empty, all elements are returned. Pagination is OPTIONAL and back-ends and clients may not support it. Therefore it MUST be implemented in a way that clients not supporting pagination get all resources regardless. Back-ends not supporting pagination will return all resources. If the response is paginated, the links array MUST be used to propagate the links for pagination with pre-defined `rel` types. See the links array schema for supported `rel` types. *Note:* Implementations can use all kind of pagination techniques, depending on what is supported best by their infrastructure. So it doesn't care whether it is page-based, offset-based or uses tokens for pagination. The clients will use whatever is specified in the links with the corresponding `rel` types.") @Valid @RequestParam(value = "limit", required = false) Integer limit) {
Collections collectionsList = new Collections();
for (EngineTypes type : collectionsMap.keySet()) {
for (Collection currentCollection : collectionsMap.get(type).getCollections()) {
collectionsList.addCollectionsItem(currentCollection);
}
}
return new ResponseEntity<Collections>(collectionsList, HttpStatus.OK);
}
/**
* GET /collections/{collection_id} : Full metadata for a specific dataset Lists
* **all** information about a specific collection specified by the identifier
* `collection_id`. This endpoint is compatible with [STAC
* 0.9.0](https://stacspec.org) and [OGC API -
* Features](http://docs.opengeospatial.org/is/17-069r3/17-069r3.html). [STAC
* API](https://github.com/radiantearth/stac-spec/tree/v0.9.0/api-spec) features
* / extensions and [STAC
* extensions](https://github.com/radiantearth/stac-spec/tree/v0.9.0/extensions)
* can be implemented in addition to what is documented here.
*
* @param collectionId Collection identifier (required)
* @return JSON object with the full collection metadata. (status code 200) or
* The request can't be fulfilled due to an error on client-side,
* i.e. the request is invalid. The client should not repeat the request
* without modifications. The response body SHOULD contain a JSON error
* object. MUST be any HTTP status code specified in [RFC
* 7231](https://tools.ietf.org/html/rfc7231#section-6.6). This request
* MUST respond with HTTP status codes 401 if authorization is required
* or 403 if the authorization failed or access is forbidden in general
* to the authenticated user. HTTP status code 404 should be used if the
* value of a path parameter is invalid. See also: * [Error
* Handling](#section/API-Principles/Error-Handling) in the API in
* general. * [Common Error Codes](errors.json) (status code 400) or The
* request can't be fulfilled due to an error at the back-end. The
* error is never the client’s fault and therefore it is reasonable for
* the client to retry the exact same request that triggered this
* response. The response body SHOULD contain a JSON error object. MUST
* be any HTTP status code specified in [RFC
* 7231](https://tools.ietf.org/html/rfc7231#section-6.6). See also: *
* [Error Handling](#section/API-Principles/Error-Handling) in the API
* in general. * [Common Error Codes](errors.json) (status code 500)
*/
@Operation(summary = "Full metadata for a specific dataset", operationId = "describeCollecion", description = "Lists **all** information about a specific collection specified by the identifier `collection_id`. This endpoint is compatible with [STAC 0.9.0](https://stacspec.org) and [OGC API - Features](http://docs.opengeospatial.org/is/17-069r3/17-069r3.html). [STAC API](https://github.com/radiantearth/stac-spec/tree/v0.9.0/api-spec) features / extensions and [STAC extensions](https://github.com/radiantearth/stac-spec/tree/v0.9.0/extensions) can be implemented in addition to what is documented here.", tags = {
"EO Data Discovery", })
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "JSON object with the full collection metadata."),
@ApiResponse(responseCode = "400", description = "The request can't be fulfilled due to an error on client-side, i.e. the request is invalid. The client should not repeat the request without modifications. The response body SHOULD contain a JSON error object. MUST be any HTTP status code specified in [RFC 7231](https://tools.ietf.org/html/rfc7231#section-6.6). This request MUST respond with HTTP status codes 401 if authorization is required or 403 if the authorization failed or access is forbidden in general to the authenticated user. HTTP status code 404 should be used if the value of a path parameter is invalid. See also: * [Error Handling](#section/API-Principles/Error-Handling) in the API in general. * [Common Error Codes](errors.json)"),
@ApiResponse(responseCode = "500", description = "The request can't be fulfilled due to an error at the back-end. The error is never the client’s fault and therefore it is reasonable for the client to retry the exact same request that triggered this response. The response body SHOULD contain a JSON error object. MUST be any HTTP status code specified in [RFC 7231](https://tools.ietf.org/html/rfc7231#section-6.6). See also: * [Error Handling](#section/API-Principles/Error-Handling) in the API in general. * [Common Error Codes](errors.json)") })
@GetMapping(value = "/collections/{collection_id}", produces = { "application/json" })
@Override
public ResponseEntity<Collection> describeCollection(
@Pattern(regexp = "^[\\w\\-\\.~/]+$") @Parameter(name = "Collection identifier", required = true) @PathVariable("collection_id") String collectionId,
Principal principal) {
// log.debug("The following user is authenticated: " + principal.getName());
// Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
// if (!(authentication instanceof AnonymousAuthenticationToken)) {
// String currentUserName = authentication.getName();
// log.debug("The following user is authenticated: " + currentUserName);
// }else {
// log.warn("The current user is not authenticated!");
// }
Collection currentCollection = collectionMap.get(collectionId);
if (currentCollection != null) {
return new ResponseEntity<Collection>(currentCollection, HttpStatus.OK);
}
return new ResponseEntity<Collection>(HttpStatus.NOT_FOUND);
}
/**
* GET /collections/{collection_id}/coverage : Full metadata for a specific dataset Lists
* **all** information about a specific collection specified by the identifier
* `collection_id`. This endpoint is compatible with [STAC
* 0.9.0](https://stacspec.org) and [OGC API -
* Features](http://docs.opengeospatial.org/is/17-069r3/17-069r3.html). [STAC
* API](https://github.com/radiantearth/stac-spec/tree/v0.9.0/api-spec) features
* / extensions and [STAC
* extensions](https://github.com/radiantearth/stac-spec/tree/v0.9.0/extensions)
* can be implemented in addition to what is documented here.
*
* @deprecated NOTE: this feature is experimental
*
* @param collectionId Collection identifier (required)
* @return JSON object with the full collection metadata. (status code 200) or
* The request can't be fulfilled due to an error on client-side,
* i.e. the request is invalid. The client should not repeat the request
* without modifications. The response body SHOULD contain a JSON error
* object. MUST be any HTTP status code specified in [RFC
* 7231](https://tools.ietf.org/html/rfc7231#section-6.6). This request
* MUST respond with HTTP status codes 401 if authorization is required
* or 403 if the authorization failed or access is forbidden in general
* to the authenticated user. HTTP status code 404 should be used if the
* value of a path parameter is invalid. See also: * [Error
* Handling](#section/API-Principles/Error-Handling) in the API in
* general. * [Common Error Codes](errors.json) (status code 400) or The
* request can't be fulfilled due to an error at the back-end. The
* error is never the client’s fault and therefore it is reasonable for
* the client to retry the exact same request that triggered this
* response. The response body SHOULD contain a JSON error object. MUST
* be any HTTP status code specified in [RFC
* 7231](https://tools.ietf.org/html/rfc7231#section-6.6). See also: *
* [Error Handling](#section/API-Principles/Error-Handling) in the API
* in general. * [Common Error Codes](errors.json) (status code 500)
*/
@Deprecated
@Operation(summary = "Full metadata for a specific dataset", operationId = "describeCollecion", description = "Lists **all** information about a specific collection specified by the identifier `collection_id`. This endpoint is compatible with [STAC 0.9.0](https://stacspec.org) and [OGC API - Features](http://docs.opengeospatial.org/is/17-069r3/17-069r3.html). [STAC API](https://github.com/radiantearth/stac-spec/tree/v0.9.0/api-spec) features / extensions and [STAC extensions](https://github.com/radiantearth/stac-spec/tree/v0.9.0/extensions) can be implemented in addition to what is documented here.", tags = {
"EO Data Discovery", })
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "JSON object with the full collection metadata."),
@ApiResponse(responseCode = "400", description = "The request can't be fulfilled due to an error on client-side, i.e. the request is invalid. The client should not repeat the request without modifications. The response body SHOULD contain a JSON error object. MUST be any HTTP status code specified in [RFC 7231](https://tools.ietf.org/html/rfc7231#section-6.6). This request MUST respond with HTTP status codes 401 if authorization is required or 403 if the authorization failed or access is forbidden in general to the authenticated user. HTTP status code 404 should be used if the value of a path parameter is invalid. See also: * [Error Handling](#section/API-Principles/Error-Handling) in the API in general. * [Common Error Codes](errors.json)"),
@ApiResponse(responseCode = "500", description = "The request can't be fulfilled due to an error at the back-end. The error is never the client’s fault and therefore it is reasonable for the client to retry the exact same request that triggered this response. The response body SHOULD contain a JSON error object. MUST be any HTTP status code specified in [RFC 7231](https://tools.ietf.org/html/rfc7231#section-6.6). See also: * [Error Handling](#section/API-Principles/Error-Handling) in the API in general. * [Common Error Codes](errors.json)") })
@GetMapping(value = "/collections/{collection_id}/coverage", produces = { "*" })
@Override
public ResponseEntity<?> getCoverage(@Pattern(regexp="^[\\w\\-\\.~/]+$") @Parameter(name = "Collection identifier",required=true) @PathVariable("collection_id") String collectionId,
@RequestParam(name = "subset") Optional<String> subsetList,
@RequestParam(name = "bbox") Optional<List<Double>> bboxList,
@RequestParam(name = "datetime") Optional<String> temporalString,
@RequestParam(name = "f") Optional<String> requestedFileFormat,
@RequestParam(name = "properties") Optional<List<String>> requestedProperties,
Principal principal) {
if (principal == null) {
return ApiUtil.errorResponse(HttpStatus.FORBIDDEN,"Please authenticate!");
}
// TODO: scale-axis -> spatial dimension -> map to openEO resample_spatial. Temporal and other -> return NOT_IMPLEMENTED
// TODO: if only one slice provided (subset or datetime), translate into reduce_dimension + mean.
// String username = new String();
try{
principal.getName();
} catch(Exception e){
log.error("Can't get name from principal. Trying to continue.");
// TODO why this?
}
AccessToken token = TokenUtil.getAccessToken(principal, tokenService);
// if (null != token) {
// username = token.getName();
// }
// URL url;
// Parse requested media type from request header
String requestedFileFormatHeader = null;
// FIXME all this is not robust:
final String NETCDF = "netcdf";
final String TIFF = "tif";
final String GTIFF = "geotiff";
if (getRequest().isPresent()){
Iterator<String> headerNames = request.getHeaderNames();
if (headerNames.hasNext()) {
String header = null;
String headerValue = null;
for (Iterator<String> iter = headerNames; iter.hasNext();) {
header = iter.next();
headerValue = request.getHeader(header);
log.debug("Header {}:{}:", header, headerValue);
if (HttpHeaders.ACCEPT.equals(header)) {
if (headerValue.contains(NETCDF)) {
requestedFileFormatHeader = NETCDF;
}
else if (headerValue.contains(TIFF)) {
requestedFileFormatHeader = GTIFF;
}
}
}
}
}
//We enforce the bbox and datetime parameters only if we want to return the data and not only the metadata
//"png" "geotiff" "netcdf" "json" "covjson" "html"
String fileFormat = NETCDF;
if (requestedFileFormat.isPresent()){
fileFormat = requestedFileFormat.get();
}
// The file format from the request header has higher priority
if (requestedFileFormatHeader != null) {
fileFormat = requestedFileFormatHeader;
}
// Return an error if bbox or datetime are specified together as subset
// if ((bboxList.isPresent() || temporalString.isPresent()) && (subsetList.isPresent())) {
// return ApiUtil.errorResponse(HttpStatus.FORBIDDEN,"Please provide either bbox and datetime parameters, or subset. Can't parse both at the same time!");
// }
if (subsetList.isPresent()) {
return ApiUtil.errorResponse(
HttpStatus.NOT_IMPLEMENTED,
"Subset parameter not yet supported!");
}
// Return an error if no spatial and temporal subsets are specified
// FIXME
if ((bboxList.isEmpty() || temporalString.isEmpty())
&& (fileFormat==GTIFF || fileFormat==NETCDF)) {
return ApiUtil.errorResponse(
HttpStatus.FORBIDDEN,
"Please provide the bbox and datetime parameters, otherwise the file would be too large.");
}
// properties (bands) mapping
// TODO: properties can be integers. I need to map them to their label
String[] properties = null;
if (requestedProperties.isPresent()){
properties = requestedProperties.get().toArray(String[]::new);
}
// subset=Lon(9.8382568359375,11.2554931640625),Lat(43.5882568359375,45.0054931640625),time(%22023-10-01T0:00:00Z%22)
/*
* Space
*/
double reqW = bboxList.get().get(0);
double reqS = bboxList.get().get(1);
double reqE = bboxList.get().get(2);
double reqN = bboxList.get().get(3);
// for process graph:
Map<String,Double> spatialExtent = new LinkedHashMap<>();
spatialExtent.put("west", reqW);
spatialExtent.put("east", reqE);
spatialExtent.put("north", reqN);
spatialExtent.put("south", reqS);
// check that the provided spatial extent is available in the collection, otherwise return an error
Collection collection = collectionMap.get(collectionId);
double collW = collection.getExtent().getSpatial().getBbox().get(0).get(0).doubleValue();
double collE = collection.getExtent().getSpatial().getBbox().get(0).get(2).doubleValue();
double collS = collection.getExtent().getSpatial().getBbox().get(0).get(1).doubleValue();
double collN = collection.getExtent().getSpatial().getBbox().get(0).get(3).doubleValue();
boolean noIntersection =
(reqE < collW) ||
(reqW > collE) ||
(reqN < collS) ||
(reqS > collN);
if (noIntersection) {
return ResponseEntity.noContent().build(); // or:
// return ApiUtil.errorResponse(
// HttpStatus.NOT_FOUND,
// String.format("Input bbox does not intersect with collection extent: %s", collBbox));
}
/*
* Time
* (specs: https://docs.ogc.org/DRAFTS/20-024.html#datetime-parameter-requirements)
*/
String[] temporalExtent = new String[2];
String[] temporalExtentSplit = temporalString.get().split("/");
switch (temporalExtentSplit.length) {
case 1:
temporalExtent[0] = temporalExtentSplit[0];
temporalExtent[1] = temporalExtent[0];
break;
case 2:
temporalExtent[0] = temporalExtentSplit[0];
temporalExtent[1] = temporalExtentSplit[1];
break;
default:
return ApiUtil.errorResponse(
HttpStatus.BAD_REQUEST,
String.format("Illegal input time interval: %s", temporalString));
}
List<?> collTInt = collection.getExtent().getTemporal().getInterval();
OffsetDateTime collT0 = collection.getExtent().getTemporal().getInterval().get(0).get(0);
OffsetDateTime collT1 = collection.getExtent().getTemporal().getInterval().get(0).get(1);
final String DATETIME_OPEN = "..";
if (DATETIME_OPEN == temporalExtent[0] && DATETIME_OPEN == temporalExtent[1]) {
return ApiUtil.errorResponse(
HttpStatus.BAD_REQUEST,
String.format("Input time interval cannot be both open-start and open-end: %s", temporalString));
}
// fallback to collection temporal extent on open intervals
OffsetDateTime reqT0 = (DATETIME_OPEN == temporalExtent[0])
? collT0
: OffsetDateTime.parse(temporalExtent[0]);
//
OffsetDateTime reqT1 = (DATETIME_OPEN == temporalExtent[1])
? collT1
: OffsetDateTime.parse(temporalExtent[1]);
noIntersection =
(reqT1.isBefore(collT0)) ||
(reqT0.isAfter(collT1));
if (noIntersection) {
return ResponseEntity.noContent().build(); // or:
// return ApiUtil.errorResponse(
// HttpStatus.NOT_FOUND,
// String.format("Input time interval does not intersect with collection extent: %s", collTInt));
}
if (reqT0.isEqual(reqT1)) {
// ODC throws: (psycopg2.errors.DataException) range lower bound must be less than or equal to range upper bound
// but dates are actually equal. Why then? TODO
// workaround:
// reqT1 = reqT1.plusSeconds(1l);
reqT1 = reqT1.plusDays(1l); // FIXME
temporalExtent[1] = reqT1.format(OffsetDateTimeSerializer.FORMATTER);
}
//1. Mapping of Collection to CIS JSON METADATA SCHEMA, coverageByDomainAndRange
//2. Create job object for the download:
//2.1 Translate to openEO Process Graph with load_collection + save_result
//3. Submit job to jobsApiController
//4. Get /jobs/job_id/result from JobsApiController and add link to output JSON RangeSet
Map<String, Object> loadSaveProcessGraph = new LinkedHashMap<>();
Map<String, Object> loadNode = new LinkedHashMap<>();
Map<String, Object> loadArguments = new LinkedHashMap<>();
Map<String, Object> saveNode = new LinkedHashMap<>();
Map<String, Object> saveArguments = new LinkedHashMap<>();
Map<String, Object> saveArgumentsInner = new LinkedHashMap<>();
loadArguments.put("id",collectionId);
loadArguments.put("spatial_extent", spatialExtent);
loadArguments.put("temporal_extent", temporalExtent);
if (properties!=null) {
loadArguments.put("bands",properties);
}
else {
loadArguments.put("bands",null);
}
loadNode.put("arguments",loadArguments);
loadNode.put("process_id","load_collection");
loadSaveProcessGraph.put("load1",loadNode);
saveArguments.put("format",fileFormat);
saveArgumentsInner.put("from_node","load1");
saveArguments.put("data",saveArgumentsInner);
saveNode.put("arguments",saveArguments);
saveNode.put("process_id","save_result");
saveNode.put("result",true);
loadSaveProcessGraph.put("save1",saveNode);
Process process = new Process();
process.setProcessGraph(loadSaveProcessGraph);
Job job = new Job();
BatchJobResult jobResult = BatchJobResult.ofType(AssetType.FEATURE);
job.setProcess(process);
job.setTitle(String.format("OGC-Coverage-%s-%s", fileFormat, OffsetDateTime.now()));
ResponseEntity<?> response = jobsApiController.createJob(job, principal);
/*
* PROBLEM
* Information in the logs cannot be saved in the Job obj and gets lost
* for the client.
*/
job = (Job) response.getBody();
response = jobsApiController.startJob(job.getId().toString());
// response = jobsApiController.downloadResult(job.getId().toString(), filePath); ?
boolean jobFinishedSuccessfully = false;
for (int i = 0; i < 150; i++) { // FIXME ??
try {
Thread.sleep(2000); // FIXME ?
ResponseEntity<?> jobDescription = jobsApiController.describeJob(job.getId().toString());
job = (Job) jobDescription.getBody();
log.debug("Job status: {}", job.getStatus());
if (job.getStatus() == JobStates.FINISHED) {
jobFinishedSuccessfully = true;
break;
}
else if (job.getStatus() == JobStates.ERROR) {
jobFinishedSuccessfully = false;
break;
}
} catch (InterruptedException e) {
log.error("Request interrupted!");
Thread.currentThread().interrupt();
}
}
if (!jobFinishedSuccessfully) {
// FIXME
return ApiUtil.errorResponse(
HttpStatus.INTERNAL_SERVER_ERROR,
"The request did not finished within the timeout limit, or failed.");
}
ResponseEntity<?> jobListResults = jobsApiController.listResults(job.getId().toString());
jobResult = (BatchJobResult) jobListResults.getBody();
Map<String, Asset> resultAssets = jobResult.getAssets();
String resultKey = new String();
for (Map.Entry<String, Asset> entry : resultAssets.entrySet()) {
log.trace("{} : {}", entry.getKey(), entry.getValue());
if (entry.getKey().contains("result")){
resultKey = entry.getKey();
}
}
Asset outputAsset = resultAssets.get(resultKey);
String outputFilePath = outputAsset.getHref();
try {
log.debug("Result path: '{}'", outputFilePath);
File outputFile = new File(outputFilePath);
String mime = URLConnection.guessContentTypeFromName(outputFile.getName());
if (mime == null) {
try {
mime = ConvenienceHelper.getMimeFromFilename(outputFile.getName());
}
catch (Exception e){
log.warn("Could not derive MIME type from file name.", e);
}
}
log.debug("Guessed MIME type: {}", mime);
URL url = new URL(outputFilePath);
InputStream is = null;
byte[] outputFileBytes = null;
is = url.openStream();
outputFileBytes = IOUtils.toByteArray(is);
if (is != null) {
is.close();
}
if (mime == null) {
mime = "application/octet-stream";
}
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(mime))
.body(outputFileBytes);
}
catch (IOException e) {
log.error("Result file not found", e);
return ApiUtil.errorResponse(
HttpStatus.INTERNAL_SERVER_ERROR,
"Result file not found.");
}
// {
// "process_graph": {
// "load1": {
// "process_id": "load_collection",
// "arguments": {
// "id": "s2_l2a",
// "spatial_extent": null,
// "temporal_extent": null,
// "bands": null
// }
// },
// "save2": {
// "process_id": "save_result",
// "arguments": {
// "format": "NETCDF",
// "data": {
// "from_node": "load1"
// }
// },
// "result": true
// }
// },
// "parameters": []
// }
}
}