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);
}
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));
}
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;
}
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));
}
}
}
}
}
Aggregations