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