use of org.eclipse.emfcloud.modelserver.common.codecs.EncodingException in project emfcloud-modelserver by eclipse-emfcloud.
the class DefaultModelControllerTest method getAllXmiFormat.
@Test
public void getAllXmiFormat() throws EncodingException, IOException {
final AtomicReference<JsonNode> response = new AtomicReference<>();
final EClass brewingUnit = EcoreFactory.eINSTANCE.createEClass();
Answer<Void> answer = invocation -> {
response.set(invocation.getArgument(0));
return null;
};
doAnswer(answer).when(context).json(any(JsonNode.class));
final LinkedHashMap<String, List<String>> queryParams = new LinkedHashMap<>();
queryParams.put(ModelServerPathParametersV1.FORMAT, Collections.singletonList(ModelServerPathParametersV1.FORMAT_XMI));
when(context.queryParamMap()).thenReturn(queryParams);
final Map<URI, EObject> allModels = Collections.singletonMap(URI.createURI("test"), brewingUnit);
when(modelRepository.getAllModels()).thenReturn(allModels);
modelController.getAll(context);
assertThat(response.get().get(JsonResponseMember.DATA), is(equalTo(Json.object(Json.prop("test", new XmiCodec().encode(brewingUnit))))));
}
use of org.eclipse.emfcloud.modelserver.common.codecs.EncodingException in project emfcloud-modelserver by eclipse-emfcloud.
the class DefaultModelController method getAll.
@Override
public void getAll(final Context ctx) {
try {
final Map<URI, EObject> allModels = this.modelRepository.getAllModels();
Map<URI, JsonNode> encodedEntries = Maps.newLinkedHashMap();
for (Map.Entry<URI, EObject> entry : allModels.entrySet()) {
final JsonNode encoded = codecs.encode(ctx, entry.getValue());
encodedEntries.put(entry.getKey(), encoded);
}
success(ctx, JsonCodec.encode(encodedEntries));
} catch (EncodingException exception) {
encodingError(ctx, exception);
} catch (IOException exception) {
internalError(ctx, "Could not load all models");
}
}
use of org.eclipse.emfcloud.modelserver.common.codecs.EncodingException in project uml-glsp by eclipsesource.
the class UmlCodec method encode.
@Override
public JsonNode encode(final EObject eObject) throws EncodingException {
Resource originalResource = eObject.eResource();
URI originalURI = originalResource.getURI();
// temporarily set different URI to serialize
eObject.eResource().setURI(URI.createURI("virtual.uml"));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
eObject.eResource().save(outputStream, Map.of(XMLResource.OPTION_KEEP_DEFAULT_CONTENT, Boolean.TRUE, XMLResource.OPTION_PROCESS_DANGLING_HREF, XMLResource.OPTION_PROCESS_DANGLING_HREF_DISCARD));
} catch (IOException e) {
throw new EncodingException(e);
}
// reset original URI
originalResource.setURI(originalURI);
return Json.text(outputStream.toString());
}
use of org.eclipse.emfcloud.modelserver.common.codecs.EncodingException in project emfcloud-modelserver by eclipse-emfcloud.
the class DefaultModelController method create.
@Override
public void create(final Context ctx, final String modeluri) {
Optional<EObject> root = readPayload(ctx);
if (root.isEmpty()) {
return;
}
if (this.modelRepository.hasModel(modeluri)) {
conflict(ctx, "Model already exists.");
return;
}
try {
this.modelRepository.addModel(modeluri, root.get());
final JsonNode encoded = codecs.encode(ctx, root.get());
success(ctx, encoded);
this.sessionController.modelCreated(modeluri);
} catch (EncodingException ex) {
encodingError(ctx, ex);
} catch (IOException ex) {
internalError(ctx, "Could not save resource", ex);
}
}
use of org.eclipse.emfcloud.modelserver.common.codecs.EncodingException in project emfcloud-modelserver by eclipse-emfcloud.
the class DICommandCodec method serverToClient.
@Override
@SuppressWarnings({ "checkstyle:CyclomaticComplexity", "checkstyle:JavaNCSS" })
public CCommand serverToClient(final Command command) throws EncodingException {
Command actualCommand = ModelServerCommand.unwrap(command);
Optional<CCommand> userCommand = ModelServerCommand.getClientCommand(command);
if (!userCommand.isPresent()) {
throw new EncodingException("Cannot determine origin of command: " + command);
}
String commandType = userCommand.get().getType();
CommandContribution codecContribution = typeToCodec.get(commandType);
if (codecContribution == null) {
throw new EncodingException("Unknown command type: " + commandType);
}
return codecContribution.serverToClient(actualCommand, userCommand.get());
}
Aggregations