Skip to content

Commit

Permalink
修复全局请求头设置无效的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
yhyzgn committed Aug 29, 2020
1 parent e756352 commit 20856e4
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.yhyzgn.http</groupId>
<artifactId>pigeon</artifactId>
<version>1.2.6</version>
<version>1.2.7</version>
<name>Pigeon</name>
<description>Java http proxy.</description>
<url>/~https://github.com/yhyzgn/Pigeon</url>
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/com/yhy/http/pigeon/Pigeon.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import com.yhy.http.pigeon.adapter.CallAdapter;
import com.yhy.http.pigeon.converter.Converter;
import com.yhy.http.pigeon.http.HttpMethod;
import com.yhy.http.pigeon.offer.GuavaCallAdapter;
import com.yhy.http.pigeon.offer.HttpLoggerInterceptor;
import com.yhy.http.pigeon.offer.JacksonConverter;
import com.yhy.http.pigeon.internal.GuavaCallAdapter;
import com.yhy.http.pigeon.internal.HeaderInterceptor;
import com.yhy.http.pigeon.internal.HttpLoggerInterceptor;
import com.yhy.http.pigeon.internal.JacksonConverter;
import okhttp3.*;

import javax.net.ssl.HostnameVerifier;
Expand Down Expand Up @@ -37,7 +38,7 @@ public class Pigeon {
private final HostnameVerifier sslHostnameVerifier;
private final List<Interceptor> netInterceptors;
private final List<Interceptor> interceptors;
private final Map<String, Object> headers;
private final Map<String, String> headers;
private final List<CallAdapter.Factory> callFactories;
private final List<Converter.Factory> converterFactories;
private final OkHttpClient.Builder client;
Expand Down Expand Up @@ -67,7 +68,7 @@ public List<Interceptor> interceptors() {
return interceptors;
}

public Map<String, Object> headers() {
public Map<String, String> headers() {
return headers;
}

Expand Down Expand Up @@ -222,7 +223,7 @@ public static class Builder {
private HttpUrl host;
private final List<Interceptor> netInterceptors = new ArrayList<>();
private final List<Interceptor> interceptors = new ArrayList<>();
private final Map<String, Object> headers = new HashMap<>();
private final Map<String, String> headers = new HashMap<>();
private final List<CallAdapter.Factory> adapterFactories = new ArrayList<>();
private final List<Converter.Factory> converterFactories = new ArrayList<>();
private OkHttpClient.Builder client;
Expand Down Expand Up @@ -252,7 +253,7 @@ public Builder netInterceptor(Interceptor interceptor) {
return this;
}

public Builder header(String name, Object value) {
public Builder header(String name, String value) {
this.headers.put(name, value);
return this;
}
Expand Down Expand Up @@ -302,6 +303,9 @@ public Pigeon build() {
// 默认Adapter和Converter需要添加在最前面,后边需要从后往前查找
adapterFactories.add(0, new GuavaCallAdapter());
converterFactories.add(0, new JacksonConverter());

// 公共请求头拦截器
netInterceptors.add(new HeaderInterceptor(headers));
if (logging) {
netInterceptors.add(new HttpLoggerInterceptor());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.yhy.http.pigeon.offer;
package com.yhy.http.pigeon.internal;

import com.google.gson.Gson;
import com.google.gson.JsonIOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.yhy.http.pigeon.offer;
package com.yhy.http.pigeon.internal;

import com.google.common.util.concurrent.AbstractFuture;
import com.google.common.util.concurrent.ListenableFuture;
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/com/yhy/http/pigeon/internal/HeaderInterceptor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.yhy.http.pigeon.internal;

import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Map;

/**
* author : 颜洪毅
* e-mail : yhyzgn@gmail.com
* time : 2020-08-29 19:40
* version: 1.0.0
* desc : 公共请求头专用拦截器
*/
public class HeaderInterceptor implements Interceptor {
private final static Logger LOGGER = LoggerFactory.getLogger(HeaderInterceptor.class);

private final Map<String, String> headers;

public HeaderInterceptor(Map<String, String> headers) {
this.headers = headers;
}

@NotNull
@Override
public Response intercept(@NotNull Chain chain) throws IOException {
Request original = chain.request();
// 重建 request 并注入 全局请求头
Headers.Builder builder = original.headers().newBuilder();
headers.forEach((name, value) -> {
LOGGER.debug("Setting global request header: {} = {}", name, value);
builder.set(name, value);
});
Request request = original.newBuilder().headers(builder.build()).build();
// 继续请求
return chain.proceed(request);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.yhy.http.pigeon.offer;
package com.yhy.http.pigeon.internal;

import com.yhy.http.pigeon.common.Invocation;
import com.yhy.http.pigeon.common.SystemClock;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.yhy.http.pigeon.offer;
package com.yhy.http.pigeon.internal;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.DeserializationFeature;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.yhy.http.pigeon.offer;
package com.yhy.http.pigeon.internal;

import com.yhy.http.pigeon.Pigeon;
import com.yhy.http.pigeon.converter.Converter;
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/pigeon/get/ApiTester.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package pigeon.get;

import com.yhy.http.pigeon.Pigeon;
import com.yhy.http.pigeon.offer.StringResponseConverter;
import pigeon.Rmt;
import pigeon.interceptor.TestInterceptor;

import java.io.IOException;
import java.util.HashMap;
Expand All @@ -19,7 +19,7 @@ public class ApiTester {

public static void main(String[] args) throws IOException {
// Pigeon pigeon = new Pigeon.Builder().host("http://localhost:8080/dbs/test").addConverterFactory(new StringResponseConverter()).build();
Pigeon pigeon = new Pigeon.Builder().host("https://t-tio.ybsjyyn.com/reporter/api/debug").build();
Pigeon pigeon = new Pigeon.Builder().host("https://t-tio.ybsjyyn.com/reporter/api/debug").header("XX", "asdfasdf").build();
Api api = pigeon.create(Api.class);

// String test = api.test();
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/pigeon/interceptor/TestInterceptor.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package pigeon.interceptor;

import com.yhy.http.pigeon.offer.HttpLoggerInterceptor;
import com.yhy.http.pigeon.internal.HttpLoggerInterceptor;
import okhttp3.Interceptor;
import okhttp3.Response;
import org.jetbrains.annotations.NotNull;
Expand All @@ -17,7 +17,7 @@
* desc :
*/
public class TestInterceptor implements Interceptor {
private final static Logger LOGGER = LoggerFactory.getLogger(HttpLoggerInterceptor.class);
private final static Logger LOGGER = LoggerFactory.getLogger(TestInterceptor.class);

@NotNull
@Override
Expand Down

0 comments on commit 20856e4

Please sign in to comment.