Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

补充http插件全部请求类型的处理 #145

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ public class InvokeType implements java.io.Serializable {

public static InvokeType CAFFEINE_CACHE = new InvokeType("caffeine-cache");

public static InvokeType COUCH_BASE = new InvokeType("couchbase");

public static InvokeType THRIFT = new InvokeType("thrift");

public static InvokeType HIKV = new InvokeType("hikv");

public static InvokeType MONGO = new InvokeType("mongo");

private String name;

public InvokeType(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,17 @@ public static Resp invoke(String url,
case POST:
return invokePost(url, headers, paramsMap, body, 0);
case PUT:
return invokePut(url, headers, paramsMap, body, 0);
case HEAD:
return invokeHead(url, headers, paramsMap, 0);
case PATCH:
return invokePatch(url, headers, paramsMap, body, 0);
case DELETE:
return invokeDelete(url, headers, paramsMap, body, 0);
case OPTIONS:
return invokeOptions(url, headers, paramsMap, 0);
case TRACE:
return invokeTrace(url, headers, paramsMap, 0);
default:
return Resp.builder().code(500).message("Unsupported http method : " + method).build();
}
Expand Down Expand Up @@ -326,6 +332,293 @@ public static Resp invokePostBody(String url,
return executeRequest(rb.build(), 0);
}

/**
* Put方法请求
* @param url
* @param headers
* @param paramsMap
* @param retryTime
* @return
*/
private static Resp invokePut(String url,
Map<String,String> headers,
Map<String,String[]> paramsMap,
String body,
int retryTime) {
if (StringUtils.isNotEmpty(body)) {
return invokePutBody(url, headers, body);
}
FormBody.Builder fb = new FormBody.Builder();
if (MapUtils.isNotEmpty(paramsMap)) {
for (Map.Entry<String, String[]> entry : paramsMap.entrySet()) {
for (String value : entry.getValue()) {
fb.add(entry.getKey(), value);
}
}
}
Request.Builder rb = new Request.Builder().post(fb.build()).url(url);
if (MapUtils.isNotEmpty(headers)) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
rb.header(entry.getKey(), entry.getValue());
}
}
return executeRequest(rb.build(), retryTime);
}

/**
* Put方法请求
*
* @param url url地址
* @param headers 请求头
* @param body 请求body
* @return resp
*/
public static Resp invokePutBody(String url,
Map<String, String> headers,
String body) {
String contentType = headers.get("Content-Type");
if (contentType == null) {
contentType = headers.get("content-type");
}
if (contentType == null) {
contentType = "application/x-www-form-urlencoded; charset=utf-8";
}
RequestBody b = RequestBody.create(MediaType.parse(contentType), body);
Request.Builder rb = new Request.Builder().post(b).url(url);
if (MapUtils.isNotEmpty(headers)) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
rb.header(entry.getKey(), entry.getValue());
}
}
return executeRequest(rb.build());
}


/**
* Head方法请求
*
* @param url url地址
* @param headers 请求头
* @param paramsMap 请求参数
* @return resp
*/
private static Resp invokeHead(String url,
Map<String, String> headers,
Map<String, String[]> paramsMap,
int retryTime) {
HttpUrl hu = HttpUrl.parse(url);
if (hu == null) {
return Resp.builder().code(500).message("Parse http url failed,url=" + url).build();
}
if (MapUtils.isNotEmpty(paramsMap)) {
HttpUrl.Builder builder = hu.newBuilder();
for (Map.Entry<String, String[]> entry : paramsMap.entrySet()) {
for (String value : entry.getValue()) {
builder.addQueryParameter(entry.getKey(), value);
}
}
hu = builder.build();
}
Request.Builder rb = new Request.Builder().get().url(hu);
if (MapUtils.isNotEmpty(headers)) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
rb.header(entry.getKey(), entry.getValue());
}
}
return executeRequest(rb.build(), retryTime);
}


/**
* Patch方法请求
* @param url
* @param headers
* @param paramsMap
* @param retryTime
* @return
*/
private static Resp invokePatch(String url,
Map<String, String> headers,
Map<String, String[]> paramsMap,
String body,
int retryTime) {
if (StringUtils.isNotEmpty(body)) {
return invokePatchBody(url, headers, body);
}
FormBody.Builder fb = new FormBody.Builder();
if (MapUtils.isNotEmpty(paramsMap)) {
for (Map.Entry<String, String[]> entry : paramsMap.entrySet()) {
for (String value : entry.getValue()) {
fb.add(entry.getKey(), value);
}
}
}
Request.Builder rb = new Request.Builder().post(fb.build()).url(url);
if (MapUtils.isNotEmpty(headers)) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
rb.header(entry.getKey(), entry.getValue());
}
}
return executeRequest(rb.build(), retryTime);
}

/**
* Patch方法请求
*
* @param url url地址
* @param headers 请求头
* @param body 请求body
* @return resp
*/
public static Resp invokePatchBody(String url,
Map<String, String> headers,
String body) {
String contentType = headers.get("Content-Type");
if (contentType == null) {
contentType = headers.get("content-type");
}
if (contentType == null) {
contentType = "application/x-www-form-urlencoded; charset=utf-8";
}
RequestBody b = RequestBody.create(MediaType.parse(contentType), body);
Request.Builder rb = new Request.Builder().post(b).url(url);
if (MapUtils.isNotEmpty(headers)) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
rb.header(entry.getKey(), entry.getValue());
}
}
return executeRequest(rb.build());
}

/**
* Delete方法请求
* @param url
* @param headers
* @param paramsMap
* @param retryTime
* @return
*/
private static Resp invokeDelete(String url,
Map<String, String> headers,
Map<String, String[]> paramsMap,
String body,
int retryTime) {
if (StringUtils.isNotEmpty(body)) {
return invokeDeleteBody(url, headers, body);
}
FormBody.Builder fb = new FormBody.Builder();
if (MapUtils.isNotEmpty(paramsMap)) {
for (Map.Entry<String, String[]> entry : paramsMap.entrySet()) {
for (String value : entry.getValue()) {
fb.add(entry.getKey(), value);
}
}
}
Request.Builder rb = new Request.Builder().post(fb.build()).url(url);
if (MapUtils.isNotEmpty(headers)) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
rb.header(entry.getKey(), entry.getValue());
}
}
return executeRequest(rb.build(), retryTime);
}

/**
* Delete方法请求
*
* @param url url地址
* @param headers 请求头
* @param body 请求body
* @return resp
*/
public static Resp invokeDeleteBody(String url,
Map<String, String> headers,
String body) {
String contentType = headers.get("Content-Type");
if (contentType == null) {
contentType = headers.get("content-type");
}
if (contentType == null) {
contentType = "application/x-www-form-urlencoded; charset=utf-8";
}
RequestBody b = RequestBody.create(MediaType.parse(contentType), body);
Request.Builder rb = new Request.Builder().post(b).url(url);
if (MapUtils.isNotEmpty(headers)) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
rb.header(entry.getKey(), entry.getValue());
}
}
return executeRequest(rb.build());
}

/**
* Options方法请求
* @param url
* @param headers
* @param paramsMap
* @param retryTime
* @return resp
*/
private static Resp invokeOptions(String url,
Map<String, String> headers,
Map<String, String[]> paramsMap,
int retryTime) {
HttpUrl hu = HttpUrl.parse(url);
if (hu == null) {
return Resp.builder().code(500).message("Parse http url failed,url=" + url).build();
}
if (MapUtils.isNotEmpty(paramsMap)) {
HttpUrl.Builder builder = hu.newBuilder();
for (Map.Entry<String, String[]> entry : paramsMap.entrySet()) {
for (String value : entry.getValue()) {
builder.addQueryParameter(entry.getKey(), value);
}
}
hu = builder.build();
}
Request.Builder rb = new Request.Builder().get().url(hu);
if (MapUtils.isNotEmpty(headers)) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
rb.header(entry.getKey(), entry.getValue());
}
}
return executeRequest(rb.build(), retryTime);
}

/**
* Trace方法请求
*
* @param url url地址
* @param headers 请求头
* @param paramsMap 请求参数
* @return resp
*/
private static Resp invokeTrace(String url,
Map<String, String> headers,
Map<String, String[]> paramsMap,
int retryTime) {
HttpUrl hu = HttpUrl.parse(url);
if (hu == null) {
return Resp.builder().code(500).message("Parse http url failed,url=" + url).build();
}
if (MapUtils.isNotEmpty(paramsMap)) {
HttpUrl.Builder builder = hu.newBuilder();
for (Map.Entry<String, String[]> entry : paramsMap.entrySet()) {
for (String value : entry.getValue()) {
builder.addQueryParameter(entry.getKey(), value);
}
}
hu = builder.build();
}
Request.Builder rb = new Request.Builder().get().url(hu);
if (MapUtils.isNotEmpty(headers)) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
rb.header(entry.getKey(), entry.getValue());
}
}
return executeRequest(rb.build(), retryTime);
}

/**
* 执行request
*
Expand Down
36 changes: 36 additions & 0 deletions repeater-plugins/couchbase-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>repeater-plugins</artifactId>
<groupId>com.alibaba.jvm.sandbox</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>couchbase-plugin</artifactId>

<build>
<finalName>${project.name}-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>attached</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Loading