Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Codec handling for multi module use #231

Merged
merged 6 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void create(final Context ctx, final String modeluri) {

try {
this.modelRepository.addModel(modeluri, root.get());
final JsonNode encoded = codecs.encode(ctx, root.get());
final JsonNode encoded = codecs.encode(modeluri, ctx, root.get());
success(ctx, encoded);
this.sessionController.modelCreated(modeluri);
} catch (EncodingException ex) {
Expand Down Expand Up @@ -146,7 +146,7 @@ public void getAll(final Context ctx) {
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());
final JsonNode encoded = codecs.encode(entry.getKey().toString(), ctx, entry.getValue());
encodedEntries.put(uriConverter.deresolveModelURI(ctx, entry.getKey()), encoded);
}
success(ctx, JsonCodec.encode(encodedEntries));
Expand All @@ -165,7 +165,7 @@ public void getOne(final Context ctx, final String modeluri) {
return;
}
try {
success(ctx, JsonCodec.encode(codecs.encode(ctx, root.get())));
success(ctx, JsonCodec.encode(codecs.encode(modeluri, ctx, root.get())));
} catch (EncodingException exception) {
encodingError(ctx, exception);
}
Expand All @@ -180,7 +180,7 @@ public void getModelElementById(final Context ctx, final String modeluri, final
return;
}
try {
success(ctx, codecs.encode(ctx, element.get()));
success(ctx, codecs.encode(modeluri, ctx, element.get()));
} catch (EncodingException exception) {
encodingError(ctx, exception);
}
Expand All @@ -195,7 +195,7 @@ public void getModelElementByName(final Context ctx, final String modeluri, fina
return;
}
try {
success(ctx, codecs.encode(ctx, element.get()));
success(ctx, codecs.encode(modeluri, ctx, element.get()));
} catch (EncodingException exception) {
encodingError(ctx, exception);
}
Expand All @@ -213,7 +213,7 @@ public void update(final Context ctx, final String modeluri) {
return;
}
try {
response(ctx, JsonResponse.fullUpdate(codecs.encode(ctx, newRoot.get())));
response(ctx, JsonResponse.fullUpdate(codecs.encode(modeluri, ctx, newRoot.get())));
} catch (EncodingException exception) {
encodingError(ctx, exception);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ protected void broadcastFullUpdate(final String modeluri, final EObject updatedM
// model has been deleted
session.send(fullUpdate(NullNode.getInstance()));
} else {
session.send(fullUpdate(encoder.encode(session, updatedModel)));
session.send(fullUpdate(encoder.encode(modeluri, session, updatedModel)));
}
} catch (EncodingException e) {
LOG.error("Broadcast full update of " + modeluri + " failed", e);
Expand Down Expand Up @@ -308,7 +308,8 @@ protected JsonNode rewritePathsAsURIFragments(final WsContext session, final Jso
return result;
}

private void broadcastIncrementalUpdate(final WsContext session, final Map<String, JsonNode> updates) {
private void broadcastIncrementalUpdate(final WsContext session,
final Map<String, JsonNode> updates) {
String sessionFormat = encoder.findFormat(session);
JsonNode update = updates.get(sessionFormat);
session.send(JsonResponse.incrementalUpdate(update));
Expand Down Expand Up @@ -383,7 +384,7 @@ protected Map<String, JsonNode> encodeIfPresent(final String modeluri, final EOb
Map<String, JsonNode> encodings = new HashMap<>();
if (hasSession(modeluri)) {
try {
encodings = encoder.encode(execution);
encodings = encoder.encode(modeluri, execution);
} catch (EncodingException exception) {
LOG.error("Pre encoding of undo/redo command for " + modeluri + " failed", exception);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/********************************************************************************
* Copyright (c) 2022 EclipseSource and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0, or the MIT License which is
* available at https://opensource.org/licenses/MIT.
*
* SPDX-License-Identifier: EPL-2.0 OR MIT
********************************************************************************/
package org.eclipse.emfcloud.modelserver.emf.common.codecs;

import java.util.Collection;
import java.util.Optional;
import java.util.Set;

import org.eclipse.emfcloud.modelserver.common.codecs.Codec;

public interface CodecProvider {
cdamus marked this conversation as resolved.
Show resolved Hide resolved

int NOT_SUPPORTED = -1;

/**
* This returns all known formats that this codec provider supports.
*
* @return all known formats as a set
*/
Set<String> getAllFormats();
cdamus marked this conversation as resolved.
Show resolved Hide resolved

/**
* The priority of this codec provider for the provided modelUri and format.
*
* @param modelUri The modelUri to get the priority for
* @param format The format to get the priority for
* @return a number indicating the priority, the higher the prio the more likely it is to get used,
* return NOT_SUPPORTED if not supported
*/
int getPriority(String modelUri, String format);

/**
* The codec this provider can return for the provided modelUri and format.
*
* @param modelUri The modelUri to get the codec for
* @param format The format to get the codec for
* @return the codec this provider offers for the modelUri, format combination, an empty optional if not supported
*/
Optional<Codec> getCodec(String modelUri, String format);

static Optional<? extends CodecProvider> getBestCodecProvider(
ndoschek marked this conversation as resolved.
Show resolved Hide resolved
final Collection<? extends CodecProvider> codecProviders,
final String modelUri, final String format) {
return codecProviders.stream()
.max((cp1, cp2) -> cp1.getPriority(modelUri, format) - cp2.getPriority(modelUri, format));
}

static Optional<Codec> getBestCodec(final Collection<? extends CodecProvider> codecProviders,
ndoschek marked this conversation as resolved.
Show resolved Hide resolved
final String modelUri, final String format) {
Optional<? extends CodecProvider> bestCodecProvider = getBestCodecProvider(codecProviders, modelUri, format);
if (bestCodecProvider.isEmpty()) {
return Optional.empty();
}
return bestCodecProvider.get().getCodec(modelUri, format);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -44,41 +44,45 @@ public interface CodecsManager {
/**
* Encode an EObject to JsonNodes in all available formats.
*
* @param eObject
* @param modelUri The ModelUri of the request, null if not available
* @param eObject The EObject to encode
* @return a map containing all format strings and their encoded JsonNodes
* @throws EncodingException
*/
Map<String, JsonNode> encode(EObject eObject) throws EncodingException;
Map<String, JsonNode> encode(String modelUri, EObject eObject) throws EncodingException;
ndoschek marked this conversation as resolved.
Show resolved Hide resolved

/**
* Encode an EObject to a JsonNode.
*
* @param context the javalin http context
* @param eObject EObject to encode
* @param modelUri The ModelUri of the request, null if not available
* @param context the javalin http context
* @param eObject EObject to encode
* @return JsonNode
* @throws EncodingException when encoding failed
*/
JsonNode encode(Context context, EObject eObject) throws EncodingException;
JsonNode encode(String modelUri, Context context, EObject eObject) throws EncodingException;

/**
* Encode an EObject to a JsonNode.
*
* @param context the javalin websocket context
* @param eObject EObject to encode
* @param modelUri The ModelUri of the request, null if not available
* @param context the javalin websocket context
* @param eObject EObject to encode
* @return JsonNode
* @throws EncodingException when encoding failed
*/
JsonNode encode(WsContext context, EObject eObject) throws EncodingException;
JsonNode encode(String modelUri, WsContext context, EObject eObject) throws EncodingException;

/**
* Decode a JsonNode to an EObject.
*
* @param context the javalin http context
* @param payload tthe String payload holding EObject definition to decode
* @param modelUri The ModelUri of the request, null if not available
* @param context the javalin http context
* @param payload tthe String payload holding EObject definition to decode
ndoschek marked this conversation as resolved.
Show resolved Hide resolved
* @return the decoded EObject
* @throws DecodingException when decoding failed
*/
Optional<EObject> decode(Context context, String payload) throws DecodingException;
Optional<EObject> decode(String modelUri, Context context, String payload) throws DecodingException;

/**
* Decode a JsonNode to an EObject.
Expand All @@ -95,12 +99,13 @@ Optional<EObject> decode(Context context, String payload, URI workspaceURI)
/**
* Decode a JsonNode to an EObject.
*
* @param context the javalin websocket context
* @param payload tthe String payload holding EObject definition to decode
* @param modelUri The ModelUri of the request, null if not available
* @param context the javalin websocket context
* @param payload tthe String payload holding EObject definition to decode
* @return the decoded EObject
* @throws DecodingException when decoding failed
*/
Optional<EObject> decode(WsContext context, String payload) throws DecodingException;
Optional<EObject> decode(String modelUri, WsContext context, String payload) throws DecodingException;

/**
* Decode a JsonNode to an EObject.
Expand All @@ -125,9 +130,10 @@ Optional<EObject> decode(WsContext context, String payload, URI workspaceURI)
/**
* Obtains the codec that handles the format of the websocket.
*
* @param context the javalin websocket context
* @param modelUri The ModelUri of the request, null if not available
* @param context the javalin websocket context
* @return the codec for the websocket's format
*/
Codec findCodec(WsContext context);
Codec findCodec(String modelUri, WsContext context);

}
Loading