From 043630500dc036482ce2671e8019dbdb041a7920 Mon Sep 17 00:00:00 2001 From: willhhan Date: Fri, 27 Oct 2023 11:55:29 +0800 Subject: [PATCH 1/2] add java template code --- README.en.md | 36 ---------- README.md | 39 ----------- pom.xml | 20 ++++++ src/main/java/Main.java | 144 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 164 insertions(+), 75 deletions(-) delete mode 100644 README.en.md delete mode 100644 README.md create mode 100644 pom.xml create mode 100644 src/main/java/Main.java diff --git a/README.en.md b/README.en.md deleted file mode 100644 index c956d46..0000000 --- a/README.en.md +++ /dev/null @@ -1,36 +0,0 @@ -# nonsdk-java-sample - -#### Description -{**When you're done, you can delete the content in this README and update the file with details for others getting started with your repository**} - -#### Software Architecture -Software architecture description - -#### Installation - -1. xxxx -2. xxxx -3. xxxx - -#### Instructions - -1. xxxx -2. xxxx -3. xxxx - -#### Contribution - -1. Fork the repository -2. Create Feat_xxx branch -3. Commit your code -4. Create Pull Request - - -#### Gitee Feature - -1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md -2. Gitee blog [blog.gitee.com](https://blog.gitee.com) -3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore) -4. The most valuable open source project [GVP](https://gitee.com/gvp) -5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help) -6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) diff --git a/README.md b/README.md deleted file mode 100644 index f4cf5bb..0000000 --- a/README.md +++ /dev/null @@ -1,39 +0,0 @@ -# nonsdk-java-sample - -#### 介绍 -{**以下是 Gitee 平台说明,您可以替换此简介** -Gitee 是 OSCHINA 推出的基于 Git 的代码托管平台(同时支持 SVN)。专为开发者提供稳定、高效、安全的云端软件开发协作平台 -无论是个人、团队、或是企业,都能够用 Gitee 实现代码托管、项目管理、协作开发。企业项目请看 [https://gitee.com/enterprises](https://gitee.com/enterprises)} - -#### 软件架构 -软件架构说明 - - -#### 安装教程 - -1. xxxx -2. xxxx -3. xxxx - -#### 使用说明 - -1. xxxx -2. xxxx -3. xxxx - -#### 参与贡献 - -1. Fork 本仓库 -2. 新建 Feat_xxx 分支 -3. 提交代码 -4. 新建 Pull Request - - -#### 特技 - -1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md -2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) -3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 -4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 -5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) -6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..a9c5c53 --- /dev/null +++ b/pom.xml @@ -0,0 +1,20 @@ + + + 4.0.0 + + com.example + sdk-demo + 1.0.0-SNAPSHOT + SDK Demo Project + + + + com.squareup.okhttp3 + okhttp + 3.12.13 + + + + \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 0000000..50133fa --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,144 @@ +import okhttp3.*; + +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.security.InvalidKeyException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +public class Main { + public static void main(String[] args) throws IOException, NoSuchAlgorithmException, InvalidKeyException { + String secretId = "$secretId"; + String secretKey = "$secretKey"; + String service = "%(product_name)s"; + String version = "%(product_version)s"; + String action = "%(product_action)s"; + String body = $json; + String region = "$region"; + String resp = doRequest(secretId, secretKey, service, version, action, body, region); + + System.out.println(resp); + } + + // singleton client for connection reuse and better performance + private static final OkHttpClient client = new OkHttpClient(); + + public static String doRequest( + String secretId, String secretKey, + String service, String version, String action, + String body, String region + ) throws IOException, NoSuchAlgorithmException, InvalidKeyException { + + Request request = buildRequest(secretId, secretKey, service, version, action, body, region); + + System.out.println(request.method() + " " + request.url()); + System.out.println(request.headers()); + System.out.println(body); + + Response response = client.newCall(request).execute(); + return response.body().string(); + } + + public static Request buildRequest( + String secretId, String secretKey, + String service, String version, String action, + String body, String region + ) throws NoSuchAlgorithmException, InvalidKeyException { + String host = "$url"; + String endpoint = "https://" + host; + String contentType = "application/json; charset=utf-8"; + String timestamp = String.valueOf(System.currentTimeMillis() / 1000); + String auth = getAuth(secretId, secretKey, host, contentType, timestamp, body); + return new Request.Builder() + .header("Host", host) + .header("X-TC-Timestamp", timestamp) + .header("X-TC-Version", version) + .header("X-TC-Action", action) + .header("X-TC-Region", region) + .header("X-TC-RequestClient", "SDK_JAVA_BAREBONE") + .header("Authorization", auth) + .url(endpoint) + .post(RequestBody.create(MediaType.parse(contentType), body)) + .build(); + } + + private static String getAuth( + String secretId, String secretKey, String host, String contentType, + String timestamp, String body + ) throws NoSuchAlgorithmException, InvalidKeyException { + String canonicalUri = "/"; + String canonicalQueryString = ""; + String canonicalHeaders = "content-type:" + contentType + "\nhost:" + host + "\n"; + String signedHeaders = "content-type;host"; + + String hashedRequestPayload = sha256Hex(body.getBytes(StandardCharsets.UTF_8)); + String canonicalRequest = "POST" + + "\n" + + canonicalUri + + "\n" + + canonicalQueryString + + "\n" + + canonicalHeaders + + "\n" + + signedHeaders + + "\n" + + hashedRequestPayload; + + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + sdf.setTimeZone(TimeZone.getTimeZone("UTC")); + String date = sdf.format(new Date(Long.valueOf(timestamp + "000"))); + String service = host.split("\\.")[0]; + String credentialScope = date + "/" + service + "/" + "tc3_request"; + String hashedCanonicalRequest = + sha256Hex(canonicalRequest.getBytes(StandardCharsets.UTF_8)); + String stringToSign = + "TC3-HMAC-SHA256\n" + timestamp + "\n" + credentialScope + "\n" + hashedCanonicalRequest; + + byte[] secretDate = hmac256(("TC3" + secretKey).getBytes(StandardCharsets.UTF_8), date); + byte[] secretService = hmac256(secretDate, service); + byte[] secretSigning = hmac256(secretService, "tc3_request"); + String signature = + printHexBinary(hmac256(secretSigning, stringToSign)).toLowerCase(); + return "TC3-HMAC-SHA256 " + + "Credential=" + + secretId + + "/" + + credentialScope + + ", " + + "SignedHeaders=" + + signedHeaders + + ", " + + "Signature=" + + signature; + } + + public static String sha256Hex(byte[] b) throws NoSuchAlgorithmException { + MessageDigest md; + md = MessageDigest.getInstance("SHA-256"); + byte[] d = md.digest(b); + return printHexBinary(d).toLowerCase(); + } + + private static final char[] hexCode = "0123456789ABCDEF".toCharArray(); + + public static String printHexBinary(byte[] data) { + StringBuilder r = new StringBuilder(data.length * 2); + for (byte b : data) { + r.append(hexCode[(b >> 4) & 0xF]); + r.append(hexCode[(b & 0xF)]); + } + return r.toString(); + } + + public static byte[] hmac256(byte[] key, String msg) throws NoSuchAlgorithmException, InvalidKeyException { + Mac mac = Mac.getInstance("HmacSHA256"); + SecretKeySpec secretKeySpec = new SecretKeySpec(key, mac.getAlgorithm()); + mac.init(secretKeySpec); + return mac.doFinal(msg.getBytes(StandardCharsets.UTF_8)); + } +} \ No newline at end of file -- Gitee From 367803dcf6752f16af9d2cd53f26a8012ee5003e Mon Sep 17 00:00:00 2001 From: sesky4 <13594241+sesky4@user.noreply.gitee.com> Date: Mon, 11 Dec 2023 02:53:43 +0000 Subject: [PATCH 2/2] update pom.xml. Signed-off-by: sesky4 <13594241+sesky4@user.noreply.gitee.com> --- pom.xml | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index a9c5c53..943a827 100644 --- a/pom.xml +++ b/pom.xml @@ -5,9 +5,9 @@ 4.0.0 com.example - sdk-demo - 1.0.0-SNAPSHOT - SDK Demo Project + java-nonsdk + 1.0.0 + Non SDK Demo Project @@ -17,4 +17,19 @@ + + + + org.apache.maven.plugins + maven-assembly-plugin + 2.6 + + + jar-with-dependencies + + + + + + \ No newline at end of file -- Gitee