use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.
the class FilebasedRepository method getZippedContents.
@Override
public void getZippedContents(final GenericId id, OutputStream out) throws WineryRepositoryException {
Objects.requireNonNull(id);
Objects.requireNonNull(out);
SortedSet<RepositoryFileReference> containedFiles = this.getContainedFiles(id);
try (final ArchiveOutputStream zos = new ArchiveStreamFactory().createArchiveOutputStream("zip", out)) {
for (RepositoryFileReference ref : containedFiles) {
ZipArchiveEntry zipArchiveEntry;
final Optional<Path> subDirectory = ref.getSubDirectory();
if (subDirectory.isPresent()) {
zipArchiveEntry = new ZipArchiveEntry(subDirectory.get().resolve(ref.getFileName()).toString());
} else {
zipArchiveEntry = new ZipArchiveEntry(ref.getFileName());
}
zos.putArchiveEntry(zipArchiveEntry);
try (InputStream is = RepositoryFactory.getRepository().newInputStream(ref)) {
IOUtils.copy(is, zos);
}
zos.closeArchiveEntry();
}
} catch (ArchiveException e) {
throw new WineryRepositoryException("Internal error while generating archive", e);
} catch (IOException e) {
throw new WineryRepositoryException("I/O exception during export", e);
}
}
use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.
the class RepositoryBasedXsdImportManager method getAllDefinedLocalNames.
// we need "unchecked", because of the parsing of the cache
@SuppressWarnings("unchecked")
private List<String> getAllDefinedLocalNames(final XSDImportId id, final boolean getTypes) {
Objects.requireNonNull(id);
Optional<RepositoryFileReference> ref = this.getXsdFileReference(id);
if (!ref.isPresent()) {
return Collections.emptyList();
}
short type = getTypes ? XSConstants.TYPE_DEFINITION : XSConstants.ELEMENT_DECLARATION;
Date lastUpdate = RepositoryFactory.getRepository().getLastUpdate(ref.get());
@NonNull final String cacheFileName = "definedLocalNames " + Integer.toString(type) + ".cache";
@NonNull final RepositoryFileReference cacheRef = new RepositoryFileReference(id, cacheFileName);
boolean cacheNeedsUpdate = true;
if (RepositoryFactory.getRepository().exists(cacheRef)) {
Date lastUpdateCache = RepositoryFactory.getRepository().getLastUpdate(cacheRef);
if (lastUpdate.compareTo(lastUpdateCache) <= 0) {
cacheNeedsUpdate = false;
}
}
List<String> result;
if (cacheNeedsUpdate) {
final Optional<XSModel> model = BackendUtils.getXSModel(ref.get());
if (!model.isPresent()) {
return Collections.emptyList();
}
XSNamedMap components = model.get().getComponents(type);
// @SuppressWarnings("unchecked")
int len = components.getLength();
result = new ArrayList<>(len);
for (int i = 0; i < len; i++) {
XSObject item = components.item(i);
// We want to return only types defined in the namespace of this resource
if (id.getNamespace().getDecoded().equals(item.getNamespace())) {
result.add(item.getName());
}
}
String cacheContent = null;
try {
cacheContent = BackendUtils.mapper.writeValueAsString(result);
} catch (JsonProcessingException e) {
LOGGER.error("Could not generate cache content", e);
}
try {
RepositoryFactory.getRepository().putContentToFile(cacheRef, cacheContent, MediaTypes.MEDIATYPE_APPLICATION_JSON);
} catch (IOException e) {
LOGGER.error("Could not update cache", e);
}
} else {
// cache should contain most recent information
try (InputStream is = RepositoryFactory.getRepository().newInputStream(cacheRef)) {
result = BackendUtils.mapper.readValue(is, java.util.List.class);
} catch (IOException e) {
LOGGER.error("Could not read from cache", e);
result = Collections.emptyList();
}
}
return result;
}
use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.
the class RepositoryBasedXsdImportManager method getMapFromLocalNameToXSD.
@Override
public Map<String, RepositoryFileReference> getMapFromLocalNameToXSD(final Namespace namespace, final boolean getTypes) {
Set<XSDImportId> importsOfNamespace = this.getImportsOfNamespace(namespace);
Map<String, RepositoryFileReference> result = new HashMap<>();
for (XSDImportId imp : importsOfNamespace) {
final List<String> allDefinedLocalNames = this.getAllDefinedLocalNames(namespace, getTypes);
Optional<RepositoryFileReference> ref = getXsdFileReference(imp);
if (!ref.isPresent()) {
LOGGER.error("Ref is not defined");
} else {
for (String localName : allDefinedLocalNames) {
result.put(localName, ref.get());
}
}
}
return result;
}
use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.
the class CsarExporter method addManifest.
private void addManifest(IRepository repository, DefinitionsChildId id, Collection<String> definitionNames, Map<RepositoryFileReference, String> refMap, ArchiveOutputStream out) throws IOException {
String entryDefinitionsReference = CsarExporter.getDefinitionsPathInsideCSAR(repository, id);
out.putArchiveEntry(new ZipArchiveEntry("TOSCA-Metadata/TOSCA.meta"));
PrintWriter pw = new PrintWriter(out);
// Setting Versions
pw.println("TOSCA-Meta-Version: 1.0");
pw.println("CSAR-Version: 1.0");
String versionString = "Created-By: Winery " + Environment.getVersion();
pw.println(versionString);
// Winery currently is unaware of tDefinitions, therefore, we use the
// name of the service template
pw.println("Entry-Definitions: " + entryDefinitionsReference);
pw.println();
assert (definitionNames.contains(entryDefinitionsReference));
for (String name : definitionNames) {
pw.println("Name: " + name);
pw.println("Content-Type: " + org.eclipse.winery.common.constants.MimeTypes.MIMETYPE_TOSCA_DEFINITIONS);
pw.println();
}
// Setting other files, mainly files belonging to artifacts
for (RepositoryFileReference ref : refMap.keySet()) {
String archivePath = refMap.get(ref);
pw.println("Name: " + archivePath);
String mimeType;
if (ref instanceof DummyRepositoryFileReferenceForGeneratedXSD) {
mimeType = MimeTypes.MIMETYPE_XSD;
} else {
mimeType = repository.getMimeType(ref);
}
pw.println("Content-Type: " + mimeType);
pw.println();
}
pw.flush();
out.closeArchiveEntry();
}
use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.
the class CsarExporter method writeCsar.
/**
* Writes a complete CSAR containing all necessary things reachable from the given service template
*
* @param entryId the id of the service template to export
* @param out the output stream to write to
*/
public void writeCsar(IRepository repository, DefinitionsChildId entryId, OutputStream out) throws ArchiveException, IOException, JAXBException, RepositoryCorruptException {
CsarExporter.LOGGER.trace("Starting CSAR export with {}", entryId.toString());
Map<RepositoryFileReference, String> refMap = new HashMap<>();
Collection<String> definitionNames = new ArrayList<>();
try (final ArchiveOutputStream zos = new ArchiveStreamFactory().createArchiveOutputStream("zip", out)) {
ToscaExportUtil exporter = new ToscaExportUtil();
Map<String, Object> conf = new HashMap<>();
ExportedState exportedState = new ExportedState();
DefinitionsChildId currentId = entryId;
do {
String defName = CsarExporter.getDefinitionsPathInsideCSAR(repository, currentId);
definitionNames.add(defName);
zos.putArchiveEntry(new ZipArchiveEntry(defName));
Collection<DefinitionsChildId> referencedIds;
referencedIds = exporter.exportTOSCA(repository, currentId, zos, refMap, conf);
zos.closeArchiveEntry();
exportedState.flagAsExported(currentId);
exportedState.flagAsExportRequired(referencedIds);
currentId = exportedState.pop();
} while (currentId != null);
// if we export a ServiceTemplate, data for the self-service portal might exist
if (entryId instanceof ServiceTemplateId) {
ServiceTemplateId serviceTemplateId = (ServiceTemplateId) entryId;
this.addSelfServiceMetaData(repository, serviceTemplateId, refMap);
this.addSelfServiceFiles(repository, serviceTemplateId, refMap, zos);
}
// now, refMap contains all files to be added to the CSAR
// write manifest directly after the definitions to have it more at the beginning of the ZIP rather than having it at the very end
this.addManifest(repository, entryId, definitionNames, refMap, zos);
// used for generated XSD schemas
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer;
try {
transformer = tFactory.newTransformer();
} catch (TransformerConfigurationException e1) {
CsarExporter.LOGGER.debug(e1.getMessage(), e1);
throw new IllegalStateException("Could not instantiate transformer", e1);
}
// write all referenced files
for (RepositoryFileReference ref : refMap.keySet()) {
String archivePath = refMap.get(ref);
CsarExporter.LOGGER.trace("Creating {}", archivePath);
if (ref instanceof DummyRepositoryFileReferenceForGeneratedXSD) {
addDummyRepositoryFileReferenceForGeneratedXSD(zos, transformer, (DummyRepositoryFileReferenceForGeneratedXSD) ref, archivePath);
} else {
if (ref.getParent() instanceof DirectoryId) {
// special handling for artifact template directories "source" and "files"
addArtifactTemplateToZipFile(zos, repository, ref, archivePath);
} else {
addFileToZipArchive(zos, repository, ref, archivePath);
zos.closeArchiveEntry();
}
}
}
this.addNamespacePrefixes(zos, repository);
}
}
Aggregations