Search in sources :

Example 21 with ArtifactTemplateId

use of org.eclipse.winery.common.ids.definitions.ArtifactTemplateId in project winery by eclipse.

the class AbstractComponentsResource method onPost.

/**
 * Creates a new component instance in the given namespace
 *
 * @param namespace plain namespace
 * @param name      the name; used as id
 */
protected ResourceCreationResult onPost(String namespace, String name) {
    ResourceCreationResult res;
    if (StringUtils.isEmpty(namespace) || StringUtils.isEmpty(name)) {
        res = new ResourceCreationResult(Status.BAD_REQUEST);
    } else {
        String id = RestUtils.createXMLidAsString(name);
        DefinitionsChildId tcId;
        try {
            tcId = this.getDefinitionsChildId(namespace, id, false);
            res = this.createComponentInstance(tcId);
            // in case the resource additionally supports a name attribute, we set the original name
            if (res.getStatus().equals(Status.CREATED)) {
                if ((tcId instanceof ServiceTemplateId) || (tcId instanceof ArtifactTemplateId) || (tcId instanceof PolicyTemplateId)) {
                    // these three types have an additional name (instead of a pure id)
                    // we store the name
                    IHasName resource = (IHasName) AbstractComponentsResource.getComponentInstaceResource(tcId);
                    resource.setName(name);
                }
            }
        } catch (Exception e) {
            AbstractComponentsResource.LOGGER.debug("Could not create id instance", e);
            res = new ResourceCreationResult(Status.INTERNAL_SERVER_ERROR);
        }
    }
    return res;
}
Also used : DefinitionsChildId(org.eclipse.winery.common.ids.definitions.DefinitionsChildId) PolicyTemplateId(org.eclipse.winery.common.ids.definitions.PolicyTemplateId) ServiceTemplateId(org.eclipse.winery.common.ids.definitions.ServiceTemplateId) ArtifactTemplateId(org.eclipse.winery.common.ids.definitions.ArtifactTemplateId) NotFoundException(com.sun.jersey.api.NotFoundException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 22 with ArtifactTemplateId

use of org.eclipse.winery.common.ids.definitions.ArtifactTemplateId in project winery by eclipse.

the class GenericArtifactsResource method generateArtifact.

// @formatter:off
/**
 * @return TImplementationArtifact | TDeploymentArtifact (XML) | URL of generated IA zip (in case of autoGenerateIA)
 */
@POST
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Creates a new implementation/deployment artifact. " + "If an implementation artifact with the same name already exists, it is <em>overridden</em>.")
@SuppressWarnings("unchecked")
public Response generateArtifact(GenerateArtifactApiData apiData, @Context UriInfo uriInfo) {
    // we assume that the parent ComponentInstance container exists
    final IRepository repository = RepositoryFactory.getRepository();
    if (StringUtils.isEmpty(apiData.artifactName)) {
        return Response.status(Status.BAD_REQUEST).entity("Empty artifactName").build();
    }
    if (StringUtils.isEmpty(apiData.artifactType)) {
        if (StringUtils.isEmpty(apiData.artifactTemplateName) || StringUtils.isEmpty(apiData.artifactTemplateNamespace)) {
            if (StringUtils.isEmpty(apiData.artifactTemplate)) {
                return Response.status(Status.BAD_REQUEST).entity("No artifact type given and no template given. Cannot guess artifact type").build();
            }
        }
    }
    if (!StringUtils.isEmpty(apiData.autoGenerateIA)) {
        if (StringUtils.isEmpty(apiData.javaPackage)) {
            return Response.status(Status.BAD_REQUEST).entity("no java package name supplied for IA auto generation.").build();
        }
        if (StringUtils.isEmpty(apiData.interfaceName)) {
            return Response.status(Status.BAD_REQUEST).entity("no interface name supplied for IA auto generation.").build();
        }
    }
    // convert second calling form to first calling form
    if (!StringUtils.isEmpty(apiData.artifactTemplate)) {
        QName qname = QName.valueOf(apiData.artifactTemplate);
        apiData.artifactTemplateName = qname.getLocalPart();
        apiData.artifactTemplateNamespace = qname.getNamespaceURI();
    }
    Document doc = null;
    // if invalid, abort and do not create anything
    if (!StringUtils.isEmpty(apiData.artifactSpecificContent)) {
        try {
            DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            InputSource is = new InputSource();
            StringReader sr = new StringReader(apiData.artifactSpecificContent);
            is.setCharacterStream(sr);
            doc = db.parse(is);
        } catch (Exception e) {
            // FIXME: currently we allow a single element only. However, the content should be internally wrapped by an (arbitrary) XML element as the content will be nested in the artifact element, too
            GenericArtifactsResource.LOGGER.debug("Invalid content", e);
            return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
        }
    }
    // determine artifactTemplate and artifactType
    ArtifactTypeId artifactTypeId;
    ArtifactTemplateId artifactTemplateId = null;
    ArtifactTemplateResource artifactTemplateResource = null;
    boolean doAutoCreateArtifactTemplate = !(StringUtils.isEmpty(apiData.autoCreateArtifactTemplate) || apiData.autoCreateArtifactTemplate.equalsIgnoreCase("no") || apiData.autoCreateArtifactTemplate.equalsIgnoreCase("false"));
    if (!doAutoCreateArtifactTemplate) {
        // no auto creation
        if (!StringUtils.isEmpty(apiData.artifactTemplateName) && !StringUtils.isEmpty(apiData.artifactTemplateNamespace)) {
            QName artifactTemplateQName = new QName(apiData.artifactTemplateNamespace, apiData.artifactTemplateName);
            artifactTemplateId = BackendUtils.getDefinitionsChildId(ArtifactTemplateId.class, artifactTemplateQName);
        }
        if (StringUtils.isEmpty(apiData.artifactType)) {
            // derive the type from the artifact template
            if (artifactTemplateId == null) {
                return Response.status(Status.NOT_ACCEPTABLE).entity("No artifactTemplate and no artifactType provided. Deriving the artifactType is not possible.").build();
            }
            @NonNull final QName type = repository.getElement(artifactTemplateId).getType();
            artifactTypeId = BackendUtils.getDefinitionsChildId(ArtifactTypeId.class, type);
        } else {
            // artifactTypeStr is directly given, use that
            artifactTypeId = BackendUtils.getDefinitionsChildId(ArtifactTypeId.class, apiData.artifactType);
        }
    } else {
        if (StringUtils.isEmpty(apiData.artifactType)) {
            return Response.status(Status.BAD_REQUEST).entity("Artifact template auto creation requested, but no artifact type supplied.").build();
        }
        artifactTypeId = BackendUtils.getDefinitionsChildId(ArtifactTypeId.class, apiData.artifactType);
        // ensure that given type exists
        if (!repository.exists(artifactTypeId)) {
            LOGGER.debug("Artifact type {} is created", apiData.artifactType);
            final TArtifactType element = repository.getElement(artifactTypeId);
            try {
                repository.setElement(artifactTypeId, element);
            } catch (IOException e) {
                throw new WebApplicationException(e);
            }
        }
        if (StringUtils.isEmpty(apiData.artifactTemplateName) || StringUtils.isEmpty(apiData.artifactTemplateNamespace)) {
            // no explicit name provided
            // we use the artifactNameStr as prefix for the
            // artifact template name
            // we create a new artifact template in the namespace of the parent
            // element
            Namespace namespace = this.resWithNamespace.getNamespace();
            artifactTemplateId = new ArtifactTemplateId(namespace, new XmlId(apiData.artifactName + "artifactTemplate", false));
        } else {
            QName artifactTemplateQName = new QName(apiData.artifactTemplateNamespace, apiData.artifactTemplateName);
            artifactTemplateId = new ArtifactTemplateId(artifactTemplateQName);
        }
        // even if artifactTemplate does not exist, it is loaded
        final TArtifactTemplate artifactTemplate = repository.getElement(artifactTemplateId);
        artifactTemplate.setType(artifactTypeId.getQName());
        try {
            repository.setElement(artifactTemplateId, artifactTemplate);
        } catch (IOException e) {
            throw new WebApplicationException(e);
        }
    }
    // variable artifactTypeId is set
    // variable artifactTemplateId is not null if artifactTemplate has been generated
    // we have to generate the DA/IA resource now
    // Doing it here instead of doing it at the subclasses is dirty on the
    // one hand, but quicker to implement on the other hand
    // Create the artifact itself
    ArtifactT resultingArtifact;
    if (this instanceof ImplementationArtifactsResource) {
        ImplementationArtifact a = new ImplementationArtifact();
        // Winery internal id is the name of the artifact:
        // store the name
        a.setName(apiData.artifactName);
        a.setInterfaceName(apiData.interfaceName);
        a.setOperationName(apiData.operationName);
        assert (artifactTypeId != null);
        a.setArtifactType(artifactTypeId.getQName());
        if (artifactTemplateId != null) {
            a.setArtifactRef(artifactTemplateId.getQName());
        }
        if (doc != null) {
            // the content has been checked for validity at the beginning of the method.
            // If this point in the code is reached, the XML has been parsed into doc
            // just copy over the dom node. Hopefully, that works...
            a.getAny().add(doc.getDocumentElement());
        }
        this.list.add((ArtifactT) a);
        resultingArtifact = (ArtifactT) a;
    } else {
        // for comments see other branch
        TDeploymentArtifact a = new TDeploymentArtifact();
        a.setName(apiData.artifactName);
        assert (artifactTypeId != null);
        a.setArtifactType(artifactTypeId.getQName());
        if (artifactTemplateId != null) {
            a.setArtifactRef(artifactTemplateId.getQName());
        }
        if (doc != null) {
            a.getAny().add(doc.getDocumentElement());
        }
        this.list.add((ArtifactT) a);
        resultingArtifact = (ArtifactT) a;
    }
    Response persistResponse = RestUtils.persist(super.res);
    if (StringUtils.isEmpty(apiData.autoGenerateIA)) {
        return Response.created(RestUtils.createURI(Util.URLencode(apiData.artifactName))).entity(resultingArtifact).build();
    } else {
        // after everything was created, we fire up the artifact generation
        return this.generateImplementationArtifact(apiData.interfaceName, apiData.javaPackage, uriInfo, artifactTemplateId);
    }
}
Also used : InputSource(org.xml.sax.InputSource) WebApplicationException(javax.ws.rs.WebApplicationException) ArtifactTypeId(org.eclipse.winery.common.ids.definitions.ArtifactTypeId) QName(javax.xml.namespace.QName) IOException(java.io.IOException) Document(org.w3c.dom.Document) WebApplicationException(javax.ws.rs.WebApplicationException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ArtifactTemplateId(org.eclipse.winery.common.ids.definitions.ArtifactTemplateId) ArtifactTemplateResource(org.eclipse.winery.repository.rest.resources.entitytemplates.artifacttemplates.ArtifactTemplateResource) Namespace(org.eclipse.winery.common.ids.Namespace) ImplementationArtifact(org.eclipse.winery.model.tosca.TImplementationArtifacts.ImplementationArtifact) Response(javax.ws.rs.core.Response) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NonNull(org.eclipse.jdt.annotation.NonNull) StringReader(java.io.StringReader) XmlId(org.eclipse.winery.common.ids.XmlId) IRepository(org.eclipse.winery.repository.backend.IRepository) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation)

Example 23 with ArtifactTemplateId

use of org.eclipse.winery.common.ids.definitions.ArtifactTemplateId 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

ArtifactTemplateId (org.eclipse.winery.common.ids.definitions.ArtifactTemplateId)23 QName (javax.xml.namespace.QName)10 Test (org.junit.Test)9 RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)8 ArtifactTypeId (org.eclipse.winery.common.ids.definitions.ArtifactTypeId)6 ArtifactTemplateSourceDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateSourceDirectoryId)6 IOException (java.io.IOException)4 Path (java.nio.file.Path)4 DefinitionsChildId (org.eclipse.winery.common.ids.definitions.DefinitionsChildId)4 TArtifactTemplate (org.eclipse.winery.model.tosca.TArtifactTemplate)4 InputStream (java.io.InputStream)3 HashSet (java.util.HashSet)3 PolicyTemplateId (org.eclipse.winery.common.ids.definitions.PolicyTemplateId)3 ServiceTemplateId (org.eclipse.winery.common.ids.definitions.ServiceTemplateId)3 ArtifactTemplateFilesDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Response (javax.ws.rs.core.Response)2 JAXBException (javax.xml.bind.JAXBException)2 CapabilityTypeId (org.eclipse.winery.common.ids.definitions.CapabilityTypeId)2 NodeTypeId (org.eclipse.winery.common.ids.definitions.NodeTypeId)2