From c02157be8c463197ac90fee69bea63080056fa4f Mon Sep 17 00:00:00 2001 From: George Kalampokis Date: Mon, 5 Oct 2020 11:26:35 +0300 Subject: [PATCH] Replace HttpUrlConnection with RestTemplate for the external Urls --- .../logic/proxy/fetching/RemoteFetcher.java | 51 ++++++++++++------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/proxy/fetching/RemoteFetcher.java b/dmp-backend/web/src/main/java/eu/eudat/logic/proxy/fetching/RemoteFetcher.java index 9b824a81c..f795ea10d 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/logic/proxy/fetching/RemoteFetcher.java +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/proxy/fetching/RemoteFetcher.java @@ -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 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 {