15
15
import no .digipost .signature .client .core .exceptions .JobCannotBeCancelledException ;
16
16
import no .digipost .signature .client .core .exceptions .NotCancellableException ;
17
17
import no .digipost .signature .client .core .exceptions .TooEagerPollingException ;
18
+ import no .digipost .signature .client .core .internal .http .ApacheHttpMarshallingSupport ;
18
19
import no .digipost .signature .client .core .internal .http .ResponseStatus ;
19
20
import no .digipost .signature .client .core .internal .http .SignatureServiceRoot ;
20
21
import no .digipost .signature .client .core .internal .http .StatusCode ;
21
- import no .digipost .signature .client .core .internal .xml .Marshalling ;
22
22
import no .digipost .signature .client .direct .WithSignerUrl ;
23
+ import no .digipost .signature .jaxb .JaxbMarshaller ;
23
24
import org .apache .hc .client5 .http .classic .HttpClient ;
24
25
import org .apache .hc .client5 .http .classic .methods .HttpGet ;
25
- import org .apache .hc .client5 .http .classic .methods .HttpPost ;
26
26
import org .apache .hc .client5 .http .entity .mime .ByteArrayBody ;
27
- import org .apache .hc .client5 .http .entity .mime .InputStreamBody ;
28
27
import org .apache .hc .client5 .http .entity .mime .MultipartEntityBuilder ;
29
28
import org .apache .hc .client5 .http .entity .mime .MultipartPartBuilder ;
30
29
import org .apache .hc .core5 .http .ClassicHttpRequest ;
34
33
import org .apache .hc .core5 .http .ProtocolException ;
35
34
import org .apache .hc .core5 .http .io .support .ClassicRequestBuilder ;
36
35
37
- import java .io .ByteArrayOutputStream ;
38
36
import java .io .IOException ;
39
37
import java .io .InputStream ;
40
38
import java .io .UncheckedIOException ;
59
57
import static org .apache .hc .core5 .http .ContentType .MULTIPART_MIXED ;
60
58
import static org .apache .hc .core5 .http .HttpHeaders .ACCEPT ;
61
59
import static org .apache .hc .core5 .http .HttpHeaders .CONTENT_TYPE ;
60
+ import static org .apache .hc .core5 .http .io .support .ClassicRequestBuilder .post ;
62
61
63
62
public class ClientHelper {
64
63
@@ -69,37 +68,38 @@ public class ClientHelper {
69
68
70
69
private final SignatureServiceRoot serviceRoot ;
71
70
private final HttpClient httpClient ;
71
+ private final ApacheHttpMarshallingSupport requestMarshalling ;
72
+ private final JaxbMarshaller responseMarshaller ;
72
73
73
74
74
75
public ClientHelper (SignatureServiceRoot serviceRoot , HttpClient httpClient ) {
76
+ this (serviceRoot , httpClient , JaxbMarshaller .ForRequestsOfAllApis .singleton (), JaxbMarshaller .ForResponsesOfAllApis .singleton ());
77
+ }
78
+
79
+ public ClientHelper (SignatureServiceRoot serviceRoot , HttpClient httpClient , JaxbMarshaller requestMarshaller , JaxbMarshaller responseMarshaller ) {
75
80
this .serviceRoot = serviceRoot ;
76
81
this .httpClient = httpClient ;
82
+ this .requestMarshalling = new ApacheHttpMarshallingSupport (requestMarshaller );
83
+ this .responseMarshaller = responseMarshaller ;
77
84
}
78
85
79
86
public <RESPONSE , REQUEST > RESPONSE sendSignatureJobRequest (ApiFlow <REQUEST , RESPONSE , ?> target , REQUEST signatureJobRequest , DocumentBundle documentBundle , Sender sender ) {
80
- MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder .create ();
81
- multipartEntityBuilder .setContentType (MULTIPART_MIXED );
82
-
83
- try (ByteArrayOutputStream os = new ByteArrayOutputStream ()) {
84
- Marshalling .marshal (signatureJobRequest , os );
85
- multipartEntityBuilder .addPart (MultipartPartBuilder .create ()
86
- .setBody (new ByteArrayBody (os .toByteArray (), APPLICATION_XML , "" ))
87
+ MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder .create ()
88
+ .setContentType (MULTIPART_MIXED )
89
+ .addPart (MultipartPartBuilder .create ()
90
+ .setBody (requestMarshalling .createContentBody (signatureJobRequest ))
87
91
.addHeader (CONTENT_TYPE , APPLICATION_XML .getMimeType ())
88
- .build ());
89
-
90
- multipartEntityBuilder .addPart (MultipartPartBuilder .create ()
91
- .setBody (new InputStreamBody (documentBundle .getInputStream (), APPLICATION_OCTET_STREAM , "" ))
92
+ .build ())
93
+ .addPart (MultipartPartBuilder .create ()
94
+ .setBody (new ByteArrayBody (documentBundle .toByteArray (), APPLICATION_OCTET_STREAM , "" ))
92
95
.addHeader (CONTENT_TYPE , APPLICATION_OCTET_STREAM .getMimeType ()).build ());
93
- } catch (IOException e ) {
94
- throw new UncheckedIOException (e );
95
- }
96
96
97
- try (HttpEntity multiPart = multipartEntityBuilder .build ()) {
98
- ClassicHttpRequest request = ClassicRequestBuilder
99
- .post (serviceRoot .constructUrl (uri -> uri .appendPath (target .path (sender ))))
100
- .addHeader (ACCEPT , APPLICATION_XML .getMimeType ())
101
- .build ();
97
+ ClassicHttpRequest request = ClassicRequestBuilder
98
+ .post (serviceRoot .constructUrl (uri -> uri .appendPath (target .path (sender ))))
99
+ .addHeader (ACCEPT , APPLICATION_XML .getMimeType ())
100
+ .build ();
102
101
102
+ try (HttpEntity multiPart = multipartEntityBuilder .build ()) {
103
103
request .setEntity (multiPart );
104
104
return call (() -> {
105
105
try {
@@ -114,14 +114,14 @@ public <RESPONSE, REQUEST> RESPONSE sendSignatureJobRequest(ApiFlow<REQUEST, RES
114
114
}
115
115
116
116
public XMLDirectSignerResponse requestNewRedirectUrl (WithSignerUrl url ) {
117
- try ( ByteArrayOutputStream os = new ByteArrayOutputStream ()) {
118
- Marshalling . marshal ( new XMLDirectSignerUpdateRequest (). withRedirectUrl ( new XMLEmptyElement ()), os );
119
- ClassicHttpRequest request = new HttpPost ( url . getSignerUrl ());
120
- request . addHeader ( ACCEPT , APPLICATION_XML . getMimeType () );
121
-
117
+ ClassicHttpRequest request = post ( url . getSignerUrl ())
118
+ . addHeader ( ACCEPT , APPLICATION_XML . getMimeType ())
119
+ . setEntity ( requestMarshalling . createEntity ( new XMLDirectSignerUpdateRequest (). withRedirectUrl ( new XMLEmptyElement ())))
120
+ . build ( );
121
+ try {
122
122
return httpClient .execute (request , response -> parseResponse (response , XMLDirectSignerResponse .class ));
123
123
} catch (IOException e ) {
124
- throw new UncheckedIOException ( e );
124
+ throw new HttpIOException ( request , e );
125
125
}
126
126
}
127
127
@@ -156,12 +156,12 @@ public XMLDirectSignatureJobStatusResponse sendSignatureJobStatusRequest(URI sta
156
156
157
157
158
158
public void cancel (Cancellable cancellable ) {
159
- if (cancellable .getCancellationUrl () != null ) {
160
- postEmptyEntity (cancellable .getCancellationUrl ().getUrl (), httpResponse -> ResponseStatus .fromHttpResponse (httpResponse )
161
- .throwIf (CONFLICT , status -> new JobCannotBeCancelledException (status , extractError (httpResponse ))));
162
- } else {
163
- throw new NotCancellableException ();
164
- }
159
+ if (cancellable .getCancellationUrl () != null ) {
160
+ postEmptyEntity (cancellable .getCancellationUrl ().getUrl (), httpResponse -> ResponseStatus .fromHttpResponse (httpResponse )
161
+ .throwIf (CONFLICT , status -> new JobCannotBeCancelledException (status , extractError (httpResponse ))));
162
+ } else {
163
+ throw new NotCancellableException ();
164
+ }
165
165
}
166
166
167
167
public <RES > JobStatusResponse <RES > getStatusChange (ApiFlow <?, ?, RES > target , Sender sender ) {
@@ -177,7 +177,7 @@ public <RES> JobStatusResponse<RES> getStatusChange(ApiFlow<?, ?, RES> target, S
177
177
StatusCode status = ResponseStatus .fromHttpResponse (response )
178
178
.throwIf (TOO_MANY_REQUESTS , s -> new TooEagerPollingException ())
179
179
.expect (SUCCESSFUL ).orThrow (unexpectedStatus -> exceptionForGeneralError (response ));
180
- RES statusResponseBody = status .equals (NO_CONTENT ) ? null : Marshalling .unmarshal (response .getEntity ().getContent (), target .statusResponseType );
180
+ RES statusResponseBody = status .equals (NO_CONTENT ) ? null : responseMarshaller .unmarshal (response .getEntity ().getContent (), target .statusResponseType );
181
181
return new JobStatusResponse <>(statusResponseBody , getNextPermittedPollTime (response ));
182
182
});
183
183
} catch (IOException e ) {
@@ -242,10 +242,10 @@ private void delete(URI uri) {
242
242
});
243
243
}
244
244
245
- private static <T > T parseResponse (ClassicHttpResponse response , Class <T > responseType ) {
245
+ private <T > T parseResponse (ClassicHttpResponse response , Class <T > responseType ) {
246
246
ResponseStatus .fromHttpResponse (response ).expect (SUCCESSFUL ).orThrow (unexpectedStatus -> exceptionForGeneralError (response ));
247
247
try (InputStream body = response .getEntity ().getContent ()) {
248
- return Marshalling .unmarshal (body , responseType );
248
+ return responseMarshaller .unmarshal (body , responseType );
249
249
} catch (IOException e ) {
250
250
throw new HttpIOException (response , e );
251
251
}
0 commit comments