Search in sources :

Example 1 with ToscaElementId

use of org.eclipse.winery.model.ids.elements.ToscaElementId in project winery by eclipse.

the class IdUtil method getPathFragment.

/**
 * Executes the real conversion to a path fragment
 *
 * @param id           the id to transform to a path
 * @param doubleEncode true if each sub fragment should be double encoded,
 *                     false if it should be encoded only once
 */
private static String getPathFragment(final GenericId id, final boolean doubleEncode) {
    String toInsert;
    if (id instanceof DefinitionsChildId) {
        // @return "[ComponentName]s/{namespace}/{id}/"
        DefinitionsChildId tId = (DefinitionsChildId) id;
        String res = IdUtil.getRootPathFragment(tId.getClass());
        toInsert = tId.getNamespace().getEncoded();
        if (doubleEncode) {
            toInsert = EncodingUtil.URLencode(toInsert);
        }
        res = res + toInsert + "/";
        toInsert = tId.getXmlId().getEncoded();
        if (doubleEncode) {
            toInsert = EncodingUtil.URLencode(toInsert);
        }
        res = res + toInsert + "/";
        return res;
    } else if (id instanceof ToscaElementId) {
        toInsert = id.getXmlId().getEncoded();
        if (doubleEncode) {
            toInsert = EncodingUtil.URLencode(toInsert);
        }
        return IdUtil.getPathFragment(id.getParent()) + toInsert + "/";
    } else {
        throw new IllegalStateException("Unknown subclass of GenericId " + id.getClass());
    }
}
Also used : ToscaElementId(org.eclipse.winery.model.ids.elements.ToscaElementId) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId)

Example 2 with ToscaElementId

use of org.eclipse.winery.model.ids.elements.ToscaElementId 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

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