use of org.eclipse.vorto.repository.api.upload.UploadModelResult in project vorto by eclipse.
the class BulkUploadHelper method uploadMultiple.
public List<UploadModelResult> uploadMultiple(byte[] content, String zipFileName, String callerId) {
if (!isValid(zipFileName)) {
throw new FatalModelRepositoryException("Filename/type is invalid", null);
}
List<UploadModelResult> invalidResult = new ArrayList<UploadModelResult>();
List<UploadModelResult> validResult = new ArrayList<UploadModelResult>();
Set<ModelInfo> parsedModels = new HashSet<>();
ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(content));
ZipEntry entry = null;
try {
while ((entry = zis.getNextEntry()) != null) {
if (!entry.isDirectory()) {
final String fileName = entry.getName();
if (ModelParserFactory.hasParserFor(fileName)) {
try {
parsedModels.add(ModelParserFactory.getParser(fileName).parse(new ByteArrayInputStream(copyStream(zis, entry))));
} catch (ValidationException grammarProblem) {
invalidResult.add(UploadModelResultFactory.create(grammarProblem));
}
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
try {
List<IModelValidator> bulkUploadValidators = constructBulkUploadValidators(parsedModels);
for (ModelInfo modelResource : parsedModels) {
try {
for (IModelValidator validator : bulkUploadValidators) {
validator.validate(modelResource, InvocationContext.create(callerId));
}
UploadModelResult result = UploadModelResult.valid(modelResource);
if (!invalidResult.contains(result)) {
validResult.add(result);
}
} catch (ValidationException validationException) {
invalidResult.add(UploadModelResultFactory.create(validationException));
}
}
if (invalidResult.isEmpty()) {
DependencyManager dm = new DependencyManager(parsedModels);
return safelyUpload(dm.getSorted());
} else {
List<UploadModelResult> completeResult = new ArrayList<>();
completeResult.addAll(invalidResult);
completeResult.addAll(validResult);
return completeResult;
}
} catch (Exception e) {
throw new FatalModelRepositoryException("Invalid zip file", e);
}
}
use of org.eclipse.vorto.repository.api.upload.UploadModelResult in project vorto by eclipse.
the class DefaultModelPublisher method publish.
@Override
public ModelId publish(ModelType type, String content) throws ModelPublishException {
String uploadModelsUrl = String.format("%s/rest/secure", getRequestContext().getBaseUrl());
HttpPost query = new HttpPost(uploadModelsUrl);
HttpEntity entity = MultipartEntityBuilder.create().addPart("fileName", new StringBody("vortomodel" + type.getExtension(), ContentType.DEFAULT_TEXT)).addPart("fileDescription", new StringBody("", ContentType.DEFAULT_TEXT)).addPart("file", new ByteArrayBody(content.getBytes(), ContentType.APPLICATION_OCTET_STREAM, "vortomodel" + type.getExtension())).build();
query.setEntity(entity);
try {
CompletableFuture<UploadModelResponse> response = execute(query, new TypeToken<UploadModelResponse>() {
}.getType());
List<UploadModelResult> result = response.get().getObj();
if (response.get().getIsSuccess()) {
String checkinModelUrl = String.format("%s/rest/secure/%s", getRequestContext().getBaseUrl(), result.get(0).getHandleId());
HttpPut checkInModel = new HttpPut(checkinModelUrl);
CompletableFuture<ModelId> checkedInResult = execute(checkInModel, new TypeToken<ModelId>() {
}.getType());
return (ModelId) checkedInResult.get();
} else {
throw new ModelPublishException(result.get(0));
}
} catch (Throwable ex) {
if (!(ex instanceof ModelPublishException)) {
throw new RuntimeException(ex);
} else {
throw ((ModelPublishException) ex);
}
}
}
use of org.eclipse.vorto.repository.api.upload.UploadModelResult in project vorto by eclipse.
the class AbstractIntegrationTest method checkinModel.
protected void checkinModel(String modelName, String callerId) {
try {
UploadModelResult uploadResult = modelRepository.upload(IOUtils.toByteArray(new ClassPathResource("sample_models/" + modelName).getInputStream()), modelName, callerId);
Assert.isTrue(uploadResult.isValid(), uploadResult.getErrorMessage());
when(userRepository.findAll()).thenReturn(Collections.emptyList());
modelRepository.checkin(uploadResult.getHandleId(), callerId);
modelRepository.search("*");
} catch (Exception ex) {
throw new RuntimeException(ex);
} finally {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
use of org.eclipse.vorto.repository.api.upload.UploadModelResult in project vorto by eclipse.
the class MappingTest method testCheckinValidMapping.
@Test
public void testCheckinValidMapping() throws Exception {
UploadModelResult uploadResult = modelRepository.upload(IOUtils.toByteArray(new ClassPathResource("sample_models/Color.type").getInputStream()), "Color.type", "admin");
assertEquals(true, uploadResult.isValid());
assertEquals(0, modelRepository.search("*").size());
User user = new User();
user.setUsername("alex");
user.setHasWatchOnRepository(true);
Collection<User> users = new ArrayList<User>();
users.add(user);
when(userRepository.findAll()).thenReturn(users);
modelRepository.checkin(uploadResult.getHandleId(), "alex");
// hack coz it might take awhile until index is
Thread.sleep(2000);
// updated to do a search
assertEquals(1, modelRepository.search("*").size());
uploadResult = modelRepository.upload(IOUtils.toByteArray(new ClassPathResource("sample_models/sample.mapping").getInputStream()), "sample.mapping", "admin");
assertEquals(true, uploadResult.isValid());
modelRepository.checkin(uploadResult.getHandleId(), "alex");
assertEquals(1, modelRepository.search("-Mapping").size());
}
use of org.eclipse.vorto.repository.api.upload.UploadModelResult in project vorto by eclipse.
the class ModelRepositoryTest method testUploadCorruptModelMissingVersion.
@Test
public void testUploadCorruptModelMissingVersion() throws Exception {
UploadModelResult uploadResult = modelRepository.upload(IOUtils.toByteArray(new ClassPathResource("sample_models/Corrupt-model_missingVersion.type").getInputStream()), "sample_models/Corrupt-model_missingVersion.type", "admin");
assertEquals(false, uploadResult.isValid());
assertNotNull(uploadResult.getErrorMessage());
}
Aggregations