Search in sources :

Example 1 with CsarExporter

use of org.eclipse.winery.repository.export.CsarExporter in project winery by eclipse.

the class RestUtils method getCSARofSelectedResource.

public static Response getCSARofSelectedResource(final AbstractComponentInstanceResource resource) {
    final CsarExporter exporter = new CsarExporter();
    StreamingOutput so = output -> {
        try {
            exporter.writeCsar(RepositoryFactory.getRepository(), resource.getId(), output);
        } catch (Exception e) {
            throw new WebApplicationException(e);
        }
    };
    StringBuilder sb = new StringBuilder();
    sb.append("attachment;filename=\"");
    sb.append(resource.getXmlId().getEncoded());
    sb.append(org.eclipse.winery.repository.Constants.SUFFIX_CSAR);
    sb.append("\"");
    return Response.ok().header("Content-Disposition", sb.toString()).type(MimeTypes.MIMETYPE_ZIP).entity(so).build();
}
Also used : XLogger(org.slf4j.ext.XLogger) MimeTypes(org.eclipse.winery.common.constants.MimeTypes) ToscaElementId(org.eclipse.winery.common.ids.elements.ToscaElementId) XLoggerFactory(org.slf4j.ext.XLoggerFactory) FileTime(java.nio.file.attribute.FileTime) ArtifactTemplatesResource(org.eclipse.winery.repository.rest.resources.entitytemplates.artifacttemplates.ArtifactTemplatesResource) NamespaceAndDefinedLocalNamesForAngular(org.eclipse.winery.repository.rest.datatypes.NamespaceAndDefinedLocalNamesForAngular) FormDataContentDisposition(com.sun.jersey.core.header.FormDataContentDisposition) ResourceCreationResult(org.eclipse.winery.repository.rest.resources._support.ResourceCreationResult) Constants(org.eclipse.winery.repository.Constants) ServiceTemplateResource(org.eclipse.winery.repository.rest.resources.servicetemplates.ServiceTemplateResource) IPersistable(org.eclipse.winery.repository.rest.resources._support.IPersistable) URI(java.net.URI) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) QNameWithTypeApiData(org.eclipse.winery.repository.rest.resources.apiData.QNameWithTypeApiData) GenericId(org.eclipse.winery.common.ids.GenericId) ContentDisposition(com.sun.jersey.core.header.ContentDisposition) Util(org.eclipse.winery.common.Util) JAXBException(javax.xml.bind.JAXBException) Collectors(java.util.stream.Collectors) RepositoryFactory(org.eclipse.winery.repository.backend.RepositoryFactory) TopologyGraphElementEntityTypeResource(org.eclipse.winery.repository.rest.resources.entitytypes.TopologyGraphElementEntityTypeResource) ArtifactTemplateResource(org.eclipse.winery.repository.rest.resources.entitytemplates.artifacttemplates.ArtifactTemplateResource) NamespaceAndDefinedLocalNames(org.eclipse.winery.repository.backend.xsd.NamespaceAndDefinedLocalNames) org.eclipse.winery.model.tosca(org.eclipse.winery.model.tosca) AccessControlException(java.security.AccessControlException) MediaTypes(org.eclipse.winery.repository.backend.constants.MediaTypes) WebApplicationException(javax.ws.rs.WebApplicationException) Namespace(org.eclipse.winery.common.ids.Namespace) QName(javax.xml.namespace.QName) Converter(org.eclipse.winery.yaml.converter.Converter) java.util(java.util) ServiceTemplateId(org.eclipse.winery.common.ids.definitions.ServiceTemplateId) ClientResponse(com.sun.jersey.api.client.ClientResponse) Marshaller(javax.xml.bind.Marshaller) XmlId(org.eclipse.winery.common.ids.XmlId) ArtifactTemplateId(org.eclipse.winery.common.ids.definitions.ArtifactTemplateId) BackendUtils(org.eclipse.winery.repository.backend.BackendUtils) Application(org.eclipse.winery.model.selfservice.Application) MultiException(org.eclipse.winery.yaml.common.exception.MultiException) WebResource(com.sun.jersey.api.client.WebResource) Environment(org.eclipse.winery.repository.configuration.Environment) Status(javax.ws.rs.core.Response.Status) JAXBContext(javax.xml.bind.JAXBContext) CsarExporter(org.eclipse.winery.repository.export.CsarExporter) ToscaExportUtil(org.eclipse.winery.repository.export.ToscaExportUtil) FormDataBodyPart(com.sun.jersey.multipart.FormDataBodyPart) AbstractComponentInstanceResource(org.eclipse.winery.repository.rest.resources._support.AbstractComponentInstanceResource) StringWriter(java.io.StringWriter) IOException(java.io.IOException) javax.ws.rs.core(javax.ws.rs.core) DefinitionsChildId(org.eclipse.winery.common.ids.definitions.DefinitionsChildId) Family(javax.ws.rs.core.Response.Status.Family) Client(com.sun.jersey.api.client.Client) ModelUtilities(org.eclipse.winery.model.tosca.utils.ModelUtilities) AbstractComponentsResource(org.eclipse.winery.repository.rest.resources._support.AbstractComponentsResource) InputStream(java.io.InputStream) WebApplicationException(javax.ws.rs.WebApplicationException) CsarExporter(org.eclipse.winery.repository.export.CsarExporter) JAXBException(javax.xml.bind.JAXBException) AccessControlException(java.security.AccessControlException) WebApplicationException(javax.ws.rs.WebApplicationException) MultiException(org.eclipse.winery.yaml.common.exception.MultiException) IOException(java.io.IOException)

Example 2 with CsarExporter

use of org.eclipse.winery.repository.export.CsarExporter in project winery by eclipse.

the class ConsistencyChecker method checkCsar.

private void checkCsar(DefinitionsChildId id, Path tempCsar, IRepository repository) {
    CsarExporter exporter = new CsarExporter(repository);
    Map<String, Object> exportConfiguration = new HashMap<>();
    try (OutputStream outputStream = Files.newOutputStream(tempCsar, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
        try {
            exporter.writeCsar(id, outputStream, exportConfiguration);
        } catch (IOException e) {
            LOGGER.debug("Error during checking ZIP", e);
            printAndAddError(id, "I/O error: " + e.getMessage());
            return;
        } catch (RepositoryCorruptException e) {
            LOGGER.debug("Repository is corrupt", e);
            printAndAddError(id, "Corrupt: " + e.getMessage());
            return;
        } catch (Exception e) {
            LOGGER.debug("Inner error at writing to temporary CSAR file", e);
            printAndAddError(id, e.toString());
            return;
        }
    } catch (Exception e) {
        final String error = "Could not write to temp CSAR file";
        LOGGER.debug(error, e);
        printAndAddError(id, error);
        return;
    }
    try (InputStream inputStream = Files.newInputStream(tempCsar);
        ZipInputStream zis = new ZipInputStream(inputStream)) {
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            if (configuration.getVerbosity().contains(ConsistencyCheckerVerbosity.OUTPUT_CURRENT_TOSCA_COMPONENT_ID)) {
                LOGGER.info("Identified item in CSAR: {}", entry.getName());
            }
        }
    } catch (Exception e) {
        final String error = "Could not read from temp CSAR file";
        LOGGER.debug(error, e);
        printAndAddError(id, error);
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) HashMap(java.util.HashMap) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) CsarExporter(org.eclipse.winery.repository.export.CsarExporter) OutputStream(java.io.OutputStream) ZipEntry(java.util.zip.ZipEntry) RepositoryCorruptException(org.eclipse.winery.repository.exceptions.RepositoryCorruptException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) RepositoryCorruptException(org.eclipse.winery.repository.exceptions.RepositoryCorruptException) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException)

Example 3 with CsarExporter

use of org.eclipse.winery.repository.export.CsarExporter in project winery by eclipse.

the class RestUtils method getCsarOfSelectedResource.

/**
 * @param options the set of options that are applicable for exporting a csar
 */
public static Response getCsarOfSelectedResource(final AbstractComponentInstanceResource resource, CsarExportOptions options) {
    long start = System.currentTimeMillis();
    final CsarExporter exporter = new CsarExporter(RepositoryFactory.getRepository());
    Map<String, Object> exportConfiguration = new HashMap<>();
    StreamingOutput so = output -> {
        try {
            // check which options are chosen
            if (options.isAddToProvenance()) {
                // We wait for the accountability layer to confirm the transaction
                String result = exporter.writeCsarAndSaveManifestInProvenanceLayer(resource.getId(), output).get();
                LOGGER.debug("Stored state in accountability layer in transaction " + result);
            } else if (options.isIncludeDependencies() && resource.getId() instanceof ServiceTemplateId) {
                SelfContainmentPackager packager = new SelfContainmentPackager(RepositoryFactory.getRepository());
                DefinitionsChildId selfContainedVersion = packager.createSelfContainedVersion(resource.getId());
                exporter.writeSelfContainedCsar(RepositoryFactory.getRepository(), selfContainedVersion, output, exportConfiguration);
            } else {
                exporter.writeCsar(resource.getId(), output, exportConfiguration);
            }
            long duration = (System.currentTimeMillis() - start) / 1000;
            LOGGER.debug("CSAR export lasted {} min {} s", (int) duration / 60, duration % 60);
        } catch (Exception e) {
            LOGGER.error("Error while exporting CSAR", e);
            throw new WebApplicationException(e);
        }
    };
    String contentDisposition = String.format("attachment;filename=\"%s%s\"", resource.getXmlId().getEncoded(), Constants.SUFFIX_CSAR);
    return Response.ok().header("Content-Disposition", contentDisposition).type(MimeTypes.MIMETYPE_ZIP).entity(so).build();
}
Also used : MimeTypes(org.eclipse.winery.common.constants.MimeTypes) FormDataContentDisposition(org.glassfish.jersey.media.multipart.FormDataContentDisposition) FileTime(java.nio.file.attribute.FileTime) HasType(org.eclipse.winery.model.tosca.HasType) CsarExportConfiguration(org.eclipse.winery.repository.export.CsarExportConfiguration) TRelationshipTypeImplementation(org.eclipse.winery.model.tosca.TRelationshipTypeImplementation) WineryVersion(org.eclipse.winery.common.version.WineryVersion) MediaType(javax.ws.rs.core.MediaType) IPersistable(org.eclipse.winery.repository.rest.resources._support.IPersistable) Duration(java.time.Duration) Map(java.util.Map) GenericId(org.eclipse.winery.model.ids.GenericId) QNameApiData(org.eclipse.winery.repository.rest.resources.apiData.QNameApiData) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) WineryVersionUtils(org.eclipse.winery.repository.backend.WineryVersionUtils) EdmmManager(org.eclipse.winery.edmm.EdmmManager) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) QNameWithTypeApiData(org.eclipse.winery.repository.rest.resources.apiData.QNameWithTypeApiData) Constants(org.eclipse.winery.common.Constants) Set(java.util.Set) RelationshipTypeId(org.eclipse.winery.model.ids.definitions.RelationshipTypeId) CsarExportOptions(org.eclipse.winery.repository.export.CsarExportOptions) VersionUtils(org.eclipse.winery.common.version.VersionUtils) WebApplicationException(javax.ws.rs.WebApplicationException) LocalNameForAngular(org.eclipse.winery.repository.rest.datatypes.LocalNameForAngular) UriInfo(javax.ws.rs.core.UriInfo) QName(javax.xml.namespace.QName) LocalDateTime(java.time.LocalDateTime) ArrayList(java.util.ArrayList) RelationshipTypeImplementationId(org.eclipse.winery.model.ids.definitions.RelationshipTypeImplementationId) EdmmType(org.eclipse.winery.edmm.model.EdmmType) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) XmlId(org.eclipse.winery.model.ids.XmlId) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) TNodeTemplate(org.eclipse.winery.model.tosca.TNodeTemplate) ResourceResult(org.eclipse.winery.repository.rest.resources._support.ResourceResult) StringWriter(java.io.StringWriter) IOException(java.io.IOException) Family(javax.ws.rs.core.Response.Status.Family) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) UiConfigurationObject(org.eclipse.winery.common.configuration.UiConfigurationObject) ModelUtilities(org.eclipse.winery.model.tosca.utils.ModelUtilities) TNodeTypeImplementation(org.eclipse.winery.model.tosca.TNodeTypeImplementation) SortedSet(java.util.SortedSet) Date(java.util.Date) EdmmConverter(org.eclipse.winery.edmm.model.EdmmConverter) LoggerFactory(org.slf4j.LoggerFactory) Namespace(org.eclipse.winery.model.ids.Namespace) ArtifactTemplatesResource(org.eclipse.winery.repository.rest.resources.entitytemplates.artifacttemplates.ArtifactTemplatesResource) ContentDisposition(org.glassfish.jersey.media.multipart.ContentDisposition) NamespaceAndDefinedLocalNamesForAngular(org.eclipse.winery.repository.rest.datatypes.NamespaceAndDefinedLocalNamesForAngular) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) SelfContainmentPackager(org.eclipse.winery.repository.backend.selfcontainmentpackager.SelfContainmentPackager) ServiceTemplateResource(org.eclipse.winery.repository.rest.resources.servicetemplates.ServiceTemplateResource) Locale(java.util.Locale) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) Util(org.eclipse.winery.repository.common.Util) URI(java.net.URI) QNameConverter(org.eclipse.winery.repository.rest.resources.apiData.converter.QNameConverter) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) Collection(java.util.Collection) StreamingOutput(javax.ws.rs.core.StreamingOutput) TNodeType(org.eclipse.winery.model.tosca.TNodeType) Collectors(java.util.stream.Collectors) JAXBException(javax.xml.bind.JAXBException) RepositoryFactory(org.eclipse.winery.repository.backend.RepositoryFactory) Objects(java.util.Objects) ArtifactTemplateResource(org.eclipse.winery.repository.rest.resources.entitytemplates.artifacttemplates.ArtifactTemplateResource) List(java.util.List) NamespaceAndDefinedLocalNames(org.eclipse.winery.repository.backend.xsd.NamespaceAndDefinedLocalNames) HttpHeaders(javax.ws.rs.core.HttpHeaders) TExtensibleElements(org.eclipse.winery.model.tosca.TExtensibleElements) YamlExporter(org.eclipse.winery.repository.yaml.export.YamlExporter) Response(javax.ws.rs.core.Response) IRepository(org.eclipse.winery.repository.backend.IRepository) AccessControlException(java.security.AccessControlException) MediaTypes(org.eclipse.winery.repository.backend.constants.MediaTypes) Marshaller(javax.xml.bind.Marshaller) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) TEntityTemplate(org.eclipse.winery.model.tosca.TEntityTemplate) HashMap(java.util.HashMap) HashSet(java.util.HashSet) ClientBuilder(javax.ws.rs.client.ClientBuilder) TConstraint(org.eclipse.winery.model.tosca.TConstraint) BackendUtils(org.eclipse.winery.repository.backend.BackendUtils) Application(org.eclipse.winery.model.selfservice.Application) NodeTypeImplementationId(org.eclipse.winery.model.ids.definitions.NodeTypeImplementationId) Status(javax.ws.rs.core.Response.Status) JAXBContext(javax.xml.bind.JAXBContext) CsarExporter(org.eclipse.winery.repository.export.CsarExporter) ToscaExportUtil(org.eclipse.winery.repository.export.ToscaExportUtil) Logger(org.slf4j.Logger) Environments(org.eclipse.winery.common.configuration.Environments) AbstractComponentInstanceResource(org.eclipse.winery.repository.rest.resources._support.AbstractComponentInstanceResource) TTag(org.eclipse.winery.model.tosca.TTag) ToscaElementId(org.eclipse.winery.model.ids.elements.ToscaElementId) GitBasedRepository(org.eclipse.winery.repository.backend.filebased.GitBasedRepository) EntityGraph(io.github.edmm.core.parser.EntityGraph) NamespaceManager(org.eclipse.winery.repository.backend.NamespaceManager) TRelationshipType(org.eclipse.winery.model.tosca.TRelationshipType) ComponentId(org.eclipse.winery.repository.rest.datatypes.ComponentId) PolicyTemplateId(org.eclipse.winery.model.ids.definitions.PolicyTemplateId) AbstractComponentsResource(org.eclipse.winery.repository.rest.resources._support.AbstractComponentsResource) InputStream(java.io.InputStream) NodeTypeId(org.eclipse.winery.model.ids.definitions.NodeTypeId) IHasName(org.eclipse.winery.repository.rest.resources._support.IHasName) WebApplicationException(javax.ws.rs.WebApplicationException) HashMap(java.util.HashMap) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) SelfContainmentPackager(org.eclipse.winery.repository.backend.selfcontainmentpackager.SelfContainmentPackager) StreamingOutput(javax.ws.rs.core.StreamingOutput) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) WebApplicationException(javax.ws.rs.WebApplicationException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) AccessControlException(java.security.AccessControlException) CsarExporter(org.eclipse.winery.repository.export.CsarExporter) UiConfigurationObject(org.eclipse.winery.common.configuration.UiConfigurationObject)

Aggregations

IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 CsarExporter (org.eclipse.winery.repository.export.CsarExporter)3 StringWriter (java.io.StringWriter)2 URI (java.net.URI)2 FileTime (java.nio.file.attribute.FileTime)2 AccessControlException (java.security.AccessControlException)2 HashMap (java.util.HashMap)2 Collectors (java.util.stream.Collectors)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 Status (javax.ws.rs.core.Response.Status)2 Family (javax.ws.rs.core.Response.Status.Family)2 JAXBContext (javax.xml.bind.JAXBContext)2 JAXBException (javax.xml.bind.JAXBException)2 Marshaller (javax.xml.bind.Marshaller)2 QName (javax.xml.namespace.QName)2 MimeTypes (org.eclipse.winery.common.constants.MimeTypes)2 Application (org.eclipse.winery.model.selfservice.Application)2 ModelUtilities (org.eclipse.winery.model.tosca.utils.ModelUtilities)2 BackendUtils (org.eclipse.winery.repository.backend.BackendUtils)2