Skip to content

Commit

Permalink
Harmonizes path handling for sync/async invocation (#64)
Browse files Browse the repository at this point in the history
* Harmonizes path handling for sync/async invocation

Signed-off-by: Frank Schnicke <[email protected]>

* Fixes formatting issue

Signed-off-by: Frank Schnicke <[email protected]>
  • Loading branch information
FrankSchnicke authored Mar 21, 2022
1 parent f63f35a commit aac1e64
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -275,6 +291,6 @@ protected void setAPI(ISubmodelAPI api) {
}

private String removeSMElementPrefix(String path) {
return path.replaceFirst(MultiSubmodelElementProvider.ELEMENTS, "");
return path.replaceFirst(MultiSubmodelElementProvider.ELEMENTS, "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit aac1e64

Please sign in to comment.