Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,30 @@
*/
package org.fairdatateam.fairdatapoint.config;

import org.springframework.boot.restclient.RestTemplateBuilder;
import org.springframework.boot.http.client.HttpClientSettings;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.RestTemplate;

import java.time.Duration;

@Configuration
public class HttpClientConfig {
// TODO: use newer WebClient (Spring 5)

private static final Duration TIMEOUT = Duration.ofSeconds(5);

@Bean RestClient restClient(RestClient.Builder builder) {
@Bean
public RestClient restClient(RestClient.Builder builder) {
// The builder argument is required for autoconfiguration of MockRestServiceServer in tests
// https://docs.spring.io/spring-framework/reference/integration/rest-clients.html
return builder.build();
}

@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder
.connectTimeout(TIMEOUT)
.readTimeout(TIMEOUT)
.build();
public HttpClientSettings httpClientSettings() {
return HttpClientSettings.defaults()
.withConnectTimeout(TIMEOUT)
.withReadTimeout(TIMEOUT);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@
import org.eclipse.rdf4j.model.vocabulary.LDP;
import org.eclipse.rdf4j.model.vocabulary.RDF;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.*;

Expand All @@ -55,13 +54,21 @@
@Service
public class HarvesterService {

private static final MediaType TURTLE = MediaType.parseMediaType(RDFFormat.TURTLE.getDefaultMIMEType());

private static final String DEFAULT_NAVIGATION_SHACL = "defaultNavigationShacl.ttl";

@Autowired
private GenericMetadataRepository genericMetadataRepository;
private final GenericMetadataRepository genericMetadataRepository;

private final RestClient client;

@Autowired
private RestTemplate restTemplate;
/**
* Constructor (autowired)
*/
public HarvesterService(GenericMetadataRepository genericMetadataRepository, RestClient client) {
this.genericMetadataRepository = genericMetadataRepository;
this.client = client;
}

public void deleteHarvestedData(String clientUrl) throws MetadataRepositoryException {
genericMetadataRepository.remove(i(clientUrl));
Expand Down Expand Up @@ -137,12 +144,12 @@ model, i(container.stringValue()), LDP.CONTAINS

private Model makeRequest(String uri) {
log.info("Making request to '{}'", uri);
final HttpHeaders headers = new HttpHeaders();
headers.setAccept(List.of(MediaType.parseMediaType(RDFFormat.TURTLE.getDefaultMIMEType())));
final HttpEntity<Void> entity = new HttpEntity<>(null, headers);
try {
final ResponseEntity<String> response =
restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
final ResponseEntity<String> response = client.method(HttpMethod.GET)
.uri(uri)
.headers(httpHeaders -> httpHeaders.setAccept(List.of(TURTLE)))
.retrieve()
.toEntity(String.class);
if (!response.getStatusCode().is2xxSuccessful()) {
log.info("Request to '{}' failed ({})", uri, response.getStatusCode());
throw new HttpClientErrorException(response.getStatusCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@
import org.fairdatateam.fairdatapoint.config.properties.PingProperties;
import org.fairdatateam.fairdatapoint.entity.settings.Settings;
import org.fairdatateam.fairdatapoint.service.settings.SettingsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestClient;

import java.util.List;
import java.util.Map;
Expand All @@ -43,17 +42,23 @@
@ConditionalOnProperty(name = "ping.enabled", havingValue = "true", matchIfMissing = true)
public class PingService {

@Autowired
private PingProperties pingProperties;
private final PingProperties pingProperties;

@Autowired
private InstanceProperties instanceProperties;
private final InstanceProperties instanceProperties;

@Autowired
private SettingsService settingsService;
private final SettingsService settingsService;

@Autowired
private RestTemplate client;
private final RestClient client;

/**
* Constructor (autowired)
*/
public PingService(PingProperties pingProperties, InstanceProperties instanceProperties, SettingsService settingsService, RestClient client) {
this.pingProperties = pingProperties;
this.instanceProperties = instanceProperties;
this.settingsService = settingsService;
this.client = client;
}

@Scheduled(
initialDelayString = "${ping.initDelay:#{10*1000}}",
Expand All @@ -80,7 +85,7 @@ public void ping() {
void pingEndpoint(String endpoint, Map<String, String> request) {
try {
log.info("Pinging {}", endpoint);
client.postForEntity(endpoint, request, String.class);
client.post().uri(endpoint).body(request).retrieve().toEntity(String.class);
}
catch (Exception exception) {
log.warn("Failed to ping {}: {}", endpoint, exception.getMessage());
Expand Down
Loading