Search in sources :

Example 1 with Converter

use of org.eclipse.winery.yaml.converter.Converter in project winery by eclipse.

the class YAMLParserResource method importYAML.

@POST
@ApiOperation(value = "Imports the given zipped YAML files")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public Response importYAML(@FormDataParam("file") InputStream uploadInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail, @FormDataParam("overwrite") @ApiParam(value = "true/false both not used") Boolean overwrite, @Context UriInfo uriInfo) {
    LOGGER.debug("File {}", fileDetail);
    Converter converter = new Converter();
    try {
        converter.convertY2X(uploadInputStream);
    } catch (Exception e) {
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(StringEscapeUtils.escapeHtml(e.getMessage().trim())).type("text/plain").build();
    }
    return Response.noContent().build();
}
Also used : Converter(org.eclipse.winery.yaml.converter.Converter) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation)

Example 2 with Converter

use of org.eclipse.winery.yaml.converter.Converter in project winery by eclipse.

the class Showcase method zipTypeTest.

@Test
public void zipTypeTest() throws Exception {
    String name = "Showcase";
    String path = this.path + File.separator + name + ".csar";
    // Read DriverInjectionTest and import the csar into the repository
    InputStream fis = new FileInputStream(path);
    IRepository repository = RepositoryFactory.getRepository(Utils.getTmpDir(Paths.get("repository")));
    CsarImporter csarImporter = new CsarImporter();
    csarImporter.readCSAR(fis, true, true);
    // Read the csar again and convert it to yaml the resulting yaml service templates
    // are written to a temporary dir and converted to a input stream of a zip file
    Converter converter = new Converter(repository);
    fis = new FileInputStream(path);
    InputStream zip = converter.convertX2Y(fis);
    // Write the zip file to the output path and rename it
    File zipFile = new File(outPath + File.separator + name + ".csar");
    if (!zipFile.getParentFile().exists()) {
        zipFile.getParentFile().mkdirs();
    }
    Files.copy(zip, zipFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
    // clean temporary repository
    Utils.deleteTmpDir(Paths.get("repository"));
    Assert.assertTrue(zipFile.exists());
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CsarImporter(org.eclipse.winery.repository.importing.CsarImporter) Converter(org.eclipse.winery.yaml.converter.Converter) IRepository(org.eclipse.winery.repository.backend.IRepository) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 3 with Converter

use of org.eclipse.winery.yaml.converter.Converter in project winery by eclipse.

the class Showcases method zipTypeTest.

@Test
public void zipTypeTest() throws Exception {
    String name = "Showcase.csar";
    InputStream inputStream = new FileInputStream(path + File.separator + name);
    Converter converter = new Converter();
    converter.convertY2X(inputStream);
    TServiceTemplate serviceTemplate = RepositoryFactory.getRepository().getElement(new ServiceTemplateId(new Namespace(Namespaces.DEFAULT_NS, false), new XmlId("Showcase", false)));
    Assert.assertNotNull(serviceTemplate);
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Converter(org.eclipse.winery.yaml.converter.Converter) XmlId(org.eclipse.winery.common.ids.XmlId) ServiceTemplateId(org.eclipse.winery.common.ids.definitions.ServiceTemplateId) FileInputStream(java.io.FileInputStream) Namespace(org.eclipse.winery.common.ids.Namespace) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) Test(org.junit.Test)

Example 4 with Converter

use of org.eclipse.winery.yaml.converter.Converter in project winery by eclipse.

the class Y2XConverter method convert.

/**
 * Converts TOSCA YAML TImportDefinitions and returns list of TOSCA XML TImports
 */
private TImport convert(TImportDefinition node, String name) {
    Reader reader = Reader.getReader();
    String namespace = node.getNamespaceUri() == null ? this.namespace : node.getNamespaceUri();
    try {
        org.eclipse.winery.model.tosca.yaml.TServiceTemplate serviceTemplate = reader.readImportDefinition(node, path, namespace);
        Converter converter = new Converter(this.repository);
        Definitions definitions = converter.convertY2X(serviceTemplate, getFileNameFromFile(node.getFile()), namespace, path, outPath);
        WriterUtils.saveDefinitions(definitions, outPath, namespace, name);
        TImport.Builder builder = new TImport.Builder(Namespaces.XML_NS);
        builder.setLocation(WriterUtils.getDefinitionsLocation(namespace, name));
        builder.setNamespace(namespace);
        return builder.build();
    } catch (MultiException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : AssignmentBuilder(org.eclipse.winery.yaml.converter.yaml.support.AssignmentBuilder) Reader(org.eclipse.winery.yaml.common.reader.yaml.Reader) TypeConverter(org.eclipse.winery.yaml.converter.yaml.support.TypeConverter) Converter(org.eclipse.winery.yaml.converter.Converter) MultiException(org.eclipse.winery.yaml.common.exception.MultiException) org.eclipse.winery.model.tosca.yaml(org.eclipse.winery.model.tosca.yaml)

Example 5 with Converter

use of org.eclipse.winery.yaml.converter.Converter in project winery by eclipse.

the class RestUtils method getYamlCSARofSelectedResource.

public static Response getYamlCSARofSelectedResource(final AbstractComponentInstanceResource resource) {
    final Converter converter = new Converter();
    StreamingOutput so = output -> {
        try {
            InputStream is = converter.convertX2Y(resource.getId());
            byte[] buffer = new byte[1024];
            int bytesRead;
            // read from is to buffer
            while ((bytesRead = is.read(buffer)) != -1) {
                output.write(buffer, 0, bytesRead);
            }
            is.close();
            // flush OutputStream to write any buffered data to file
            output.flush();
            output.close();
        } 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) InputStream(java.io.InputStream) Converter(org.eclipse.winery.yaml.converter.Converter) 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)

Aggregations

Converter (org.eclipse.winery.yaml.converter.Converter)5 InputStream (java.io.InputStream)3 FileInputStream (java.io.FileInputStream)2 Namespace (org.eclipse.winery.common.ids.Namespace)2 XmlId (org.eclipse.winery.common.ids.XmlId)2 ServiceTemplateId (org.eclipse.winery.common.ids.definitions.ServiceTemplateId)2 MultiException (org.eclipse.winery.yaml.common.exception.MultiException)2 Test (org.junit.Test)2 Client (com.sun.jersey.api.client.Client)1 ClientResponse (com.sun.jersey.api.client.ClientResponse)1 WebResource (com.sun.jersey.api.client.WebResource)1 ContentDisposition (com.sun.jersey.core.header.ContentDisposition)1 FormDataContentDisposition (com.sun.jersey.core.header.FormDataContentDisposition)1 FormDataBodyPart (com.sun.jersey.multipart.FormDataBodyPart)1 ApiOperation (io.swagger.annotations.ApiOperation)1 File (java.io.File)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 URI (java.net.URI)1 FileTime (java.nio.file.attribute.FileTime)1