use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.
the class CsarImporter method importAllFiles.
/**
* Imports all given files from the file system to the repository
*
* @param rootPath used to make the path p relative in order to determine the mime type and the
* RepositoryFileReference
* @param files list of all files
* @param directoryId the id of the directory of the artifact template the files to attach to
* @param tmf the TOSCAMetaFile object used to determine the mimetype. Must not be null.
* @param errors list where import errors should be stored to
*/
private void importAllFiles(Path rootPath, Collection<Path> files, DirectoryId directoryId, TOSCAMetaFile tmf, final List<String> errors) {
// remove the filePathInsideRepo to correctly store the files in the files folder inside an artifact template
// otherwise, the files are saved in the sub directory of the artifact template
// this is required, to enable the cycle CSAR export, clean , import CSAR
String pathInsideRepo = Util.getPathInsideRepo(directoryId);
for (Path p : files) {
if (!Files.exists(p)) {
errors.add(String.format("File %1$s does not exist", p.toString()));
return;
}
Path subDirectory = rootPath.relativize(p).getParent();
RepositoryFileReference fref;
// files are stored in "files/" or in "sources/"
if ((subDirectory == null) || (subDirectory.getParent().toString().endsWith(pathInsideRepo))) {
fref = new RepositoryFileReference(directoryId, p.getFileName().toString());
} else {
fref = new RepositoryFileReference(directoryId, subDirectory, p.getFileName().toString());
}
importFile(p, fref, tmf, rootPath, errors);
}
}
use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.
the class CsarImporter method importSelfServiceMetaData.
/**
* Imports a self-service meta data description (if available)
* <p>
* The first service template in the provided entry definitions is taken
*/
private void importSelfServiceMetaData(final TOSCAMetaFile tmf, final Path rootPath, Path entryDefinitions, final List<String> errors) {
final Path selfServiceDir = rootPath.resolve(Constants.DIRNAME_SELF_SERVICE_METADATA);
if (!Files.exists(selfServiceDir)) {
CsarImporter.LOGGER.debug("Self-service Portal directory does not exist in CSAR");
return;
}
if (!Files.exists(entryDefinitions)) {
CsarImporter.LOGGER.debug("Entry definitions does not exist.");
return;
}
Unmarshaller um = JAXBSupport.createUnmarshaller();
TDefinitions defs;
try {
defs = (TDefinitions) um.unmarshal(entryDefinitions.toFile());
} catch (JAXBException e) {
errors.add("Could not unmarshal definitions " + entryDefinitions.getFileName() + " " + e.getMessage());
return;
} catch (ClassCastException e) {
errors.add("Definitions " + entryDefinitions.getFileName() + " is not a TDefinitions " + e.getMessage());
return;
}
final int cutLength = selfServiceDir.toString().length() + 1;
Iterator<TExtensibleElements> iterator = defs.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().iterator();
boolean found = false;
TExtensibleElements next = null;
while (iterator.hasNext() && !found) {
next = iterator.next();
if (next instanceof TServiceTemplate) {
found = true;
}
}
if (found) {
TServiceTemplate serviceTemplate = (TServiceTemplate) next;
String namespace = serviceTemplate.getTargetNamespace();
if (namespace == null) {
namespace = defs.getTargetNamespace();
}
ServiceTemplateId stId = new ServiceTemplateId(namespace, serviceTemplate.getId(), false);
final SelfServiceMetaDataId id = new SelfServiceMetaDataId(stId);
try {
Files.walkFileTree(selfServiceDir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
String name = file.toString().substring(cutLength);
// check: if name contains "/", this could lead to exceptions
RepositoryFileReference ref = new RepositoryFileReference(id, name);
if (name.equals("data.xml")) {
// We quickly replace it via String replacement instead of XSLT
try {
String oldContent = org.apache.commons.io.FileUtils.readFileToString(file.toFile(), "UTF-8");
String newContent = oldContent.replace("http://opentosca.org/self-service", "http://www.eclipse.org/winery/model/selfservice");
newContent = newContent.replace(":application", ":Application");
if (!oldContent.equals(newContent)) {
// we replaced something -> write new content to old file
org.apache.commons.io.FileUtils.writeStringToFile(file.toFile(), newContent, "UTF-8");
}
} catch (IOException e) {
CsarImporter.LOGGER.debug("Could not replace content in data.xml", e);
}
}
importFile(file, ref, tmf, rootPath, errors);
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
CsarImporter.LOGGER.debug(e.getMessage(), e);
errors.add("Self-service Meta Data: " + e.getMessage());
}
}
}
use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.
the class CsarImporter method importOtherImport.
/**
* SIDE EFFECT: modifies the location of imp to point to the correct relative location (when read from the exported
* CSAR)
*
* @param rootPath the absolute path where to resolve files from
*/
private void importOtherImport(Path rootPath, TImport imp, final List<String> errors, String type, boolean overwrite) {
assert (!type.equals(Namespaces.TOSCA_NAMESPACE));
String loc = imp.getLocation();
if (!Util.isRelativeURI(loc)) {
// This is just an information message
errors.add("Absolute URIs are not resolved by Winery (" + loc + ")");
return;
}
// location URLs are encoded: http://www.w3.org/TR/2001/WD-charmod-20010126/#sec-URIs, RFC http://www.ietf.org/rfc/rfc2396.txt
loc = Util.URLdecode(loc);
Path path;
try {
path = rootPath.resolve(loc);
} catch (Exception e) {
// java.nio.file.InvalidPathException could be thrown which is a RuntimeException
errors.add(e.getMessage());
return;
}
if (!Files.exists(path)) {
// fallback for older CSARs, where the location is given from the root
path = rootPath.getParent().resolve(loc);
if (!Files.exists(path)) {
errors.add(String.format("File %1$s does not exist", loc));
return;
}
}
String namespace = imp.getNamespace();
String fileName = path.getFileName().toString();
String id = fileName;
id = FilenameUtils.removeExtension(id);
// Convention: id of import is filename without extension
GenericImportId rid;
if (type.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
rid = new XSDImportId(namespace, id, false);
} else {
rid = new GenericImportId(namespace, id, false, type);
}
boolean importDataExistsInRepo = RepositoryFactory.getRepository().exists(rid);
if (!importDataExistsInRepo) {
// We have to
// a) create a .definitions file
// b) put the file itself in the repo
// Create the definitions file
TDefinitions defs = BackendUtils.createWrapperDefinitions(rid);
defs.getImport().add(imp);
// QUICK HACK: We change the imp object's location here and below again
// This is "OK" as "storeDefinitions" serializes the current state and not the future state of the imp object
// change the location to point to the file in the folder of the .definitions file
imp.setLocation(fileName);
// put the definitions file to the repository
CsarImporter.storeDefinitions(rid, defs);
}
// put the file itself to the repo
// ref is required to generate fileRef
RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(rid);
RepositoryFileReference fileRef = new RepositoryFileReference(ref.getParent(), fileName);
// location is relative to Definitions/
// even if the import already exists, we have to adapt the path
// URIs are encoded
String newLoc = "../" + Util.getUrlPath(fileRef);
imp.setLocation(newLoc);
if (!importDataExistsInRepo || overwrite) {
// finally write the file to the storage
try (InputStream is = Files.newInputStream(path);
BufferedInputStream bis = new BufferedInputStream(is)) {
MediaType mediaType;
if (type.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
mediaType = MediaTypes.MEDIATYPE_XSD;
} else {
mediaType = BackendUtils.getMimeType(bis, path.getFileName().toString());
}
RepositoryFactory.getRepository().putContentToFile(fileRef, bis, mediaType);
} catch (IllegalArgumentException | IOException e) {
throw new IllegalStateException(e);
}
// we have to update the cache in case of a new XSD to speedup usage of winery
if (rid instanceof XSDImportId) {
// We do the initialization asynchronously
// We do not check whether the XSD has already been checked
// We cannot just checck whether an XSD already has been handled since the XSD could change over time
// Synchronization at org.eclipse.winery.repository.resources.imports.xsdimports.XSDImportResource.getAllDefinedLocalNames(short) also isn't feasible as the backend doesn't support locks
CsarImporter.xsdParsingService.submit(() -> {
CsarImporter.LOGGER.debug("Updating XSD import cache data");
// We call the queries without storing the result:
// We use the SIDEEFFECT that a cache is created
final XsdImportManager xsdImportManager = RepositoryFactory.getRepository().getXsdImportManager();
xsdImportManager.getAllDeclaredElementsLocalNames();
xsdImportManager.getAllDefinedTypesLocalNames();
CsarImporter.LOGGER.debug("Updated XSD import cache data");
});
}
}
}
use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.
the class RestUtils method cloneServiceTemplate.
public static ServiceTemplateId cloneServiceTemplate(ServiceTemplateId serviceTemplate, String newName, String artifactName) throws JAXBException, IllegalArgumentException, IOException {
ServiceTemplateId newServiceTemplateId = new ServiceTemplateId(serviceTemplate.getNamespace().getDecoded(), newName, false);
RepositoryFileReference fileRef = new RepositoryFileReference(newServiceTemplateId, "ServiceTemplate.tosca");
Definitions defs = new ServiceTemplateResource(serviceTemplate).getDefinitions();
defs.setId(newName + "Definitions");
defs.setName(newName + "Definitions generated from Artifact " + artifactName);
TServiceTemplate oldSTModel = null;
for (TExtensibleElements el : defs.getServiceTemplateOrNodeTypeOrNodeTypeImplementation()) {
if (el instanceof TServiceTemplate) {
oldSTModel = (TServiceTemplate) el;
}
}
oldSTModel.setId(newName);
oldSTModel.setName(newName + " generated from Artifact " + artifactName);
// remove xaaspackager tags
Collection<TTag> toRemove = new ArrayList<TTag>();
for (TTag tag : oldSTModel.getTags().getTag()) {
switch(tag.getName()) {
case "xaasPackageNode":
case "xaasPackageArtifactType":
case "xaasPackageDeploymentArtifact":
toRemove.add(tag);
break;
default:
break;
}
}
oldSTModel.getTags().getTag().removeAll(toRemove);
JAXBContext context = JAXBContext.newInstance(Definitions.class);
Marshaller m = context.createMarshaller();
StringWriter sw = new StringWriter();
m.marshal(defs, sw);
String xmlString = sw.toString();
RepositoryFactory.getRepository().putContentToFile(fileRef, xmlString, MediaTypes.MEDIATYPE_TOSCA_DEFINITIONS);
return newServiceTemplateId;
}
use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.
the class ArtifactTemplateResource method copySourceToFilesResource.
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response copySourceToFilesResource(@ApiParam(value = "if data contains a non-empty array than only the files" + " whose names are included are copied ", required = true) ArtifactResourcesApiData data) {
List<String> artifactList = data.getArtifactNames();
DirectoryId sourceDir = new ArtifactTemplateSourceDirectoryId((ArtifactTemplateId) this.id);
FilesResource filesResource = getFilesResource();
for (RepositoryFileReference ref : RepositoryFactory.getRepository().getContainedFiles(sourceDir)) {
if (artifactList == null || artifactList.contains(ref.getFileName())) {
try (InputStream inputStream = RepositoryFactory.getRepository().newInputStream(ref)) {
String fileName = ref.getFileName();
String subDirectory = ref.getSubDirectory().map(s -> s.toString()).orElse("");
filesResource.putFile(fileName, subDirectory, inputStream);
} catch (IOException e) {
LOGGER.debug("The artifact source " + ref.getFileName() + " could not be copied to the files directory.", e);
return Response.status(Status.INTERNAL_SERVER_ERROR).build();
}
}
}
return Response.status(Status.CREATED).build();
}
Aggregations