Search in sources :

Example 11 with UploadModelResult

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);
    }
}
Also used : UploadModelResult(org.eclipse.vorto.repository.api.upload.UploadModelResult) ModelInfo(org.eclipse.vorto.repository.api.ModelInfo) ValidationException(org.eclipse.vorto.repository.core.impl.validation.ValidationException) FatalModelRepositoryException(org.eclipse.vorto.repository.core.FatalModelRepositoryException) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) FatalModelRepositoryException(org.eclipse.vorto.repository.core.FatalModelRepositoryException) ValidationException(org.eclipse.vorto.repository.core.impl.validation.ValidationException) IModelValidator(org.eclipse.vorto.repository.core.impl.validation.IModelValidator) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) HashSet(java.util.HashSet)

Example 12 with UploadModelResult

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);
        }
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) UploadModelResult(org.eclipse.vorto.repository.api.upload.UploadModelResult) HttpEntity(org.apache.http.HttpEntity) ModelPublishException(org.eclipse.vorto.repository.api.upload.ModelPublishException) ByteArrayBody(org.apache.http.entity.mime.content.ByteArrayBody) UploadModelResponse(org.eclipse.vorto.repository.api.upload.UploadModelResponse) HttpPut(org.apache.http.client.methods.HttpPut) StringBody(org.apache.http.entity.mime.content.StringBody) TypeToken(com.google.gson.reflect.TypeToken) ModelId(org.eclipse.vorto.repository.api.ModelId)

Example 13 with UploadModelResult

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();
        }
    }
}
Also used : UploadModelResult(org.eclipse.vorto.repository.api.upload.UploadModelResult) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 14 with UploadModelResult

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());
}
Also used : UploadModelResult(org.eclipse.vorto.repository.api.upload.UploadModelResult) User(org.eclipse.vorto.repository.account.impl.User) ArrayList(java.util.ArrayList) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test) AbstractIntegrationTest(org.eclipse.vorto.repository.AbstractIntegrationTest)

Example 15 with UploadModelResult

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());
}
Also used : UploadModelResult(org.eclipse.vorto.repository.api.upload.UploadModelResult) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test) AbstractIntegrationTest(org.eclipse.vorto.repository.AbstractIntegrationTest)

Aggregations

UploadModelResult (org.eclipse.vorto.repository.api.upload.UploadModelResult)17 ClassPathResource (org.springframework.core.io.ClassPathResource)13 AbstractIntegrationTest (org.eclipse.vorto.repository.AbstractIntegrationTest)12 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)3 ModelInfo (org.eclipse.vorto.repository.api.ModelInfo)3 UploadModelResponse (org.eclipse.vorto.repository.api.upload.UploadModelResponse)3 ApiOperation (io.swagger.annotations.ApiOperation)2 IOException (java.io.IOException)2 User (org.eclipse.vorto.repository.account.impl.User)2 UploadTooLargeException (org.eclipse.vorto.repository.web.core.exceptions.UploadTooLargeException)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 TypeToken (com.google.gson.reflect.TypeToken)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 HashSet (java.util.HashSet)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 HttpEntity (org.apache.http.HttpEntity)1 HttpPost (org.apache.http.client.methods.HttpPost)1 HttpPut (org.apache.http.client.methods.HttpPut)1