Search in sources :

Example 6 with ArtifactTemplateFilesDirectoryId

use of org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId in project winery by eclipse.

the class ToscaExportUtil method prepareForExport.

/**
 * Determines the referenced definition children Ids and also updates the references in the Artifact Template
 *
 * @return a collection of referenced definition child Ids
 */
private void prepareForExport(IRepository repository, ArtifactTemplateId id) throws RepositoryCorruptException, IOException {
    // Export files
    // This method is called BEFORE the concrete definitions element is written.
    // Therefore, we adapt the content of the attached files to the really existing files
    BackendUtils.synchronizeReferences(id);
    DirectoryId fileDir = new ArtifactTemplateFilesDirectoryId(id);
    SortedSet<RepositoryFileReference> files = repository.getContainedFiles(fileDir);
    for (RepositoryFileReference ref : files) {
        // Even if writing a TOSCA only (!this.writingCSAR),
        // we put the virtual path in the TOSCA
        // Reason: Winery is mostly used as a service and local storage
        // reference to not make sense
        // The old implementation had absolutePath.toUri().toString();
        // there, but this does not work when using a cloud blob store.
        this.putRefAsReferencedItemInCsar(ref);
    }
}
Also used : ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) DirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference)

Example 7 with ArtifactTemplateFilesDirectoryId

use of org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId in project winery by eclipse.

the class CsarImporter method adjustArtifactTemplate.

/**
 * Adjusts the given artifact template to conform with the repository format
 * <p>
 * We import the files given at the artifact references
 */
private void adjustArtifactTemplate(Path rootPath, TOSCAMetaFile tmf, ArtifactTemplateId atid, TArtifactTemplate ci, final List<String> errors) {
    ArtifactReferences refs = ci.getArtifactReferences();
    if (refs == null) {
        // no references stored - break
        return;
    }
    List<TArtifactReference> refList = refs.getArtifactReference();
    Iterator<TArtifactReference> iterator = refList.iterator();
    while (iterator.hasNext()) {
        TArtifactReference ref = iterator.next();
        String reference = ref.getReference();
        // URLs are stored encoded -> undo the encoding
        reference = Util.URLdecode(reference);
        URI refURI;
        try {
            refURI = new URI(reference);
        } catch (URISyntaxException e) {
            errors.add(String.format("Invalid URI %1$s", ref));
            continue;
        }
        if (refURI.isAbsolute()) {
            // We have to do nothing
            continue;
        }
        // we remove the current element as it will be handled during the export
        iterator.remove();
        Path path = rootPath.resolve(reference);
        if (!Files.exists(path)) {
            errors.add(String.format("Reference %1$s not found", reference));
            return;
        }
        Set<Path> allFiles;
        if (Files.isRegularFile(path)) {
            allFiles = new HashSet<>();
            allFiles.add(path);
        } else {
            if (!Files.isDirectory(path)) {
                LOGGER.error("path {} is not a directory", path);
            }
            Path localRoot = rootPath.resolve(path);
            List<Object> includeOrExclude = ref.getIncludeOrExclude();
            if (includeOrExclude.get(0) instanceof TArtifactReference.Exclude) {
                // Implicit semantics of an exclude listed first:
                // include all files and then exclude the files matched by the pattern
                allFiles = this.getAllFiles(localRoot);
            } else {
                // semantics if include listed as first:
                // same as listed at other places
                allFiles = new HashSet<>();
            }
            for (Object object : includeOrExclude) {
                if (object instanceof TArtifactReference.Include) {
                    this.handleInclude((TArtifactReference.Include) object, localRoot, allFiles);
                } else {
                    assert (object instanceof TArtifactReference.Exclude);
                    this.handleExclude((TArtifactReference.Exclude) object, localRoot, allFiles);
                }
            }
        }
        DirectoryId fileDir = new ArtifactTemplateFilesDirectoryId(atid);
        this.importAllFiles(rootPath, allFiles, fileDir, tmf, errors);
    }
    if (refList.isEmpty()) {
        // everything is imported and is a file stored locally
        // we don't need the references stored locally: they are generated on the fly when exporting
        ci.setArtifactReferences(null);
    }
}
Also used : Exclude(org.eclipse.winery.model.tosca.TArtifactReference.Exclude) ArtifactReferences(org.eclipse.winery.model.tosca.TArtifactTemplate.ArtifactReferences) Include(org.eclipse.winery.model.tosca.TArtifactReference.Include) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) DirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId) Exclude(org.eclipse.winery.model.tosca.TArtifactReference.Exclude)

Aggregations

ArtifactTemplateFilesDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId)7 RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)5 DirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId)4 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 Path (java.nio.file.Path)2 ArtifactTemplateId (org.eclipse.winery.common.ids.definitions.ArtifactTemplateId)2 TArtifactTemplate (org.eclipse.winery.model.tosca.TArtifactTemplate)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 JAXBException (javax.xml.bind.JAXBException)1 TransformerException (javax.xml.transform.TransformerException)1 MediaType (org.apache.tika.mime.MediaType)1 DefinitionsChildId (org.eclipse.winery.common.ids.definitions.DefinitionsChildId)1 Definitions (org.eclipse.winery.model.tosca.Definitions)1 TArtifactReference (org.eclipse.winery.model.tosca.TArtifactReference)1 Exclude (org.eclipse.winery.model.tosca.TArtifactReference.Exclude)1 Include (org.eclipse.winery.model.tosca.TArtifactReference.Include)1