|
1 | | -/* |
2 | | - Copyright (c) 2022 Skyflow, Inc. |
3 | | -*/ |
4 | | -package com.example; |
| 1 | +package com.skyflow; |
5 | 2 |
|
6 | | -import com.skyflow.entities.RedactionType; |
7 | | -import com.skyflow.entities.ResponseToken; |
8 | | -import com.skyflow.entities.SkyflowConfiguration; |
9 | | -import com.skyflow.entities.TokenProvider; |
10 | | -import com.skyflow.errors.SkyflowException; |
11 | | -import com.skyflow.serviceaccount.util.Token; |
12 | | -import com.skyflow.vault.Skyflow; |
13 | | -import org.json.simple.JSONArray; |
14 | | -import org.json.simple.JSONObject; |
| 3 | +// Required imports for Skyflow API and HTTP client functionality |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.List; |
15 | 6 |
|
| 7 | +import com.skyflow.api.ApiClient; |
| 8 | +import com.skyflow.api.resources.flowservice.requests.V1DetokenizeRequest; |
| 9 | +import com.skyflow.api.types.V1DetokenizeResponse; |
| 10 | +import com.skyflow.api.types.V1TokenGroupRedactions; |
16 | 11 |
|
17 | | -public class DetokenizeExample { |
| 12 | +import okhttp3.OkHttpClient; |
| 13 | +import okhttp3.Request; |
18 | 14 |
|
| 15 | +/** |
| 16 | + * Example demonstrating detokenization operations using Skyflow API |
| 17 | + */ |
| 18 | +public class DetokenizeSample { |
19 | 19 | public static void main(String[] args) { |
20 | | - |
21 | | - try { |
22 | | - SkyflowConfiguration config = new SkyflowConfiguration( |
23 | | - "<your_vaultID>", |
24 | | - "<your_vaultURL>", |
25 | | - new DemoTokenProvider() |
26 | | - ); |
27 | | - Skyflow skyflowClient = Skyflow.init(config); |
28 | | - JSONObject records = new JSONObject(); |
29 | | - JSONArray recordsArray = new JSONArray(); |
30 | | - |
31 | | - JSONObject record1 = new JSONObject(); |
32 | | - record1.put("token", "<your_token>"); |
33 | | - record1.put("redaction", RedactionType.MASKED.toString()); |
34 | | - |
35 | | - JSONObject record2 = new JSONObject(); |
36 | | - record2.put("token", "<your_token>"); // default Redaction "PLAIN_TEXT" will be applied for record2 |
37 | | - |
38 | | - recordsArray.add(record1); |
39 | | - recordsArray.add(record2); |
40 | | - records.put("records", recordsArray); |
41 | | - |
42 | | - JSONObject response = skyflowClient.detokenize(records); |
43 | | - System.out.println(response); |
44 | | - } catch (SkyflowException e) { |
45 | | - e.printStackTrace(); |
46 | | - System.out.println(e.getData()); |
47 | | - } |
48 | | - |
49 | | - } |
50 | | - |
51 | | - static class DemoTokenProvider implements TokenProvider { |
52 | | - |
53 | | - private String bearerToken = null; |
54 | | - |
55 | | - @Override |
56 | | - public String getBearerToken() throws Exception { |
57 | | - ResponseToken response = null; |
58 | | - try { |
59 | | - String filePath = "<YOUR_CREDENTIALS_FILE_PATH>"; |
60 | | - if(Token.isExpired(bearerToken)) { |
61 | | - response = Token.generateBearerToken(filePath); |
62 | | - bearerToken = response.getAccessToken(); |
63 | | - } |
64 | | - } catch (SkyflowException e) { |
65 | | - e.printStackTrace(); |
66 | | - } |
67 | | - |
68 | | - return bearerToken; |
69 | | - } |
| 20 | + // Initialize HTTP client with Bearer token authentication for API requests |
| 21 | + // This client will automatically add the Bearer token to all requests |
| 22 | + OkHttpClient authClient = new OkHttpClient.Builder() |
| 23 | + .addInterceptor(chain -> { |
| 24 | + Request original = chain.request(); |
| 25 | + Request requestWithAuth = original.newBuilder() |
| 26 | + .header("Authorization", "Bearer " + "<BEARER_TOKEN>") |
| 27 | + .build(); |
| 28 | + return chain.proceed(requestWithAuth); |
| 29 | + }) |
| 30 | + .build(); |
| 31 | + |
| 32 | + // Create Skyflow API client with vault URL and auth client |
| 33 | + ApiClient client = ApiClient.builder().url("<VAULT_URL>").httpClient(authClient).build(); |
| 34 | + |
| 35 | + // Create a list to store tokens that need to be detokenized |
| 36 | + List<String> tokens = new ArrayList<>(); |
| 37 | + tokens.add("<TOKEN>"); |
| 38 | + |
| 39 | + // Configure redaction settings for token groups |
| 40 | + List<V1TokenGroupRedactions> red = new ArrayList<>(); |
| 41 | + // Create redaction configuration for specific token group |
| 42 | + V1TokenGroupRedactions r = V1TokenGroupRedactions.builder() |
| 43 | + .redaction("<REDACTION_TYPE>") |
| 44 | + .tokenGroupName("<TOKEN_GROUP_NAME>") |
| 45 | + .build(); |
| 46 | + red.add(r); |
| 47 | + |
| 48 | + // Build the detokenize request with vault ID, tokens and redaction settings |
| 49 | + V1DetokenizeRequest req = V1DetokenizeRequest.builder() |
| 50 | + .vaultId("<VAULT_ID>") |
| 51 | + .tokens(tokens) |
| 52 | + .tokenGroupRedactions(red) |
| 53 | + .build(); |
| 54 | + |
| 55 | + // Execute the detokenize operation and get the response |
| 56 | + V1DetokenizeResponse res = client.flowservice().detokenize(req); |
| 57 | + |
| 58 | + // Print the operation result |
| 59 | + System.out.println("response is" + res); |
70 | 60 | } |
71 | 61 | } |
0 commit comments