1 |
44743
|
claudio.at
|
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 |
44765
|
claudio.at
|
import eu.dnetlib.datasource.publisher.model.DatasourceResponse;
|
7 |
|
|
import eu.dnetlib.datasource.publisher.model.IdentifiersResponse;
|
8 |
|
|
import eu.dnetlib.datasource.publisher.model.Response;
|
9 |
46718
|
claudio.at
|
import io.swagger.annotations.ApiParam;
|
10 |
44743
|
claudio.at
|
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 |
44765
|
claudio.at
|
@Override
|
29 |
|
|
public ResponseEntity<IdentifiersResponse> listIds() {
|
30 |
44743
|
claudio.at
|
try {
|
31 |
44765
|
claudio.at
|
return rsp(dsInfoRetriever.listIds(), HttpStatus.OK);
|
32 |
44743
|
claudio.at
|
} catch (ApiException e) {
|
33 |
44765
|
claudio.at
|
return rsp(new IdentifiersResponse(), HttpStatus.INTERNAL_SERVER_ERROR);
|
34 |
44743
|
claudio.at
|
}
|
35 |
|
|
}
|
36 |
|
|
|
37 |
44765
|
claudio.at
|
@Override
|
38 |
|
|
public ResponseEntity<DatasourceResponse> getDs(@ApiParam(value = "ID of datasource to fetch", required=true ) @PathVariable("id") String id) {
|
39 |
44743
|
claudio.at
|
|
40 |
|
|
if (log.isDebugEnabled()) {
|
41 |
|
|
log.debug(String.format("getDatasourceInfo(dsId = %s)", id));
|
42 |
|
|
}
|
43 |
|
|
final long start = System.nanoTime();
|
44 |
44765
|
claudio.at
|
final ResponseEntity<DatasourceResponse> rsp = rsp(new DatasourceResponse(), HttpStatus.OK);
|
45 |
44743
|
claudio.at
|
|
46 |
|
|
final ClientResponse clientResponse = dsInfoRetriever.getInfo(id);
|
47 |
44765
|
claudio.at
|
BeanUtils.copyProperties(clientResponse.getDatasourceResponse(), rsp.getBody());
|
48 |
44743
|
claudio.at
|
|
49 |
|
|
if (!clientResponse.getErrors().isEmpty()) {
|
50 |
44765
|
claudio.at
|
return rsp(clientResponse.getDatasourceResponse(), HttpStatus.INTERNAL_SERVER_ERROR);
|
51 |
44743
|
claudio.at
|
}
|
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 |
44765
|
claudio.at
|
private <T extends Response> ResponseEntity<T> rsp(final T info, final HttpStatus status) {
|
62 |
|
|
info.getResponseHeader().setStatusCode(status.value());
|
63 |
44743
|
claudio.at
|
return new ResponseEntity<>(info, status);
|
64 |
|
|
}
|
65 |
|
|
|
66 |
|
|
}
|