Search in sources :

Example 6 with XmlId

use of org.eclipse.winery.model.ids.XmlId in project winery by eclipse.

the class PlansResource method saveFile.

private Response saveFile(TPlan tPlan, InputStream uploadedInputStream, FormDataContentDisposition fileDetail, FormDataBodyPart body) {
    boolean bpmn4toscaMode = Namespaces.URI_BPMN4TOSCA_20.equals(tPlan.getPlanLanguage());
    if (uploadedInputStream != null || bpmn4toscaMode) {
        // Determine Id
        PlansId plansId = new PlansId((ServiceTemplateId) ((ServiceTemplateResource) this.res).getId());
        PlanId planId = new PlanId(plansId, new XmlId(tPlan.getId(), false));
        // Ensure overwriting
        if (RepositoryFactory.getRepository().exists(planId)) {
            try {
                RepositoryFactory.getRepository().forceDelete(planId);
                // Quick hack to remove the deleted plan from the plans element
                ((ServiceTemplateResource) this.res).synchronizeReferences();
            } catch (IOException e) {
                return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
            }
        }
        String fileName;
        if (bpmn4toscaMode) {
            fileName = tPlan.getId() + Constants.SUFFIX_BPMN4TOSCA;
            RepositoryFileReference ref = new RepositoryFileReference(planId, fileName);
            // Errors are ignored in the following call
            RestUtils.putContentToFile(ref, "{}", MediaType.APPLICATION_JSON_TYPE);
        } else {
            // We use the filename also as local file name. Alternatively, we could use the xml id
            // With URL encoding, this should not be an issue
            fileName = EncodingUtil.URLencode(fileDetail.getFileName());
            // Really store it
            RepositoryFileReference ref = new RepositoryFileReference(planId, fileName);
            // Errors are ignored in the following call
            RestUtils.putContentToFile(ref, uploadedInputStream, body.getMediaType());
        }
        PlansResource.setPlanModelReference(tPlan, planId, fileName);
    }
    return RestUtils.persist(this.res);
}
Also used : RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) PlanId(org.eclipse.winery.model.ids.elements.PlanId) ServiceTemplateResource(org.eclipse.winery.repository.rest.resources.servicetemplates.ServiceTemplateResource) XmlId(org.eclipse.winery.model.ids.XmlId) IOException(java.io.IOException) PlansId(org.eclipse.winery.model.ids.elements.PlansId)

Example 7 with XmlId

use of org.eclipse.winery.model.ids.XmlId in project winery by eclipse.

the class PlanResource method getId.

/**
 * Determines the id of the current resource
 */
private PlanId getId() {
    ServiceTemplateId sId = (ServiceTemplateId) this.getServiceTemplateResource().getId();
    PlansId psId = new PlansId(sId);
    return new PlanId(psId, new XmlId(this.o.getId(), false));
}
Also used : PlanId(org.eclipse.winery.model.ids.elements.PlanId) XmlId(org.eclipse.winery.model.ids.XmlId) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) PlansId(org.eclipse.winery.model.ids.elements.PlansId)

Example 8 with XmlId

use of org.eclipse.winery.model.ids.XmlId in project winery by eclipse.

the class XmlRepository method getDefinitionsChildIds.

public <T extends DefinitionsChildId> SortedSet<T> getDefinitionsChildIds(Class<T> idClass, boolean omitDevelopmentVersions) {
    SortedSet<T> res = new TreeSet<>();
    String rootPathFragment = IdUtil.getRootPathFragment(idClass);
    Path dir = makeAbsolute(Paths.get(rootPathFragment));
    if (!Files.exists(dir)) {
        // return empty list if no ids are available
        return res;
    }
    assert (Files.isDirectory(dir));
    final OnlyNonHiddenDirectories hiddenDirectories = new OnlyNonHiddenDirectories();
    // list all directories contained in this directory
    try (DirectoryStream<Path> ds = Files.newDirectoryStream(dir, hiddenDirectories)) {
        for (Path nsP : ds) {
            // the current path is the namespace
            Namespace ns = new Namespace(nsP.getFileName().toString(), true);
            try (DirectoryStream<Path> idDS = Files.newDirectoryStream(nsP, hiddenDirectories)) {
                for (Path idP : idDS) {
                    XmlId xmlId = new XmlId(idP.getFileName().toString(), true);
                    if (omitDevelopmentVersions) {
                        WineryVersion version = VersionUtils.getVersion(xmlId.getDecoded());
                        if (version.toString().length() > 0 && version.getWorkInProgressVersion() > 0) {
                            continue;
                        }
                    }
                    Constructor<T> constructor;
                    try {
                        constructor = idClass.getConstructor(Namespace.class, XmlId.class);
                    } catch (Exception e) {
                        LOGGER.debug("Internal error at determining id constructor", e);
                        // abort everything, return invalid result
                        return res;
                    }
                    T id;
                    try {
                        id = constructor.newInstance(ns, xmlId);
                    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                        LOGGER.debug("Internal error at invocation of id constructor", e);
                        // abort everything, return invalid result
                        return res;
                    }
                    res.add(id);
                }
            }
        }
    } catch (IOException e) {
        LOGGER.debug("Cannot close ds", e);
    }
    return res;
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) Namespace(org.eclipse.winery.model.ids.Namespace) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) OnlyNonHiddenDirectories(org.eclipse.winery.repository.backend.filebased.OnlyNonHiddenDirectories) TreeSet(java.util.TreeSet) XmlId(org.eclipse.winery.model.ids.XmlId) WineryVersion(org.eclipse.winery.common.version.WineryVersion)

Example 9 with XmlId

use of org.eclipse.winery.model.ids.XmlId in project winery by eclipse.

the class CsarImporter method adjustServiceTemplate.

/**
 * In case plans are provided, the plans are imported into Winery's storage
 *
 * @param rootPath the root path of the extracted csar
 * @param tmf      the TOSCAMetaFile object used to determine the mime type of the plan
 * @param wid      Winery's internal id of the service template
 * @param st       the the service template to be imported {@inheritDoc}
 */
private void adjustServiceTemplate(Path rootPath, TOSCAMetaFile tmf, ServiceTemplateId wid, TServiceTemplate st, final List<String> errors) {
    List<TPlan> plans = st.getPlans();
    if (plans != null) {
        for (TPlan plan : plans) {
            PlanModelReference refContainer = plan.getPlanModelReference();
            if (refContainer != null) {
                String ref = refContainer.getReference();
                if (ref != null) {
                    // URLs are stored encoded -> undo the encoding
                    ref = EncodingUtil.URLdecode(ref);
                    URI refURI;
                    try {
                        refURI = new URI(ref);
                    } catch (URISyntaxException e) {
                        errors.add(String.format("Invalid URI %1$s", ref));
                        continue;
                    }
                    if (refURI.isAbsolute()) {
                        // We have to do nothing
                        continue;
                    }
                    Path path = rootPath.resolve(ref);
                    if (!Files.exists(path)) {
                        // possibly, the reference is relative to the Definitions subfolder
                        // COS01 does not make any explicit statement how to resolve the reference here
                        path = rootPath.resolve("Definitions").resolve(ref);
                        if (!Files.exists(path)) {
                            errors.add(String.format("Plan reference %1$s not found", ref));
                            // we quickly remove the reference to reflect the not-found in the data
                            refContainer.setReference(null);
                            continue;
                        }
                    }
                    PlansId plansId = new PlansId(wid);
                    PlanId pid = new PlanId(plansId, new XmlId(plan.getId(), false));
                    if (Files.isDirectory(path)) {
                        errors.add(String.format("Reference %1$s is a directory and Winery currently does not support importing directories", ref));
                        continue;
                    }
                    RepositoryFileReference fref = new RepositoryFileReference(pid, path.getFileName().toString());
                    importFile(path, fref, tmf, rootPath, errors);
                    // file is imported
                    // Adjust the reference
                    refContainer.setReference("../" + Util.getUrlPath(fref));
                }
            }
        }
    }
}
Also used : Path(java.nio.file.Path) PlanModelReference(org.eclipse.winery.model.tosca.TPlan.PlanModelReference) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) PlanId(org.eclipse.winery.model.ids.elements.PlanId) TPlan(org.eclipse.winery.model.tosca.TPlan) XmlId(org.eclipse.winery.model.ids.XmlId) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) PlansId(org.eclipse.winery.model.ids.elements.PlansId)

Aggregations

XmlId (org.eclipse.winery.model.ids.XmlId)9 IOException (java.io.IOException)5 Path (java.nio.file.Path)4 Namespace (org.eclipse.winery.model.ids.Namespace)4 PlanId (org.eclipse.winery.model.ids.elements.PlanId)4 PlansId (org.eclipse.winery.model.ids.elements.PlansId)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 TreeSet (java.util.TreeSet)3 RepositoryFileReference (org.eclipse.winery.repository.common.RepositoryFileReference)3 JAXBException (javax.xml.bind.JAXBException)2 WineryVersion (org.eclipse.winery.common.version.WineryVersion)2 TPlan (org.eclipse.winery.model.tosca.TPlan)2 OnlyNonHiddenDirectories (org.eclipse.winery.repository.backend.filebased.OnlyNonHiddenDirectories)2 ApiOperation (io.swagger.annotations.ApiOperation)1 StringReader (java.io.StringReader)1 Constructor (java.lang.reflect.Constructor)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1