Search in sources :

Example 36 with RepositoryFileReference

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

the class FilebasedRepository method getZippedContents.

@Override
public void getZippedContents(final GenericId id, OutputStream out) throws WineryRepositoryException {
    Objects.requireNonNull(id);
    Objects.requireNonNull(out);
    SortedSet<RepositoryFileReference> containedFiles = this.getContainedFiles(id);
    try (final ArchiveOutputStream zos = new ArchiveStreamFactory().createArchiveOutputStream("zip", out)) {
        for (RepositoryFileReference ref : containedFiles) {
            ZipArchiveEntry zipArchiveEntry;
            final Optional<Path> subDirectory = ref.getSubDirectory();
            if (subDirectory.isPresent()) {
                zipArchiveEntry = new ZipArchiveEntry(subDirectory.get().resolve(ref.getFileName()).toString());
            } else {
                zipArchiveEntry = new ZipArchiveEntry(ref.getFileName());
            }
            zos.putArchiveEntry(zipArchiveEntry);
            try (InputStream is = RepositoryFactory.getRepository().newInputStream(ref)) {
                IOUtils.copy(is, zos);
            }
            zos.closeArchiveEntry();
        }
    } catch (ArchiveException e) {
        throw new WineryRepositoryException("Internal error while generating archive", e);
    } catch (IOException e) {
        throw new WineryRepositoryException("I/O exception during export", e);
    }
}
Also used : ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) ZipInputStream(java.util.zip.ZipInputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ArchiveException(org.apache.commons.compress.archivers.ArchiveException) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream) WineryRepositoryException(org.eclipse.winery.repository.exceptions.WineryRepositoryException)

Example 37 with RepositoryFileReference

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

the class RepositoryBasedXsdImportManager method getAllDefinedLocalNames.

// we need "unchecked", because of the parsing of the cache
@SuppressWarnings("unchecked")
private List<String> getAllDefinedLocalNames(final XSDImportId id, final boolean getTypes) {
    Objects.requireNonNull(id);
    Optional<RepositoryFileReference> ref = this.getXsdFileReference(id);
    if (!ref.isPresent()) {
        return Collections.emptyList();
    }
    short type = getTypes ? XSConstants.TYPE_DEFINITION : XSConstants.ELEMENT_DECLARATION;
    Date lastUpdate = RepositoryFactory.getRepository().getLastUpdate(ref.get());
    @NonNull final String cacheFileName = "definedLocalNames " + Integer.toString(type) + ".cache";
    @NonNull final RepositoryFileReference cacheRef = new RepositoryFileReference(id, cacheFileName);
    boolean cacheNeedsUpdate = true;
    if (RepositoryFactory.getRepository().exists(cacheRef)) {
        Date lastUpdateCache = RepositoryFactory.getRepository().getLastUpdate(cacheRef);
        if (lastUpdate.compareTo(lastUpdateCache) <= 0) {
            cacheNeedsUpdate = false;
        }
    }
    List<String> result;
    if (cacheNeedsUpdate) {
        final Optional<XSModel> model = BackendUtils.getXSModel(ref.get());
        if (!model.isPresent()) {
            return Collections.emptyList();
        }
        XSNamedMap components = model.get().getComponents(type);
        // @SuppressWarnings("unchecked")
        int len = components.getLength();
        result = new ArrayList<>(len);
        for (int i = 0; i < len; i++) {
            XSObject item = components.item(i);
            // We want to return only types defined in the namespace of this resource
            if (id.getNamespace().getDecoded().equals(item.getNamespace())) {
                result.add(item.getName());
            }
        }
        String cacheContent = null;
        try {
            cacheContent = BackendUtils.mapper.writeValueAsString(result);
        } catch (JsonProcessingException e) {
            LOGGER.error("Could not generate cache content", e);
        }
        try {
            RepositoryFactory.getRepository().putContentToFile(cacheRef, cacheContent, MediaTypes.MEDIATYPE_APPLICATION_JSON);
        } catch (IOException e) {
            LOGGER.error("Could not update cache", e);
        }
    } else {
        // cache should contain most recent information
        try (InputStream is = RepositoryFactory.getRepository().newInputStream(cacheRef)) {
            result = BackendUtils.mapper.readValue(is, java.util.List.class);
        } catch (IOException e) {
            LOGGER.error("Could not read from cache", e);
            result = Collections.emptyList();
        }
    }
    return result;
}
Also used : InputStream(java.io.InputStream) XSObject(org.apache.xerces.xs.XSObject) IOException(java.io.IOException) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) NonNull(org.eclipse.jdt.annotation.NonNull) XSModel(org.apache.xerces.xs.XSModel) XSNamedMap(org.apache.xerces.xs.XSNamedMap) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 38 with RepositoryFileReference

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

the class RepositoryBasedXsdImportManager method getMapFromLocalNameToXSD.

@Override
public Map<String, RepositoryFileReference> getMapFromLocalNameToXSD(final Namespace namespace, final boolean getTypes) {
    Set<XSDImportId> importsOfNamespace = this.getImportsOfNamespace(namespace);
    Map<String, RepositoryFileReference> result = new HashMap<>();
    for (XSDImportId imp : importsOfNamespace) {
        final List<String> allDefinedLocalNames = this.getAllDefinedLocalNames(namespace, getTypes);
        Optional<RepositoryFileReference> ref = getXsdFileReference(imp);
        if (!ref.isPresent()) {
            LOGGER.error("Ref is not defined");
        } else {
            for (String localName : allDefinedLocalNames) {
                result.put(localName, ref.get());
            }
        }
    }
    return result;
}
Also used : XSDImportId(org.eclipse.winery.common.ids.definitions.imports.XSDImportId) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference)

Example 39 with RepositoryFileReference

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

the class CsarExporter method addManifest.

private void addManifest(IRepository repository, DefinitionsChildId id, Collection<String> definitionNames, Map<RepositoryFileReference, String> refMap, ArchiveOutputStream out) throws IOException {
    String entryDefinitionsReference = CsarExporter.getDefinitionsPathInsideCSAR(repository, id);
    out.putArchiveEntry(new ZipArchiveEntry("TOSCA-Metadata/TOSCA.meta"));
    PrintWriter pw = new PrintWriter(out);
    // Setting Versions
    pw.println("TOSCA-Meta-Version: 1.0");
    pw.println("CSAR-Version: 1.0");
    String versionString = "Created-By: Winery " + Environment.getVersion();
    pw.println(versionString);
    // Winery currently is unaware of tDefinitions, therefore, we use the
    // name of the service template
    pw.println("Entry-Definitions: " + entryDefinitionsReference);
    pw.println();
    assert (definitionNames.contains(entryDefinitionsReference));
    for (String name : definitionNames) {
        pw.println("Name: " + name);
        pw.println("Content-Type: " + org.eclipse.winery.common.constants.MimeTypes.MIMETYPE_TOSCA_DEFINITIONS);
        pw.println();
    }
    // Setting other files, mainly files belonging to artifacts
    for (RepositoryFileReference ref : refMap.keySet()) {
        String archivePath = refMap.get(ref);
        pw.println("Name: " + archivePath);
        String mimeType;
        if (ref instanceof DummyRepositoryFileReferenceForGeneratedXSD) {
            mimeType = MimeTypes.MIMETYPE_XSD;
        } else {
            mimeType = repository.getMimeType(ref);
        }
        pw.println("Content-Type: " + mimeType);
        pw.println();
    }
    pw.flush();
    out.closeArchiveEntry();
}
Also used : RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry)

Example 40 with RepositoryFileReference

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

the class CsarExporter method writeCsar.

/**
 * Writes a complete CSAR containing all necessary things reachable from the given service template
 *
 * @param entryId the id of the service template to export
 * @param out     the output stream to write to
 */
public void writeCsar(IRepository repository, DefinitionsChildId entryId, OutputStream out) throws ArchiveException, IOException, JAXBException, RepositoryCorruptException {
    CsarExporter.LOGGER.trace("Starting CSAR export with {}", entryId.toString());
    Map<RepositoryFileReference, String> refMap = new HashMap<>();
    Collection<String> definitionNames = new ArrayList<>();
    try (final ArchiveOutputStream zos = new ArchiveStreamFactory().createArchiveOutputStream("zip", out)) {
        ToscaExportUtil exporter = new ToscaExportUtil();
        Map<String, Object> conf = new HashMap<>();
        ExportedState exportedState = new ExportedState();
        DefinitionsChildId currentId = entryId;
        do {
            String defName = CsarExporter.getDefinitionsPathInsideCSAR(repository, currentId);
            definitionNames.add(defName);
            zos.putArchiveEntry(new ZipArchiveEntry(defName));
            Collection<DefinitionsChildId> referencedIds;
            referencedIds = exporter.exportTOSCA(repository, currentId, zos, refMap, conf);
            zos.closeArchiveEntry();
            exportedState.flagAsExported(currentId);
            exportedState.flagAsExportRequired(referencedIds);
            currentId = exportedState.pop();
        } while (currentId != null);
        // if we export a ServiceTemplate, data for the self-service portal might exist
        if (entryId instanceof ServiceTemplateId) {
            ServiceTemplateId serviceTemplateId = (ServiceTemplateId) entryId;
            this.addSelfServiceMetaData(repository, serviceTemplateId, refMap);
            this.addSelfServiceFiles(repository, serviceTemplateId, refMap, zos);
        }
        // now, refMap contains all files to be added to the CSAR
        // write manifest directly after the definitions to have it more at the beginning of the ZIP rather than having it at the very end
        this.addManifest(repository, entryId, definitionNames, refMap, zos);
        // used for generated XSD schemas
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer;
        try {
            transformer = tFactory.newTransformer();
        } catch (TransformerConfigurationException e1) {
            CsarExporter.LOGGER.debug(e1.getMessage(), e1);
            throw new IllegalStateException("Could not instantiate transformer", e1);
        }
        // write all referenced files
        for (RepositoryFileReference ref : refMap.keySet()) {
            String archivePath = refMap.get(ref);
            CsarExporter.LOGGER.trace("Creating {}", archivePath);
            if (ref instanceof DummyRepositoryFileReferenceForGeneratedXSD) {
                addDummyRepositoryFileReferenceForGeneratedXSD(zos, transformer, (DummyRepositoryFileReferenceForGeneratedXSD) ref, archivePath);
            } else {
                if (ref.getParent() instanceof DirectoryId) {
                    // special handling for artifact template directories "source" and "files"
                    addArtifactTemplateToZipFile(zos, repository, ref, archivePath);
                } else {
                    addFileToZipArchive(zos, repository, ref, archivePath);
                    zos.closeArchiveEntry();
                }
            }
        }
        this.addNamespacePrefixes(zos, repository);
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) DefinitionsChildId(org.eclipse.winery.common.ids.definitions.DefinitionsChildId) ServiceTemplateId(org.eclipse.winery.common.ids.definitions.ServiceTemplateId) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) ServiceTemplateSelfServiceFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ServiceTemplateSelfServiceFilesDirectoryId) DirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream)

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