|
| 1 | +/* |
| 2 | + * Copyright 2020 Conductor Authors. |
| 3 | + * <p> |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * <p> |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * <p> |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | +package com.netflix.conductor.client.http; |
| 14 | + |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +import org.apache.commons.lang3.Validate; |
| 18 | + |
| 19 | +import com.netflix.conductor.client.http.ConductorClientRequest.Method; |
| 20 | +import com.netflix.conductor.common.metadata.workflow.SharedResourceModel; |
| 21 | + |
| 22 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 23 | + |
| 24 | +/** |
| 25 | + * Client for resource sharing operations in Conductor |
| 26 | + */ |
| 27 | +public final class ResourceSharingClient { |
| 28 | + |
| 29 | + private ConductorClient client; |
| 30 | + |
| 31 | + /** Creates a default resource sharing client */ |
| 32 | + public ResourceSharingClient() { |
| 33 | + } |
| 34 | + |
| 35 | + public ResourceSharingClient(ConductorClient client) { |
| 36 | + this.client = client; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Kept only for backwards compatibility |
| 41 | + * |
| 42 | + * @param rootUri basePath for the ApiClient |
| 43 | + */ |
| 44 | + @Deprecated |
| 45 | + public void setRootURI(String rootUri) { |
| 46 | + if (client != null) { |
| 47 | + client.shutdown(); |
| 48 | + } |
| 49 | + client = new ConductorClient(rootUri); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Share a resource with another user or group |
| 54 | + * |
| 55 | + * @param resourceType Type of resource to share (e.g., "WORKFLOW", "TASK") |
| 56 | + * @param resourceName Name of the resource to share |
| 57 | + * @param sharedWith User or group to share the resource with |
| 58 | + */ |
| 59 | + public void shareResource(String resourceType, String resourceName, String sharedWith) { |
| 60 | + Validate.notBlank(resourceType, "ResourceType cannot be blank"); |
| 61 | + Validate.notBlank(resourceName, "ResourceName cannot be blank"); |
| 62 | + Validate.notBlank(sharedWith, "SharedWith cannot be blank"); |
| 63 | + |
| 64 | + ConductorClientRequest request = ConductorClientRequest.builder() |
| 65 | + .method(Method.POST) |
| 66 | + .path("/share/shareResource") |
| 67 | + .addQueryParam("resourceType", resourceType) |
| 68 | + .addQueryParam("resourceName", resourceName) |
| 69 | + .addQueryParam("sharedWith", sharedWith) |
| 70 | + .build(); |
| 71 | + |
| 72 | + client.execute(request); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Remove sharing of a resource with a user or group |
| 77 | + * |
| 78 | + * @param resourceType Type of resource to unshare |
| 79 | + * @param resourceName Name of the resource to unshare |
| 80 | + * @param sharedWith User or group to remove sharing from |
| 81 | + */ |
| 82 | + public void removeSharingResource(String resourceType, String resourceName, String sharedWith) { |
| 83 | + Validate.notBlank(resourceType, "ResourceType cannot be blank"); |
| 84 | + Validate.notBlank(resourceName, "ResourceName cannot be blank"); |
| 85 | + Validate.notBlank(sharedWith, "SharedWith cannot be blank"); |
| 86 | + |
| 87 | + ConductorClientRequest request = ConductorClientRequest.builder() |
| 88 | + .method(Method.DELETE) |
| 89 | + .path("/share/removeSharingResource") |
| 90 | + .addQueryParam("resourceType", resourceType) |
| 91 | + .addQueryParam("resourceName", resourceName) |
| 92 | + .addQueryParam("sharedWith", sharedWith) |
| 93 | + .build(); |
| 94 | + |
| 95 | + client.execute(request); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Get all shared resources accessible to the current user |
| 100 | + * |
| 101 | + * @return List of shared resources |
| 102 | + */ |
| 103 | + public List<SharedResourceModel> getSharedResources() { |
| 104 | + ConductorClientRequest request = ConductorClientRequest.builder() |
| 105 | + .method(Method.GET) |
| 106 | + .path("/share/getSharedResources") |
| 107 | + .build(); |
| 108 | + |
| 109 | + ConductorClientResponse<List<SharedResourceModel>> resp = client.execute(request, new TypeReference<>() { |
| 110 | + }); |
| 111 | + |
| 112 | + return resp.getData(); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Get shared resources filtered by resource type |
| 117 | + * |
| 118 | + * @param resourceType Type of resource to filter by |
| 119 | + * @return List of shared resources of the specified type |
| 120 | + */ |
| 121 | + public List<SharedResourceModel> getSharedResources(String resourceType) { |
| 122 | + Validate.notBlank(resourceType, "ResourceType cannot be blank"); |
| 123 | + |
| 124 | + ConductorClientRequest request = ConductorClientRequest.builder() |
| 125 | + .method(Method.GET) |
| 126 | + .path("/share/getSharedResources") |
| 127 | + .addQueryParam("resourceType", resourceType) |
| 128 | + .build(); |
| 129 | + |
| 130 | + ConductorClientResponse<List<SharedResourceModel>> resp = client.execute(request, new TypeReference<>() { |
| 131 | + }); |
| 132 | + |
| 133 | + return resp.getData(); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Check if a specific resource is shared with a user or group |
| 138 | + * |
| 139 | + * @param resourceType Type of resource |
| 140 | + * @param resourceName Name of the resource |
| 141 | + * @param sharedWith User or group to check |
| 142 | + * @return true if the resource is shared, false otherwise |
| 143 | + */ |
| 144 | + public boolean isResourceShared(String resourceType, String resourceName, String sharedWith) { |
| 145 | + List<SharedResourceModel> sharedResources = getSharedResources(resourceType); |
| 146 | + return sharedResources.stream() |
| 147 | + .anyMatch(resource -> |
| 148 | + resource.getResourceName().equals(resourceName) && |
| 149 | + resource.getSharedWith().equals(sharedWith)); |
| 150 | + } |
| 151 | +} |
0 commit comments