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