Search in sources :

Example 1 with GeneratedOutput

use of org.eclipse.vorto.repository.plugin.generator.GeneratedOutput in project vorto by eclipse.

the class GeneratedOutputAttachmentHandlerTest method attachGeneratedOutput.

@Test
public void attachGeneratedOutput() {
    // setup
    String filename = "thiswasgenerated.json";
    byte[] content = { 0x01 };
    List<Attachment> attList = new ArrayList<>();
    ModelId id = new ModelId();
    id.setName("TestInfoModel");
    attList.add(Attachment.newInstance(id, filename));
    when(repository.getAttachmentsByTags(any(), any())).thenReturn(attList);
    ModelInfo modelInfo = new ModelInfo();
    modelInfo.setId(id);
    Map<String, String> requestParams = new HashMap<>();
    GeneratorPluginConfiguration plugin = GeneratorPluginConfiguration.of("eclipseditto", "v2", "http://localhost:8888", "1.0.0");
    when(repository.getAttachmentContent(any(), any())).thenReturn(Optional.of(new FileContent(filename, content)));
    when(modelRepositoryFactory.getRepositoryByModel(any(), any())).thenReturn(repository);
    IUserContext userContext = new IUserContext() {

        @Override
        public Authentication getAuthentication() {
            return null;
        }

        @Override
        public String getUsername() {
            return "testuser";
        }

        @Override
        public String getWorkspaceId() {
            return null;
        }

        @Override
        public String getHashedUsername() {
            return null;
        }

        @Override
        public boolean isAnonymous() {
            return false;
        }

        @Override
        public boolean isSysAdmin() {
            return false;
        }
    };
    GeneratedOutput generatedOutput = sut.getGeneratedOutputFromAttachment(modelInfo, requestParams, plugin, repository).get();
    // execute
    GeneratedOutput result = sut.attachGeneratedOutput(userContext, modelInfo.getId(), "eclipseditto", requestParams, generatedOutput, plugin);
    // verify
    assertNotNull(result);
    assertEquals(1, result.getSize());
    assertEquals("generated_eclipseditto_1.0.0_TestInfoModel.json", result.getFileName());
    assertEquals(content, result.getContent());
}
Also used : GeneratorPluginConfiguration(org.eclipse.vorto.repository.plugin.generator.GeneratorPluginConfiguration) GeneratedOutput(org.eclipse.vorto.repository.plugin.generator.GeneratedOutput) ModelId(org.eclipse.vorto.model.ModelId) Test(org.junit.Test)

Example 2 with GeneratedOutput

use of org.eclipse.vorto.repository.plugin.generator.GeneratedOutput in project vorto by eclipse.

the class GeneratedOutputAttachmentHandlerTest method getGeneratedOutputFromAttachment.

@Test
public void getGeneratedOutputFromAttachment() {
    // setup
    String filename = "thiswasgenerated.json";
    byte[] content = { 0x01 };
    List<Attachment> attList = new ArrayList<>();
    ModelId id = new ModelId();
    id.setName("TestInfoModel");
    attList.add(Attachment.newInstance(id, filename));
    when(repository.getAttachmentsByTags(any(), any())).thenReturn(attList);
    ModelInfo modelInfo = new ModelInfo();
    modelInfo.setId(id);
    Map<String, String> requestParams = new HashMap<>();
    GeneratorPluginConfiguration plugin = GeneratorPluginConfiguration.of("eclipseditto", "v2", "http://localhost:8888", "1.0.0");
    when(repository.getAttachmentContent(any(), any())).thenReturn(Optional.of(new FileContent(filename, content)));
    // execute
    Optional<GeneratedOutput> result = sut.getGeneratedOutputFromAttachment(modelInfo, requestParams, plugin, repository);
    // verify
    assertTrue(result.isPresent());
    assertEquals(content, result.get().getContent());
    assertEquals(filename, result.get().getFileName());
    assertEquals(1, result.get().getSize());
}
Also used : GeneratorPluginConfiguration(org.eclipse.vorto.repository.plugin.generator.GeneratorPluginConfiguration) GeneratedOutput(org.eclipse.vorto.repository.plugin.generator.GeneratedOutput) ModelId(org.eclipse.vorto.model.ModelId) Test(org.junit.Test)

Example 3 with GeneratedOutput

use of org.eclipse.vorto.repository.plugin.generator.GeneratedOutput in project vorto by eclipse.

the class GenericGeneratorController method generateAndExtract.

@RequestMapping(value = "/{serviceKey}/models/{modelId:.+}/!/**", method = RequestMethod.GET)
@CrossOrigin(origins = "https://www.eclipse.org")
public void generateAndExtract(@ApiParam(value = "The iD of vorto model, e.g. com.mycompany:Car:1.0.0", required = true) @PathVariable final String modelId, @ApiParam(value = "Service key for a specified platform, e.g. lwm2m", required = true) @PathVariable String serviceKey, final HttpServletRequest request, final HttpServletResponse response) {
    try {
        ModelId modelIdToGen = ModelId.fromPrettyFormat(modelId);
        GeneratedOutput generatedOutput = generatorService.generate(getUserContext(modelIdToGen), modelIdToGen, URLDecoder.decode(serviceKey, "utf-8"), getRequestParams(request));
        String extractPath = getExtractPath(request);
        if (extractPath == null || extractPath.trim().isEmpty()) {
            writeToResponse(response, generatedOutput);
            return;
        }
        if (generatedOutput.getFileName().endsWith(ZIPFILE_EXTENSION)) {
            Optional<GeneratedOutput> extractionResult = extractFromZip(generatedOutput.getContent(), extractPath);
            if (extractionResult.isPresent()) {
                writeToResponse(response, extractionResult.get());
                return;
            }
        }
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
    } catch (IOException e) {
        throw new RuntimeException("Error copying file.", e);
    }
}
Also used : GeneratedOutput(org.eclipse.vorto.repository.plugin.generator.GeneratedOutput) IOException(java.io.IOException) ModelId(org.eclipse.vorto.model.ModelId) CrossOrigin(org.springframework.web.bind.annotation.CrossOrigin) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with GeneratedOutput

use of org.eclipse.vorto.repository.plugin.generator.GeneratedOutput in project vorto by eclipse.

the class GenericGeneratorController method extractFromZip.

private Optional<GeneratedOutput> extractFromZip(byte[] zipFile, String filenameInZip) throws IOException {
    Objects.requireNonNull(zipFile);
    Objects.requireNonNull(filenameInZip);
    ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(zipFile));
    ZipEntry ze = null;
    while ((ze = zipInputStream.getNextEntry()) != null) {
        if (ze.getName().equals(filenameInZip)) {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream(9000);
            byte[] buffer = new byte[9000];
            int len = 0;
            while ((len = zipInputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }
            byte[] outputBytes = outputStream.toByteArray();
            return Optional.of(new GeneratedOutput(outputBytes, Paths.get(ze.getName()).getFileName().toString(), outputBytes.length));
        }
    }
    return Optional.empty();
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipEntry(java.util.zip.ZipEntry) GeneratedOutput(org.eclipse.vorto.repository.plugin.generator.GeneratedOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 5 with GeneratedOutput

use of org.eclipse.vorto.repository.plugin.generator.GeneratedOutput in project vorto by eclipse.

the class AbstractGeneratorController method generateAndWriteToOutputStream.

protected void generateAndWriteToOutputStream(String modelId, String pluginKey, Map<String, String> params, HttpServletResponse response) {
    ModelId modelIdToGen = ModelId.fromPrettyFormat(modelId);
    try {
        GeneratedOutput generatedOutput = generatorService.generate(getUserContext(modelIdToGen), modelIdToGen, URLDecoder.decode(pluginKey, "utf-8"), params);
        writeToResponse(response, generatedOutput);
    } catch (IOException e) {
        throw new RuntimeException("Error copying file.", e);
    }
}
Also used : GeneratedOutput(org.eclipse.vorto.repository.plugin.generator.GeneratedOutput) IOException(java.io.IOException) ModelId(org.eclipse.vorto.model.ModelId)

Aggregations

GeneratedOutput (org.eclipse.vorto.repository.plugin.generator.GeneratedOutput)8 ModelId (org.eclipse.vorto.model.ModelId)5 GeneratorPluginConfiguration (org.eclipse.vorto.repository.plugin.generator.GeneratorPluginConfiguration)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 IOException (java.io.IOException)2 ModelContent (org.eclipse.vorto.model.ModelContent)2 ModelIdToModelContentConverter (org.eclipse.vorto.repository.conversion.ModelIdToModelContentConverter)2 Test (org.junit.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1