Commit ffe27afc authored by Baptiste Toulemonde's avatar Baptiste Toulemonde
Browse files

fix: change request with webClient to post multiparfile

parent f571aaa6
......@@ -48,22 +48,14 @@ import fr.gouv.vitamui.pastis.common.dto.profiles.Notice;
import fr.gouv.vitamui.pastis.common.dto.profiles.ProfileNotice;
import fr.gouv.vitamui.pastis.common.dto.profiles.ProfileResponse;
import fr.gouv.vitamui.pastis.common.rest.RestApi;
import fr.gouv.vitamui.pastis.common.util.FileSystemResource;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.AbstractMap;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
public class PastisTransformationRestClient
extends BasePaginatingAndSortingRestClient<ProfileResponse, ExternalHttpContext> {
......@@ -125,19 +117,6 @@ public class PastisTransformationRestClient
request, String.class);
}
public ResponseEntity<ElementProperties> loadProfilePA(Resource resource, ExternalHttpContext context)
throws IOException {
LOGGER.debug("Upload profile");
MultiValueMap<String, Object> bodyMap = new LinkedMultiValueMap<>();
bodyMap.add("file", new FileSystemResource(resource.getInputStream().readAllBytes(), "test_eeee.rng"));
final HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(bodyMap, buildHeaders(context));
return restTemplate.exchange(getUrl() + RestApi.PASTIS_TRANSFORM_PROFILE_PA,
HttpMethod.POST,
request,
ElementProperties.class);
}
public ResponseEntity<ProfileResponse> createProfile(String profileType, ExternalHttpContext context)
{
LOGGER.debug("Transform profile");
......
......@@ -4,14 +4,13 @@ import fr.gouv.vitamui.commons.api.logger.VitamUILogger;
import fr.gouv.vitamui.commons.api.logger.VitamUILoggerFactory;
import fr.gouv.vitamui.commons.rest.client.BaseWebClient;
import fr.gouv.vitamui.commons.rest.client.ExternalHttpContext;
import fr.gouv.vitamui.pastis.common.dto.ElementProperties;
import fr.gouv.vitamui.pastis.common.dto.profiles.ProfileResponse;
import fr.gouv.vitamui.pastis.common.rest.RestApi;
import fr.gouv.vitamui.pastis.common.util.FileSystemResource;
import org.springframework.http.HttpEntity;
import fr.gouv.vitamui.pastis.common.util.CustomMultipartFile;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.reactive.function.client.WebClient;
......@@ -30,8 +29,7 @@ public class PastisTransformationWebClient extends BaseWebClient<ExternalHttpCon
super(webClient, baseUrl);
}
public ResponseEntity<ProfileResponse> loadProfileFromFile(MultipartFile file, ExternalHttpContext context)
throws IOException {
public ResponseEntity<ProfileResponse> loadProfileFromFile(MultipartFile file, ExternalHttpContext context) {
LOGGER.debug("Upload profile");
return ResponseEntity.ok(multipartData(getUrl() + RestApi.PASTIS_UPLOAD_PROFILE, HttpMethod.POST,
context,
......@@ -40,6 +38,18 @@ public class PastisTransformationWebClient extends BaseWebClient<ExternalHttpCon
ProfileResponse.class )) ;
}
public ResponseEntity<ElementProperties> loadProfilePA(Resource resource, ExternalHttpContext context)
throws IOException {
LOGGER.debug("Upload profile");
CustomMultipartFile multipartFile =
new CustomMultipartFile(resource.getInputStream().readAllBytes());
return ResponseEntity.ok(multipartData(getUrl() + RestApi.PASTIS_TRANSFORM_PROFILE_PA, HttpMethod.POST,
context,
Collections.singletonMap("fileName", multipartFile.getOriginalFilename()),
Optional.of(new AbstractMap.SimpleEntry<>("file", multipartFile)),
ElementProperties.class));
}
@Override
public String getPathUrl() {
return RestApi.PASTIS;
......
package fr.gouv.vitamui.pastis.common.util;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class CustomMultipartFile implements MultipartFile {
private final byte[] fileByte;
public CustomMultipartFile(byte[] fileByte) {
this.fileByte = fileByte;
}
@Override
public String getName() {
return null;
}
@Override
public String getOriginalFilename() {
return "pa.rng";
}
@Override
public String getContentType() {
return null;
}
@Override
public boolean isEmpty() {
return this.fileByte == null || this.fileByte.length == 0;
}
@Override
public long getSize() {
return this.fileByte.length;
}
@Override
public byte[] getBytes() throws IOException {
return this.fileByte;
}
@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(this.fileByte);
}
@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
new FileOutputStream(dest).write(this.fileByte);
}
}
/*
Copyright © CINES - Centre Informatique National pour l'Enseignement Supérieur (2021)
[dad@cines.fr]
This software is a computer program whose purpose is to provide
a web application to create, edit, import and export archive
profiles based on the french SEDA standard
(https://redirect.francearchives.fr/seda/).
This software is governed by the CeCILL-C license under French law and
abiding by the rules of distribution of free software. You can use,
modify and/ or redistribute the software under the terms of the CeCILL-C
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info".
As a counterpart to the access to the source code and rights to copy,
modify and redistribute granted by the license, users are provided only
with a limited warranty and the software's author, the holder of the
economic rights, and the successive licensors have only limited
liability.
In this respect, the user's attention is drawn to the risks associated
with loading, using, modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean that it is complicated to manipulate, and that also
therefore means that it is reserved for developers and experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or
data to be ensured and, more generally, to use and operate it in the
same conditions as regards security.
The fact that you are presently reading this means that you have had
knowledge of the CeCILL-C license and that you accept its terms.
*/
package fr.gouv.vitamui.pastis.common.util;
import lombok.Getter;
import lombok.Setter;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.lang.Nullable;
@Getter@Setter
public final class FileSystemResource extends ByteArrayResource {
private String fileName;
public FileSystemResource(byte[] byteArray, String filename) {
super(byteArray);
this.fileName = filename;
}
@Override
public int hashCode() {
return super.hashCode();
}
@Override
public boolean equals(@Nullable Object other) {
return super.equals(other);
}
}
......@@ -125,7 +125,7 @@ class PastisController {
@Secured({ServicesData.ROLE_UPDATE_ARCHIVE_PROFILES, ServicesData.ROLE_UPDATE_PROFILES})
@PostMapping(value = RestApi.PASTIS_TRANSFORM_PROFILE_PA,
consumes = "multipart/form-data", produces = "application/json")
ResponseEntity<ElementProperties> loadPA(@RequestParam MultipartFile file){
ResponseEntity<ElementProperties> loadPA(@RequestParam("file") MultipartFile file){
ElementProperties elementProperties = profileService.loadProfilePA(file);
if (elementProperties != null) {
return ResponseEntity.ok(elementProperties);
......
......@@ -109,7 +109,7 @@ public class PastisTransformationService extends AbstractPaginateService<Profile
public ElementProperties loadProfilePA(Resource resource, ExternalHttpContext context) throws IOException {
LOGGER.info("Start transform profile PA By ui-pastis-service");
return pastisTransformationRestClient.loadProfilePA(resource, context).getBody();
return pastisTransformationWebClient.loadProfilePA(resource, context).getBody();
}
public ResponseEntity<ProfileResponse> loadProfileFromFile(MultipartFile file, ExternalHttpContext context)
......@@ -118,14 +118,12 @@ public class PastisTransformationService extends AbstractPaginateService<Profile
return pastisTransformationWebClient.loadProfileFromFile(file, context);
}
public ResponseEntity<String> getArchiveProfile(final ElementProperties json, ExternalHttpContext context) throws
IOException {
public ResponseEntity<String> getArchiveProfile(final ElementProperties json, ExternalHttpContext context) {
LOGGER.info("Start Download PA By ui-pastis-service");
return pastisTransformationRestClient.getArchiveProfile(json, context);
}
public ResponseEntity<String> getArchiveUnitProfile(final ProfileNotice json, ExternalHttpContext context)
throws IOException {
public ResponseEntity<String> getArchiveUnitProfile(final ProfileNotice json, ExternalHttpContext context) {
LOGGER.info("Start Download PUA By ui-pastis-service");
return pastisTransformationRestClient.getArchiveUnitProfile(json, context);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment