Skip to content

Commit

Permalink
IPF 5 preparations: update dependencies, remove a lot of warnings, ja…
Browse files Browse the repository at this point in the history
…vadoc cleanup

Refactor Audit Builders
Add AuditMessageQueue for Spring RestClient
  • Loading branch information
ohr committed Dec 14, 2024
1 parent 347b6dd commit 9fba5d8
Show file tree
Hide file tree
Showing 2,821 changed files with 20,173 additions and 104,941 deletions.
12 changes: 5 additions & 7 deletions boot/ipf-atna-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,18 @@
<artifactId>spring-security-web</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<optional>true</optional>
</dependency>

<!-- dependencies for test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency>
<!--
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<scope>test</scope>
</dependency>
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.openehealth.ipf.boot.atna;

import lombok.NonNull;
import org.openehealth.ipf.commons.audit.AuditContext;
import org.openehealth.ipf.commons.audit.codes.EventOutcomeIndicator;
import org.openehealth.ipf.commons.audit.event.ApplicationActivityBuilder;
Expand All @@ -27,9 +28,6 @@

import static java.util.Objects.requireNonNull;

/**
*
*/
public class ApplicationStartEventListener implements ApplicationListener<ContextRefreshedEvent> {

private final AuditContext auditContext;
Expand All @@ -42,7 +40,7 @@ public ApplicationStartEventListener(AuditContext auditContext) {
}

@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
public void onApplicationEvent(@NonNull ContextRefreshedEvent contextRefreshedEvent) {
if (contextRefreshedEvent.getApplicationContext() == applicationContext) {
auditContext.audit(
new ApplicationActivityBuilder.ApplicationStart(EventOutcomeIndicator.Success)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.openehealth.ipf.boot.atna;

import lombok.NonNull;
import org.openehealth.ipf.commons.audit.AuditContext;
import org.openehealth.ipf.commons.audit.codes.EventOutcomeIndicator;
import org.openehealth.ipf.commons.audit.event.ApplicationActivityBuilder;
Expand All @@ -27,9 +28,6 @@

import static java.util.Objects.requireNonNull;

/**
*
*/
public class ApplicationStopEventListener implements ApplicationListener<ContextClosedEvent> {

private final AuditContext auditContext;
Expand All @@ -42,7 +40,7 @@ public ApplicationStopEventListener(AuditContext auditContext) {
}

@Override
public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
public void onApplicationEvent(@NonNull ContextClosedEvent contextClosedEvent) {
if (contextClosedEvent.getApplicationContext() == applicationContext) {
auditContext.audit(
new ApplicationActivityBuilder.ApplicationStop(EventOutcomeIndicator.Success)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.openehealth.ipf.boot.atna;

import lombok.NonNull;
import org.openehealth.ipf.commons.audit.AuditContext;
import org.openehealth.ipf.commons.audit.codes.EventOutcomeIndicator;
import org.openehealth.ipf.commons.audit.event.UserAuthenticationBuilder;
Expand All @@ -42,19 +43,17 @@ public AuthenticationListener(AuditContext auditContext) {
}

@Override
public void onApplicationEvent(AbstractAuthenticationEvent authenticationEvent) {
public void onApplicationEvent(@NonNull AbstractAuthenticationEvent authenticationEvent) {
delegateListener.onApplicationEvent(authenticationEvent);

var outcome = authenticationEvent instanceof AbstractAuthenticationFailureEvent ?
EventOutcomeIndicator.MajorFailure :
EventOutcomeIndicator.Success;

var details = authenticationEvent.getAuthentication().getDetails();
if (details instanceof WebAuthenticationDetails) {
var webAuthenticationDetails = (WebAuthenticationDetails) details;
if (details instanceof WebAuthenticationDetails webAuthenticationDetails) {
var principal = authenticationEvent.getAuthentication().getPrincipal();
if (principal instanceof UserDetails) {
var userDetails = (UserDetails) principal;
if (principal instanceof UserDetails userDetails) {

var builder = new UserAuthenticationBuilder.Login(outcome)
.setAuditSource(auditContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@

import static org.apache.commons.lang3.StringUtils.isNotBlank;

/**
*
*/
@Configuration
@EnableConfigurationProperties(IpfAtnaConfigurationProperties.class)
public class IpfAtnaAutoConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
import org.openehealth.ipf.commons.audit.types.AuditSource;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
*
*/
@ConfigurationProperties(prefix = "ipf.atna")
public class IpfAtnaConfigurationProperties {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.event.AbstractAuthenticationEvent;

/**
*
*/
@ConditionalOnClass({AbstractAuthenticationEvent.class})
@Configuration
@EnableConfigurationProperties(IpfAtnaConfigurationProperties.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openehealth.ipf.boot.atna;

import org.openehealth.ipf.commons.audit.AuditContext;
import org.openehealth.ipf.commons.audit.queue.AbstractAuditMessageQueue;
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.RestClientException;

import java.net.URI;
import java.util.concurrent.TimeUnit;

import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
import static org.springframework.http.HttpHeaders.encodeBasicAuth;

/**
* Alternative to {@link org.openehealth.ipf.commons.audit.queue.BasicHttpAuditMessageQueue},
* using Spring's RestClient / RestClient.Builder classes
*/
public class RestClientAuditMessageQueue extends AbstractAuditMessageQueue {

private final RestClient.Builder restClientBuilder;
private final URI uri;
private long connectTimeout = -1;
private long readTimeout = -1;

private RestClient restClient;

public RestClientAuditMessageQueue(RestClient.Builder restClientBuilder, URI uri) {
this(restClientBuilder, uri, null, null);
}

/**
* @param uri URL
* @param user user name, maybe null
* @param password password, maybe null
*/
public RestClientAuditMessageQueue(RestClient.Builder restClientBuilder, URI uri, String user, String password) {
var builder = restClientBuilder
.defaultHeader(CONTENT_TYPE, MediaType.TEXT_XML.toString());
if (user != null && !user.isEmpty() && password != null && !password.isEmpty()) {
builder = builder.defaultHeader(
HttpHeaders.AUTHORIZATION,
encodeBasicAuth(user, password, null));
}
this.uri = uri;
this.restClientBuilder = customize(builder);
initRestClient();
}

protected RestClient.Builder customize(RestClient.Builder builder) {
return builder;
}

@Override
protected void handle(AuditContext auditContext, String auditRecord) {
try {
var response = restClient.post()
.uri(uri)
.headers(httpHeaders -> {
httpHeaders.add(X_IPF_ATNA_TIMESTAMP, auditContext.getAuditMetadataProvider().getTimestamp());
httpHeaders.add(X_IPF_ATNA_HOSTNAME, auditContext.getAuditMetadataProvider().getHostname());
httpHeaders.add(X_IPF_ATNA_PROCESSID, auditContext.getAuditMetadataProvider().getProcessID());
httpHeaders.add(X_IPF_ATNA_APPLICATION, auditContext.getAuditMetadataProvider().getSendingApplication());
})
.body(auditRecord)
.retrieve()
.toBodilessEntity();
} catch (RestClientException e) {
auditContext.getAuditExceptionHandler().handleException(auditContext, e, auditRecord);
}
}

public void setConnectTimeout(long connectTimeoutMillis) {
this.connectTimeout = connectTimeoutMillis;
initRestClient();
}

public void setReadTimeout(long readTimeoutMillis) {
this.readTimeout = readTimeoutMillis;
initRestClient();
}

private synchronized void initRestClient() {
this.restClient = this.restClientBuilder
.requestFactory(ClientHttpRequestFactoryBuilder.httpComponents()
.withDefaultRequestConfigCustomizer(rcc -> {
if (connectTimeout > 0) {
rcc.setConnectionRequestTimeout(connectTimeout, TimeUnit.MILLISECONDS);
}
if (readTimeout > 0) {
rcc.setResponseTimeout(readTimeout, TimeUnit.MILLISECONDS);
}
})
.build())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.springframework.web.client.RestOperations;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.time.Duration;

import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -62,19 +61,19 @@ public RestTemplateAuditMessageQueue(RestTemplateBuilder restTemplateBuilder, UR
this.restTemplateBuilder = restTemplateBuilder
.defaultHeader(
CONTENT_TYPE,
new MediaType("text", "xml", StandardCharsets.UTF_8).toString());
MediaType.TEXT_XML.toString());
initRestTemplate();
}

@Override
protected void handle(AuditContext auditContext, String auditRecord) {
try {
HttpHeaders httpHeaders = new HttpHeaders();
var httpHeaders = new HttpHeaders();
httpHeaders.add(X_IPF_ATNA_TIMESTAMP, auditContext.getAuditMetadataProvider().getTimestamp());
httpHeaders.add(X_IPF_ATNA_HOSTNAME, auditContext.getAuditMetadataProvider().getHostname());
httpHeaders.add(X_IPF_ATNA_PROCESSID, auditContext.getAuditMetadataProvider().getProcessID());
httpHeaders.add(X_IPF_ATNA_APPLICATION, auditContext.getAuditMetadataProvider().getSendingApplication());
HttpEntity<String> entity = new HttpEntity<>(auditRecord, httpHeaders);
var entity = new HttpEntity<>(auditRecord, httpHeaders);
restTemplate.postForEntity(uri, entity, Void.class);
} catch (RestClientException e) {
auditContext.getAuditExceptionHandler().handleException(auditContext, e, auditRecord);
Expand All @@ -94,10 +93,10 @@ public void setReadTimeout(long readTimeoutMillis) {
private synchronized void initRestTemplate() {
var builder = this.restTemplateBuilder;
if (connectTimeout >= 0) {
builder = builder.setConnectTimeout(Duration.ofMillis(connectTimeout));
builder = builder.connectTimeout(Duration.ofMillis(connectTimeout));
}
if (readTimeout >= 0) {
builder = builder.setReadTimeout(Duration.ofMillis(readTimeout));
builder = builder.readTimeout(Duration.ofMillis(readTimeout));
}
if (user != null && !user.isEmpty() && password != null && !password.isEmpty()) {
builder = builder.basicAuthentication(user, password);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;


/**
Expand All @@ -39,13 +38,13 @@ public class IpfAtnaAutoConfigurationTest {
private AuditContext auditContext;

@Test
public void testAtnaSettings() throws Exception {
public void testAtnaSettings() {
assertEquals("atna-test", auditContext.getAuditSourceId());
assertEquals("mysite", auditContext.getAuditEnterpriseSiteId());
assertEquals("localhost", auditContext.getAuditRepositoryHostName());
assertEquals(1342, auditContext.getAuditRepositoryPort());
assertEquals("TLS", auditContext.getAuditTransmissionProtocol().getTransportName());
assertTrue(auditContext.getAuditMessageQueue() instanceof AsynchronousAuditMessageQueue);
assertInstanceOf(AsynchronousAuditMessageQueue.class, auditContext.getAuditMessageQueue());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.openehealth.ipf.boot.atna;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.openehealth.ipf.commons.audit.AuditContext;
Expand All @@ -27,9 +26,7 @@
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;


/**
Expand All @@ -44,15 +41,15 @@ public class IpfAtnaBalpAutoConfigurationTest {
private AuditContext auditContext;

@Test
public void testAtnaWithBalpSettings() throws Exception {
assertTrue(auditContext instanceof BalpAuditContext);
public void testAtnaWithBalpSettings() {
assertInstanceOf(BalpAuditContext.class, auditContext);

assertEquals("atna-test", auditContext.getAuditSourceId());
assertEquals("mysite", auditContext.getAuditEnterpriseSiteId());
assertEquals("localhost", auditContext.getAuditRepositoryHostName());
assertEquals(1342, auditContext.getAuditRepositoryPort());
assertEquals("FHIR-REST-TLS", auditContext.getAuditTransmissionProtocol().getTransportName());
assertTrue(auditContext.getAuditMessageQueue() instanceof AsynchronousAuditMessageQueue);
assertInstanceOf(AsynchronousAuditMessageQueue.class, auditContext.getAuditMessageQueue());

assertEquals("fhir", ((BalpAuditContext)auditContext).getAuditRepositoryContextPath());
assertArrayEquals(new String[]{"cid","client-id","my-client-id-path"},
Expand Down
Loading

0 comments on commit 9fba5d8

Please sign in to comment.