-
Notifications
You must be signed in to change notification settings - Fork 3
/
PnPaB2bExternalClientImpl.java
271 lines (221 loc) · 10.9 KB
/
PnPaB2bExternalClientImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package it.pagopa.pn.client.b2b.pa.impl;
import com.fasterxml.jackson.annotation.JsonProperty;
import it.pagopa.pn.client.b2b.pa.generated.openapi.clients.externalb2bpa.ApiClient;
import it.pagopa.pn.client.b2b.pa.generated.openapi.clients.externalb2bpa.api.*;
import it.pagopa.pn.client.b2b.pa.generated.openapi.clients.externalb2bpa.model.*;
import it.pagopa.pn.client.b2b.pa.testclient.PnInteropTokenOauth2Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import java.math.BigDecimal;
import java.util.List;
@Component()
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@ConditionalOnProperty( name = IPnPaB2bClient.IMPLEMENTATION_TYPE_PROPERTY, havingValue = "external", matchIfMissing = true)
public class PnPaB2bExternalClientImpl implements IPnPaB2bClient {
private final ApplicationContext ctx;
private final RestTemplate restTemplate;
private final NewNotificationApi newNotificationApi;
private final SenderReadB2BApi senderReadB2BApi;
private final PaymentEventsApi paymentEventsApi;
private final LegalFactsApi legalFactsApi;
private final NotificationPriceApi notificationPriceApi;
private final String basePath;
private final String apiKeyMvp1;
private final String apiKeyMvp2;
private final String apiKeyGa;
private ApiKeyType apiKeySetted = ApiKeyType.MVP_1;
//private PnInteropTokenOauth2Client pnInteropTokenOauth2Client;
private String bearerTokenInterop;
private final String interopBaseUrl;
private final String tokenOauth2Path;
private final String clientAssertion;
private final String enableInterop;
public PnPaB2bExternalClientImpl(
ApplicationContext ctx,
RestTemplate restTemplate,
@Value("${pn.external.base-url}") String basePath,
@Value("${pn.external.api-key}") String apiKeyMvp1,
@Value("${pn.external.api-key-2}") String apiKeyMvp2,
@Value("${pn.external.api-key-GA}") String apiKeyGa,
@Value("${pn.interop.base-url}") String interopBaseUrl,
@Value("${pn.interop.token-oauth2.path}") String tokenOauth2Path,
@Value("${pn.interop.token-oauth2.client-assertion}") String clientAssertion,
@Value("${pn.interop.enable}") String enableInterop
//PnInteropTokenOauth2Client pnInteropTokenOauth2Client
) {
this.ctx = ctx;
this.restTemplate = restTemplate;
this.basePath = basePath;
this.apiKeyMvp1 = apiKeyMvp1;
this.apiKeyMvp2 = apiKeyMvp2;
this.apiKeyGa = apiKeyGa;
//this.pnInteropTokenOauth2Client = pnInteropTokenOauth2Client;
this.enableInterop = enableInterop;
this.interopBaseUrl = interopBaseUrl;
this.tokenOauth2Path = tokenOauth2Path;
this.clientAssertion = clientAssertion;
if ("true".equalsIgnoreCase(enableInterop)) {
this.bearerTokenInterop = getBearerToken();
}
this.newNotificationApi = new NewNotificationApi( newApiClient( restTemplate, basePath, apiKeyMvp1, bearerTokenInterop,enableInterop) );
this.senderReadB2BApi = new SenderReadB2BApi( newApiClient( restTemplate, basePath, apiKeyMvp1, bearerTokenInterop,enableInterop) );
this.legalFactsApi = new LegalFactsApi(newApiClient( restTemplate, basePath, apiKeyMvp1, bearerTokenInterop,enableInterop));
this.notificationPriceApi = new NotificationPriceApi(newApiClient( restTemplate, basePath, apiKeyMvp1, bearerTokenInterop,enableInterop));
this.paymentEventsApi = new PaymentEventsApi(newApiClient( restTemplate, basePath, apiKeyMvp1, bearerTokenInterop,enableInterop));
}
private static ApiClient newApiClient(RestTemplate restTemplate, String basePath, String apikey, String bearerToken, String enableInterop ) {
ApiClient newApiClient = new ApiClient( restTemplate );
newApiClient.setBasePath( basePath );
newApiClient.addDefaultHeader("x-api-key", apikey );
if ("true".equalsIgnoreCase(enableInterop)) {
newApiClient.addDefaultHeader("Authorization", "Bearer " + bearerToken);
}
return newApiClient;
}
@Override
public boolean setApiKeys(ApiKeyType apiKey) {
boolean beenSet = false;
switch(apiKey){
case MVP_1:
if(this.apiKeySetted != ApiKeyType.MVP_1){
setApiKey(apiKeyMvp1);
this.apiKeySetted = ApiKeyType.MVP_1;
}
beenSet = true;
break;
case MVP_2:
if(this.apiKeySetted != ApiKeyType.MVP_2) {
setApiKey(apiKeyMvp2);
this.apiKeySetted = ApiKeyType.MVP_2;
}
beenSet = true;
break;
case GA:
if(this.apiKeySetted != ApiKeyType.GA) {
setApiKey(apiKeyGa);
this.apiKeySetted = ApiKeyType.GA;
}
beenSet = true;
break;
}
return beenSet;
}
@Override
public ApiKeyType getApiKeySetted() {
return this.apiKeySetted;
}
public void setApiKey(String apiKey){
this.newNotificationApi.setApiClient(newApiClient(restTemplate, basePath, apiKey, bearerTokenInterop,enableInterop));
this.senderReadB2BApi.setApiClient(newApiClient(restTemplate, basePath, apiKey, bearerTokenInterop,enableInterop));
this.legalFactsApi.setApiClient(newApiClient(restTemplate, basePath, apiKey,bearerTokenInterop,enableInterop));
this.notificationPriceApi.setApiClient(newApiClient(restTemplate, basePath, apiKey,bearerTokenInterop,enableInterop));
this.paymentEventsApi.setApiClient(newApiClient( restTemplate, basePath, apiKey,bearerTokenInterop,enableInterop));
}
public NotificationAttachmentDownloadMetadataResponse getSentNotificationDocument(String iun, Integer docidx) {
return senderReadB2BApi.retrieveSentNotificationDocument(iun, docidx);
}
public NotificationAttachmentDownloadMetadataResponse getSentNotificationAttachment(String iun, Integer recipientIdx, String attachname) {
return senderReadB2BApi.retrieveSentNotificationAttachment(iun, recipientIdx, attachname);
}
public LegalFactDownloadMetadataResponse getLegalFact(String iun, LegalFactCategory legalFactType, String legalFactId) {
return legalFactsApi.retrieveLegalFact(iun, legalFactType, legalFactId);
}
@Override
public NotificationPriceResponse getNotificationPrice(String paTaxId, String noticeCode) throws RestClientException {
return this.notificationPriceApi.retrieveNotificationPrice(paTaxId,noticeCode);
}
public List<PreLoadResponse> presignedUploadRequest(List<PreLoadRequest> preLoadRequest) {
return newNotificationApi.presignedUploadRequest( preLoadRequest );
}
public NewNotificationResponse sendNewNotification(NewNotificationRequest newNotificationRequest) {
return newNotificationApi.sendNewNotification( newNotificationRequest );
}
@Override
public FullSentNotification getSentNotification(String iun) {
return senderReadB2BApi.retrieveSentNotification( iun );
}
@Override
public NewNotificationRequestStatusResponse getNotificationRequestStatus(String notificationRequestId) {
return senderReadB2BApi.retrieveNotificationRequestStatus( notificationRequestId, null, null );
}
@Override
public NewNotificationRequestStatusResponse getNotificationRequestStatusAllParam(String notificationRequestId, String paProtocolNumber, String idempotenceToken) {
return senderReadB2BApi.retrieveNotificationRequestStatus(notificationRequestId,paProtocolNumber,idempotenceToken);
}
@Override
public void paymentEventsRequestPagoPa(PaymentEventsRequestPagoPa paymentEventsRequestPagoPa) throws RestClientException {
this.paymentEventsApi.paymentEventsRequestPagoPaWithHttpInfo(paymentEventsRequestPagoPa);
}
@Override
public void paymentEventsRequestF24(PaymentEventsRequestF24 paymentEventsRequestF24) throws RestClientException {
this.paymentEventsApi.paymentEventsRequestF24WithHttpInfo(paymentEventsRequestF24);
}
public String getBearerToken() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
map.add("client_assertion", clientAssertion);
map.add("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer");
map.add("grant_type", "client_credentials");
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
ResponseEntity<InteropResponse> response = this.restTemplate.postForEntity( interopBaseUrl + tokenOauth2Path, request , InteropResponse.class );
return (response.getStatusCode().is2xxSuccessful() ? response.getBody().getAccessToken() : null);
}
public static class InteropResponse {
private String correlationId;
private Integer status;
private String title;
private String type;
private List<PnInteropTokenOauth2Client.Error> errors;
@JsonProperty("access_token")
private String accessToken;
@JsonProperty("expires_in")
private Integer expiresIn;
@JsonProperty("token_type")
private String tokenType;
public String getCorrelationId() {
return correlationId;
}
public String getTitle() {
return title;
}
public String getType() {
return type;
}
public List<PnInteropTokenOauth2Client.Error> getErrors() {
return errors;
}
public String getAccessToken() {
return accessToken;
}
public Integer getExpiresIn() {
return expiresIn;
}
public String getTokenType() {
return tokenType;
}
}
public static class Error {
private String code;
private String detail;
public String getCode() {
return code;
}
public String getDetail() {
return detail;
}
}
}