use of org.eclipse.winery.repository.importing.CsarImporter in project winery by eclipse.
the class MainResource method importDefinitions.
@POST
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_JSON)
public Response importDefinitions(InputStream is) throws IOException {
File toscaFile;
toscaFile = File.createTempFile("TOSCA", ".tosca");
FileUtils.copyInputStreamToFile(is, toscaFile);
CsarImporter importer = new CsarImporter();
List<String> errors = new ArrayList<>();
importer.importDefinitions(null, toscaFile.toPath(), errors, false, true);
if (errors.isEmpty()) {
return Response.noContent().build();
} else {
return Response.status(Status.BAD_REQUEST).entity(errors).build();
}
}
use of org.eclipse.winery.repository.importing.CsarImporter in project winery by eclipse.
the class MainResource method importCSAR.
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@ApiOperation(value = "Imports the given CSAR (sent by simplesinglefileupload.jsp)")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", responseHeaders = @ResponseHeader(description = "If the CSAR could be partially imported, the points where it failed are returned in the body")) })
public // @formatter:off
Response importCSAR(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail, @FormDataParam("overwrite") @ApiParam(value = "true: content of CSAR overwrites existing content. false (default): existing content is kept") Boolean overwrite, @Context UriInfo uriInfo) {
// @formatter:on
CsarImporter importer = new CsarImporter();
boolean ow;
ow = (overwrite != null) && overwrite;
ImportMetaInformation importMetaInformation;
try {
importMetaInformation = importer.readCSAR(uploadedInputStream, ow, true);
} catch (Exception e) {
return Response.serverError().entity("Could not import CSAR").entity(e.getMessage()).build();
}
if (importMetaInformation.errors.isEmpty()) {
if (importMetaInformation.entryServiceTemplate.isPresent()) {
URI url = uriInfo.getBaseUri().resolve(RestUtils.getAbsoluteURL(importMetaInformation.entryServiceTemplate.get()));
return Response.created(url).build();
} else {
return Response.noContent().build();
}
} else {
// In case there are errors, we send them as "bad request"
return Response.status(Status.BAD_REQUEST).entity(importMetaInformation.errors).build();
}
}
use of org.eclipse.winery.repository.importing.CsarImporter in project winery by eclipse.
the class Showcase method zipTypeTest.
@Test
public void zipTypeTest() throws Exception {
String name = "Showcase";
String path = this.path + File.separator + name + ".csar";
// Read DriverInjectionTest and import the csar into the repository
InputStream fis = new FileInputStream(path);
IRepository repository = RepositoryFactory.getRepository(Utils.getTmpDir(Paths.get("repository")));
CsarImporter csarImporter = new CsarImporter();
csarImporter.readCSAR(fis, true, true);
// Read the csar again and convert it to yaml the resulting yaml service templates
// are written to a temporary dir and converted to a input stream of a zip file
Converter converter = new Converter(repository);
fis = new FileInputStream(path);
InputStream zip = converter.convertX2Y(fis);
// Write the zip file to the output path and rename it
File zipFile = new File(outPath + File.separator + name + ".csar");
if (!zipFile.getParentFile().exists()) {
zipFile.getParentFile().mkdirs();
}
Files.copy(zip, zipFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
// clean temporary repository
Utils.deleteTmpDir(Paths.get("repository"));
Assert.assertTrue(zipFile.exists());
}
use of org.eclipse.winery.repository.importing.CsarImporter in project winery by eclipse.
the class WriterUtils method storeDefinitions.
public static void storeDefinitions(Definitions definitions, boolean overwrite, Path dir) {
Path path = null;
try {
path = Files.createTempDirectory("winery");
} catch (IOException e) {
e.printStackTrace();
}
LOGGER.debug("Store definition: {}", definitions.getId());
saveDefinitions(definitions, path, definitions.getTargetNamespace(), definitions.getId());
Definitions cleanDefinitions = loadDefinitions(path, definitions.getTargetNamespace(), definitions.getId());
CsarImporter csarImporter = new CsarImporter();
List<Exception> exceptions = new ArrayList<>();
cleanDefinitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().forEach(entry -> {
String namespace = csarImporter.getNamespace(entry, definitions.getTargetNamespace());
csarImporter.setNamespace(entry, namespace);
String id = ModelUtilities.getId(entry);
Class<? extends DefinitionsChildId> widClazz = Util.getComponentIdClassForTExtensibleElements(entry.getClass());
final DefinitionsChildId wid = BackendUtils.getDefinitionsChildId(widClazz, namespace, id, false);
if (RepositoryFactory.getRepository().exists(wid)) {
if (overwrite) {
try {
RepositoryFactory.getRepository().forceDelete(wid);
} catch (IOException e) {
exceptions.add(e);
}
} else {
return;
}
}
if (entry instanceof TArtifactTemplate) {
TArtifactTemplate.ArtifactReferences artifactReferences = ((TArtifactTemplate) entry).getArtifactReferences();
Stream.of(artifactReferences).filter(Objects::nonNull).flatMap(ref -> ref.getArtifactReference().stream()).filter(Objects::nonNull).forEach(ref -> {
String reference = ref.getReference();
URI refURI;
try {
refURI = new URI(reference);
} catch (URISyntaxException e) {
LOGGER.error("Invalid URI {}", reference);
return;
}
if (refURI.isAbsolute()) {
return;
}
Path artifactPath = dir.resolve(reference);
if (!Files.exists(artifactPath)) {
LOGGER.error("File not found {}", artifactPath);
return;
}
ArtifactTemplateFilesDirectoryId aDir = new ArtifactTemplateFilesDirectoryId((ArtifactTemplateId) wid);
RepositoryFileReference aFile = new RepositoryFileReference(aDir, artifactPath.getFileName().toString());
MediaType mediaType = null;
try (InputStream is = Files.newInputStream(artifactPath);
BufferedInputStream bis = new BufferedInputStream(is)) {
mediaType = BackendUtils.getMimeType(bis, artifactPath.getFileName().toString());
RepositoryFactory.getRepository().putContentToFile(aFile, bis, mediaType);
} catch (IOException e) {
LOGGER.error("Could not read artifact template file: {}", artifactPath);
return;
}
});
}
final Definitions part = BackendUtils.createWrapperDefinitions(wid);
part.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().add(entry);
RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(wid);
String content = BackendUtils.getXMLAsString(part, true);
try {
RepositoryFactory.getRepository().putContentToFile(ref, content, MediaTypes.MEDIATYPE_TOSCA_DEFINITIONS);
} catch (Exception e) {
exceptions.add(e);
}
});
}
Aggregations