Search in sources :

Example 1 with CsarImporter

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

the class MainResource method importDefinitions.

@POST
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_JSON)
public Response importDefinitions(InputStream is) throws IOException {
    File toscaFile;
    toscaFile = File.createTempFile("TOSCA", ".tosca");
    FileUtils.copyInputStreamToFile(is, toscaFile);
    CsarImporter importer = new CsarImporter();
    List<String> errors = new ArrayList<>();
    importer.importDefinitions(null, toscaFile.toPath(), errors, false, true);
    if (errors.isEmpty()) {
        return Response.noContent().build();
    } else {
        return Response.status(Status.BAD_REQUEST).entity(errors).build();
    }
}
Also used : CsarImporter(org.eclipse.winery.repository.importing.CsarImporter) ArrayList(java.util.ArrayList) File(java.io.File)

Example 2 with CsarImporter

use of org.eclipse.winery.repository.importing.CsarImporter 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, @FormDataParam("overwrite") @ApiParam(value = "true: content of CSAR overwrites existing content. false (default): existing content is kept") Boolean overwrite, @Context UriInfo uriInfo) {
    // @formatter:on
    CsarImporter importer = new CsarImporter();
    boolean ow;
    ow = (overwrite != null) && overwrite;
    ImportMetaInformation importMetaInformation;
    try {
        importMetaInformation = importer.readCSAR(uploadedInputStream, ow, true);
    } catch (Exception e) {
        return Response.serverError().entity("Could not import CSAR").entity(e.getMessage()).build();
    }
    if (importMetaInformation.errors.isEmpty()) {
        if (importMetaInformation.entryServiceTemplate.isPresent()) {
            URI url = uriInfo.getBaseUri().resolve(RestUtils.getAbsoluteURL(importMetaInformation.entryServiceTemplate.get()));
            return Response.created(url).build();
        } else {
            return Response.noContent().build();
        }
    } else {
        // In case there are errors, we send them as "bad request"
        return Response.status(Status.BAD_REQUEST).entity(importMetaInformation.errors).build();
    }
}
Also used : CsarImporter(org.eclipse.winery.repository.importing.CsarImporter) URI(java.net.URI) IOException(java.io.IOException) ImportMetaInformation(org.eclipse.winery.repository.importing.ImportMetaInformation)

Example 3 with CsarImporter

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

the class Showcase method zipTypeTest.

@Test
public void zipTypeTest() throws Exception {
    String name = "Showcase";
    String path = this.path + File.separator + name + ".csar";
    // Read DriverInjectionTest and import the csar into the repository
    InputStream fis = new FileInputStream(path);
    IRepository repository = RepositoryFactory.getRepository(Utils.getTmpDir(Paths.get("repository")));
    CsarImporter csarImporter = new CsarImporter();
    csarImporter.readCSAR(fis, true, true);
    // Read the csar again and convert it to yaml the resulting yaml service templates
    // are written to a temporary dir and converted to a input stream of a zip file
    Converter converter = new Converter(repository);
    fis = new FileInputStream(path);
    InputStream zip = converter.convertX2Y(fis);
    // Write the zip file to the output path and rename it
    File zipFile = new File(outPath + File.separator + name + ".csar");
    if (!zipFile.getParentFile().exists()) {
        zipFile.getParentFile().mkdirs();
    }
    Files.copy(zip, zipFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
    // clean temporary repository
    Utils.deleteTmpDir(Paths.get("repository"));
    Assert.assertTrue(zipFile.exists());
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CsarImporter(org.eclipse.winery.repository.importing.CsarImporter) Converter(org.eclipse.winery.yaml.converter.Converter) IRepository(org.eclipse.winery.repository.backend.IRepository) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 4 with CsarImporter

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

the class WriterUtils method storeDefinitions.

public static void storeDefinitions(Definitions definitions, boolean overwrite, Path dir) {
    Path path = null;
    try {
        path = Files.createTempDirectory("winery");
    } catch (IOException e) {
        e.printStackTrace();
    }
    LOGGER.debug("Store definition: {}", definitions.getId());
    saveDefinitions(definitions, path, definitions.getTargetNamespace(), definitions.getId());
    Definitions cleanDefinitions = loadDefinitions(path, definitions.getTargetNamespace(), definitions.getId());
    CsarImporter csarImporter = new CsarImporter();
    List<Exception> exceptions = new ArrayList<>();
    cleanDefinitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().forEach(entry -> {
        String namespace = csarImporter.getNamespace(entry, definitions.getTargetNamespace());
        csarImporter.setNamespace(entry, namespace);
        String id = ModelUtilities.getId(entry);
        Class<? extends DefinitionsChildId> widClazz = Util.getComponentIdClassForTExtensibleElements(entry.getClass());
        final DefinitionsChildId wid = BackendUtils.getDefinitionsChildId(widClazz, namespace, id, false);
        if (RepositoryFactory.getRepository().exists(wid)) {
            if (overwrite) {
                try {
                    RepositoryFactory.getRepository().forceDelete(wid);
                } catch (IOException e) {
                    exceptions.add(e);
                }
            } else {
                return;
            }
        }
        if (entry instanceof TArtifactTemplate) {
            TArtifactTemplate.ArtifactReferences artifactReferences = ((TArtifactTemplate) entry).getArtifactReferences();
            Stream.of(artifactReferences).filter(Objects::nonNull).flatMap(ref -> ref.getArtifactReference().stream()).filter(Objects::nonNull).forEach(ref -> {
                String reference = ref.getReference();
                URI refURI;
                try {
                    refURI = new URI(reference);
                } catch (URISyntaxException e) {
                    LOGGER.error("Invalid URI {}", reference);
                    return;
                }
                if (refURI.isAbsolute()) {
                    return;
                }
                Path artifactPath = dir.resolve(reference);
                if (!Files.exists(artifactPath)) {
                    LOGGER.error("File not found {}", artifactPath);
                    return;
                }
                ArtifactTemplateFilesDirectoryId aDir = new ArtifactTemplateFilesDirectoryId((ArtifactTemplateId) wid);
                RepositoryFileReference aFile = new RepositoryFileReference(aDir, artifactPath.getFileName().toString());
                MediaType mediaType = null;
                try (InputStream is = Files.newInputStream(artifactPath);
                    BufferedInputStream bis = new BufferedInputStream(is)) {
                    mediaType = BackendUtils.getMimeType(bis, artifactPath.getFileName().toString());
                    RepositoryFactory.getRepository().putContentToFile(aFile, bis, mediaType);
                } catch (IOException e) {
                    LOGGER.error("Could not read artifact template file: {}", artifactPath);
                    return;
                }
            });
        }
        final Definitions part = BackendUtils.createWrapperDefinitions(wid);
        part.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().add(entry);
        RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(wid);
        String content = BackendUtils.getXMLAsString(part, true);
        try {
            RepositoryFactory.getRepository().putContentToFile(ref, content, MediaTypes.MEDIATYPE_TOSCA_DEFINITIONS);
        } catch (Exception e) {
            exceptions.add(e);
        }
    });
}
Also used : Path(java.nio.file.Path) DefinitionsChildId(org.eclipse.winery.common.ids.definitions.DefinitionsChildId) CsarImporter(org.eclipse.winery.repository.importing.CsarImporter) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) Definitions(org.eclipse.winery.model.tosca.Definitions) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) TransformerException(javax.xml.transform.TransformerException) URISyntaxException(java.net.URISyntaxException) JAXBException(javax.xml.bind.JAXBException) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) MediaType(org.apache.tika.mime.MediaType)

Aggregations

CsarImporter (org.eclipse.winery.repository.importing.CsarImporter)4 File (java.io.File)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URISyntaxException (java.net.URISyntaxException)1 Path (java.nio.file.Path)1 JAXBException (javax.xml.bind.JAXBException)1 TransformerException (javax.xml.transform.TransformerException)1 MediaType (org.apache.tika.mime.MediaType)1 RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)1 DefinitionsChildId (org.eclipse.winery.common.ids.definitions.DefinitionsChildId)1 Definitions (org.eclipse.winery.model.tosca.Definitions)1 TArtifactTemplate (org.eclipse.winery.model.tosca.TArtifactTemplate)1 TDefinitions (org.eclipse.winery.model.tosca.TDefinitions)1 IRepository (org.eclipse.winery.repository.backend.IRepository)1 ArtifactTemplateFilesDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId)1 ImportMetaInformation (org.eclipse.winery.repository.importing.ImportMetaInformation)1