Search in sources :

Example 26 with ModelId

use of org.eclipse.vorto.repository.api.ModelId in project vorto by eclipse.

the class ModelRepositoryController method getModelImage.

@ApiOperation(value = "Returns the image of a vorto model")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Wrong input"), @ApiResponse(code = 404, message = "Model not found") })
@RequestMapping(value = "/image/{namespace}/{name}/{version:.+}", method = RequestMethod.GET)
public void getModelImage(@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 = "Response", required = true) 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");
    final ModelId modelId = new ModelId(name, namespace, version);
    byte[] modelImage = modelRepository.getModelImage(modelId);
    response.setHeader(CONTENT_DISPOSITION, ATTACHMENT_FILENAME + modelId.getName() + ".png");
    response.setContentType(APPLICATION_OCTET_STREAM);
    try {
        IOUtils.copy(new ByteArrayInputStream(modelImage), response.getOutputStream());
        response.flushBuffer();
    } catch (IOException e) {
        throw new RuntimeException("Error copying file.", e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) 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 27 with ModelId

use of org.eclipse.vorto.repository.api.ModelId in project vorto by eclipse.

the class ModelRepositoryController method getModelContent.

@ApiOperation(value = "Returns the model content")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Wrong input"), @ApiResponse(code = 404, message = "Model not found") })
@RequestMapping(value = "/content/{namespace}/{name}/{version:.+}", method = RequestMethod.GET)
public AbstractModel getModelContent(@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) {
    byte[] modelContent = createZipWithAllDependencies(new ModelId(name, namespace, version), ContentType.DSL);
    IModelWorkspace workspace = IModelWorkspace.newReader().addZip(new ZipInputStream(new ByteArrayInputStream(modelContent))).read();
    return ModelDtoFactory.createResource(workspace.get().stream().filter(p -> p.getName().equals(name)).findFirst().get(), Optional.empty());
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URLDecoder(java.net.URLDecoder) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ApiParam(io.swagger.annotations.ApiParam) ApiResponses(io.swagger.annotations.ApiResponses) ModelInfo(org.eclipse.vorto.repository.api.ModelInfo) IModelRepository(org.eclipse.vorto.repository.core.IModelRepository) Value(org.springframework.beans.factory.annotation.Value) Logger(org.apache.log4j.Logger) ApiOperation(io.swagger.annotations.ApiOperation) AbstractRepositoryController(org.eclipse.vorto.repository.web.AbstractRepositoryController) ByteArrayInputStream(java.io.ByteArrayInputStream) AbstractModel(org.eclipse.vorto.repository.api.AbstractModel) Api(io.swagger.annotations.Api) ZipEntry(java.util.zip.ZipEntry) ModelId(org.eclipse.vorto.repository.api.ModelId) UploadTooLargeException(org.eclipse.vorto.repository.web.core.exceptions.UploadTooLargeException) ContentType(org.eclipse.vorto.repository.core.IModelRepository.ContentType) HttpServletResponse(javax.servlet.http.HttpServletResponse) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) RestController(org.springframework.web.bind.annotation.RestController) ModelNotFoundException(org.eclipse.vorto.repository.api.exception.ModelNotFoundException) IModelWorkspace(org.eclipse.vorto.server.commons.reader.IModelWorkspace) Objects(java.util.Objects) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) ApiResponse(io.swagger.annotations.ApiResponse) Optional(java.util.Optional) MultipartFile(org.springframework.web.multipart.MultipartFile) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ModelId(org.eclipse.vorto.repository.api.ModelId) IModelWorkspace(org.eclipse.vorto.server.commons.reader.IModelWorkspace) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 28 with ModelId

use of org.eclipse.vorto.repository.api.ModelId in project vorto by eclipse.

the class ModelRepositoryController method getMappingResources.

@ApiOperation(value = "Getting all mapping resources")
@RequestMapping(value = "/mapping/zip/{namespace}/{name}/{version:.+}/{targetPlatform}", method = RequestMethod.GET)
public void getMappingResources(@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 = "The name of target platform, e.g. lwm2m", required = true) @PathVariable final String targetPlatform, 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");
    final ModelId modelId = new ModelId(name, namespace, version);
    List<ModelInfo> mappingResources = modelRepository.getMappingModelsForTargetPlatform(modelId, targetPlatform);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(baos);
    final ContentType contentType = ContentType.DSL;
    try {
        for (ModelInfo mappingResource : mappingResources) {
            addModelToZip(zos, mappingResource.getId(), contentType);
        }
        zos.close();
        baos.close();
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
    response.setHeader(CONTENT_DISPOSITION, ATTACHMENT_FILENAME + modelId.getNamespace() + "_" + modelId.getName() + "_" + modelId.getVersion() + ".zip");
    response.setContentType(APPLICATION_OCTET_STREAM);
    try {
        IOUtils.copy(new ByteArrayInputStream(baos.toByteArray()), response.getOutputStream());
        response.flushBuffer();
    } catch (IOException e) {
        throw new RuntimeException("Error copying file.", e);
    }
}
Also used : ModelInfo(org.eclipse.vorto.repository.api.ModelInfo) ContentType(org.eclipse.vorto.repository.core.IModelRepository.ContentType) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ModelId(org.eclipse.vorto.repository.api.ModelId) UploadTooLargeException(org.eclipse.vorto.repository.web.core.exceptions.UploadTooLargeException) IOException(java.io.IOException) ModelNotFoundException(org.eclipse.vorto.repository.api.exception.ModelNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 29 with ModelId

use of org.eclipse.vorto.repository.api.ModelId in project vorto by eclipse.

the class DependencyManagerTest method create.

protected ModelInfo create(String name, ModelType type, ModelInfo... references) {
    final ModelId id = new ModelId(name, "org.eclipse.vorto", "1.0.0");
    ModelInfo resource = new ModelInfo(id, type);
    resource.setReferences(Arrays.asList(references).stream().map(reference -> reference.getId()).collect(Collectors.toList()));
    Arrays.asList(references).stream().forEach(it -> it.addReferencedBy(id));
    return resource;
}
Also used : ModelInfo(org.eclipse.vorto.repository.api.ModelInfo) ModelId(org.eclipse.vorto.repository.api.ModelId)

Example 30 with ModelId

use of org.eclipse.vorto.repository.api.ModelId in project vorto by eclipse.

the class ModelRepositoryTest method testGetModelWithImage.

@Test
public void testGetModelWithImage() throws Exception {
    final ModelId modelId = new ModelId("HueLightStrips", "com.mycompany", "1.0.0");
    checkinModel("Color.type");
    checkinModel("Colorlight.fbmodel");
    checkinModel("Switcher.fbmodel");
    checkinModel("HueLightStrips.infomodel");
    this.modelRepository.addModelImage(modelId, IOUtils.toByteArray(new ClassPathResource("sample_models/sample.png").getInputStream()));
    assertEquals(true, this.modelRepository.getById(modelId).isHasImage());
}
Also used : ModelId(org.eclipse.vorto.repository.api.ModelId) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test) AbstractIntegrationTest(org.eclipse.vorto.repository.AbstractIntegrationTest)

Aggregations

ModelId (org.eclipse.vorto.repository.api.ModelId)36 ModelInfo (org.eclipse.vorto.repository.api.ModelInfo)12 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)11 ApiOperation (io.swagger.annotations.ApiOperation)10 IOException (java.io.IOException)10 ApiResponses (io.swagger.annotations.ApiResponses)9 Test (org.junit.Test)9 ModelNotFoundException (org.eclipse.vorto.repository.api.exception.ModelNotFoundException)8 AbstractIntegrationTest (org.eclipse.vorto.repository.AbstractIntegrationTest)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 MappingModel (org.eclipse.vorto.core.api.model.mapping.MappingModel)6 ContentType (org.eclipse.vorto.repository.core.IModelRepository.ContentType)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 UploadTooLargeException (org.eclipse.vorto.repository.web.core.exceptions.UploadTooLargeException)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 Optional (java.util.Optional)4 ZipEntry (java.util.zip.ZipEntry)4 ZipOutputStream (java.util.zip.ZipOutputStream)4 MappingRule (org.eclipse.vorto.core.api.model.mapping.MappingRule)4 StereoTypeTarget (org.eclipse.vorto.core.api.model.mapping.StereoTypeTarget)4