|
| 1 | +import java.io.IOException; |
| 2 | +import java.time.Duration; |
| 3 | +import java.net.HttpURLConnection; |
| 4 | +import java.net.URI; |
| 5 | + |
| 6 | +import java.net.http.HttpClient; |
| 7 | +import java.net.http.HttpRequest; |
| 8 | +import java.net.http.HttpResponse; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Path; |
| 11 | + |
| 12 | +public class DecisionServiceExecution { |
| 13 | + |
| 14 | + public static void main(String[] args) { |
| 15 | + |
| 16 | + String endpointURI = "https://<DECISION_SERVER_RUNTIME_ROUTE>/DecisionService/rest/v1/production_deployment/1.0/loan_validation_production/1.0"; |
| 17 | + |
| 18 | + Path payloadFilePath = Path.of("./payload.json"); |
| 19 | + |
| 20 | + System.setProperty("javax.net.ssl.trustStore", "./server-truststore.p12"); |
| 21 | + System.setProperty("javax.net.ssl.trustStorePassword", "<TRUSTSTORE-PASSWORD>"); |
| 22 | + System.setProperty("javax.net.ssl.trustStoreType", "PKCS12"); |
| 23 | + |
| 24 | + System.setProperty("javax.net.debug","ssl:handshake"); |
| 25 | + |
| 26 | + System.setProperty("javax.net.ssl.keyStore", "./client-keystore.p12"); |
| 27 | + System.setProperty("javax.net.ssl.keyStorePassword", "<KEYSTORE-PASSWORD>"); |
| 28 | + System.setProperty("javax.net.ssl.keyStoreType", "PKCS12"); |
| 29 | + |
| 30 | + |
| 31 | + // Create client |
| 32 | + HttpClient client = HttpClient.newBuilder() |
| 33 | + .version(HttpClient.Version.HTTP_2) |
| 34 | + .connectTimeout(Duration.ofSeconds(10)) |
| 35 | + .build(); |
| 36 | + |
| 37 | + try { |
| 38 | + |
| 39 | + // POST request with JSON |
| 40 | + HttpRequest postRequest = null; |
| 41 | + try { |
| 42 | + postRequest = HttpRequest.newBuilder() |
| 43 | + .uri(URI.create(endpointURI)) |
| 44 | + .header("Content-Type", "application/json") |
| 45 | + .header("Authorization", "Basic b2RtQWRtaW46b2RtQWRtaW4=") // Can be commented if executed without authorization |
| 46 | + .POST(HttpRequest.BodyPublishers.ofString(Files.readString(payloadFilePath))) |
| 47 | + .build(); |
| 48 | + } catch (IOException e) { |
| 49 | + // TODO Auto-generated catch block |
| 50 | + e.printStackTrace(); |
| 51 | + } |
| 52 | + |
| 53 | + // Send request and get response |
| 54 | + HttpResponse<String> postResponse; |
| 55 | + try { |
| 56 | + postResponse = client.send(postRequest, HttpResponse.BodyHandlers.ofString()); |
| 57 | + if (postResponse.statusCode() != HttpURLConnection.HTTP_OK) { |
| 58 | + System.err.println("Error with status Code: " + postResponse.statusCode()); |
| 59 | + } else { |
| 60 | + System.out.println(postResponse.body()); |
| 61 | + } |
| 62 | + } catch (IOException e) { |
| 63 | + // TODO Auto-generated catch block |
| 64 | + e.printStackTrace(); |
| 65 | + } catch (InterruptedException e) { |
| 66 | + // TODO Auto-generated catch block |
| 67 | + e.printStackTrace(); |
| 68 | + } |
| 69 | + |
| 70 | + } finally { |
| 71 | + client.close(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments