commit
e66101b6c3
@ -0,0 +1,26 @@
|
|||||||
|
package com.java3y.austin.pojo;
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 3y
|
||||||
|
* @date 2021/11/4
|
||||||
|
* 发送短信参数
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
public class SmsParam {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 需要发送的手机号
|
||||||
|
*/
|
||||||
|
private Set<String> phones;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送文案
|
||||||
|
*/
|
||||||
|
private String content;
|
||||||
|
}
|
@ -0,0 +1,87 @@
|
|||||||
|
package com.java3y.austin.config;
|
||||||
|
|
||||||
|
|
||||||
|
import okhttp3.ConnectionPool;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import javax.net.ssl.*;
|
||||||
|
import java.security.*;
|
||||||
|
import java.security.cert.CertificateException;
|
||||||
|
import java.security.cert.X509Certificate;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 3y
|
||||||
|
* @date 2021/11/4
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class OkHttpConfiguration {
|
||||||
|
|
||||||
|
@Value("${ok.http.connect-timeout}")
|
||||||
|
private Integer connectTimeout;
|
||||||
|
|
||||||
|
@Value("${ok.http.read-timeout}")
|
||||||
|
private Integer readTimeout;
|
||||||
|
|
||||||
|
@Value("${ok.http.write-timeout}")
|
||||||
|
private Integer writeTimeout;
|
||||||
|
|
||||||
|
@Value("${ok.http.max-idle-connections}")
|
||||||
|
private Integer maxIdleConnections;
|
||||||
|
|
||||||
|
@Value("${ok.http.keep-alive-duration}")
|
||||||
|
private Long keepAliveDuration;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public OkHttpClient okHttpClient() {
|
||||||
|
return new OkHttpClient.Builder()
|
||||||
|
.sslSocketFactory(sslSocketFactory(), x509TrustManager())
|
||||||
|
.retryOnConnectionFailure(false)
|
||||||
|
.connectionPool(pool())
|
||||||
|
.connectTimeout(connectTimeout, TimeUnit.SECONDS)
|
||||||
|
.readTimeout(readTimeout, TimeUnit.SECONDS)
|
||||||
|
.writeTimeout(writeTimeout,TimeUnit.SECONDS)
|
||||||
|
.hostnameVerifier((hostname, session) -> true)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public X509TrustManager x509TrustManager() {
|
||||||
|
return new X509TrustManager() {
|
||||||
|
@Override
|
||||||
|
public void checkClientTrusted(X509Certificate[] chain, String authType)
|
||||||
|
throws CertificateException {
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void checkServerTrusted(X509Certificate[] chain, String authType)
|
||||||
|
throws CertificateException {
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public X509Certificate[] getAcceptedIssuers() {
|
||||||
|
return new X509Certificate[0];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public SSLSocketFactory sslSocketFactory() {
|
||||||
|
try {
|
||||||
|
// 信任任何链接
|
||||||
|
SSLContext sslContext = SSLContext.getInstance("TLS");
|
||||||
|
sslContext.init(null, new TrustManager[]{x509TrustManager()}, new SecureRandom());
|
||||||
|
return sslContext.getSocketFactory();
|
||||||
|
} catch (NoSuchAlgorithmException | KeyManagementException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ConnectionPool pool() {
|
||||||
|
return new ConnectionPool(maxIdleConnections, keepAliveDuration, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,195 @@
|
|||||||
|
package com.java3y.austin.utils;
|
||||||
|
|
||||||
|
import cn.hutool.core.map.MapUtil;
|
||||||
|
import com.google.common.base.Throwables;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import okhttp3.*;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 3y
|
||||||
|
* @date 2021/11/4
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class OkHttpUtils {
|
||||||
|
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
|
||||||
|
private static final MediaType XML = MediaType.parse("application/xml; charset=utf-8");
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private OkHttpClient okHttpClient;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get 请求
|
||||||
|
*
|
||||||
|
* @param url 请求url地址
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public String doGet(String url) {
|
||||||
|
return doGet(url, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get 请求
|
||||||
|
*
|
||||||
|
* @param url 请求url地址
|
||||||
|
* @param params 请求参数 map
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public String doGet(String url, Map<String, String> params) {
|
||||||
|
return doGet(url, params, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get 请求
|
||||||
|
*
|
||||||
|
* @param url 请求url地址
|
||||||
|
* @param headers 请求头字段 {k1, v1 k2, v2, ...}
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public String doGetWithHeaders(String url, Map<String,String> headers) {
|
||||||
|
return doGet(url, null, headers);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get 请求
|
||||||
|
*
|
||||||
|
* @param url 请求url地址
|
||||||
|
* @param params 请求参数 map
|
||||||
|
* @param headers 请求头字段 {k1, v1 k2, v2, ...}
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public String doGet(String url, Map<String, String> params, Map<String,String> headers) {
|
||||||
|
StringBuilder sb = new StringBuilder(url);
|
||||||
|
if (params != null && params.keySet().size() > 0) {
|
||||||
|
boolean firstFlag = true;
|
||||||
|
for (String key : params.keySet()) {
|
||||||
|
if (firstFlag) {
|
||||||
|
sb.append("?").append(key).append("=").append(params.get(key));
|
||||||
|
firstFlag = false;
|
||||||
|
} else {
|
||||||
|
sb.append("&").append(key).append("=").append(params.get(key));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Request.Builder builder = getBuilderWithHeaders(headers);
|
||||||
|
Request request = builder.url(sb.toString()).build();
|
||||||
|
|
||||||
|
log.info("do get request and url[{}]", sb.toString());
|
||||||
|
return execute(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* post 请求
|
||||||
|
*
|
||||||
|
* @param url 请求url地址
|
||||||
|
* @param params 请求参数 map
|
||||||
|
* @param headers 请求头字段 {k1, v1 k2, v2, ...}
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public String doPost(String url, Map<String, String> params, Map<String,String> headers) {
|
||||||
|
FormBody.Builder formBuilder = new FormBody.Builder();
|
||||||
|
|
||||||
|
if (params != null && params.keySet().size() > 0) {
|
||||||
|
for (String key : params.keySet()) {
|
||||||
|
formBuilder.add(key, params.get(key));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Request.Builder builder = getBuilderWithHeaders(headers);
|
||||||
|
|
||||||
|
Request request = builder.url(url).post(formBuilder.build()).build();
|
||||||
|
log.info("do post request and url[{}]", url);
|
||||||
|
|
||||||
|
return execute(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取request Builder
|
||||||
|
*
|
||||||
|
* @param headers 请求头字段 {k1, v1 k2, v2, ...}
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private Request.Builder getBuilderWithHeaders(Map<String,String> headers) {
|
||||||
|
Request.Builder builder = new Request.Builder();
|
||||||
|
if (!MapUtil.isEmpty(headers)) {
|
||||||
|
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
||||||
|
builder.addHeader(entry.getKey(), entry.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* post 请求, 请求数据为 json 的字符串
|
||||||
|
*
|
||||||
|
* @param url 请求url地址
|
||||||
|
* @param json 请求数据, json 字符串
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public String doPostJson(String url, String json) {
|
||||||
|
log.info("do post request and url[{}]", url);
|
||||||
|
return executePost(url, json, JSON, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* post 请求, 请求数据为 json 的字符串
|
||||||
|
*
|
||||||
|
* @param url 请求url地址
|
||||||
|
* @param json 请求数据, json 字符串
|
||||||
|
* @param headers 请求头字段 {k1, v1 k2, v2, ...}
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public String doPostJsonWithHeaders(String url, String json, Map<String, String> headers) {
|
||||||
|
log.info("do post request and url[{}]", url);
|
||||||
|
return executePost(url, json, JSON, headers);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* post 请求, 请求数据为 xml 的字符串
|
||||||
|
*
|
||||||
|
* @param url 请求url地址
|
||||||
|
* @param xml 请求数据, xml 字符串
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public String doPostXml(String url, String xml) {
|
||||||
|
log.info("do post request and url[{}]", url);
|
||||||
|
return executePost(url, xml, XML, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private String executePost(String url, String data, MediaType contentType, Map<String,String> headers) {
|
||||||
|
RequestBody requestBody = RequestBody.create(data.getBytes(StandardCharsets.UTF_8), contentType);
|
||||||
|
Request.Builder builder = getBuilderWithHeaders(headers);
|
||||||
|
Request request = builder.url(url).post(requestBody).build();
|
||||||
|
|
||||||
|
return execute(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String execute(Request request) {
|
||||||
|
Response response = null;
|
||||||
|
try {
|
||||||
|
response = okHttpClient.newCall(request).execute();
|
||||||
|
if (response.isSuccessful()) {
|
||||||
|
return response.body().string();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(Throwables.getStackTraceAsString(e));
|
||||||
|
} finally {
|
||||||
|
if (response != null) {
|
||||||
|
response.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,34 +1,37 @@
|
|||||||
package com.java3y.austin;
|
package com.java3y.austin;
|
||||||
|
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import com.java3y.austin.pojo.SmsParam;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.java3y.austin.script.TencentSmsScript;
|
||||||
import lombok.Data;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashSet;
|
||||||
|
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@RestController
|
@RestController
|
||||||
public class AustinApplication {
|
public class AustinApplication {
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(AustinApplication.class);
|
@Autowired
|
||||||
|
private TencentSmsScript tencentSmsScript;
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(AustinApplication.class, args);
|
SpringApplication.run(AustinApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/hello")
|
@GetMapping("/hello")
|
||||||
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
|
public String hello() {
|
||||||
|
|
||||||
|
SmsParam smsParam = SmsParam.builder()
|
||||||
|
.phones(new HashSet<>(Arrays.asList("//TODO PHONE ")))
|
||||||
|
.content("3333")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return tencentSmsScript.send(smsParam);
|
||||||
|
|
||||||
logger.error("error logback for austin");
|
|
||||||
logger.info("info logback for austin");
|
|
||||||
return String.format("Hello %s!", name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,6 @@
|
|||||||
|
# ok http配置信息 TODO
|
||||||
|
ok.http.connect-timeout=30
|
||||||
|
ok.http.read-timeout=30
|
||||||
|
ok.http.write-timeout=30
|
||||||
|
ok.http.max-idle-connections=200
|
||||||
|
ok.http.keep-alive-duration=300
|
Loading…
Reference in new issue