Search in sources :

Example 66 with RepositoryFileReference

use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.

the class CsarImporter method importLicenseAndReadme.

void importLicenseAndReadme(Path rootPath, DefinitionsChildId wid, TOSCAMetaFile tmf, final List<String> errors) {
    String pathInsideRepo = Util.getPathInsideRepo(wid);
    Path defPath = rootPath.resolve(pathInsideRepo);
    Path readmeFile = defPath.resolve(Constants.README_FILE_NAME);
    Path licenseFile = defPath.resolve(Constants.LICENSE_FILE_NAME);
    if (Files.exists(readmeFile)) {
        RepositoryFileReference ref = new RepositoryFileReference(wid, Constants.README_FILE_NAME);
        importFile(readmeFile, ref, tmf, rootPath, errors);
    }
    if (Files.exists(licenseFile)) {
        RepositoryFileReference ref = new RepositoryFileReference(wid, Constants.LICENSE_FILE_NAME);
        importFile(licenseFile, ref, tmf, rootPath, errors);
    }
}
Also used : Path(java.nio.file.Path) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference)

Example 67 with RepositoryFileReference

use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.

the class CsarImporter method adjustServiceTemplate.

/**
 * In case plans are provided, the plans are imported into Winery's storage
 *
 * @param rootPath the root path of the extracted csar
 * @param tmf      the TOSCAMetaFile object used to determine the mime type of the plan
 * @param wid      Winery's internal id of the service template
 * @param st       the the service template to be imported {@inheritDoc}
 */
private void adjustServiceTemplate(Path rootPath, TOSCAMetaFile tmf, ServiceTemplateId wid, TServiceTemplate st, final List<String> errors) {
    List<TPlan> plans = st.getPlans();
    if (plans != null) {
        for (TPlan plan : plans) {
            PlanModelReference refContainer = plan.getPlanModelReference();
            if (refContainer != null) {
                String ref = refContainer.getReference();
                if (ref != null) {
                    // URLs are stored encoded -> undo the encoding
                    ref = EncodingUtil.URLdecode(ref);
                    URI refURI;
                    try {
                        refURI = new URI(ref);
                    } catch (URISyntaxException e) {
                        errors.add(String.format("Invalid URI %1$s", ref));
                        continue;
                    }
                    if (refURI.isAbsolute()) {
                        // We have to do nothing
                        continue;
                    }
                    Path path = rootPath.resolve(ref);
                    if (!Files.exists(path)) {
                        // possibly, the reference is relative to the Definitions subfolder
                        // COS01 does not make any explicit statement how to resolve the reference here
                        path = rootPath.resolve("Definitions").resolve(ref);
                        if (!Files.exists(path)) {
                            errors.add(String.format("Plan reference %1$s not found", ref));
                            // we quickly remove the reference to reflect the not-found in the data
                            refContainer.setReference(null);
                            continue;
                        }
                    }
                    PlansId plansId = new PlansId(wid);
                    PlanId pid = new PlanId(plansId, new XmlId(plan.getId(), false));
                    if (Files.isDirectory(path)) {
                        errors.add(String.format("Reference %1$s is a directory and Winery currently does not support importing directories", ref));
                        continue;
                    }
                    RepositoryFileReference fref = new RepositoryFileReference(pid, path.getFileName().toString());
                    importFile(path, fref, tmf, rootPath, errors);
                    // file is imported
                    // Adjust the reference
                    refContainer.setReference("../" + Util.getUrlPath(fref));
                }
            }
        }
    }
}
Also used : Path(java.nio.file.Path) PlanModelReference(org.eclipse.winery.model.tosca.TPlan.PlanModelReference) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) PlanId(org.eclipse.winery.model.ids.elements.PlanId) TPlan(org.eclipse.winery.model.tosca.TPlan) XmlId(org.eclipse.winery.model.ids.XmlId) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) PlansId(org.eclipse.winery.model.ids.elements.PlansId)

Example 68 with RepositoryFileReference

use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.

the class CsarImporter method importOtherImport.

/**
 * SIDE EFFECT: modifies the location of imp to point to the correct relative location (when read from the exported
 * CSAR)
 *
 * @param rootPath the absolute path where to resolve files from
 * @param options  the set of options applicable while importing a CSAR
 */
private void importOtherImport(Path rootPath, TImport imp, final List<String> errors, String type, CsarImportOptions options) {
    assert (!type.equals(Namespaces.TOSCA_NAMESPACE));
    String loc = imp.getLocation();
    if (!Util.isRelativeURI(loc)) {
        // This is just an information message
        errors.add("Absolute URIs are not resolved by Winery (" + loc + ")");
        return;
    }
    // location URLs are encoded: http://www.w3.org/TR/2001/WD-charmod-20010126/#sec-URIs, RFC http://www.ietf.org/rfc/rfc2396.txt
    loc = EncodingUtil.URLdecode(loc);
    Path path;
    try {
        path = rootPath.resolve(loc);
    } catch (Exception e) {
        // java.nio.file.InvalidPathException could be thrown which is a RuntimeException
        errors.add(e.getMessage());
        return;
    }
    if (!Files.exists(path)) {
        // fallback for older CSARs, where the location is given from the root
        path = rootPath.getParent().resolve(loc);
        if (!Files.exists(path)) {
            errors.add(String.format("File %1$s does not exist", loc));
            return;
        }
    }
    String namespace = imp.getNamespace();
    String fileName = path.getFileName().toString();
    String id = fileName;
    id = FilenameUtils.removeExtension(id);
    // Convention: id of import is filename without extension
    GenericImportId rid;
    if (type.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
        rid = new XSDImportId(namespace, id, false);
    } else {
        rid = new GenericImportId(namespace, id, false, type);
    }
    boolean importDataExistsInRepo = targetRepository.exists(rid);
    if (!importDataExistsInRepo) {
        // We have to
        // a) create a .definitions file
        // b) put the file itself in the repo
        // Create the definitions file
        TDefinitions defs = BackendUtils.createWrapperDefinitions(rid, targetRepository);
        defs.getImport().add(imp);
        // QUICK HACK: We change the imp object's location here and below again
        // This is "OK" as "storeDefinitions" serializes the current state and not the future state of the imp object
        // change the location to point to the file in the folder of the .definitions file
        imp.setLocation(fileName);
        // put the definitions file to the repository
        CsarImporter.storeDefinitions(targetRepository, rid, defs);
    }
    // put the file itself to the repo
    // ref is required to generate fileRef
    RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(rid);
    RepositoryFileReference fileRef = new RepositoryFileReference(ref.getParent(), fileName);
    // location is relative to Definitions/
    // even if the import already exists, we have to adapt the path
    // URIs are encoded
    String newLoc = "../" + Util.getUrlPath(fileRef);
    imp.setLocation(newLoc);
    if (!importDataExistsInRepo || options.isOverwrite()) {
        // finally write the file to the storage
        try (InputStream is = Files.newInputStream(path);
            BufferedInputStream bis = new BufferedInputStream(is)) {
            MediaType mediaType;
            if (type.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
                mediaType = MediaTypes.MEDIATYPE_XSD;
            } else {
                mediaType = BackendUtils.getMimeType(bis, path.getFileName().toString());
            }
            targetRepository.putContentToFile(fileRef, bis, mediaType);
        } catch (IllegalArgumentException | IOException e) {
            throw new IllegalStateException(e);
        }
        // we have to update the cache in case of a new XSD to speedup usage of winery
        if (rid instanceof XSDImportId) {
            // We do the initialization asynchronously
            // We do not check whether the XSD has already been checked
            // We cannot just check whether an XSD already has been handled since the XSD could change over time
            // Synchronization at org.eclipse.winery.repository.resources.imports.xsdimports.XSDImportResource.getAllDefinedLocalNames(short) also isn't feasible as the backend doesn't support locks
            CsarImporter.xsdParsingService.submit(() -> {
                CsarImporter.LOGGER.debug("Updating XSD import cache data");
                // We call the queries without storing the result:
                // We use the SIDEEFFECT that a cache is created
                final XsdImportManager xsdImportManager = targetRepository.getXsdImportManager();
                xsdImportManager.getAllDeclaredElementsLocalNames();
                xsdImportManager.getAllDefinedTypesLocalNames();
                CsarImporter.LOGGER.debug("Updated XSD import cache data");
            });
        }
    }
}
Also used : Path(java.nio.file.Path) XSDImportId(org.eclipse.winery.model.ids.definitions.imports.XSDImportId) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) AccountabilityException(org.eclipse.winery.accountability.exceptions.AccountabilityException) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) URISyntaxException(java.net.URISyntaxException) JAXBException(javax.xml.bind.JAXBException) GenericImportId(org.eclipse.winery.model.ids.definitions.imports.GenericImportId) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) BufferedInputStream(java.io.BufferedInputStream) MediaType(org.apache.tika.mime.MediaType) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) XsdImportManager(org.eclipse.winery.repository.backend.xsd.XsdImportManager)

Example 69 with RepositoryFileReference

use of org.eclipse.winery.repository.common.RepositoryFileReference 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
 */
protected void prepareForExport(IRepository repository, ArtifactTemplateId id) throws 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(repository, 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.
        putRefAsReferencedItemInCsar(repository, ref);
    }
}
Also used : 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) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference)

Example 70 with RepositoryFileReference

use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.

the class ToscaExportUtil method addVisualAppearanceToCSAR.

protected void addVisualAppearanceToCSAR(IRepository repository, TopologyGraphElementEntityTypeId id) {
    VisualAppearanceId visId = new VisualAppearanceId(id);
    if (repository.exists(visId)) {
        // we do NOT check for the id, but simply check for bigIcon.png (only exists in NodeType) and smallIcon.png (exists in NodeType and RelationshipType)
        RepositoryFileReference ref = new RepositoryFileReference(visId, Filename.FILENAME_BIG_ICON);
        if (repository.exists(ref)) {
            putRefAsReferencedItemInCsar(repository, ref);
        }
        ref = new RepositoryFileReference(visId, Filename.FILENAME_SMALL_ICON);
        if (repository.exists(ref)) {
            putRefAsReferencedItemInCsar(repository, ref);
        }
    }
}
Also used : RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) VisualAppearanceId(org.eclipse.winery.repository.datatypes.ids.elements.VisualAppearanceId)

Aggregations

RepositoryFileReference (org.eclipse.winery.repository.common.RepositoryFileReference)81 IOException (java.io.IOException)33 Path (java.nio.file.Path)26 TDefinitions (org.eclipse.winery.model.tosca.TDefinitions)17 QName (javax.xml.namespace.QName)14 ArtifactTemplateFilesDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId)14 InputStream (java.io.InputStream)13 ArrayList (java.util.ArrayList)12 ArtifactTemplateId (org.eclipse.winery.model.ids.definitions.ArtifactTemplateId)12 IRepository (org.eclipse.winery.repository.backend.IRepository)12 ServiceTemplateId (org.eclipse.winery.model.ids.definitions.ServiceTemplateId)11 PlanId (org.eclipse.winery.model.ids.elements.PlanId)10 PlansId (org.eclipse.winery.model.ids.elements.PlansId)10 XmlId (org.eclipse.winery.model.ids.XmlId)9 Test (org.junit.jupiter.api.Test)9 DefinitionsChildId (org.eclipse.winery.model.ids.definitions.DefinitionsChildId)8 MediaType (org.apache.tika.mime.MediaType)7 TArtifactTemplate (org.eclipse.winery.model.tosca.TArtifactTemplate)7 BufferedInputStream (java.io.BufferedInputStream)6 HashMap (java.util.HashMap)6