Skip to content

Commit d15c031

Browse files
authored
Merge pull request #2 from uksrc/jsonSupport
Json support - finally merging!
2 parents 9792a1f + 86ff567 commit d15c031

File tree

13 files changed

+562
-373
lines changed

13 files changed

+562
-373
lines changed

detail.md

Lines changed: 39 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,36 @@ Details of the functionality of the archive-service endpoints.
55
------------------------------------------------------------------------------------------
66
Example resources suitable for **minimal** testing (Mandatory properties only).
77
#### Example Simple Observation
8+
Namespace details must conform with the current vo-dml model used.
89
```xml
9-
<observation>
10-
<id>123456</id>
10+
<SimpleObservation xmlns:caom2="http://ivoa.net/dm/models/vo-dml/experiment/caom2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="caom2:caom2.SimpleObservation">
11+
<id>988</id>
1112
<collection>e-merlin</collection>
12-
<intent>science</intent>
1313
<uri>auri</uri>
14-
</observation>
14+
<intent>science</intent>
15+
</SimpleObservation>
16+
```
17+
18+
*@type* has to be set for JSON
19+
```json
20+
{
21+
"@type": "caom2:caom2.SimpleObservation",
22+
"id": "myData12345",
23+
"collection": "test",
24+
"uri": "auri",
25+
"intent": "science"
26+
}
1527
```
1628
#### Example Derived Observation
1729
```xml
18-
<Observation>
19-
<id>999</id>
20-
<collection>e-merlin</collection>
21-
<intent>science</intent>
30+
<DerivedObservation xmlns:caom2="http://ivoa.net/dm/models/vo-dml/experiment/caom2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="caom2:caom2.DerivedObservation">
31+
<id>10</id>
32+
<collection>test</collection>
2233
<uri>auri</uri>
23-
<members>anyURI</members>
24-
</Observation>
34+
<intent>science</intent>
35+
<members>jbo-simple1</members>
36+
<members>jbo-simple2</members>
37+
</DerivedObservation>
2538
```
2639
------------------------------------------------------------------------------------------
2740
### REST API details
@@ -34,10 +47,11 @@ Endpoints available for interaction with the archive-service.
3447

3548
##### Parameters
3649

37-
> | name | type | data type | description |
38-
> |------|----------|-----------|--------------------------------------------------------------------------------|
39-
> | page | optional | integer | The page index, zero-indexed |
40-
> | size | optional | integer | The number of observations to return for each page, must be greater than zero. |
50+
> | name | type | data type | description |
51+
> |--------------|----------|-----------|--------------------------------------------------------------------------------|
52+
> | collectionId | optional | String | Filter by collection Id if required (not supplying will return all). |
53+
> | page | optional | integer | The page index, zero-indexed |
54+
> | size | optional | integer | The number of observations to return for each page, must be greater than zero. |
4155
4256

4357
##### Responses
@@ -82,39 +96,14 @@ Endpoints available for interaction with the archive-service.
8296
8397
</details>
8498
85-
<details>
86-
<summary><code>GET</code> <code><b>/observations/collection/{collectionId}</b></code> <code>(Returns all observations for the supplied collectionId, if found)</code></summary>
87-
88-
##### Parameters
89-
90-
> | name | type | data type | description |
91-
> |--------------|----------|-----------|--------------------------------------------------------------------------------|
92-
> | collectionId | required | String | The unique identifier of a specific collection |
93-
> | page | optional | integer | The page index, zero-indexed |
94-
> | size | optional | integer | The number of observations to return for each page, must be greater than zero. |
9599
96100
97-
##### Responses
98-
99-
> | http code | content-type | response |
100-
> |-----------|-------------------|-------------------------------------------------------------------------------|
101-
> | `201` | `application/xml` | `List of Observation (Simple and/or Derived) found and returned successfully` |
102-
> | `400` | `text/plain` | `{"code":"400","message":"Bad Request"}` |
103-
104-
##### Example cURL
105-
106-
> ```
107-
> curl -X 'GET' 'http://localhost:8080/observations/23456' -H 'accept: application/xml'
108-
> ```
109-
110-
</details>
111-
112101
------------------------------------------------------------------------------------------
113102
114103
#### Adding new Observations
115104
116105
<details>
117-
<summary><code>POST</code> <code><b>/observations/add</b></code> <code>(Add a new observation)</code></summary>
106+
<summary><code>POST</code> <code><b>/observations</b></code> <code>(Add a new observation)</code></summary>
118107
119108
##### Responses
120109
@@ -123,38 +112,25 @@ Endpoints available for interaction with the archive-service.
123112
> | `201` | `application/xml` | `Observation added successfully, body contains new Observation` |
124113
> | `400` | `text/plain` | `{"code":"400","message":"Bad Request"}` |
125114
126-
##### Example cURL
115+
##### Example XML cURL
127116
128117
> ```
129-
> curl -v --header "Content-Type: application/xml" -T observation1.xml http://localhost:8080/observations/add
118+
> curl -X 'POST' -H 'Content-Type: application/xml' -H 'accept: application/xml' --data "@observation2.json" http://localhost:8080/observations
130119
> ```
131120
132-
</details>
133-
134-
<details>
135-
<summary><code>POST</code> <code><b>/observations/derived/add</b></code> <code>(Add a new derived observation)</code></summary>
136-
137-
##### Responses
138-
139-
> | http code | content-type | response |
140-
> |---------------|-------------------|------------------------------------------------------------------------|
141-
> | `201` | `application/xml` | `Observation added successfully, body contains new DerivedObservation` |
142-
> | `400` | `text/plain` | `{"code":"400","message":"Bad Request"}` |
143-
144-
##### Example cURL
145-
121+
##### Example JSON cURL
122+
with JSON response also
146123
> ```
147-
> curl -v --header "Content-Type: application/xml" -T observation1.xml http://localhost:8080/observations/derived/add
124+
> curl -X 'POST' -H 'Content-Type: application/json' -H 'accept: application/json' --data "@observation2.json" http://localhost:8080/observations
148125
> ```
149-
150126
</details>
151127
152128
------------------------------------------------------------------------------------------
153129
154130
#### Updating observations
155131
156132
<details>
157-
<summary><code>PUT</code> <code><b>/observations/update/{observationId}</b></code> <code>(Updates an observation (Simple or Derived) with the same observationId)</code></summary>
133+
<summary><code>UPDATE</code> <code><b>/observations/{observationId}</b></code> <code>(Updates an observation (Simple or Derived) with the same observationId)</code></summary>
158134
159135
##### Parameters
160136
@@ -174,7 +150,7 @@ Endpoints available for interaction with the archive-service.
174150
##### Example cURL
175151
176152
> ```
177-
> curl -v --header "Content-Type: application/xml" -T observation123.xml http://localhost:8080/observations/update/123
153+
> curl -X 'PUT' -H 'Content-Type: application/xml' -H 'Accept: application/xml' -T observation2.xml http://localhost:8080/observations/988
178154
> ```
179155
180156
</details>
@@ -184,7 +160,7 @@ Endpoints available for interaction with the archive-service.
184160
#### Deleting Observations
185161
186162
<details>
187-
<summary><code>GET</code> <code><b>/observations/{observationId}</b></code> <code>(Delete an Observation with the supplied ID, if found)</code></summary>
163+
<summary><code>DELETE</code> <code><b>/observations/{observationId}</b></code> <code>(Delete an Observation with the supplied ID, if found)</code></summary>
188164
189165
##### Parameters
190166
@@ -214,7 +190,7 @@ Endpoints available for interaction with the archive-service.
214190
#### Retrieving collections
215191
216192
<details>
217-
<summary><code>GET</code> <code><b>/observations/collections</b></code> <code>(Returns the names of all the collections as a TSV (Tab Separated List))</code></summary>
193+
<summary><code>GET</code> <code><b>/collections</b></code> <code>(Returns the names of all the collections as a TSV (Tab Separated List))</code></summary>
218194
219195
##### Responses
220196
@@ -227,7 +203,7 @@ Endpoints available for interaction with the archive-service.
227203
##### Example cURL
228204
229205
> ```
230-
> curl -X 'GET' -H 'accept: application/xml' 'http://localhost:8080/observations/collections'
206+
> curl -X 'GET' -H 'accept: application/xml' 'http://localhost:8080/collections'
231207
> ```
232208
233209
</details>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.uksrc.archive;
2+
3+
import jakarta.persistence.EntityManager;
4+
import jakarta.persistence.PersistenceContext;
5+
import jakarta.persistence.TypedQuery;
6+
import jakarta.ws.rs.*;
7+
import jakarta.ws.rs.core.MediaType;
8+
import jakarta.ws.rs.core.Response;
9+
import org.eclipse.microprofile.openapi.annotations.Operation;
10+
import org.eclipse.microprofile.openapi.annotations.media.Content;
11+
import org.eclipse.microprofile.openapi.annotations.media.Schema;
12+
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
13+
import org.uksrc.archive.utils.responses.Responses;
14+
import org.uksrc.archive.utils.tools.Tools;
15+
16+
import java.util.List;
17+
18+
@Path("/collections")
19+
public class CollectionResource {
20+
21+
@PersistenceContext
22+
protected EntityManager em;
23+
24+
@GET
25+
@Path("/")
26+
@Operation(summary = "Retrieve all collection IDs", description = "Returns a list of unique collectionIds as a TSV (Tab Separated List).")
27+
@APIResponse(
28+
responseCode = "200",
29+
description = "CollectionIds retrieved successfully",
30+
content = @Content(
31+
mediaType = MediaType.TEXT_PLAIN, schema = @Schema(implementation = String.class)
32+
)
33+
)
34+
@APIResponse(
35+
responseCode = "400",
36+
description = "Internal error whilst retrieving collectionIds."
37+
)
38+
@Produces(MediaType.TEXT_PLAIN)
39+
public Response getCollections(){
40+
try {
41+
TypedQuery<String> query = em.createQuery("SELECT DISTINCT o.collection FROM Observation o", String.class);
42+
List<String> uniqueCollections = query.getResultList();
43+
44+
return Response.ok()
45+
.type(MediaType.TEXT_PLAIN)
46+
.entity(Tools.convertListToTsv(uniqueCollections))
47+
.build();
48+
} catch (Exception e) {
49+
return Responses.errorResponse(e);
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)