Commit ef704a59 authored by descamps's avatar descamps
Browse files

KDE - 15/11/2021 - Importer un fichier xsd ou rng dans un profil Correctif

parent 4e201427
......@@ -44,6 +44,7 @@ import fr.gouv.vitamui.commons.rest.client.ExternalHttpContext;
import fr.gouv.vitamui.referential.common.dto.ProfileDto;
import fr.gouv.vitamui.referential.common.rest.RestApi;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
......@@ -100,11 +101,11 @@ public class ProfileExternalRestClient extends BasePaginatingAndSortingRestClien
}
public ResponseEntity<JsonNode> updateProfileFile(ExternalHttpContext context, String id, MultipartFile profileFile) {
public ResponseEntity<JsonNode> updateProfileFile(ExternalHttpContext context, String id, MultipartFile profileFile) throws IOException {
final UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl(getUrl() + "/updateProfileFile" + CommonConstants.PATH_ID);
MultiValueMap<String, Object> bodyMap = new LinkedMultiValueMap<>();
bodyMap.add("file", new FileSystemResource(convert(profileFile)));
bodyMap.add("file", new FileSystemResource(profileFile.getBytes(), profileFile.getOriginalFilename()));
final HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(bodyMap, buildHeaders(context));
return restTemplate.exchange(uriBuilder.build(id),
HttpMethod.PUT,
......@@ -112,21 +113,18 @@ public class ProfileExternalRestClient extends BasePaginatingAndSortingRestClien
JsonNode.class);
}
public static File convert(MultipartFile file)
{
File convFile = new File(file.getOriginalFilename());
try {
convFile.createNewFile();
FileOutputStream fos = new FileOutputStream(convFile);
fos.write(file.getBytes());
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
public static class FileSystemResource extends ByteArrayResource {
private String fileName;
public FileSystemResource(byte[] byteArray , String filename) {
super(byteArray);
this.fileName = filename;
}
return convFile;
}
public String getFilename() { return fileName; }
public void setFilename(String fileName) { this.fileName= fileName; }
}
}
......@@ -62,6 +62,7 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
......@@ -119,7 +120,7 @@ public class ProfileExternalController {
*/
@PutMapping(value = "/updateProfileFile" + CommonConstants.PATH_ID)
public ResponseEntity<JsonNode> importProfileFile(final @PathVariable("id") String id,
@RequestParam("file") MultipartFile file) {
@RequestParam("file") MultipartFile file) throws IOException {
LOGGER.debug("Update {} profile file with id :{}", id);
LOGGER.debug("Import profile file {}", file);
ParameterChecker.checkParameter("profileFile stream is a mandatory parameter: ", file);
......
......@@ -55,6 +55,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
......@@ -145,7 +146,7 @@ public class ProfileExternalService extends AbstractResourceClientService<Profil
}
public ResponseEntity<JsonNode> updateProfileFile(String id, MultipartFile profileFile) {
public ResponseEntity<JsonNode> updateProfileFile(String id, MultipartFile profileFile) throws IOException {
return profileInternalRestClient.updateProfileFile(getInternalHttpContext(),id,profileFile);
}
}
......@@ -46,6 +46,7 @@ import fr.gouv.vitamui.commons.rest.client.InternalHttpContext;
import fr.gouv.vitamui.referential.common.dto.ProfileDto;
import fr.gouv.vitamui.referential.common.rest.RestApi;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
......@@ -104,10 +105,10 @@ public class ProfileInternalRestClient extends BasePaginatingAndSortingRestClien
public ResponseEntity<JsonNode> updateProfileFile(InternalHttpContext context, String id, MultipartFile profileFile) {
public ResponseEntity<JsonNode> updateProfileFile(InternalHttpContext context, String id, MultipartFile profileFile) throws IOException {
final UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl(getUrl() + "/updateProfileFile" + CommonConstants.PATH_ID);
MultiValueMap<String, Object> bodyMap = new LinkedMultiValueMap<>();
bodyMap.add("file", new FileSystemResource(convert(profileFile)));
bodyMap.add("file", new FileSystemResource(profileFile.getBytes(), profileFile.getOriginalFilename()));
final HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(bodyMap, buildHeaders(context));
return restTemplate.exchange(uriBuilder.build(id),
......@@ -116,38 +117,17 @@ public class ProfileInternalRestClient extends BasePaginatingAndSortingRestClien
JsonNode.class);
}
public static File convert(MultipartFile file)
{
File convFile = new File(file.getOriginalFilename());
try {
convFile.createNewFile();
FileOutputStream fos = new FileOutputStream(convFile);
fos.write(file.getBytes());
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return convFile;
}
/** Resource resouceFile = new FileSystemResource(file.getBytes(), file.getOriginalFilename());
body.add("files", resouceFile);
);
public static class FileSystemResource extends ByteArrayResource {
private String fileName;
public static class FileSystemResource extends ByteArrayResource {
private String fileName;
public FileSystemResource(byte[] byteArray , String filename) {
super(byteArray);
this.fileName = filename;
}
public FileSystemResource(byte[] byteArray , String filename) {
super(byteArray);
this.fileName = filename;
}
public String getFilename() { return fileName; }
public void setFilename(String fileName) { this.fileName= fileName; }
public String getFilename() { return fileName; }
public void setFilename(String fileName) { this.fileName= fileName; }
}*/
}
}
......@@ -63,6 +63,7 @@ import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
......@@ -158,7 +159,7 @@ public class ProfileController extends AbstractUiRestController {
@ApiOperation(value = "Importer un fichier xsd ou rng dans un profil")
@PutMapping(value = "/updateProfileFile" + CommonConstants.PATH_ID)
public ResponseEntity<JsonNode> importProfileFile(final @PathVariable("id") String id,
@RequestParam("file") MultipartFile file) {
@RequestParam("file") MultipartFile file) throws IOException {
LOGGER.debug("Update profile file with id :{}", id);
ParameterChecker.checkParameter("profileFile stream is a mandatory parameter: ", file);
ParameterChecker.checkParameter("The Identifier is a mandatory parameter: ", id);
......
......@@ -55,6 +55,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.Collection;
import java.util.Optional;
......@@ -121,7 +122,7 @@ public class ProfileService extends AbstractPaginateService<ProfileDto> {
return webClient.importProfiles(context, file);
}
public ResponseEntity<JsonNode> updateProfileFile(ExternalHttpContext context, String id, MultipartFile profileFile) {
public ResponseEntity<JsonNode> updateProfileFile(ExternalHttpContext context, String id, MultipartFile profileFile) throws IOException {
return client.updateProfileFile(context, id, profileFile);
}
}
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