Skip to content

Commit

Permalink
fix: eno xml wrong zip name (#1163)
Browse files Browse the repository at this point in the history
  • Loading branch information
RemiVerriez authored Dec 2, 2024
1 parent 0be5e39 commit a77584d
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 25 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ java {

allprojects {
group = "fr.insee.eno"
version = "3.29.1-SNAPSHOT"
version = "3.29.2-SNAPSHOT.4"
}

subprojects {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ public ResponseEntity<byte[]> generateXformsCustomParams(
addMultipartToBody(multipartBodyBuilder, specificTreatment, "specificTreatment");
//
URI uri = xmlControllerUtils.newUriBuilder().path("questionnaire/ddi-2-xforms").build().toUri();
String outFilename = questionnaireFilename(OutFormat.XFORMS, true);
return xmlControllerUtils.sendPostRequestByte(uri, multipartBodyBuilder, outFilename);
return xmlControllerUtils.sendPostRequestByte(uri, multipartBodyBuilder);
}

@Operation(
Expand All @@ -136,7 +135,7 @@ public ResponseEntity<byte[]> generateXformsCustomParams(
"You can get a parameters file by using the endpoint `/parameters/xml/{context}/FO`")
@PostMapping(value = "ddi-2-fo",
produces = MediaType.APPLICATION_OCTET_STREAM_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> generateFOCustomParams(
public ResponseEntity<byte[]> generateFOCustomParams(
@RequestPart(value="in") MultipartFile in,
@RequestPart(value="params") MultipartFile params,
@RequestPart(value="metadata") MultipartFile metadata,
Expand All @@ -152,8 +151,7 @@ public ResponseEntity<String> generateFOCustomParams(
addMultipartToBody(multipartBodyBuilder, specificTreatment, "specificTreatment");
//
URI uri = xmlControllerUtils.newUriBuilder().path("questionnaire/ddi-2-fo").build().toUri();
String outFilename = questionnaireFilename(OutFormat.XFORMS, false);
return xmlControllerUtils.sendPostRequest(uri, multipartBodyBuilder, outFilename);
return xmlControllerUtils.sendPostRequestByte(uri, multipartBodyBuilder);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ public ResponseEntity<byte[]> generateXforms(
.path("/questionnaire/{context}/xforms")
.queryParam("multi-model", multiModel)
.build(context);
String outFilename = questionnaireFilename(OutFormat.XFORMS, multiModel);
return xmlControllerUtils.sendPostRequestByte(uri, multipartBodyBuilder, outFilename);
return xmlControllerUtils.sendPostRequestByte(uri, multipartBodyBuilder);
}

@Operation(
Expand All @@ -164,7 +163,7 @@ public ResponseEntity<byte[]> generateXforms(
"If the multi-model option is set to true, the output questionnaire(s) are put in a zip file." )
@PostMapping(value = "{context}/fo",
produces = MediaType.APPLICATION_OCTET_STREAM_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> generateFO(
public ResponseEntity<byte[]> generateFO(
@RequestPart(value="in") MultipartFile in,
@RequestPart(value="metadata", required = false) MultipartFile metadata,
@RequestPart(value="specificTreatment", required=false) MultipartFile specificTreatment,
Expand Down Expand Up @@ -192,8 +191,7 @@ public ResponseEntity<String> generateFO(
.queryParam("Capture", capture)
.queryParam("multi-model", multiModel)
.build(context);
String outFilename = questionnaireFilename(OutFormat.FO, multiModel);
return xmlControllerUtils.sendPostRequest(uri, multipartBodyBuilder, outFilename);
return xmlControllerUtils.sendPostRequestByte(uri, multipartBodyBuilder);
}

@Operation(
Expand All @@ -203,16 +201,15 @@ public ResponseEntity<String> generateFO(
"context.")
@PostMapping(value = "{context}/fodt",
produces = MediaType.APPLICATION_OCTET_STREAM_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> generateFODT(
public ResponseEntity<byte[]> generateFODT(
@RequestPart(value="in") MultipartFile in,
@PathVariable Context context) throws EnoControllerException {
//
MultipartBodyBuilder multipartBodyBuilder = new MultipartBodyBuilder();
addMultipartToBody(multipartBodyBuilder, in, "in");
//
URI uri = xmlControllerUtils.newUriBuilder().path("/questionnaire/{context}/fodt").build(context);
String outFilename = questionnaireFilename(OutFormat.FODT, false);
return xmlControllerUtils.sendPostRequest(uri, multipartBodyBuilder, outFilename);
return xmlControllerUtils.sendPostRequestByte(uri, multipartBodyBuilder);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public ResponseEntity<String> generate(
addMultipartToBody(multipartBodyBuilder, mapping, "mapping");
//
URI uri = xmlControllerUtils.newUriBuilder().path("questionnaire/in-2-out").build().toUri();
String outFilename = multiModel ? "questionnaire.zip" : "questionnaire.txt";
String outFilename = multiModel ? "questionnaires.zip" : "questionnaire.txt";
return xmlControllerUtils.sendPostRequest(uri, multipartBodyBuilder, outFilename);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import fr.insee.eno.core.exceptions.business.EnoParametersException;
import fr.insee.eno.treatments.exceptions.SpecificTreatmentsDeserializationException;
import fr.insee.eno.treatments.exceptions.SpecificTreatmentsValidationException;
import fr.insee.eno.ws.exception.ContextException;
import fr.insee.eno.ws.exception.MetadataFileException;
import fr.insee.eno.ws.exception.ModeParameterException;
import fr.insee.eno.ws.exception.MultiModelException;
import fr.insee.eno.ws.exception.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -59,6 +56,13 @@ public ResponseEntity<Object> exception(IOException ioException) {
return new ResponseEntity<>("I/O error: " + ioException.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}

@ExceptionHandler(EnoRedirectionException.class)
public ResponseEntity<Object> exception(EnoRedirectionException enoRedirectionException) {
String message = enoRedirectionException.getMessage();
log.error(message, enoRedirectionException);
return new ResponseEntity<>(message, enoRedirectionException.getHttpStatusCode());
}

@ExceptionHandler(value = Exception.class)
public ResponseEntity<Object> exception(Exception exception) {
log.error("Unhandled exception thrown in controller: ", exception);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package fr.insee.eno.ws.controller.utils;

import fr.insee.eno.ws.exception.EnoControllerException;
import fr.insee.eno.ws.exception.EnoRedirectionException;
import fr.insee.eno.ws.legacy.parameters.OutFormat;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.MultipartBodyBuilder;
Expand Down Expand Up @@ -60,17 +62,33 @@ public ResponseEntity<String> sendPostRequest(URI uri, MultipartBodyBuilder mult
.body(result);
}

public ResponseEntity<byte[]> sendPostRequestByte(URI uri, MultipartBodyBuilder multipartBodyBuilder, String outFilename) {
byte[] result = webClient.post()
public ResponseEntity<byte[]> sendPostRequestByte(URI uri, MultipartBodyBuilder multipartBodyBuilder) {
ResponseEntity<byte[]> responseEntity = webClient.post()
.uri(uri)
.accept(MediaType.APPLICATION_OCTET_STREAM)
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(BodyInserters.fromMultipartData(multipartBodyBuilder.build()))
.exchangeToMono(clientResponse -> clientResponse.bodyToMono(byte[].class))
.retrieve()
.onStatus(
HttpStatusCode::is4xxClientError,
clientResponse -> clientResponse.bodyToMono(String.class)
.map(responseBody ->
new EnoRedirectionException(responseBody, clientResponse.statusCode()))
)
.onStatus(
HttpStatusCode::is5xxServerError,
clientResponse -> clientResponse.bodyToMono(String.class)
.map(responseBody ->
new EnoRedirectionException("Server error: " + responseBody, clientResponse.statusCode()))
)
.toEntity(byte[].class)
.block();
if (responseEntity == null)
throw new EnoRedirectionException("null result from Eno Xml call.");
String fileName = responseEntity.getHeaders().getContentDisposition().getFilename();
return ResponseEntity.ok()
.headers(HeadersUtils.with(outFilename))
.body(result);
.headers(HeadersUtils.with(fileName))
.body(responseEntity.getBody());
}

public ResponseEntity<String> sendPostRequest(URI uri) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ public static HttpHeaders with(String fileName) {
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return headers;
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
package fr.insee.eno.ws.exception;

import lombok.Getter;
import org.springframework.http.HttpStatusCode;

@Getter
public class EnoRedirectionException extends RuntimeException {

private final HttpStatusCode httpStatusCode;

public EnoRedirectionException(String message) {
super(message);
httpStatusCode = null;
}

public EnoRedirectionException(String message, HttpStatusCode httpStatusCode) {
super(message);
this.httpStatusCode = httpStatusCode;
}

}

0 comments on commit a77584d

Please sign in to comment.