|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 T-Systems International GmbH. |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | +package com.otc.sdk.samples.services.vpv; |
| 16 | + |
| 17 | +import org.apache.http.HttpEntity; |
| 18 | +import org.apache.http.HttpResponse; |
| 19 | +import org.apache.http.client.methods.HttpRequestBase; |
| 20 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 21 | +import org.apache.http.util.EntityUtils; |
| 22 | +import org.slf4j.Logger; |
| 23 | +import org.slf4j.LoggerFactory; |
| 24 | + |
| 25 | +import com.google.gson.JsonArray; |
| 26 | +import com.google.gson.JsonElement; |
| 27 | +import com.google.gson.JsonObject; |
| 28 | +import com.google.gson.JsonParser; |
| 29 | +import com.otc.sdk.core.http.HttpMethodName; |
| 30 | +import com.otc.sdk.core.util.Constant; |
| 31 | +import com.otc.sdk.core.util.HostName; |
| 32 | +import com.otc.sdk.core.util.SSLCipherSuiteUtil; |
| 33 | +import com.otc.sdk.service.Client; |
| 34 | +import com.otc.sdk.service.Request; |
| 35 | + |
| 36 | +/** |
| 37 | + * HttpClientListVPC class demonstrates how to list Virtual Private Cloud (VPC) resources using |
| 38 | + * the OTC SDK. |
| 39 | + * It shows how to create a request, sign it, and execute it using an HTTP client. |
| 40 | + * The response body is printed to the console. |
| 41 | + */ |
| 42 | +public class HttpClientListVPC { |
| 43 | + private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientListVPC.class); |
| 44 | + |
| 45 | + public static void main(String[] args) throws Exception { |
| 46 | + // Create a new request. |
| 47 | + Request httpClientRequest = new Request(); |
| 48 | + try { |
| 49 | + // Set the request parameters. |
| 50 | + |
| 51 | + String ak = System.getenv("OTC_SDK_AK"); |
| 52 | + String sk = System.getenv("OTC_SDK_SK"); |
| 53 | + String projectId = System.getenv("OTC_SDK_PROJECTID"); |
| 54 | + |
| 55 | + httpClientRequest.setKey(ak); |
| 56 | + httpClientRequest.setSecret(sk); |
| 57 | + httpClientRequest.setMethod(HttpMethodName.GET.toString()); |
| 58 | + |
| 59 | + String url = String.format("https://vpc.eu-de.otc.t-systems.com/v1/%s/vpcs", projectId); |
| 60 | + |
| 61 | + httpClientRequest.setUrl(url); |
| 62 | + httpClientRequest.addHeader("Content-type", "application/json;charset=utf8"); |
| 63 | + |
| 64 | + httpClientRequest.addHeader("X-Project-Id", projectId); |
| 65 | + |
| 66 | + httpClientRequest.setBody(null); |
| 67 | + |
| 68 | + } catch (Exception e) { |
| 69 | + LOGGER.error(e.getMessage()); |
| 70 | + return; |
| 71 | + } |
| 72 | + CloseableHttpClient client = null; |
| 73 | + try { |
| 74 | + // Sign the request. |
| 75 | + HttpRequestBase signedRequest = Client.sign(httpClientRequest, Constant.SIGNATURE_ALGORITHM_SDK_HMAC_SHA256); |
| 76 | + |
| 77 | + if (Constant.DO_VERIFY) { |
| 78 | + // create httpClient and verify ssl certificate |
| 79 | + HostName.setUrlHostName(httpClientRequest.getHost()); |
| 80 | + client = (CloseableHttpClient) SSLCipherSuiteUtil.createHttpClientWithVerify(Constant.INTERNATIONAL_PROTOCOL); |
| 81 | + } else { |
| 82 | + // create httpClient and do not verify ssl certificate |
| 83 | + client = (CloseableHttpClient) SSLCipherSuiteUtil.createHttpClient(Constant.INTERNATIONAL_PROTOCOL); |
| 84 | + } |
| 85 | + |
| 86 | + HttpResponse response = client.execute(signedRequest); |
| 87 | + // Print the body of the response. |
| 88 | + HttpEntity resEntity = response.getEntity(); |
| 89 | + if (resEntity != null) { |
| 90 | + String jsonString = EntityUtils.toString(resEntity, "UTF-8"); |
| 91 | + |
| 92 | + LOGGER.info("json: " + jsonString); |
| 93 | + |
| 94 | + JsonObject obj = JsonParser.parseString(jsonString).getAsJsonObject(); |
| 95 | + |
| 96 | + LOGGER.info("vpc count: " + obj.get("vpcs").getAsJsonArray().size()); |
| 97 | + |
| 98 | + JsonArray vpcs = obj.get("vpcs").getAsJsonArray(); |
| 99 | + for (JsonElement jsonElement : vpcs) { |
| 100 | + LOGGER.info(jsonElement.toString()); |
| 101 | + } |
| 102 | + } |
| 103 | + } catch (Exception e) { |
| 104 | + LOGGER.error(e.getMessage()); |
| 105 | + } finally { |
| 106 | + if (client != null) { |
| 107 | + client.close(); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments