Search in sources :

Example 1 with YamlCsarImporter

use of org.eclipse.winery.repository.importing.YamlCsarImporter in project winery by eclipse.

the class MainResource method importCSAR.

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@ApiOperation(value = "Imports the given CSAR (sent by simplesinglefileupload.jsp)")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", responseHeaders = @ResponseHeader(description = "If the CSAR could be partially imported, the points where it failed are returned in the body")) })
public // @formatter:off
Response importCSAR(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail, @ApiParam(value = "true: content of CSAR overwrites existing content. false (default): existing content is kept") @FormDataParam("overwrite") Boolean overwrite, @ApiParam(value = "true: validates the hash of the manifest file with the one stored in the accountability layer") @FormDataParam("validate") Boolean validate, @Context UriInfo uriInfo) {
    LocalDateTime start = LocalDateTime.now();
    // @formatter:on
    CsarImporter importer;
    IRepository repository = RepositoryFactory.getRepository();
    if (Environments.getInstance().getUiConfig().getFeatures().get(YAML.toString())) {
        if (repository instanceof GitBasedRepository) {
            importer = new YamlCsarImporter((YamlRepository) ((GitBasedRepository) repository).getRepository());
        } else {
            importer = new YamlCsarImporter((YamlRepository) repository);
        }
    } else {
        importer = new CsarImporter(repository);
    }
    CsarImportOptions options = new CsarImportOptions();
    options.setOverwrite((overwrite != null) && overwrite);
    options.setAsyncWPDParsing(false);
    options.setValidate((validate != null) && validate);
    ImportMetaInformation importMetaInformation;
    try {
        importMetaInformation = importer.readCSAR(uploadedInputStream, options);
    } catch (Exception e) {
        return Response.serverError().entity("Could not import CSAR").entity(e.getMessage()).build();
    }
    if (importMetaInformation.errors.isEmpty()) {
        if (options.isValidate()) {
            return Response.ok(importMetaInformation, MediaType.APPLICATION_JSON).build();
        } else if (Objects.nonNull(importMetaInformation.entryServiceTemplate)) {
            URI url = uriInfo.getBaseUri().resolve(RestUtils.getAbsoluteURL(importMetaInformation.entryServiceTemplate));
            LOGGER.debug("CSAR import lasted {}", Duration.between(LocalDateTime.now(), start).toString());
            return Response.created(url).build();
        } else {
            LOGGER.debug("CSAR import lasted {}", Duration.between(LocalDateTime.now(), start).toString());
            return Response.noContent().build();
        }
    } else {
        LOGGER.debug("CSAR import lasted {}", Duration.between(LocalDateTime.now(), start).toString());
        // In case there are errors, we send them as "bad request"
        return Response.status(Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON).entity(importMetaInformation).build();
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) CsarImportOptions(org.eclipse.winery.repository.importing.CsarImportOptions) YamlCsarImporter(org.eclipse.winery.repository.importing.YamlCsarImporter) CsarImporter(org.eclipse.winery.repository.importing.CsarImporter) YamlRepository(org.eclipse.winery.repository.yaml.YamlRepository) IRepository(org.eclipse.winery.repository.backend.IRepository) URI(java.net.URI) GitBasedRepository(org.eclipse.winery.repository.backend.filebased.GitBasedRepository) YamlCsarImporter(org.eclipse.winery.repository.importing.YamlCsarImporter) IOException(java.io.IOException) ImportMetaInformation(org.eclipse.winery.repository.importing.ImportMetaInformation) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 IOException (java.io.IOException)1 URI (java.net.URI)1 LocalDateTime (java.time.LocalDateTime)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 IRepository (org.eclipse.winery.repository.backend.IRepository)1 GitBasedRepository (org.eclipse.winery.repository.backend.filebased.GitBasedRepository)1 CsarImportOptions (org.eclipse.winery.repository.importing.CsarImportOptions)1 CsarImporter (org.eclipse.winery.repository.importing.CsarImporter)1 ImportMetaInformation (org.eclipse.winery.repository.importing.ImportMetaInformation)1 YamlCsarImporter (org.eclipse.winery.repository.importing.YamlCsarImporter)1 YamlRepository (org.eclipse.winery.repository.yaml.YamlRepository)1