Search in sources :

Example 1 with YamlRepository

use of org.eclipse.winery.repository.yaml.YamlRepository 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)

Example 2 with YamlRepository

use of org.eclipse.winery.repository.yaml.YamlRepository in project winery by eclipse.

the class YamlCsarImporter method storeDefs.

private void storeDefs(DefinitionsChildId id, TDefinitions defs) {
    RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(id);
    IRepository repo = RepositoryFactory.getRepository();
    FromCanonical converter = null;
    if (repo instanceof GitBasedRepository) {
        GitBasedRepository wrapper = (GitBasedRepository) RepositoryFactory.getRepository();
        converter = new FromCanonical((YamlRepository) wrapper.getRepository());
    } else if (repo instanceof YamlRepository) {
        converter = new FromCanonical((YamlRepository) repo);
    }
    if (Objects.nonNull(converter)) {
        YamlWriter writer = new YamlWriter();
        writer.write(converter.convert((TDefinitions) defs), repo.ref2AbsolutePath(ref));
    }
}
Also used : RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) FromCanonical(org.eclipse.winery.repository.yaml.converter.FromCanonical) YamlRepository(org.eclipse.winery.repository.yaml.YamlRepository) IRepository(org.eclipse.winery.repository.backend.IRepository) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) GitBasedRepository(org.eclipse.winery.repository.backend.filebased.GitBasedRepository) YamlWriter(org.eclipse.winery.repository.converter.writer.YamlWriter)

Aggregations

IRepository (org.eclipse.winery.repository.backend.IRepository)2 GitBasedRepository (org.eclipse.winery.repository.backend.filebased.GitBasedRepository)2 YamlRepository (org.eclipse.winery.repository.yaml.YamlRepository)2 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 TDefinitions (org.eclipse.winery.model.tosca.TDefinitions)1 RepositoryFileReference (org.eclipse.winery.repository.common.RepositoryFileReference)1 YamlWriter (org.eclipse.winery.repository.converter.writer.YamlWriter)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 FromCanonical (org.eclipse.winery.repository.yaml.converter.FromCanonical)1