-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathSandboxController.groovy
225 lines (201 loc) · 7.63 KB
/
SandboxController.groovy
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
/*
* Copyright (C) 2016 Atlas of Living Australia
* All Rights Reserved.
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*/
package au.org.ala.spatial
import au.ala.org.ws.security.RequireApiKey
import au.org.ala.plugins.openapi.Path
import au.org.ala.spatial.dto.SandboxIngress
import au.org.ala.web.AuthService
import grails.converters.JSON
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.Parameter
import io.swagger.v3.oas.annotations.media.Content
import io.swagger.v3.oas.annotations.media.Schema
import io.swagger.v3.oas.annotations.parameters.RequestBody
import io.swagger.v3.oas.annotations.responses.ApiResponse
import io.swagger.v3.oas.annotations.security.SecurityRequirement
import org.springframework.web.multipart.MultipartFile
import javax.ws.rs.Produces
import static io.swagger.v3.oas.annotations.enums.ParameterIn.QUERY
class SandboxController {
SpatialObjectsService spatialObjectsService
SpatialConfig spatialConfig
def dataSource
def sandboxService
AuthService authService
@Operation(
method = "POST",
tags = "uploads",
operationId = "uploadCSV",
summary = "Upload a CSV or zipped CSV file containing occurrence points",
parameters = [
@Parameter(
name = "name",
in = QUERY,
description = "datasetName",
schema = @Schema(implementation = String),
required = true
)
],
requestBody = @RequestBody(
description = "Uploaded CSV or zipped CSV file",
content = @Content(
mediaType = 'application/zip',
schema = @Schema(
type = "string",
format = "binary"
)
)
),
responses = [
@ApiResponse(
description = "Recognised header and a dataResourceUid",
responseCode = "200",
content = [
@Content(
mediaType = "application/json"
)
]
)
],
security = [@SecurityRequirement(name = 'openIdConnect')]
)
@Path("/sandbox/upload")
@Produces("application/json")
@RequireApiKey
def upload() {
if (!spatialConfig.sandboxEnabled) {
return [error: "Sandbox is disabled"] as JSON
}
// Use linked hash map to maintain key ordering
Map<Object, Object> retMap = new LinkedHashMap<Object, Object>()
// Parse the request
Map<String, MultipartFile> items = request.getFileMap()
if (items.size() == 1) {
MultipartFile fileItem = items.values()[0]
SandboxIngress info = sandboxService.upload(fileItem, (String) params.name, authService.getUserId())
if (info) {
render info as JSON
return
} else {
retMap.put("error", "Failed to upload file")
}
} else {
retMap.put("error", items.size() + " files sent in request. A single zipped CSV file should be supplied.")
}
render retMap as JSON
}
// delete service
@Operation(
method = "DELETE",
tags = "uploads",
operationId = "deleteCSV",
summary = "Delete a CSV or zipped CSV file containing occurrence points",
parameters = [
@Parameter(
name = "id",
in = QUERY,
description = "datasetId",
schema = @Schema(implementation = String),
required = true
)
],
responses = [
@ApiResponse(
description = "Delete the file",
responseCode = "200",
content = [
@Content(
mediaType = "application/json"
)
]
)
],
security = [@SecurityRequirement(name = 'openIdConnect')]
)
@Path("/sandbox/delete")
@Produces("application/json")
@RequireApiKey
def delete() {
if (!spatialConfig.sandboxEnabled) {
return [error: "Sandbox is disabled"] as JSON
}
// Use linked hash map to maintain key ordering
Map<Object, Object> retMap = new LinkedHashMap<Object, Object>()
// Parse the request
String id = params.id
if (id) {
boolean successful = sandboxService.delete(id, authService.getUserId(), authService.userInRole("ROLE_ADMIN"))
if (successful) {
retMap.put("message", "File deleted")
render retMap as JSON
return
}
} else {
retMap.put("error", "No file id supplied")
}
render retMap as JSON, status: 500
}
// status service
@Operation(
method = "GET",
tags = "uploads",
operationId = "statusCSV",
summary = "Get the status of a CSV or zipped CSV file containing occurrence points",
parameters = [
@Parameter(
name = "id",
in = QUERY,
description = "datasetId",
schema = @Schema(implementation = String),
required = true
)
],
responses = [
@ApiResponse(
description = "Status of the file",
responseCode = "200",
content = [
@Content(
mediaType = "application/json"
)
]
)
],
security = [@SecurityRequirement(name = 'openIdConnect')]
)
@Path("/sandbox/status")
@Produces("application/json")
def status() {
if (!spatialConfig.sandboxEnabled) {
return [error: "Sandbox is disabled"] as JSON
}
// Use linked hash map to maintain key ordering
Map<Object, Object> retMap = new LinkedHashMap<Object, Object>()
// Parse the request
String id = params.id
if (id) {
SandboxIngress info = sandboxService.getStatus(id)
if (info) {
render info as JSON
return
} else {
retMap.put("error", "Failed to get status")
}
} else {
retMap.put("error", "No file id supplied")
}
render retMap as JSON, status: 500
}
}