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

Bug/Vas-11487: fix downloading objects issue #1383

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import fr.gouv.vitam.common.client.VitamContext;
import fr.gouv.vitam.common.exception.InvalidParseOperationException;
import fr.gouv.vitam.common.exception.VitamClientException;
import fr.gouv.vitamui.archive.internal.server.service.ArchiveSearchEliminationInternalService;
Expand All @@ -48,8 +49,8 @@
import fr.gouv.vitamui.common.security.SanityChecker;
import fr.gouv.vitamui.commons.api.CommonConstants;
import fr.gouv.vitamui.commons.api.ParameterChecker;
import fr.gouv.vitamui.commons.api.dtos.VitamUiOntologyDto;
import fr.gouv.vitamui.commons.api.dtos.SearchCriteriaDto;
import fr.gouv.vitamui.commons.api.dtos.VitamUiOntologyDto;
import fr.gouv.vitamui.commons.api.exception.PreconditionFailedException;
import fr.gouv.vitamui.commons.api.logger.VitamUILogger;
import fr.gouv.vitamui.commons.api.logger.VitamUILoggerFactory;
Expand Down Expand Up @@ -183,9 +184,11 @@ public Mono<ResponseEntity<Resource>> downloadObjectFromUnit(final @PathVariable
.checkParameter(IDENTIFIER_MANDATORY, objectId);
SanityChecker.checkSecureParameter(objectId, usage);
LOGGER.debug("Download Archive Unit Object with id {}", objectId);
VitamContext vitamContext = new VitamContext(securityService.getTenantIdentifier()).setAccessContract(
externalParametersService.retrieveAccessContractFromExternalParam())
.setApplicationSessionId(securityService.getApplicationId());
return Mono.<Resource>fromCallable(() -> {
Response response = archiveInternalService.downloadObjectFromUnit(objectId, usage, version,
externalParametersService.buildVitamContextFromExternalParam());
Response response = archiveInternalService.downloadObjectFromUnit(objectId, usage, version, vitamContext);
return new InputStreamResource((InputStream) response.getEntity());
}).subscribeOn(Schedulers.boundedElastic())
.flatMap(resource -> Mono.just(ResponseEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

package fr.gouv.vitamui.collect.internal.server.rest;

import fr.gouv.vitam.common.client.VitamContext;
import fr.gouv.vitam.common.exception.InvalidParseOperationException;
import fr.gouv.vitam.common.exception.VitamClientException;
import fr.gouv.vitamui.collect.common.rest.RestApi;
Expand All @@ -41,6 +42,7 @@
import fr.gouv.vitamui.commons.api.logger.VitamUILogger;
import fr.gouv.vitamui.commons.api.logger.VitamUILoggerFactory;
import fr.gouv.vitamui.commons.vitam.api.dto.ResultsDto;
import fr.gouv.vitamui.iam.security.service.InternalSecurityService;
import io.swagger.annotations.Api;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
Expand Down Expand Up @@ -69,27 +71,34 @@ public class ProjectObjectGroupInternalController {
VitamUILoggerFactory.getInstance(ProjectObjectGroupInternalController.class);
private final ProjectObjectGroupInternalService projectObjectGroupInternalService;
private final ExternalParametersService externalParametersService;

private final InternalSecurityService securityService;
private static final String IDENTIFIER_MANDATORY = "The identifier is mandatory parameter: ";

public ProjectObjectGroupInternalController(ProjectObjectGroupInternalService projectObjectGroupInternalService,
final ExternalParametersService externalParametersService) {
final ExternalParametersService externalParametersService, InternalSecurityService securityService) {
this.projectObjectGroupInternalService = projectObjectGroupInternalService;
this.externalParametersService = externalParametersService;
this.securityService = securityService;
}

@GetMapping(value = DOWNLOAD_ARCHIVE_UNIT +
CommonConstants.PATH_ID, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public Mono<ResponseEntity<Resource>> downloadObjectFromUnit(final @PathVariable("id") String id,
final @RequestParam("usage") String usage, final @RequestParam("version") Integer version
) throws InvalidParseOperationException, PreconditionFailedException {

ParameterChecker
.checkParameter(IDENTIFIER_MANDATORY, id);
SanityChecker.checkSecureParameter(id, usage);
LOGGER.debug("Download Archive Unit Object with id {}", id);

VitamContext vitamContext = new VitamContext(securityService.getTenantIdentifier()).setAccessContract(
externalParametersService.retrieveAccessContractFromExternalParam())
.setApplicationSessionId(securityService.getApplicationId());

return Mono.<Resource>fromCallable(() -> {
Response response = projectObjectGroupInternalService.downloadObjectFromUnit(id, usage, version,
externalParametersService.buildVitamContextFromExternalParam());
vitamContext);
return new InputStreamResource((InputStream) response.getEntity());
}).subscribeOn(Schedulers.boundedElastic())
.flatMap(resource -> Mono.just(ResponseEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import java.io.InputStream;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -174,20 +174,22 @@ public ResponseEntity<ResultsDto> findObjectById(final @PathVariable("id") Strin
}

@ApiOperation(value = "Download Object from the Archive Unit ")
@GetMapping(value = RestApi.DOWNLOAD_ARCHIVE_UNIT + PATH_ID, produces = APPLICATION_OCTET_STREAM_VALUE)
@GetMapping(value = RestApi.DOWNLOAD_ARCHIVE_UNIT + PATH_ID, produces = APPLICATION_OCTET_STREAM_VALUE, params = {
"tenantId"})
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<Resource> downloadObjectFromUnit(final @PathVariable("id") String id,
@QueryParam("qualifier") String qualifier,
@QueryParam("version") Integer version,
@QueryParam("tenantId") Integer tenantId) throws PreconditionFailedException,
public ResponseEntity<Resource> downloadObjectFromUnit(final @PathVariable("id") String unitId,
@RequestParam(value = "qualifier", required = false) String qualifier,
@RequestParam(value = "version", required = false) Integer version,
@RequestParam("tenantId") Integer tenantId) throws PreconditionFailedException,
InvalidParseOperationException {
ParameterChecker.checkParameter("The Identifier and The tenantId are mandatory parameters: ",
id, String.valueOf(tenantId));
SanityChecker.checkSecureParameter(id, String.valueOf(tenantId));
LOGGER.debug("Download the Archive Unit Object with ID {}", id);
unitId, String.valueOf(tenantId));
SanityChecker.checkSecureParameter(unitId, String.valueOf(tenantId));
LOGGER.debug("Download the Archive Unit Object with ID {}", unitId);
ObjectData objectData = new ObjectData();
ResponseEntity<Resource> responseResource = archivesSearchService.downloadObjectFromUnit(id, qualifier, version,
objectData, buildUiHttpContext(tenantId)).block();
ResponseEntity<Resource> responseResource =
archivesSearchService.downloadObjectFromUnit(unitId, qualifier, version,
objectData, buildUiHttpContext(tenantId)).block();
List<String> headersValuesContentDispo = responseResource.getHeaders().get(CONTENT_DISPOSITION);
LOGGER.info("Content-Disposition value is {} ", headersValuesContentDispo);
String fileNameHeader = isNotEmpty(objectData.getFilename())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,10 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import java.util.List;

import static fr.gouv.vitamui.archives.search.common.rest.RestApi.DOWNLOAD_ARCHIVE_UNIT;
Expand All @@ -63,8 +61,6 @@
@Api(tags = "Collect")
@RequestMapping("${ui-collect.prefix}/projects/object-groups")
@RestController
@Consumes("application/json")
@Produces("application/json")
public class ProjectObjectGroupController extends AbstractUiRestController {

static final VitamUILogger LOGGER = VitamUILoggerFactory.getInstance(ProjectObjectGroupController.class);
Expand All @@ -78,14 +74,15 @@ public ProjectObjectGroupController(final ProjectObjectGroupService service) {
}

@ApiOperation(value = "Download Archive Unit Object")
@GetMapping(value = DOWNLOAD_ARCHIVE_UNIT + PATH_ID, produces = APPLICATION_OCTET_STREAM_VALUE)
@GetMapping(value = DOWNLOAD_ARCHIVE_UNIT + PATH_ID, produces = APPLICATION_OCTET_STREAM_VALUE, params = {
"objectId", "tenantId"})
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<Resource> downloadObjectFromUnit(
final @PathVariable("id") String unitId,
@QueryParam("objectId") String objectId,
@QueryParam("tenantId") Integer tenantId,
@QueryParam("qualifier") String qualifier,
@QueryParam("version") Integer version) throws PreconditionFailedException,
@RequestParam(name = "objectId") String objectId,
@RequestParam(name = "tenantId") Integer tenantId,
@RequestParam(name = "qualifier", required = false) String qualifier,
@RequestParam(name = "version", required = false) Integer version) throws PreconditionFailedException,
InvalidParseOperationException {
ParameterChecker.checkParameter("The Identifier, and The tenantId are mandatory parameters: ",
unitId, objectId, String.valueOf(tenantId));
Expand Down