Revision 57166
Added by Alessia Bardi over 4 years ago
modules/dnet-parthenos-publisher/trunk/src/main/java/eu/dnetlib/parthenos/catalogue/CatalogueAPIClient.java | ||
---|---|---|
21 | 21 |
import org.springframework.stereotype.Component; |
22 | 22 |
import org.springframework.web.client.RestTemplate; |
23 | 23 |
|
24 |
import javax.annotation.PostConstruct; |
|
25 |
|
|
24 | 26 |
/** |
25 | 27 |
* Created by Alessia Bardi on 08/03/2018. |
26 | 28 |
* |
... | ... | |
47 | 49 |
@Autowired |
48 | 50 |
private RestTemplate restTemplate; |
49 | 51 |
|
50 |
public CatalogueAPIClient(){ |
|
52 |
|
|
53 |
public CatalogueAPIClient(){} |
|
54 |
|
|
55 |
@PostConstruct |
|
56 |
public void init(){ |
|
51 | 57 |
headersForResolver = new HttpHeaders(); |
52 | 58 |
headersForResolver.setContentType(MediaType.APPLICATION_JSON); |
53 | 59 |
|
... | ... | |
81 | 87 |
} |
82 | 88 |
} |
83 | 89 |
|
84 |
protected boolean isRegistered(final String resCatName) throws ParthenosPublisherException {
|
|
90 |
public boolean isRegistered(final String resCatName) throws ParthenosPublisherException {
|
|
85 | 91 |
log.debug(String.format("Catalogue --> Checking if item %s exists", resCatName)); |
86 | 92 |
HttpEntity<String> entity = new HttpEntity<String>(headersForCatalogue); |
87 | 93 |
try { |
88 |
URI uri = new URIBuilder(getBaseURL()+itemPath).build(); |
|
94 |
URI uri = new URIBuilder(getBaseURL()+itemPath+resCatName).build(); |
|
95 |
log.info(uri); |
|
96 |
log.info(headersForCatalogue.toString()); |
|
89 | 97 |
ResponseEntity<String> response |
90 |
= restTemplate.exchange(uri + "/"+resCatName, HttpMethod.GET, entity, String.class);
|
|
98 |
= restTemplate.exchange(uri, HttpMethod.GET, entity, String.class); |
|
91 | 99 |
return response.getStatusCode().is2xxSuccessful(); |
92 | 100 |
} catch (Throwable t) { |
93 | 101 |
throw new ParthenosPublisherException(t); |
... | ... | |
101 | 109 |
try { |
102 | 110 |
URI uri = new URIBuilder(getBaseURL()+itemPath).build(); |
103 | 111 |
ResponseEntity<String> response |
104 |
= restTemplate.exchange(uri + "/"+resCatName, HttpMethod.GET, entity, String.class);
|
|
112 |
= restTemplate.exchange(uri +resCatName, HttpMethod.GET, entity, String.class); |
|
105 | 113 |
if(response.getStatusCode().is2xxSuccessful()){ |
106 | 114 |
CatalogueAPIResponse body = new CatalogueAPIResponse(); |
107 | 115 |
body.setResponseBody(response.getBody()); |
... | ... | |
144 | 152 |
log.debug(String.format("Catalogue --> Purge Item %s", resCatName)); |
145 | 153 |
HttpEntity<String> entity = new HttpEntity<String>(headersForCatalogue); |
146 | 154 |
try { |
147 |
URI uri = new URIBuilder(getBaseURL()+itemPath).build(); |
|
155 |
URI uri = new URIBuilder(getBaseURL()+itemPath+resCatName).addParameter("purge", "true").build();
|
|
148 | 156 |
ResponseEntity<String> res |
149 |
= restTemplate.exchange(uri + "/"+resCatName+"?purge=true", HttpMethod.DELETE, entity, String.class);
|
|
157 |
= restTemplate.exchange(uri, HttpMethod.DELETE, entity, String.class); |
|
150 | 158 |
if (res.getStatusCode().is2xxSuccessful()) { |
151 | 159 |
return true; |
152 | 160 |
} else { |
... | ... | |
162 | 170 |
log.debug("Catalogue --> Purge All Item"); |
163 | 171 |
int count = 0; |
164 | 172 |
try { |
165 |
List<String> items = list(bulkSize); |
|
166 |
while (items.size() > 0){ |
|
167 |
for(String itemName : items) { |
|
168 |
purgeItem(itemName); |
|
169 |
count++; |
|
173 |
List<String> items = list(bulkSize);
|
|
174 |
while (items.size() > 0){
|
|
175 |
for(String itemName : items) {
|
|
176 |
purgeItem(itemName);
|
|
177 |
count++;
|
|
170 | 178 |
|
171 |
Thread.sleep(50); |
|
179 |
Thread.sleep(50);
|
|
172 | 180 |
|
181 |
} |
|
182 |
items = list(200); |
|
183 |
Thread.sleep(TimeUnit.SECONDS.toMillis(5)); |
|
173 | 184 |
} |
174 |
items = list(200);
|
|
175 |
Thread.sleep(TimeUnit.SECONDS.toMillis(5));
|
|
185 |
} catch (Throwable t) {
|
|
186 |
throw new ParthenosPublisherException(t);
|
|
176 | 187 |
} |
177 |
} catch (Throwable t) { |
|
178 |
throw new ParthenosPublisherException(t); |
|
179 |
|
|
180 |
} |
|
181 | 188 |
log.debug(String.format("Catalogue --> Purged all %d Items", count)); |
182 | 189 |
return count; |
183 | 190 |
} |
Also available in: Unified diff
Assume baseUrl and itemPath ends with '/'. Added PostConstruct