Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/eno xml wrong zip name #1163

Merged
merged 21 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}

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 @@ -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,17 +1,20 @@
package fr.insee.eno.ws.controller.utils;

import fr.insee.eno.core.exceptions.business.EnoParametersException;
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.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.*;
import org.springframework.http.client.MultipartBodyBuilder;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Mono;

import java.io.IOException;
import java.net.URI;
Expand Down Expand Up @@ -60,17 +63,27 @@ 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) {
return 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)
.flatMap(responseBody -> Mono.error(new EnoRedirectionException(
responseBody, clientResponse.statusCode())))
)
.onStatus(
HttpStatusCode::is5xxServerError,
clientResponse -> clientResponse.bodyToMono(String.class)
.flatMap(responseBody -> Mono.error(new EnoRedirectionException(
"Server error: " + responseBody, clientResponse.statusCode())))
)
.toEntity(byte[].class)
.block();
return ResponseEntity.ok()
.headers(HeadersUtils.with(outFilename))
.body(result);
}

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;
}

}
3 changes: 2 additions & 1 deletion eno-ws/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ eno.ws.url=
eno.release.note.url=

# URL of a Eno "xml" web-service where some requests are redirected
eno.legacy.ws.url=https://eno-url.insee.fr
#eno.legacy.ws.url=https://eno-url.insee.fr
eno.legacy.ws.url=http://localhost:8082
nsenave marked this conversation as resolved.
Show resolved Hide resolved

# Flag to allow the direct Pogues to Lunatic transformation
eno.direct.pogues.lunatic=false
Expand Down
Loading