-
Notifications
You must be signed in to change notification settings - Fork 8
/
AdminResource.java
387 lines (337 loc) · 13.6 KB
/
AdminResource.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
package org.icatproject.topcat.web.rest;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.ParseException;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.FormParam;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.icatproject.topcat.domain.Download;
import org.icatproject.topcat.domain.DownloadStatus;
import org.icatproject.topcat.domain.DownloadType;
import org.icatproject.topcat.domain.ConfVar;
import org.icatproject.topcat.exceptions.TopcatException;
import org.icatproject.topcat.exceptions.NotFoundException;
import org.icatproject.topcat.exceptions.BadRequestException;
import org.icatproject.topcat.exceptions.ForbiddenException;
import org.icatproject.topcat.exceptions.InternalException;
import org.icatproject.topcat.repository.DownloadRepository;
import org.icatproject.topcat.repository.DownloadTypeRepository;
import org.icatproject.topcat.repository.CacheRepository;
import org.icatproject.topcat.repository.ConfVarRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.icatproject.topcat.FacilityMap;
import org.icatproject.topcat.IcatClient;
@Stateless
@LocalBean
@Path("admin")
public class AdminResource {
private static final Logger logger = LoggerFactory.getLogger(AdminResource.class);
@EJB
private DownloadRepository downloadRepository;
@EJB
private DownloadTypeRepository downloadTypeRepository;
@EJB
private ConfVarRepository confVarRepository;
@EJB
private CacheRepository cacheRepository;
/**
* Returns whether or not the session provided has admin access - i.e. can use this "v1/admin/* api."
*
* @summary isValidSession
*
* @param facilityName
* a facility name - properties must map this to a url to a valid ICAT REST api.
*
* @param sessionId a valid session id which takes the form <code>0d9a3706-80d4-4d29-9ff3-4d65d4308a24</code>
*
* @return "true" or "false" if the session has admin access or not.
*
* @throws MalformedURLException if facilityName is invalid.
*
* @throws TopcatException if anything else goes wrong.
*/
@GET
@Path("/isValidSession")
@Produces({MediaType.APPLICATION_JSON})
public Response isValidSession(
@QueryParam("facilityName") String facilityName,
@QueryParam("sessionId") String sessionId)
throws MalformedURLException, TopcatException {
logger.info("isValidSession() called");
String icatUrl = getIcatUrl( facilityName );
IcatClient icatClient = new IcatClient(icatUrl, sessionId);
String isAdmin = icatClient.isAdmin() ? "true" : "false";
return Response.ok().entity(isAdmin).build();
}
/**
* Returns a list of downloads filtered by a partial JPQL expression.
*
* @summary getDownloads
*
* @param facilityName
* a facility name - properties must map this to a url to a valid ICAT REST api.
*
* @param sessionId a valid session id which takes the form <code>0d9a3706-80d4-4d29-9ff3-4d65d4308a24</code>
*
* @param queryOffset
* any JPQL expression that can be appended to "SELECT download from Download download",
* e.g. "where download.isDeleted = false". Note that like ICAT the syntax has been extended
* allowing (sql like) limit clauses in the form "limit [offset], [row count]" e.g. "limit 10, 20".
* Note the "status" attribute is an enum (not a string) i.e. org.icatproject.topcat.domain.Status
* with the following possible states: 'ONLINE', 'ARCHIVE' or 'RESTORING'. So an example query involving
* the status attribute could be "where download.status = org.icatproject.topcat.domain.Status.ARCHIVE limit 0, 10"
*
* @return returns an array of downloads in the form
* [{"completedAt":"2016-03-18T16:02:36","createdAt":"2016-03-18T16:02:36","deletedAt":"2016-03-18T16:02:47","downloadItems":[{"entityId":18064,"entityType":"datafile","id":2},{"entityId":18061,"entityType":"datafile","id":3}],"email":"","facilityName":"test","fileName":"test_2016-3-18_16-05-59","id":2,"isDeleted":false,"isTwoLevel":false,"preparedId":"6d3aaca5-da9f-4e6a-922d-eceeefcc07e0","status":"COMPLETE","size":324675,"transport":"https","userName":"simple/root"}]
*/
@GET
@Path("/downloads")
@Produces({MediaType.APPLICATION_JSON})
public Response getDownloads(
@QueryParam("facilityName") String facilityName,
@QueryParam("sessionId") String sessionId,
@QueryParam("queryOffset") String queryOffset)
throws TopcatException, MalformedURLException, ParseException {
String icatUrl = getIcatUrl( facilityName );
onlyAllowAdmin(icatUrl, sessionId);
Map<String, Object> params = new HashMap<String, Object>();
params.put("queryOffset", queryOffset);
List<Download> downloads = new ArrayList<Download>();
downloads = downloadRepository.getDownloads(params);
return Response.ok().entity(new GenericEntity<List<Download>>(downloads){}).build();
}
/**
* Sets the download status.
*
* @summary setDownloadStatus
*
* @param facilityName
* a facility name - properties must map this to a url to a valid ICAT REST api.
*
* @param sessionId a valid session id which takes the form <code>0d9a3706-80d4-4d29-9ff3-4d65d4308a24</code>
*
* @param id the download id in the database.
*
* @param value the status value i.e. 'ONLINE', 'ARCHIVE' or 'RESTORING'.
*
* @return an empty Response
*
* @throws MalformedURLException if facilityName is invalid.
*
* @throws ParseException if a JPQL query is malformed.
*
* @throws TopcatException if anything else goes wrong.
*/
@PUT
@Path("/download/{id}/status")
@Produces({MediaType.APPLICATION_JSON})
public Response setDownloadStatus(
@PathParam("id") Long id,
@FormParam("facilityName") String facilityName,
@FormParam("sessionId") String sessionId,
@FormParam("value") String value)
throws TopcatException, MalformedURLException, ParseException {
String icatUrl = getIcatUrl( facilityName );
onlyAllowAdmin(icatUrl, sessionId);
Download download = downloadRepository.getDownload(id);
if(download == null){
throw new NotFoundException("could not find download");
}
download.setStatus(DownloadStatus.valueOf(value));
if(value.equals("COMPLETE")){
download.setCompletedAt(new Date());
}
downloadRepository.save(download);
return Response.ok().build();
}
/**
* Sets whether or not a download is deleted.
*
* @summary deleteDownload
*
* @param facilityName
* a facility name - properties must map this to a url to a valid ICAT REST api.
*
* @param sessionId a valid session id which takes the form <code>0d9a3706-80d4-4d29-9ff3-4d65d4308a24</code>
*
* @param id the download id in the database.
*
* @param value either true or false.
*
* @return an empty Response
*
* @throws MalformedURLException if facilityName is invalid.
*
* @throws ParseException if a JPQL query is malformed.
*
* @throws TopcatException if anything else goes wrong.
*/
@PUT
@Path("/download/{id}/isDeleted")
@Produces({MediaType.APPLICATION_JSON})
public Response deleteDownload(
@PathParam("id") Long id,
@FormParam("facilityName") String facilityName,
@FormParam("sessionId") String sessionId,
@FormParam("value") Boolean value)
throws TopcatException, MalformedURLException, ParseException {
String icatUrl = getIcatUrl( facilityName );
onlyAllowAdmin(icatUrl, sessionId);
Download download = downloadRepository.getDownload(id);
if(download == null){
throw new NotFoundException("could not find download");
}
download.setIsDeleted(value);
if(value){
download.setDeletedAt(new Date());
}
downloadRepository.save(download);
return Response.ok().build();
}
/**
* Sets the specified downloadType's status.
*
* @summary setDownloadTypeStatus
*
* @param type name of the download type (as configured in topcat.json - downloadTransportTypes[].type)
* @param facilityName name of the facility
* @param sessionId a valid ICAT sessionId
* @param disabled flag: use True to disable, False to enable
* @param message a message to display to users to indicate that/why this download type is disabled
* @return an empty Response
* @throws MalformedURLException if facilityName is invalid
* @throws TopcatException if anything else goes wrong
*/
@PUT
@Path("/downloadType/{type}/status")
@Produces({MediaType.APPLICATION_JSON})
public Response setDownloadTypeStatus(
@PathParam("type") String type,
@FormParam("facilityName") String facilityName,
@FormParam("sessionId") String sessionId,
@FormParam("disabled") Boolean disabled,
@FormParam("message") String message)
throws MalformedURLException, TopcatException
{
String icatUrl = getIcatUrl( facilityName );
onlyAllowAdmin(icatUrl, sessionId);
// Update existing entry, or create a new one
DownloadType downloadType = downloadTypeRepository.getDownloadType(facilityName, type);
if( downloadType == null ) {
downloadType = new DownloadType();
downloadType.setFacilityName(facilityName);
downloadType.setDownloadType(type);
}
downloadType.setDisabled(disabled);
downloadType.setMessage(message);
downloadTypeRepository.save(downloadType);
return Response.ok().build();
}
/**
* Removes any cached value for the size of the specified entity.
*
* @summary clearCachedSize
*
* @param entityType the type of the entity: "investigation", "dataset" or "datafile".
*
* @param id the entity id.
*
* @param facilityName
* a facility name - properties must map this to a url to a valid ICAT REST api.
*
* @param sessionId a valid session id which takes the form <code>0d9a3706-80d4-4d29-9ff3-4d65d4308a24</code>
*
* @return an empty Response
*
* @throws MalformedURLException if facilityName is invalid.
*
* @throws TopcatException if anything else goes wrong.
*/
@DELETE
@Path("/clearSize/{entityType}/{id}")
@Produces({MediaType.APPLICATION_JSON})
public Response clearCachedSize(
@PathParam("entityType") String entityType,
@PathParam("id") Long id,
@QueryParam("facilityName") String facilityName,
@QueryParam("sessionId") String sessionId)
throws TopcatException, MalformedURLException {
String icatUrl = getIcatUrl( facilityName );
onlyAllowAdmin(icatUrl, sessionId);
String key = "getSize:" + entityType + ":" + id;
cacheRepository.remove(key);
return Response.ok().build();
}
private void onlyAllowAdmin(String icatUrl, String sessionId) throws TopcatException, MalformedURLException {
IcatClient icatClient = new IcatClient(icatUrl, sessionId);
if(icatUrl == null || sessionId == null || !icatClient.isAdmin()){
throw new ForbiddenException("please provide a valid facilityName and sessionId");
}
}
/**
* Stores a configuration variable.
*
* @summary setConfVar
*
* @param name name of the configuration variable
*
* @param facilityName
* a facility name - properties must map this to a url to a valid ICAT REST api.
*
* @param sessionId a valid session id which takes the form <code>0d9a3706-80d4-4d29-9ff3-4d65d4308a24</code>
*
* @param value a string containing the configuration variable value.
*
* @return an empty Response
*
* @throws TopcatException if anything else goes wrong.
*/
@PUT
@Path("/confVars/{name}")
@Produces({MediaType.APPLICATION_JSON})
public Response setConfVar(
@PathParam("name") String name,
@FormParam("facilityName") String facilityName,
@FormParam("sessionId") String sessionId,
@FormParam("value") String value)
throws TopcatException, MalformedURLException {
String icatUrl = getIcatUrl( facilityName );
onlyAllowAdmin(icatUrl, sessionId);
ConfVar confVar = confVarRepository.getConfVar(name);
if(confVar == null){
confVar = new ConfVar();
confVar.setName(name);
}
confVar.setValue(value);
confVarRepository.save(confVar);
return Response.ok().build();
}
private String getIcatUrl( String facilityName ) throws BadRequestException{
// Pass nulls through - subsequent code may want to handle them
if( facilityName == null){
logger.warn("AdminResource.getIcatUrl: facilityName is null. May be a request using the old API.");
return null;
}
try {
return FacilityMap.getInstance().getIcatUrl(facilityName);
} catch (InternalException ie){
throw new BadRequestException( ie.getMessage() );
}
}
}