Revision 50051
Added by Panagiotis Kanakakis over 6 years ago
RepositoryApiImpl.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import com.fasterxml.jackson.databind.DeserializationFeature; |
4 | 4 |
import com.fasterxml.jackson.databind.ObjectMapper; |
5 |
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; |
|
5 |
import eu.dnetlib.domain.data.Repository; |
|
6 |
import eu.dnetlib.domain.data.RepositoryInterface; |
|
6 | 7 |
import eu.dnetlib.domain.enabling.Vocabulary; |
7 | 8 |
import eu.dnetlib.repo.manager.service.utils.Converter; |
8 |
import eu.dnetlib.domain.data.Repository; |
|
9 |
import eu.dnetlib.domain.data.RepositoryInterface; |
|
9 |
import eu.dnetlib.repo.manager.shared.Aggregations; |
|
10 |
import eu.dnetlib.repo.manager.shared.Constants; |
|
11 |
import eu.dnetlib.repo.manager.shared.Country; |
|
12 |
import eu.dnetlib.repo.manager.shared.Timezone; |
|
10 | 13 |
import gr.uoa.di.driver.enabling.vocabulary.VocabularyLoader; |
11 |
import io.swagger.annotations.Api; |
|
12 | 14 |
import org.apache.log4j.Logger; |
13 | 15 |
import org.json.JSONArray; |
14 | 16 |
import org.json.JSONException; |
15 | 17 |
import org.json.JSONObject; |
16 | 18 |
import org.springframework.beans.factory.annotation.Autowired; |
17 | 19 |
import org.springframework.beans.factory.annotation.Value; |
20 |
import org.springframework.http.HttpEntity; |
|
21 |
import org.springframework.http.HttpHeaders; |
|
18 | 22 |
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; |
19 | 23 |
import org.springframework.stereotype.Component; |
20 | 24 |
import org.springframework.web.bind.annotation.PathVariable; |
... | ... | |
23 | 27 |
import org.springframework.web.client.RestTemplate; |
24 | 28 |
import org.springframework.web.util.UriComponents; |
25 | 29 |
import org.springframework.web.util.UriComponentsBuilder; |
30 |
|
|
26 | 31 |
import javax.annotation.PostConstruct; |
32 |
import java.io.IOException; |
|
27 | 33 |
import java.sql.Timestamp; |
28 | 34 |
import java.text.Normalizer; |
29 | 35 |
import java.util.*; |
30 | 36 |
import java.util.concurrent.ConcurrentHashMap; |
31 |
import eu.dnetlib.repo.manager.shared.*; |
|
32 | 37 |
|
33 | 38 |
@Component |
34 | 39 |
public class RepositoryApiImpl implements RepositoryApi { |
... | ... | |
38 | 43 |
|
39 | 44 |
private RestTemplate restTemplate = null; |
40 | 45 |
|
46 |
private HttpHeaders httpHeaders; |
|
47 |
|
|
41 | 48 |
private final String[] vocabularyNames = {"dnet:countries", "dnet:datasource_typologies", "dnet:compatibilityLevel"}; |
42 | 49 |
|
43 | 50 |
private static final Logger LOGGER = Logger.getLogger(RepositoryApiImpl.class); |
... | ... | |
58 | 65 |
restTemplate = new RestTemplate(); |
59 | 66 |
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); |
60 | 67 |
|
68 |
httpHeaders = new HttpHeaders(); |
|
69 |
httpHeaders.set("Content-Type", "application/json"); |
|
70 |
|
|
61 | 71 |
for (String vocName : vocabularyNames) { |
62 | 72 |
vocabularyMap.put(vocName, vocabularyLoader.getVocabulary(vocName, Locale.ENGLISH, Locale.ROOT)); |
63 | 73 |
} |
... | ... | |
196 | 206 |
String rs = restTemplate.getForObject(uriComponents.toUri(), String.class); |
197 | 207 |
List<Repository> repos = Converter.jsonToRepositoryList(new JSONArray(rs)); |
198 | 208 |
for (Repository r : repos){ |
199 |
// r.setInterfaces(this.getRepositoryInterface(r.getId()));
|
|
209 |
r.setInterfaces(this.getRepositoryInterface(r.getId())); |
|
200 | 210 |
r.setCountryName(getCountryName(r.getCountryCode())); |
201 | 211 |
} |
202 | 212 |
|
... | ... | |
213 | 223 |
|
214 | 224 |
String rs = restTemplate.getForObject(uriComponents.toUri(), String.class); |
215 | 225 |
Repository repo = Converter.jsonToRepositoryObject(new JSONObject(rs)); |
216 |
//repo.setInterfaces(this.getRepositoryInterface(repo.getId())); |
|
217 |
if (repo != null) |
|
226 |
if (repo != null) { |
|
218 | 227 |
repo.setCountryName(getCountryName(repo.getCountryCode())); |
228 |
repo.setInterfaces(this.getRepositoryInterface(repo.getId())); |
|
229 |
} |
|
219 | 230 |
return repo; |
220 | 231 |
} |
221 | 232 |
|
... | ... | |
378 | 389 |
private void updateInterface(String datatype,RepositoryInterface iFace) { |
379 | 390 |
//TODO call update base url |
380 | 391 |
//((DatasourceManagerService) this.dmService.getService()).updateBaseUrl(repo.getId(), iFace.getId(), iFace.getBaseUrl()); |
381 |
|
|
382 | 392 |
if (!iFace.getAccessSet().isEmpty()) { |
383 | 393 |
LOGGER.debug("set not empty: " + iFace.getAccessSet()); |
384 | 394 |
//TODO call update method for access params |
... | ... | |
426 | 436 |
iFace.setRemovable(true); |
427 | 437 |
iFace.setAccessProtocol("oai"); |
428 | 438 |
iFace.setMetadataIdentifierPath("//*[local-name()='header']/*[local-name()='identifier']"); |
429 |
iFace.setId("api_________::" + repo.getId() + "::" + UUID.randomUUID().toString().substring(0, 8)); |
|
439 |
// iFace.setId("api_________::" + repo.getId() + "::" + UUID.randomUUID().toString().substring(0, 8));
|
|
430 | 440 |
if (iFace.getAccessSet().isEmpty()) { |
431 | 441 |
LOGGER.debug("set is empty: " + iFace.getAccessSet()); |
432 | 442 |
iFace.removeAccessSet(); |
... | ... | |
445 | 455 |
.fromHttpUrl(baseAddress + "/ds/add/") |
446 | 456 |
.build() |
447 | 457 |
.encode(); |
448 |
try { |
|
449 |
LOGGER.debug("Repository equals : " + Converter.repositoryObjectToJson(repository)); |
|
450 |
}catch (Exception e){ |
|
451 |
LOGGER.debug("JSON exception ", e); |
|
452 |
throw e; |
|
453 |
} |
|
454 |
restTemplate.postForObject(uriComponents.toUri(),Converter.repositoryObjectToJson(repository),String.class); |
|
458 |
String json_repository = Converter.repositoryObjectToJson(repository); |
|
459 |
HttpEntity<String> httpEntity = new HttpEntity <String> (json_repository,httpHeaders); |
|
460 |
restTemplate.postForObject(uriComponents.toUri(),httpEntity,String.class); |
|
455 | 461 |
} |
456 | 462 |
|
457 | 463 |
@Override |
... | ... | |
467 | 473 |
public RepositoryInterface addRepositoryInterface(@RequestBody String params) throws JSONException { |
468 | 474 |
|
469 | 475 |
JSONObject json_params = new JSONObject(params); |
476 |
|
|
477 |
LOGGER.debug(params); |
|
478 |
//TODO iFace parameter from gui. Object to json |
|
470 | 479 |
String datatype = json_params.getString("datatype"); |
471 | 480 |
String repoId = json_params.getString("repoId"); |
472 | 481 |
String json_iFace = json_params.getString("iFace"); |
... | ... | |
487 | 496 |
try { |
488 | 497 |
e = this.getRepositoryById(repoId); |
489 | 498 |
iFace = createRepositoryInterface(e,iFace,datatype); |
499 |
ObjectMapper mapper = new ObjectMapper(); |
|
490 | 500 |
|
491 |
LOGGER.debug("iFace equals : " + Converter.repositoryInterfaceObjectToJson(e,iFace));
|
|
492 |
|
|
501 |
String json_interface = Converter.repositoryInterfaceObjectToJson(e,iFace);
|
|
502 |
LOGGER.debug("iFace equals -> " + json_interface); |
|
493 | 503 |
UriComponents uriComponents = UriComponentsBuilder |
494 | 504 |
.fromHttpUrl(baseAddress + "/ds/api/add/") |
495 | 505 |
.build() |
496 | 506 |
.encode(); |
497 |
String rs = restTemplate.postForObject(uriComponents.toUri(), Converter.repositoryInterfaceObjectToJson(e,iFace),String.class); |
|
498 |
return Converter.jsonToRepositoryInterfaceObject(new JSONArray(rs).getJSONObject(0)); |
|
499 | 507 |
|
508 |
|
|
509 |
HttpEntity<String> httpEntity = new HttpEntity <String> (json_interface,httpHeaders); |
|
510 |
restTemplate.postForObject(uriComponents.toUri(),httpEntity,String.class); |
|
511 |
return iFace; |
|
512 |
|
|
500 | 513 |
} catch (JSONException e1) { |
501 | 514 |
LOGGER.debug("Error parsing json ",e1); |
502 | 515 |
} |
Also available in: Unified diff
1. Change repositoryInterface to json method on converter file ( api params )
2. Add log messages and http headers on repository api.