diff --git a/src/main/java/org/eclipse/basyx/submodel/restapi/SubmodelAPIHelper.java b/src/main/java/org/eclipse/basyx/submodel/restapi/SubmodelAPIHelper.java index 104fd311..a21878cf 100644 --- a/src/main/java/org/eclipse/basyx/submodel/restapi/SubmodelAPIHelper.java +++ b/src/main/java/org/eclipse/basyx/submodel/restapi/SubmodelAPIHelper.java @@ -49,16 +49,28 @@ public static String getSubmodelElementPath(String idShortPath) { } /** - * Retrieves access path for invocation of element's operation + * Retrieves access path for async invocation of element's operation + * * @param idShortPath * @return */ - public static String getSubmodelElementInvokePath(String idShortPath) { + public static String getSubmodelElementAsyncInvokePath(String idShortPath) { return VABPathTools.concatenatePaths(getSubmodelElementPath(idShortPath), Operation.INVOKE + OperationProvider.ASYNC); } + /** + * Retrieves access path for synchroenous invocation of element's operation + * + * @param idShortPath + * @return + */ + public static String getSubmodelElementSyncInvokePath(String idShortPath) { + return VABPathTools.concatenatePaths(getSubmodelElementPath(idShortPath), Operation.INVOKE); + } + /** * Retrieves access path for element value + * * @param idShortPath * @return */ diff --git a/src/main/java/org/eclipse/basyx/submodel/restapi/SubmodelProvider.java b/src/main/java/org/eclipse/basyx/submodel/restapi/SubmodelProvider.java index ee8cc0ba..6531b8f6 100644 --- a/src/main/java/org/eclipse/basyx/submodel/restapi/SubmodelProvider.java +++ b/src/main/java/org/eclipse/basyx/submodel/restapi/SubmodelProvider.java @@ -247,23 +247,39 @@ public void deleteValue(String path, Object obj) throws ProviderException { @Override public Object invokeOperation(String path, Object... parameters) throws ProviderException { - path = removeSubmodelPrefix(path); - if (path.isEmpty()) { + String pathWithoutSubmodelPrefix = removeSubmodelPrefix(path); + if (pathWithoutSubmodelPrefix.isEmpty()) { throw new MalformedRequestException("Given path must not be empty"); + } + + if (!VABPathTools.isOperationInvokationPath(pathWithoutSubmodelPrefix)) { + throw new MalformedRequestException("Given path '" + path + "' does not end in /" + Operation.INVOKE); + } + + String pathWithoutSMElementPrefix = removeSMElementPrefix(pathWithoutSubmodelPrefix); + + if (isAsyncInvokePath(pathWithoutSMElementPrefix)) { + return invokeAsync(pathWithoutSMElementPrefix, parameters); } else { - if (VABPathTools.isOperationInvokationPath(path)) { - if(path.endsWith(OperationProvider.ASYNC)) { - path = removeSMElementPrefix(path); - path = path.replaceFirst(Pattern.quote(Operation.INVOKE + OperationProvider.ASYNC), ""); - return submodelAPI.invokeAsync(path, parameters); - } else { - path = removeSMElementPrefix(path); - return submodelAPI.invokeOperation(path, parameters); - } - } else { - throw new MalformedRequestException("Given path '" + path + "' does not end in /" + Operation.INVOKE); - } + return invokeSync(pathWithoutSMElementPrefix, parameters); } + + } + + private Object invokeSync(String path, Object... parameters) { + String pathWithoutInvoke = path.replaceFirst(Pattern.quote(Operation.INVOKE), ""); + String strippedPathWithoutInvoke = VABPathTools.stripSlashes(pathWithoutInvoke); + return submodelAPI.invokeOperation(strippedPathWithoutInvoke, parameters); + } + + private Object invokeAsync(String path, Object... parameters) { + String pathWithoutAsyncInvoke = path.replaceFirst(Pattern.quote(Operation.INVOKE + OperationProvider.ASYNC), ""); + String strippedPathWithoutAsyncInvoke = VABPathTools.stripSlashes(pathWithoutAsyncInvoke); + return submodelAPI.invokeAsync(strippedPathWithoutAsyncInvoke, parameters); + } + + private boolean isAsyncInvokePath(String path) { + return path.endsWith(OperationProvider.ASYNC); } public ISubmodelAPI getAPI() { @@ -275,6 +291,6 @@ protected void setAPI(ISubmodelAPI api) { } private String removeSMElementPrefix(String path) { - return path.replaceFirst(MultiSubmodelElementProvider.ELEMENTS, ""); + return path.replaceFirst(MultiSubmodelElementProvider.ELEMENTS, ""); } } diff --git a/src/main/java/org/eclipse/basyx/submodel/restapi/vab/VABSubmodelAPI.java b/src/main/java/org/eclipse/basyx/submodel/restapi/vab/VABSubmodelAPI.java index 96a61479..ae71080e 100644 --- a/src/main/java/org/eclipse/basyx/submodel/restapi/vab/VABSubmodelAPI.java +++ b/src/main/java/org/eclipse/basyx/submodel/restapi/vab/VABSubmodelAPI.java @@ -20,8 +20,8 @@ import org.eclipse.basyx.submodel.metamodel.map.Submodel; import org.eclipse.basyx.submodel.metamodel.map.submodelelement.SubmodelElement; import org.eclipse.basyx.submodel.restapi.MultiSubmodelElementProvider; -import org.eclipse.basyx.submodel.restapi.api.ISubmodelAPI; import org.eclipse.basyx.submodel.restapi.SubmodelAPIHelper; +import org.eclipse.basyx.submodel.restapi.api.ISubmodelAPI; import org.eclipse.basyx.vab.modelprovider.VABElementProxy; import org.eclipse.basyx.vab.modelprovider.api.IModelProvider; @@ -119,13 +119,13 @@ public ISubmodelElement getSubmodelElement(String idShortPath) { @Override public Object invokeOperation(String idShortPath, Object... params) { - return getElementProvider().invokeOperation(SubmodelAPIHelper.getSubmodelElementPath(idShortPath), params); + return getElementProvider().invokeOperation(SubmodelAPIHelper.getSubmodelElementSyncInvokePath(idShortPath), params); } @Override public Object invokeAsync(String idShortPath, Object... params) { - return getElementProvider().invokeOperation(SubmodelAPIHelper.getSubmodelElementInvokePath(idShortPath), params); + return getElementProvider().invokeOperation(SubmodelAPIHelper.getSubmodelElementAsyncInvokePath(idShortPath), params); } @Override