Search in sources :

Example 1 with IHasName

use of org.eclipse.winery.repository.rest.resources._support.IHasName in project winery by eclipse.

the class RestUtils method create.

/**
 * Generates given TOSCA element and returns appropriate response code <br  />
 * <p>
 * In the case of an existing resource, the other possible return code is 302. This code has no Status constant,
 * therefore we use Status.CONFLICT, which is also possible.
 *
 * @return <ul> <li> <ul> <li>Status.CREATED (201) if the resource has been created,</li> <li>Status.CONFLICT if the
 * resource already exists,</li> <li>Status.INTERNAL_SERVER_ERROR (500) if something went wrong</li> </ul> </li>
 * <li>URI: the absolute URI of the newly created resource</li> </ul>
 */
public static ResourceResult create(GenericId id, String name) {
    ResourceResult res = new ResourceResult();
    if (RepositoryFactory.getRepository().exists(id)) {
        // res.setStatus(302);
        res.setStatus(Status.CONFLICT);
    } else {
        if (RepositoryFactory.getRepository().flagAsExisting(id)) {
            res.setStatus(Status.CREATED);
            // @formatter:off
            // This method is a generic method
            // We cannot return an "absolute" URL as the URL is always
            // relative to the caller
            // Does not work: String path = Environment.getUrlConfiguration().getRepositoryApiUrl()
            // + "/" +
            // Utils.getUrlPathForPathInsideRepo(id.getPathInsideRepo());
            // We distinguish between two cases: DefinitionsChildId and
            // TOSCAelementId
            // @formatter:on
            String path;
            if (id instanceof DefinitionsChildId) {
                // here, we return namespace + id, as it is only possible to
                // post on the definition child*s* resource to create an
                // instance of a definition child
                DefinitionsChildId tcId = (DefinitionsChildId) id;
                path = tcId.getNamespace().getEncoded() + "/" + tcId.getXmlId().getEncoded() + "/";
                // in case the resource additionally supports a name attribute, we set the original name
                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.getComponentInstanceResource(tcId);
                    resource.setName(name);
                }
            } else {
                assert (id instanceof ToscaElementId);
                // We just return the id as we assume that only the parent
                // of this id may create sub elements
                path = id.getXmlId().getEncoded() + "/";
            }
            // we have to encode it twice to get correct URIs
            path = Util.getUrlPath(path);
            URI uri = URI.create(path);
            res.setUri(uri);
            res.setId(id);
        } else {
            res.setStatus(Status.INTERNAL_SERVER_ERROR);
        }
    }
    return res;
}
Also used : ToscaElementId(org.eclipse.winery.model.ids.elements.ToscaElementId) ResourceResult(org.eclipse.winery.repository.rest.resources._support.ResourceResult) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) PolicyTemplateId(org.eclipse.winery.model.ids.definitions.PolicyTemplateId) IHasName(org.eclipse.winery.repository.rest.resources._support.IHasName) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) URI(java.net.URI) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId)

Aggregations

URI (java.net.URI)1 ArtifactTemplateId (org.eclipse.winery.model.ids.definitions.ArtifactTemplateId)1 DefinitionsChildId (org.eclipse.winery.model.ids.definitions.DefinitionsChildId)1 PolicyTemplateId (org.eclipse.winery.model.ids.definitions.PolicyTemplateId)1 ServiceTemplateId (org.eclipse.winery.model.ids.definitions.ServiceTemplateId)1 ToscaElementId (org.eclipse.winery.model.ids.elements.ToscaElementId)1 IHasName (org.eclipse.winery.repository.rest.resources._support.IHasName)1 ResourceResult (org.eclipse.winery.repository.rest.resources._support.ResourceResult)1