Search in sources :

Example 1 with UploadModelResponse

use of org.eclipse.vorto.repository.api.upload.UploadModelResponse in project vorto by eclipse.

the class ShareModelController method uploadModel.

@ApiOperation(value = "Upload and validate a single vorto model")
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<UploadModelResponse> uploadModel(@ApiParam(value = "The vorto model file to upload", required = true) @RequestParam("file") MultipartFile file) {
    if (file.getSize() > maxModelSize) {
        throw new UploadTooLargeException("model", maxModelSize);
    }
    LOGGER.info("uploadModel: [" + file.getOriginalFilename() + "]");
    try {
        uploadModelResult = modelRepository.upload(file.getBytes(), file.getOriginalFilename(), SecurityContextHolder.getContext().getAuthentication().getName());
        List<UploadModelResult> uploadModelResults = Lists.newArrayList();
        uploadModelResults.add(uploadModelResult);
        String message = "Uploaded model " + file.getOriginalFilename() + (uploadModelResult.isValid() ? " is valid to check in." : " has errors. Cannot check in.");
        return validResponse(new UploadModelResponse(message, uploadModelResult.isValid(), uploadModelResults));
    } catch (IOException e) {
        LOGGER.error("Error upload model." + e.getStackTrace());
        UploadModelResponse errorResponse = new UploadModelResponse("Error during upload. Try again. " + e.getMessage(), false, null);
        return new ResponseEntity<UploadModelResponse>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
Also used : UploadModelResult(org.eclipse.vorto.repository.api.upload.UploadModelResult) UploadTooLargeException(org.eclipse.vorto.repository.web.core.exceptions.UploadTooLargeException) UploadModelResponse(org.eclipse.vorto.repository.api.upload.UploadModelResponse) IOException(java.io.IOException) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with UploadModelResponse

use of org.eclipse.vorto.repository.api.upload.UploadModelResponse in project vorto by eclipse.

the class ShareModelController method uploadMultipleModels.

@ApiOperation(value = "Upload and validate multiple vorto models")
@RequestMapping(value = "multiple", method = RequestMethod.POST)
public ResponseEntity<UploadModelResponse> uploadMultipleModels(@ApiParam(value = "The vorto model files to upload", required = true) @RequestParam("file") MultipartFile file) {
    if (file.getSize() > maxModelSize) {
        throw new UploadTooLargeException("model", maxModelSize);
    }
    LOGGER.info("Bulk upload Models: [" + file.getOriginalFilename() + "]");
    try {
        BulkUploadHelper bulkUploadService = new BulkUploadHelper(this.modelRepository, uploadStorage);
        List<UploadModelResult> uploadModelResults = bulkUploadService.uploadMultiple(file.getBytes(), file.getOriginalFilename(), SecurityContextHolder.getContext().getAuthentication().getName());
        LOGGER.info("Models Uploaded: [" + uploadModelResults.size() + "]");
        UploadModelResponse serverResponse = (uploadModelResults.size() == 0) ? new UploadModelResponse("Uploaded file doesn't have any valid models.", false, uploadModelResults) : createModelResponse(uploadModelResults);
        return validResponse(serverResponse);
    } catch (Exception e) {
        LOGGER.error("Error bulk upload models.", e);
        return erroredResponse("Error during upload. Try again. " + e.getMessage());
    }
}
Also used : UploadModelResult(org.eclipse.vorto.repository.api.upload.UploadModelResult) UploadTooLargeException(org.eclipse.vorto.repository.web.core.exceptions.UploadTooLargeException) BulkUploadHelper(org.eclipse.vorto.repository.core.impl.utils.BulkUploadHelper) UploadModelResponse(org.eclipse.vorto.repository.api.upload.UploadModelResponse) UploadTooLargeException(org.eclipse.vorto.repository.web.core.exceptions.UploadTooLargeException) IOException(java.io.IOException) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with UploadModelResponse

use of org.eclipse.vorto.repository.api.upload.UploadModelResponse 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)

Aggregations

UploadModelResponse (org.eclipse.vorto.repository.api.upload.UploadModelResponse)3 UploadModelResult (org.eclipse.vorto.repository.api.upload.UploadModelResult)3 ApiOperation (io.swagger.annotations.ApiOperation)2 IOException (java.io.IOException)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 HttpEntity (org.apache.http.HttpEntity)1 HttpPost (org.apache.http.client.methods.HttpPost)1 HttpPut (org.apache.http.client.methods.HttpPut)1 ByteArrayBody (org.apache.http.entity.mime.content.ByteArrayBody)1 StringBody (org.apache.http.entity.mime.content.StringBody)1 ModelId (org.eclipse.vorto.repository.api.ModelId)1 ModelPublishException (org.eclipse.vorto.repository.api.upload.ModelPublishException)1 BulkUploadHelper (org.eclipse.vorto.repository.core.impl.utils.BulkUploadHelper)1