Search in sources :

Example 1 with ModelPublishException

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

Example 2 with ModelPublishException

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

the class DefaultModelPublisher method uploadModelImage.

@Override
public void uploadModelImage(ModelId modelId, String imageBas64) throws ModelPublishException {
    String uploadImageUrl = String.format("%s/rest/model/image?namespace=%s&name=%s&version=%s", getRequestContext().getBaseUrl(), modelId.getNamespace(), modelId.getName(), modelId.getVersion());
    HttpPost uploadImage = new HttpPost(uploadImageUrl);
    HttpEntity entity = MultipartEntityBuilder.create().addPart("fileName", new StringBody("vortomodel.png", ContentType.DEFAULT_TEXT)).addPart("fileDescription", new StringBody("", ContentType.DEFAULT_TEXT)).addPart("file", new ByteArrayBody(Base64.getDecoder().decode(imageBas64.getBytes()), ContentType.APPLICATION_OCTET_STREAM, "vortomodel.png")).build();
    uploadImage.setEntity(entity);
    try {
        execute(uploadImage, new TypeToken<Void>() {
        }.getType());
    } catch (Throwable ex) {
        if (!(ex instanceof ModelPublishException)) {
            throw new RuntimeException(ex);
        } else {
            throw ((ModelPublishException) ex);
        }
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) StringBody(org.apache.http.entity.mime.content.StringBody) TypeToken(com.google.gson.reflect.TypeToken) ModelPublishException(org.eclipse.vorto.repository.api.upload.ModelPublishException) ByteArrayBody(org.apache.http.entity.mime.content.ByteArrayBody)

Aggregations

TypeToken (com.google.gson.reflect.TypeToken)2 HttpEntity (org.apache.http.HttpEntity)2 HttpPost (org.apache.http.client.methods.HttpPost)2 ByteArrayBody (org.apache.http.entity.mime.content.ByteArrayBody)2 StringBody (org.apache.http.entity.mime.content.StringBody)2 ModelPublishException (org.eclipse.vorto.repository.api.upload.ModelPublishException)2 HttpPut (org.apache.http.client.methods.HttpPut)1 ModelId (org.eclipse.vorto.repository.api.ModelId)1 UploadModelResponse (org.eclipse.vorto.repository.api.upload.UploadModelResponse)1 UploadModelResult (org.eclipse.vorto.repository.api.upload.UploadModelResult)1