Search in sources :

Example 1 with GeneratedOutput

use of org.eclipse.vorto.repository.api.generation.GeneratedOutput in project vorto by eclipse.

the class DefaultModelGeneration method getGeneratedOutput.

private GeneratedOutput getGeneratedOutput(HttpResponse response) {
    try {
        String contentDisposition = response.getFirstHeader("Content-Disposition").getValue();
        String filename = contentDisposition.substring(contentDisposition.indexOf("filename = ") + "filename = ".length());
        long length = response.getEntity().getContentLength();
        byte[] content = IOUtils.toByteArray(response.getEntity().getContent());
        return new GeneratedOutput(content, filename, length);
    } catch (IOException e) {
        throw new RepositoryClientException("Error in converting response to GeneratedOutput", e);
    }
}
Also used : RepositoryClientException(org.eclipse.vorto.repository.client.RepositoryClientException) GeneratedOutput(org.eclipse.vorto.repository.api.generation.GeneratedOutput) IOException(java.io.IOException)

Example 2 with GeneratedOutput

use of org.eclipse.vorto.repository.api.generation.GeneratedOutput in project vorto by eclipse.

the class GenerationDelegateProxyService method generate.

@Override
public GeneratedOutput generate(ModelId modelId, String serviceKey, Map<String, String> requestParams) {
    ModelInfo modelResource = modelRepositoryService.getById(modelId);
    if (modelResource == null) {
        throw new ModelNotFoundException("Model with the given ID does not exist", null);
    }
    if (modelResource.getType() == ModelType.Datatype || modelResource.getType() == ModelType.Mapping) {
        throw new GenerationException("Provided model is neither an information model nor a function block model!");
    }
    restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
    Generator generatorEntity = getGenerator(serviceKey);
    if (generatorEntity == null) {
        throw new GenerationException("Generator with key " + serviceKey + " is not a registered generator");
    }
    generatorEntity.increaseInvocationCount();
    this.registeredGeneratorsRepository.save(generatorEntity);
    ResponseEntity<byte[]> entity = restTemplate.getForEntity(generatorEntity.getGenerationEndpointUrl() + attachRequestParams(requestParams), byte[].class, modelId.getNamespace(), modelId.getName(), modelId.getVersion());
    return new GeneratedOutput(entity.getBody(), extractFileNameFromHeader(entity), entity.getHeaders().getContentLength());
}
Also used : ModelInfo(org.eclipse.vorto.repository.api.ModelInfo) ModelNotFoundException(org.eclipse.vorto.repository.api.exception.ModelNotFoundException) GeneratedOutput(org.eclipse.vorto.repository.api.generation.GeneratedOutput) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) GenerationException(org.eclipse.vorto.repository.api.exception.GenerationException)

Example 3 with GeneratedOutput

use of org.eclipse.vorto.repository.api.generation.GeneratedOutput in project vorto by eclipse.

the class ModelGenerationController 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.api.generation.GeneratedOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 4 with GeneratedOutput

use of org.eclipse.vorto.repository.api.generation.GeneratedOutput in project vorto by eclipse.

the class ModelGenerationController method generateAndExtract.

@ApiOperation(value = "Generate code for a specified platform, and extract specified path")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Wrong input"), @ApiResponse(code = 404, message = "Model not found") })
@RequestMapping(value = "/{namespace}/{name}/{version:.+}/{serviceKey:[^!]+}!/**", method = RequestMethod.GET)
public void generateAndExtract(@ApiParam(value = "The namespace of vorto model, e.g. com.mycompany", required = true) @PathVariable final String namespace, @ApiParam(value = "The name of vorto model, e.g. NewInfomodel", required = true) @PathVariable final String name, @ApiParam(value = "The version of vorto model, e.g. 1.0.0", required = true) @PathVariable final String version, @ApiParam(value = "Service key for a specified platform, e.g. lwm2m", required = true) @PathVariable String serviceKey, final HttpServletRequest request, final HttpServletResponse response) {
    try {
        GeneratedOutput generatedOutput = generatorService.generate(new ModelId(name, namespace, version), 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.api.generation.GeneratedOutput) IOException(java.io.IOException) ModelId(org.eclipse.vorto.repository.api.ModelId) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with GeneratedOutput

use of org.eclipse.vorto.repository.api.generation.GeneratedOutput in project vorto by eclipse.

the class ModelGenerationController method generate.

@ApiOperation(value = "Generate code for a specified platform")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Wrong input"), @ApiResponse(code = 404, message = "Model not found") })
@RequestMapping(value = "/{namespace}/{name}/{version:.+}/{serviceKey:[^!]+}", method = RequestMethod.GET)
public void generate(@ApiParam(value = "The namespace of vorto model, e.g. com.mycompany", required = true) @PathVariable final String namespace, @ApiParam(value = "The name of vorto model, e.g. NewInfomodel", required = true) @PathVariable final String name, @ApiParam(value = "The version of vorto model, e.g. 1.0.0", required = true) @PathVariable final String version, @ApiParam(value = "Service key for a specified platform, e.g. lwm2m", required = true) @PathVariable String serviceKey, final HttpServletRequest request, final HttpServletResponse response) {
    Objects.requireNonNull(namespace, "namespace must not be null");
    Objects.requireNonNull(name, "name must not be null");
    Objects.requireNonNull(version, "version must not be null");
    Objects.requireNonNull(serviceKey, "version must not be null");
    try {
        GeneratedOutput generatedOutput = generatorService.generate(new ModelId(name, namespace, version), URLDecoder.decode(serviceKey, "utf-8"), getRequestParams(request));
        writeToResponse(response, generatedOutput);
    } catch (IOException e) {
        throw new RuntimeException("Error copying file.", e);
    }
}
Also used : GeneratedOutput(org.eclipse.vorto.repository.api.generation.GeneratedOutput) IOException(java.io.IOException) ModelId(org.eclipse.vorto.repository.api.ModelId) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

GeneratedOutput (org.eclipse.vorto.repository.api.generation.GeneratedOutput)5 IOException (java.io.IOException)3 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 ModelId (org.eclipse.vorto.repository.api.ModelId)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 ModelInfo (org.eclipse.vorto.repository.api.ModelInfo)1 GenerationException (org.eclipse.vorto.repository.api.exception.GenerationException)1 ModelNotFoundException (org.eclipse.vorto.repository.api.exception.ModelNotFoundException)1 RepositoryClientException (org.eclipse.vorto.repository.client.RepositoryClientException)1 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)1