Revision 46876
Added by Claudio Atzori about 7 years ago
DatasourcesApiController.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.datasource.publisher; |
2 | 2 |
|
3 |
import java.util.List; |
|
4 |
|
|
3 | 5 |
import eu.dnetlib.common.rmi.DNetRestDocumentation; |
4 | 6 |
import eu.dnetlib.datasource.publisher.clients.ClientResponse; |
5 | 7 |
import eu.dnetlib.datasource.publisher.clients.DatasourceInfoRetriever; |
6 | 8 |
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; |
|
9 |
import eu.dnetlib.datasource.publisher.model.db.SearchInterfacesEntry; |
|
10 | 10 |
import org.apache.commons.logging.Log; |
11 | 11 |
import org.apache.commons.logging.LogFactory; |
12 |
import org.springframework.beans.BeanUtils; |
|
13 | 12 |
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; |
|
13 |
import org.springframework.data.domain.PageRequest; |
|
14 |
import org.springframework.web.bind.annotation.*; |
|
18 | 15 |
|
19 |
@Controller |
|
16 |
@RestController
|
|
20 | 17 |
@DNetRestDocumentation |
21 | 18 |
public class DatasourcesApiController implements DatasourcesApi { |
22 | 19 |
|
... | ... | |
26 | 23 |
private DatasourceInfoRetriever dsInfoRetriever; |
27 | 24 |
|
28 | 25 |
@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 |
} |
|
26 |
@RequestMapping(value = "/ds/list/{page}/{size}", produces = { "application/json" }, method = RequestMethod.GET) |
|
27 |
public List<String> listIds(@PathVariable int page, @PathVariable int size) throws ApiException { |
|
28 |
return dsInfoRetriever.listIds(new PageRequest(page, size)); |
|
35 | 29 |
} |
36 | 30 |
|
37 | 31 |
@Override |
38 |
public ResponseEntity<DatasourceResponse> getDs(@ApiParam(value = "ID of datasource to fetch", required=true ) @PathVariable("id") String id) { |
|
32 |
@RequestMapping(value = "/ds/get/{id}", produces = { "application/json" }, method = RequestMethod.GET) |
|
33 |
public DatasourceResponse getDs(@PathVariable final String id) { |
|
39 | 34 |
|
40 | 35 |
if (log.isDebugEnabled()) { |
41 | 36 |
log.debug(String.format("getDatasourceInfo(dsId = %s)", id)); |
42 | 37 |
} |
43 |
final long start = System.nanoTime(); |
|
44 |
final ResponseEntity<DatasourceResponse> rsp = rsp(new DatasourceResponse(), HttpStatus.OK); |
|
45 | 38 |
|
46 | 39 |
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; |
|
40 |
return clientResponse.getDatasourceResponse(); |
|
59 | 41 |
} |
60 | 42 |
|
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); |
|
43 |
@Override |
|
44 |
@RequestMapping(value = "/api/search/{field}", produces = { "application/json" }, method = RequestMethod.GET) |
|
45 |
public List<SearchInterfacesEntry> searchInterface(@PathVariable final String field, @RequestParam final String value) { |
|
46 |
return dsInfoRetriever.searchInterface(field, value); |
|
64 | 47 |
} |
65 | 48 |
|
66 | 49 |
} |
Also available in: Unified diff
cleanup, added method to search among apis