Search in sources :

Example 46 with RepositoryFileReference

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

the class CsarImporter method importAllFiles.

/**
 * Imports all given files from the file system to the repository
 *
 * @param rootPath    used to make the path p relative in order to determine the mime type and the
 *                    RepositoryFileReference
 * @param files       list of all files
 * @param directoryId the id of the directory of the artifact template the files to attach to
 * @param tmf         the TOSCAMetaFile object used to determine the mimetype. Must not be null.
 * @param errors      list where import errors should be stored to
 */
private void importAllFiles(Path rootPath, Collection<Path> files, DirectoryId directoryId, TOSCAMetaFile tmf, final List<String> errors) {
    // remove the filePathInsideRepo to correctly store the files in the files folder inside an artifact template
    // otherwise, the files are saved in the sub directory of the artifact template
    // this is required, to enable the cycle CSAR export, clean , import CSAR
    String pathInsideRepo = Util.getPathInsideRepo(directoryId);
    for (Path p : files) {
        if (!Files.exists(p)) {
            errors.add(String.format("File %1$s does not exist", p.toString()));
            return;
        }
        Path subDirectory = rootPath.relativize(p).getParent();
        RepositoryFileReference fref;
        // files are stored in "files/" or in "sources/"
        if ((subDirectory == null) || (subDirectory.getParent().toString().endsWith(pathInsideRepo))) {
            fref = new RepositoryFileReference(directoryId, p.getFileName().toString());
        } else {
            fref = new RepositoryFileReference(directoryId, subDirectory, p.getFileName().toString());
        }
        importFile(p, fref, tmf, rootPath, errors);
    }
}
Also used : RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference)

Example 47 with RepositoryFileReference

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

the class CsarImporter method importSelfServiceMetaData.

/**
 * Imports a self-service meta data description (if available)
 * <p>
 * The first service template in the provided entry definitions is taken
 */
private void importSelfServiceMetaData(final TOSCAMetaFile tmf, final Path rootPath, Path entryDefinitions, final List<String> errors) {
    final Path selfServiceDir = rootPath.resolve(Constants.DIRNAME_SELF_SERVICE_METADATA);
    if (!Files.exists(selfServiceDir)) {
        CsarImporter.LOGGER.debug("Self-service Portal directory does not exist in CSAR");
        return;
    }
    if (!Files.exists(entryDefinitions)) {
        CsarImporter.LOGGER.debug("Entry definitions does not exist.");
        return;
    }
    Unmarshaller um = JAXBSupport.createUnmarshaller();
    TDefinitions defs;
    try {
        defs = (TDefinitions) um.unmarshal(entryDefinitions.toFile());
    } catch (JAXBException e) {
        errors.add("Could not unmarshal definitions " + entryDefinitions.getFileName() + " " + e.getMessage());
        return;
    } catch (ClassCastException e) {
        errors.add("Definitions " + entryDefinitions.getFileName() + " is not a TDefinitions " + e.getMessage());
        return;
    }
    final int cutLength = selfServiceDir.toString().length() + 1;
    Iterator<TExtensibleElements> iterator = defs.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().iterator();
    boolean found = false;
    TExtensibleElements next = null;
    while (iterator.hasNext() && !found) {
        next = iterator.next();
        if (next instanceof TServiceTemplate) {
            found = true;
        }
    }
    if (found) {
        TServiceTemplate serviceTemplate = (TServiceTemplate) next;
        String namespace = serviceTemplate.getTargetNamespace();
        if (namespace == null) {
            namespace = defs.getTargetNamespace();
        }
        ServiceTemplateId stId = new ServiceTemplateId(namespace, serviceTemplate.getId(), false);
        final SelfServiceMetaDataId id = new SelfServiceMetaDataId(stId);
        try {
            Files.walkFileTree(selfServiceDir, new SimpleFileVisitor<Path>() {

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
                    String name = file.toString().substring(cutLength);
                    // check: if name contains "/", this could lead to exceptions
                    RepositoryFileReference ref = new RepositoryFileReference(id, name);
                    if (name.equals("data.xml")) {
                        // We quickly replace it via String replacement instead of XSLT
                        try {
                            String oldContent = org.apache.commons.io.FileUtils.readFileToString(file.toFile(), "UTF-8");
                            String newContent = oldContent.replace("http://opentosca.org/self-service", "http://www.eclipse.org/winery/model/selfservice");
                            newContent = newContent.replace(":application", ":Application");
                            if (!oldContent.equals(newContent)) {
                                // we replaced something -> write new content to old file
                                org.apache.commons.io.FileUtils.writeStringToFile(file.toFile(), newContent, "UTF-8");
                            }
                        } catch (IOException e) {
                            CsarImporter.LOGGER.debug("Could not replace content in data.xml", e);
                        }
                    }
                    importFile(file, ref, tmf, rootPath, errors);
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (IOException e) {
            CsarImporter.LOGGER.debug(e.getMessage(), e);
            errors.add("Self-service Meta Data: " + e.getMessage());
        }
    }
}
Also used : JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) SelfServiceMetaDataId(org.eclipse.winery.repository.datatypes.ids.elements.SelfServiceMetaDataId) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) Unmarshaller(javax.xml.bind.Unmarshaller) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 48 with RepositoryFileReference

use of org.eclipse.winery.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
 */
private void importOtherImport(Path rootPath, TImport imp, final List<String> errors, String type, boolean overwrite) {
    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 = Util.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 = RepositoryFactory.getRepository().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);
        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(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 || overwrite) {
        // 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());
            }
            RepositoryFactory.getRepository().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 checck 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 = RepositoryFactory.getRepository().getXsdImportManager();
                xsdImportManager.getAllDeclaredElementsLocalNames();
                xsdImportManager.getAllDefinedTypesLocalNames();
                CsarImporter.LOGGER.debug("Updated XSD import cache data");
            });
        }
    }
}
Also used : XSDImportId(org.eclipse.winery.common.ids.definitions.imports.XSDImportId) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) JAXBException(javax.xml.bind.JAXBException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) IOException(java.io.IOException) GenericImportId(org.eclipse.winery.common.ids.definitions.imports.GenericImportId) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) BufferedInputStream(java.io.BufferedInputStream) MediaType(org.apache.tika.mime.MediaType) XsdImportManager(org.eclipse.winery.repository.backend.xsd.XsdImportManager)

Example 49 with RepositoryFileReference

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

the class RestUtils method cloneServiceTemplate.

public static ServiceTemplateId cloneServiceTemplate(ServiceTemplateId serviceTemplate, String newName, String artifactName) throws JAXBException, IllegalArgumentException, IOException {
    ServiceTemplateId newServiceTemplateId = new ServiceTemplateId(serviceTemplate.getNamespace().getDecoded(), newName, false);
    RepositoryFileReference fileRef = new RepositoryFileReference(newServiceTemplateId, "ServiceTemplate.tosca");
    Definitions defs = new ServiceTemplateResource(serviceTemplate).getDefinitions();
    defs.setId(newName + "Definitions");
    defs.setName(newName + "Definitions generated from Artifact " + artifactName);
    TServiceTemplate oldSTModel = null;
    for (TExtensibleElements el : defs.getServiceTemplateOrNodeTypeOrNodeTypeImplementation()) {
        if (el instanceof TServiceTemplate) {
            oldSTModel = (TServiceTemplate) el;
        }
    }
    oldSTModel.setId(newName);
    oldSTModel.setName(newName + " generated from Artifact " + artifactName);
    // remove xaaspackager tags
    Collection<TTag> toRemove = new ArrayList<TTag>();
    for (TTag tag : oldSTModel.getTags().getTag()) {
        switch(tag.getName()) {
            case "xaasPackageNode":
            case "xaasPackageArtifactType":
            case "xaasPackageDeploymentArtifact":
                toRemove.add(tag);
                break;
            default:
                break;
        }
    }
    oldSTModel.getTags().getTag().removeAll(toRemove);
    JAXBContext context = JAXBContext.newInstance(Definitions.class);
    Marshaller m = context.createMarshaller();
    StringWriter sw = new StringWriter();
    m.marshal(defs, sw);
    String xmlString = sw.toString();
    RepositoryFactory.getRepository().putContentToFile(fileRef, xmlString, MediaTypes.MEDIATYPE_TOSCA_DEFINITIONS);
    return newServiceTemplateId;
}
Also used : Marshaller(javax.xml.bind.Marshaller) ServiceTemplateResource(org.eclipse.winery.repository.rest.resources.servicetemplates.ServiceTemplateResource) JAXBContext(javax.xml.bind.JAXBContext) ServiceTemplateId(org.eclipse.winery.common.ids.definitions.ServiceTemplateId) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) StringWriter(java.io.StringWriter)

Example 50 with RepositoryFileReference

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

the class ArtifactTemplateResource method copySourceToFilesResource.

@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response copySourceToFilesResource(@ApiParam(value = "if data contains a non-empty array than only the files" + " whose names are included are copied ", required = true) ArtifactResourcesApiData data) {
    List<String> artifactList = data.getArtifactNames();
    DirectoryId sourceDir = new ArtifactTemplateSourceDirectoryId((ArtifactTemplateId) this.id);
    FilesResource filesResource = getFilesResource();
    for (RepositoryFileReference ref : RepositoryFactory.getRepository().getContainedFiles(sourceDir)) {
        if (artifactList == null || artifactList.contains(ref.getFileName())) {
            try (InputStream inputStream = RepositoryFactory.getRepository().newInputStream(ref)) {
                String fileName = ref.getFileName();
                String subDirectory = ref.getSubDirectory().map(s -> s.toString()).orElse("");
                filesResource.putFile(fileName, subDirectory, inputStream);
            } catch (IOException e) {
                LOGGER.debug("The artifact source " + ref.getFileName() + " could not be copied to the files directory.", e);
                return Response.status(Status.INTERNAL_SERVER_ERROR).build();
            }
        }
    }
    return Response.status(Status.CREATED).build();
}
Also used : MimeTypes(org.eclipse.winery.common.constants.MimeTypes) DirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId) LoggerFactory(org.slf4j.LoggerFactory) ApiParam(io.swagger.annotations.ApiParam) HasType(org.eclipse.winery.model.tosca.HasType) AbstractComponentInstanceWithReferencesResource(org.eclipse.winery.repository.rest.resources._support.AbstractComponentInstanceWithReferencesResource) ArtifactTemplateSourceDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateSourceDirectoryId) MediaType(javax.ws.rs.core.MediaType) ArtifactTemplateId(org.eclipse.winery.common.ids.definitions.ArtifactTemplateId) BackendUtils(org.eclipse.winery.repository.backend.BackendUtils) Status(javax.ws.rs.core.Response.Status) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) Logger(org.slf4j.Logger) IOException(java.io.IOException) RepositoryFactory(org.eclipse.winery.repository.backend.RepositoryFactory) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) ArtifactResourcesApiData(org.eclipse.winery.repository.rest.resources.apiData.ArtifactResourcesApiData) List(java.util.List) TExtensibleElements(org.eclipse.winery.model.tosca.TExtensibleElements) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) IEntityTemplateResource(org.eclipse.winery.repository.rest.resources.entitytemplates.IEntityTemplateResource) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) RestUtils(org.eclipse.winery.repository.rest.RestUtils) IHasName(org.eclipse.winery.repository.rest.resources._support.IHasName) PropertiesResource(org.eclipse.winery.repository.rest.resources.entitytemplates.PropertiesResource) InputStream(java.io.InputStream) DirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId) ArtifactTemplateSourceDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateSourceDirectoryId) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) InputStream(java.io.InputStream) ArtifactTemplateSourceDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateSourceDirectoryId) IOException(java.io.IOException)

Aggregations

RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)51 IOException (java.io.IOException)15 InputStream (java.io.InputStream)8 ArtifactTemplateId (org.eclipse.winery.common.ids.definitions.ArtifactTemplateId)8 Definitions (org.eclipse.winery.model.tosca.Definitions)8 BufferedInputStream (java.io.BufferedInputStream)6 ArtifactTemplateSourceDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateSourceDirectoryId)6 Test (org.junit.Test)6 Path (java.nio.file.Path)5 JAXBException (javax.xml.bind.JAXBException)5 MediaType (org.apache.tika.mime.MediaType)5 ArtifactTemplateFilesDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId)5 ZipInputStream (java.util.zip.ZipInputStream)4 Unmarshaller (javax.xml.bind.Unmarshaller)4 QName (javax.xml.namespace.QName)4 XmlId (org.eclipse.winery.common.ids.XmlId)4 GenericImportId (org.eclipse.winery.common.ids.definitions.imports.GenericImportId)4 XSDImportId (org.eclipse.winery.common.ids.definitions.imports.XSDImportId)4 PlanId (org.eclipse.winery.common.ids.elements.PlanId)4 PlansId (org.eclipse.winery.common.ids.elements.PlansId)4