Search in sources :

Example 1 with ToscaElementId

use of org.eclipse.winery.common.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 ResourceCreationResult create(GenericId id) {
    ResourceCreationResult res = new ResourceCreationResult();
    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() + "/";
            } 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 = RestUtils.createURI(path);
            res.setUri(uri);
            res.setId(id);
        } else {
            res.setStatus(Status.INTERNAL_SERVER_ERROR);
        }
    }
    return res;
}
Also used : ToscaElementId(org.eclipse.winery.common.ids.elements.ToscaElementId) ResourceCreationResult(org.eclipse.winery.repository.rest.resources._support.ResourceCreationResult) DefinitionsChildId(org.eclipse.winery.common.ids.definitions.DefinitionsChildId) URI(java.net.URI)

Example 2 with ToscaElementId

use of org.eclipse.winery.common.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 = Util.getRootPathFragment(tId.getClass());
        toInsert = tId.getNamespace().getEncoded();
        if (doubleEncode) {
            toInsert = Util.URLencode(toInsert);
        }
        res = res + toInsert + "/";
        toInsert = tId.getXmlId().getEncoded();
        if (doubleEncode) {
            toInsert = Util.URLencode(toInsert);
        }
        res = res + toInsert + "/";
        return res;
    } else if (id instanceof ToscaElementId) {
        toInsert = id.getXmlId().getEncoded();
        if (doubleEncode) {
            toInsert = Util.URLencode(toInsert);
        }
        return IdUtil.getPathFragment(id.getParent()) + toInsert + "/";
    } else {
        throw new IllegalStateException("Unknown subclass of GenericId " + id.getClass());
    }
}
Also used : ToscaElementId(org.eclipse.winery.common.ids.elements.ToscaElementId) DefinitionsChildId(org.eclipse.winery.common.ids.definitions.DefinitionsChildId)

Aggregations

DefinitionsChildId (org.eclipse.winery.common.ids.definitions.DefinitionsChildId)2 ToscaElementId (org.eclipse.winery.common.ids.elements.ToscaElementId)2 URI (java.net.URI)1 ResourceCreationResult (org.eclipse.winery.repository.rest.resources._support.ResourceCreationResult)1