Replace HttpUrlConnection with RestTemplate for the external Urls

This commit is contained in:
George Kalampokis 2020-10-05 11:26:35 +03:00
parent 91f888829e
commit c02157be8c
1 changed files with 32 additions and 19 deletions

View File

@ -16,7 +16,14 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
@ -250,21 +257,32 @@ public class RemoteFetcher {
private Results getResultsFromUrl(String urlString, DataUrlConfiguration jsonDataPath, String jsonPaginationPath, String contentType) {
try {
URL url = new URL(urlString.replace(" ", "%20"));
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
HttpEntity<String> entity;
ResponseEntity response;
/*
URL url = new URL(urlString.replaceAll(" ", "%20"));
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
*/
if (contentType != null && !contentType.isEmpty()) {
con.setRequestProperty("Accept", contentType);
headers.setAccept(Collections.singletonList(MediaType.valueOf(contentType)));
}
entity = new HttpEntity<>("parameters", headers);
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) { // success
if (contentType.contains("json")) {
response = restTemplate.exchange(urlString, HttpMethod.GET, entity, Object.class);
} else {
response = restTemplate.exchange(urlString, HttpMethod.GET, entity, String.class);
}
if (response.getStatusCode() == HttpStatus.OK) { // success
//do here all the parsing
Results results = new Results();
if (con.getHeaderField("Content-Type").contains("json")) {
DocumentContext jsonContext = JsonPath.parse(con.getInputStream());
if (response.getHeaders().get("Content-Type").get(0).contains("json")) {
DocumentContext jsonContext = JsonPath.parse(response.getBody());
if (jsonDataPath.getFieldsUrlConfiguration().getSource() != null) {
results = new Results(jsonContext.read(jsonDataPath.getPath()
@ -365,11 +383,12 @@ public class RemoteFetcher {
results.results = results.results.stream().map(e -> e.entrySet().stream().collect(Collectors.toMap(x -> this.transformKey(jsonDataPath,x.getKey()), Map.Entry::getValue)))
.collect(Collectors.toList());
}
else if (con.getHeaderField("Content-Type").contains("xml")) {
else if (response.getHeaders().get("Content-Type").get(0).contains("xml")) {
Class<?> aClass = Class.forName(jsonDataPath.getParseClass());
JAXBContext jaxbContext = JAXBContext.newInstance(aClass);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Object data = unmarshaller.unmarshal(con.getInputStream());
StringReader stringReader = new StringReader(response.getBody().toString());
Object data = unmarshaller.unmarshal(stringReader);
Method reader = null;
if (jsonDataPath.getParseField() != null && !jsonDataPath.getParseField().isEmpty()) {
reader = new PropertyDescriptor(jsonDataPath.getParseField(), aClass).getReadMethod();
@ -419,13 +438,7 @@ public class RemoteFetcher {
return results;
}
} catch (MalformedURLException e1) {
logger.error(e1.getMessage(), e1);
} //maybe print smth...
catch (IOException e2) {
logger.error(e2.getMessage(), e2);
} //maybe print smth...
catch (Exception exception) {
} catch (Exception exception) {
logger.error(exception.getMessage(), exception);
} //maybe print smth...
finally {