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);
}
}
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());
}
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();
}
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);
}
}
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);
}
}
Aggregations