Skip to content

Commit

Permalink
Adds plugin specific exception class
Browse files Browse the repository at this point in the history
Signed-off-by: Darshit Chanpura <[email protected]>
  • Loading branch information
DarshitChanpura committed Dec 7, 2024
1 parent acc22c4 commit 4dc7597
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.opensearch.sample.actions.access.share.ShareResourceAction;
import org.opensearch.sample.actions.access.share.ShareResourceRequest;
import org.opensearch.sample.actions.access.share.ShareResourceResponse;
import org.opensearch.sample.utils.SampleResourcePluginException;
import org.opensearch.tasks.Task;
import org.opensearch.transport.TransportService;

Expand All @@ -36,26 +37,24 @@ public ShareResourceTransportAction(TransportService transportService, ActionFil

@Override
protected void doExecute(Task task, ShareResourceRequest request, ActionListener<ShareResourceResponse> listener) {
ResourceSharing sharing = null;
try {
shareResource(request);
sharing = shareResource(request);
if (sharing == null) {
log.error("Failed to share resource {}", request.getResourceId());
SampleResourcePluginException se = new SampleResourcePluginException("Failed to share resource " + request.getResourceId());
listener.onFailure(se);
return;
}
log.info("Shared resource : {} with {}", request.getResourceId(), sharing.toString());
listener.onResponse(new ShareResourceResponse("Resource " + request.getResourceId() + " shared successfully."));
} catch (Exception e) {
listener.onFailure(e);
}
}

private void shareResource(ShareResourceRequest request) throws Exception {
try {
ResourceService rs = SampleResourcePlugin.GuiceHolder.getResourceService();
ResourceSharing sharing = rs.getResourceAccessControlPlugin()
.shareWith(request.getResourceId(), RESOURCE_INDEX_NAME, request.getShareWith());
if (sharing == null) {
throw new Exception("Failed to share resource " + request.getResourceId());
}
log.info("Shared resource : {} with {}", request.getResourceId(), sharing.toString());
} catch (Exception e) {
log.info("Failed to share resource {}", request.getResourceId(), e);
throw e;
}
private ResourceSharing shareResource(ShareResourceRequest request) throws Exception {
ResourceService rs = SampleResourcePlugin.GuiceHolder.getResourceService();
return rs.getResourceAccessControlPlugin().shareWith(request.getResourceId(), RESOURCE_INDEX_NAME, request.getShareWith());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.sample.utils;

import org.opensearch.OpenSearchException;

public class SampleResourcePluginException extends OpenSearchException {
public SampleResourcePluginException(String msg, Object... args) {
super(msg, args);
}
}

0 comments on commit 4dc7597

Please sign in to comment.