forked from Haarlem/zds-stuf-to-zgw-api-translator
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Put zgw database logging back (#354)
- Loading branch information
Showing
6 changed files
with
258 additions
and
1 deletion.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
.../haarlem/translations/zdstozgw/requesthandler/impl/logging/LoggingRequestInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package nl.haarlem.translations.zdstozgw.requesthandler.impl.logging; | ||
|
||
import java.io.IOException; | ||
import java.io.UnsupportedEncodingException; | ||
|
||
import org.springframework.http.HttpRequest; | ||
import org.springframework.http.client.ClientHttpRequestExecution; | ||
import org.springframework.http.client.ClientHttpRequestInterceptor; | ||
import org.springframework.http.client.ClientHttpResponse; | ||
import org.springframework.web.context.request.RequestAttributes; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
|
||
import nl.haarlem.translations.zdstozgw.config.SpringContext; | ||
|
||
public class LoggingRequestInterceptor implements ClientHttpRequestInterceptor { | ||
|
||
private RequestResponseCycleService requestResponseCycleService; | ||
private ZgwRequestResponseCycle currentInterimRequestResponseCycle; | ||
// private String referentienummer; | ||
|
||
@Override | ||
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) | ||
throws IOException { | ||
this.requestResponseCycleService = SpringContext.getBean(RequestResponseCycleService.class); | ||
addRequestToDatabase(request, body); | ||
ClientHttpResponse response = execution.execute(request, body); | ||
addResponseToDatabase(response); | ||
return response; | ||
} | ||
|
||
private void addRequestToDatabase(HttpRequest request, byte[] body) throws UnsupportedEncodingException { | ||
String referentienummer = (String) RequestContextHolder.getRequestAttributes().getAttribute("referentienummer", | ||
RequestAttributes.SCOPE_REQUEST); | ||
this.currentInterimRequestResponseCycle = new ZgwRequestResponseCycle(referentienummer, request, body); | ||
//this.requestResponseCycleService.add(this.currentInterimRequestResponseCycle); | ||
} | ||
|
||
private void addResponseToDatabase(ClientHttpResponse response) throws IOException { | ||
// Added to prevent us from: | ||
// org.springframework.orm.jpa.JpaSystemException: identifier of an instance of | ||
// nl.haarlem.translations.zdstozgw.requesthandler.impl.logging.ZgwRequestResponseCycle | ||
// was altered from 133274 to 133275; nested exception is org.hibernate.HibernateException: | ||
// identifier of an instance of nl.haarlem.translations.zdstozgw.requesthandler.impl.logging.ZgwRequestResponseCycle | ||
// was altered from 133274 to 133275 | ||
//ZgwRequestResponseCycle existingRecordRef = this.requestResponseCycleService | ||
// .getZgwRequestResponseCycleRepository() | ||
// .findById(this.currentInterimRequestResponseCycle.getId()) | ||
// .orElse(this.currentInterimRequestResponseCycle); | ||
currentInterimRequestResponseCycle.setResponse(response); | ||
this.requestResponseCycleService.add(currentInterimRequestResponseCycle); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...ava/nl/haarlem/translations/zdstozgw/requesthandler/impl/logging/LoggingRestTemplate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package nl.haarlem.translations.zdstozgw.requesthandler.impl.logging; | ||
|
||
import java.security.cert.X509Certificate; | ||
|
||
import javax.net.ssl.SSLContext; | ||
|
||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; | ||
import org.apache.http.conn.ssl.TrustStrategy; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClients; | ||
import org.springframework.boot.web.client.RestTemplateCustomizer; | ||
import org.springframework.http.client.BufferingClientHttpRequestFactory; | ||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
public class LoggingRestTemplate implements RestTemplateCustomizer { | ||
|
||
public LoggingRestTemplate() { | ||
} | ||
|
||
@Override | ||
public void customize(RestTemplate restTemplate) { | ||
restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(this.getAllCertsTrustingRequestFactory())); | ||
restTemplate.getInterceptors().add(new LoggingRequestInterceptor()); | ||
} | ||
|
||
private HttpComponentsClientHttpRequestFactory getAllCertsTrustingRequestFactory() { | ||
TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true; | ||
|
||
SSLContext sslContext = null; | ||
try { | ||
sslContext = org.apache.http.ssl.SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy) | ||
.build(); | ||
} catch (Exception ex) { | ||
} | ||
|
||
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext); | ||
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(csf).build(); | ||
|
||
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); | ||
|
||
requestFactory.setHttpClient(httpClient); | ||
return requestFactory; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
.../haarlem/translations/zdstozgw/requesthandler/impl/logging/RestTemplateConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2020,2023 The Open Zaakbrug Contributors | ||
* | ||
* Licensed under the EUPL, Version 1.2 or – as soon they will be approved by the | ||
* European Commission - subsequent versions of the EUPL (the "Licence"); | ||
* | ||
* You may not use this work except in compliance with the Licence. | ||
* You may obtain a copy of the Licence at: | ||
* | ||
* https://joinup.ec.europa.eu/software/page/eupl5 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the Licence is distributed on an "AS IS" basis, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the Licence for the specific language governing permissions and limitations under the Licence. | ||
*/ | ||
package nl.haarlem.translations.zdstozgw.requesthandler.impl.logging; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class RestTemplateConfiguration { | ||
|
||
@Bean | ||
public LoggingRestTemplate customRestTemplateCustomizer() { | ||
return new LoggingRestTemplate(); | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
...nl/haarlem/translations/zdstozgw/requesthandler/impl/logging/ZgwRequestResponseCycle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright 2020-2023 The Open Zaakbrug Contributors | ||
* | ||
* Licensed under the EUPL, Version 1.2 or – as soon they will be approved by the | ||
* European Commission - subsequent versions of the EUPL (the "Licence"); | ||
* | ||
* You may not use this work except in compliance with the Licence. | ||
* You may obtain a copy of the Licence at: | ||
* | ||
* https://joinup.ec.europa.eu/software/page/eupl5 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the Licence is distributed on an "AS IS" basis, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the Licence for the specific language governing permissions and limitations under the Licence. | ||
*/ | ||
package nl.haarlem.translations.zdstozgw.requesthandler.impl.logging; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.UnsupportedEncodingException; | ||
import java.time.Duration; | ||
import java.time.LocalDateTime; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
import javax.persistence.Index; | ||
import javax.persistence.Table; | ||
|
||
import org.springframework.http.HttpRequest; | ||
import org.springframework.http.client.ClientHttpResponse; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
@Entity | ||
@Table(indexes = @Index(columnList = "referentienummer")) | ||
public class ZgwRequestResponseCycle { | ||
@Id | ||
@GeneratedValue | ||
private long id; | ||
private String referentienummer; | ||
|
||
private LocalDateTime startdatetime; | ||
private LocalDateTime stopdatetime; | ||
private Long durationInMilliseconds; | ||
|
||
private String zgwMethod; | ||
@Column(columnDefinition="TEXT") | ||
private String zgwUrl; | ||
@Column(columnDefinition="TEXT", name = "zgw_request_body") | ||
private String zgwShortenedRequestBody; | ||
private Integer zgwRequestSize; | ||
|
||
private int zgwResponseCode; | ||
@Column(columnDefinition="TEXT", name = "zgw_response_body") | ||
private String zgwShortenedResponseBody; | ||
private Integer zgwResponseSize; | ||
|
||
public ZgwRequestResponseCycle() { | ||
startdatetime = LocalDateTime.now(); | ||
}; | ||
|
||
public ZgwRequestResponseCycle(String referentienummer, HttpRequest request, byte[] body) throws UnsupportedEncodingException { | ||
this.referentienummer = referentienummer; | ||
this.zgwMethod = request.getMethodValue(); | ||
this.zgwUrl = request.getURI().toString(); | ||
|
||
var message = new String(body, "UTF-8"); | ||
this.zgwRequestSize = message.length(); | ||
this.zgwShortenedRequestBody = message; | ||
|
||
startdatetime = LocalDateTime.now(); | ||
} | ||
|
||
public long getDurationInMilliseconds() { | ||
var milliseconds = Duration.between(startdatetime, LocalDateTime.now()).toMillis(); | ||
return milliseconds; | ||
} | ||
|
||
public void setResponse(ClientHttpResponse response) throws UnsupportedEncodingException, IOException { | ||
this.zgwResponseCode = response.getStatusCode().value(); | ||
|
||
StringBuilder inputStringBuilder = new StringBuilder(); | ||
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getBody(), "UTF-8")); | ||
String line = bufferedReader.readLine(); | ||
while (line != null) { | ||
line = line.replaceAll("\u0000", ""); | ||
inputStringBuilder.append(line); | ||
inputStringBuilder.append('\n'); | ||
line = bufferedReader.readLine(); | ||
} | ||
var message = inputStringBuilder.toString(); | ||
this.zgwResponseSize = message.length(); | ||
this.zgwShortenedResponseBody = message; | ||
|
||
this.stopdatetime = LocalDateTime.now(); | ||
this.durationInMilliseconds = Duration.between(startdatetime, stopdatetime).toMillis(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
.../translations/zdstozgw/requesthandler/impl/logging/ZgwRequestResponseCycleRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright 2020-2023 The Open Zaakbrug Contributors | ||
* | ||
* Licensed under the EUPL, Version 1.2 or – as soon they will be approved by the | ||
* European Commission - subsequent versions of the EUPL (the "Licence"); | ||
* | ||
* You may not use this work except in compliance with the Licence. | ||
* You may obtain a copy of the Licence at: | ||
* | ||
* https://joinup.ec.europa.eu/software/page/eupl5 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the Licence is distributed on an "AS IS" basis, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the Licence for the specific language governing permissions and limitations under the Licence. | ||
*/ | ||
package nl.haarlem.translations.zdstozgw.requesthandler.impl.logging; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface ZgwRequestResponseCycleRepository extends JpaRepository<ZgwRequestResponseCycle, Long> { | ||
} |