1
|
package eu.dnetlib.datasource.publisher;
|
2
|
|
3
|
import java.util.ArrayList;
|
4
|
import java.util.List;
|
5
|
|
6
|
import com.google.common.collect.Lists;
|
7
|
import eu.dnetlib.common.rmi.DNetRestDocumentation;
|
8
|
import eu.dnetlib.datasource.publisher.clients.ClientResponse;
|
9
|
import eu.dnetlib.datasource.publisher.clients.DatasourceInfoRetriever;
|
10
|
import eu.dnetlib.datasource.publisher.model.DatasourceInfo;
|
11
|
|
12
|
import io.swagger.annotations.*;
|
13
|
|
14
|
import org.apache.commons.logging.Log;
|
15
|
import org.apache.commons.logging.LogFactory;
|
16
|
import org.springframework.beans.BeanUtils;
|
17
|
import org.springframework.beans.factory.annotation.Autowired;
|
18
|
import org.springframework.http.HttpStatus;
|
19
|
import org.springframework.http.ResponseEntity;
|
20
|
import org.springframework.stereotype.Controller;
|
21
|
import org.springframework.web.bind.annotation.PathVariable;
|
22
|
|
23
|
@Controller
|
24
|
@DNetRestDocumentation
|
25
|
public class DatasourcesApiController implements DatasourcesApi {
|
26
|
|
27
|
private static final Log log = LogFactory.getLog(DatasourcesApiController.class);
|
28
|
|
29
|
@Autowired
|
30
|
private DatasourceInfoRetriever dsInfoRetriever;
|
31
|
|
32
|
public ResponseEntity<List<String>> listIds() {
|
33
|
try {
|
34
|
final List<String> res = dsInfoRetriever.listIds();
|
35
|
return new ResponseEntity<>(res, HttpStatus.OK);
|
36
|
} catch (ApiException e) {
|
37
|
final List<String> res = Lists.newArrayList();
|
38
|
return new ResponseEntity<>(res, HttpStatus.INTERNAL_SERVER_ERROR);
|
39
|
}
|
40
|
}
|
41
|
|
42
|
public ResponseEntity<DatasourceInfo> getDs(@ApiParam(value = "ID of datasource to fetch", required=true ) @PathVariable("id") String id) {
|
43
|
|
44
|
if (log.isDebugEnabled()) {
|
45
|
log.debug(String.format("getDatasourceInfo(dsId = %s)", id));
|
46
|
}
|
47
|
final long start = System.nanoTime();
|
48
|
final ResponseEntity<DatasourceInfo> rsp = rsp(new DatasourceInfo(), HttpStatus.OK);
|
49
|
|
50
|
final ClientResponse clientResponse = dsInfoRetriever.getInfo(id);
|
51
|
BeanUtils.copyProperties(clientResponse.getDatasourceInfo(), rsp.getBody());
|
52
|
|
53
|
if (!clientResponse.getErrors().isEmpty()) {
|
54
|
return rsp(clientResponse.getDatasourceInfo(), HttpStatus.INTERNAL_SERVER_ERROR);
|
55
|
}
|
56
|
|
57
|
final long time = (System.nanoTime() - start) / 1000000;
|
58
|
|
59
|
rsp.getBody().getResponseHeader().setDatasourceId(id).setQueryTime(time);
|
60
|
log.debug(String.format("%s ms", time));
|
61
|
|
62
|
return rsp;
|
63
|
}
|
64
|
|
65
|
private ResponseEntity<DatasourceInfo> rsp(final DatasourceInfo info, final HttpStatus status) {
|
66
|
return new ResponseEntity<>(info, status);
|
67
|
}
|
68
|
|
69
|
}
|