Skip to content

Commit dcd01c2

Browse files
fix(convertBody): replace convertBodyTo by unmarshal (#64)
1 parent c305597 commit dcd01c2

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

core/src/main/java/io/github/project/openubl/xsender/camel/routes/RestSunatErrorResponseProcessor.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
import org.apache.camel.Processor;
2525
import org.apache.camel.http.base.HttpOperationFailedException;
2626

27+
import java.util.Collections;
2728
import java.util.List;
29+
import java.util.Optional;
2830
import java.util.stream.Collectors;
2931

3032
public class RestSunatErrorResponseProcessor implements Processor {
@@ -34,21 +36,29 @@ public void process(Exchange exchange) throws Exception {
3436
HttpOperationFailedException httpException = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, HttpOperationFailedException.class);
3537
ResponseDocumentErrorDto responseDocumentErrorDto = exchange.getIn().getBody(ResponseDocumentErrorDto.class);
3638

37-
int errorCodeInt = Integer.parseInt(responseDocumentErrorDto.getCod());
38-
List<String> notes = responseDocumentErrorDto.getErrors().stream()
39+
String responseCode = Optional.ofNullable(responseDocumentErrorDto.getCod())
40+
.orElse(responseDocumentErrorDto.getStatus());
41+
int responseCodeInt = Integer.parseInt(responseCode);
42+
43+
String responseDescription = Optional.ofNullable(responseDocumentErrorDto.getMsg())
44+
.orElse(responseDocumentErrorDto.getMessage());
45+
46+
List<String> notes = Optional.ofNullable(responseDocumentErrorDto.getErrors())
47+
.orElse(Collections.emptyList())
48+
.stream()
3949
.map(error -> error.getCod() + " - " + error.getMsg())
4050
.collect(Collectors.toList());
4151

42-
Metadata metadata = Metadata.builder()
43-
.notes(notes)
44-
.responseCode(Integer.parseInt(responseDocumentErrorDto.getCod()))
45-
.description(responseDocumentErrorDto.getMsg())
46-
.build();
47-
4852
SunatResponse sunatResponse = SunatResponse.builder()
49-
.status(Status.fromCode(errorCodeInt))
50-
.metadata(metadata)
53+
.status(Status.fromCode(responseCodeInt))
54+
.metadata(Metadata.builder()
55+
.notes(notes)
56+
.responseCode(responseCodeInt)
57+
.description(responseDescription)
58+
.build()
59+
)
5160
.build();
61+
5262
exchange.getIn().setBody(sunatResponse);
5363
}
5464

core/src/main/java/io/github/project/openubl/xsender/camel/routes/SunatRouteBuilder.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.camel.component.cxf.common.message.CxfConstants;
2727
import org.apache.camel.component.http.HttpConstants;
2828
import org.apache.camel.component.http.HttpMethods;
29+
import org.apache.camel.component.jackson.JacksonDataFormat;
2930
import org.apache.camel.http.base.HttpOperationFailedException;
3031
import org.apache.camel.model.dataformat.JsonLibrary;
3132
import org.apache.camel.util.URISupport;
@@ -53,7 +54,7 @@ public void configure() {
5354
.when(header(HttpConstants.HTTP_METHOD).isNotNull())
5455
.marshal().json(JsonLibrary.Jackson)
5556
.to("https://api-cpe.sunat.gob.pe")
56-
.convertBodyTo(ResponseDocumentSuccessDto.class)
57+
.unmarshal(new JacksonDataFormat(ResponseDocumentSuccessDto.class))
5758
.process(new RestSunatResponseProcessor())
5859
.endChoice()
5960
// Otherwise
@@ -73,12 +74,17 @@ public void configure() {
7374
HttpOperationFailedException httpException = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, HttpOperationFailedException.class);
7475
String contentType = httpException.getResponseHeaders().getOrDefault("Content-Type", "");
7576

77+
boolean isResponseJson = Objects.equals(contentType, "application/json");
78+
if (isResponseJson) {
79+
exchange.getIn().setBody(httpException.getResponseBody());
80+
}
81+
7682
exchange.getIn().setHeader("HttpResponseHeader_ContentType", contentType);
77-
return Objects.equals(contentType, "application/json");
83+
return isResponseJson;
7884
})
7985
.choice()
8086
.when(header("HttpResponseHeader_ContentType").isEqualTo("application/json"))
81-
.convertBodyTo(ResponseDocumentErrorDto.class)
87+
.unmarshal(new JacksonDataFormat(ResponseDocumentErrorDto.class))
8288
.process(new RestSunatErrorResponseProcessor())
8389
.endChoice()
8490
.otherwise()
@@ -124,7 +130,7 @@ public void configure() {
124130
}
125131
})
126132
.to("https://api-seguridad.sunat.gob.pe")
127-
.convertBodyTo(ResponseAccessTokenSuccessDto.class)
133+
.unmarshal(new JacksonDataFormat(ResponseAccessTokenSuccessDto.class))
128134
.process(exchange -> {
129135
ResponseAccessTokenSuccessDto response = exchange.getIn().getBody(ResponseAccessTokenSuccessDto.class);
130136
response.setCreated_in(ZonedDateTime.now());

core/src/main/java/io/github/project/openubl/xsender/models/rest/ResponseDocumentErrorDto.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public class ResponseDocumentErrorDto {
3333
private String msg;
3434
private String exc;
3535

36+
private String status;
37+
private String message;
38+
3639
@Singular
3740
private List<Error> errors;
3841

0 commit comments

Comments
 (0)