Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc6d5548

Browse files
committed
add optional "User-Agent" config option to use while making http calls
1 parent2751f52 commitc6d5548

File tree

6 files changed

+35
-8
lines changed

6 files changed

+35
-8
lines changed

‎changelog‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[SNAPSHOT]
22
* simplify async/sync usages
3+
* add optional "User-Agent" config option to use while making http calls
34

45
[2.5.3]
56
* fix - do not send two Content-Type header in async requests

‎scribejava-core/src/main/java/com/github/scribejava/core/builder/ServiceBuilder.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class ServiceBuilder {
2323
privateOutputStreamdebugStream;
2424
privateStringgrantType;
2525
privateStringresponseType ="code";
26+
privateStringuserAgent;
2627

2728
//sync version only
2829
privateIntegerconnectTimeout;
@@ -150,6 +151,11 @@ public ServiceBuilder asyncHttpProviderClassName(String asyncHttpProviderClassNa
150151
returnthis;
151152
}
152153

154+
publicServiceBuilderuserAgent(StringuserAgent) {
155+
this.userAgent =userAgent;
156+
returnthis;
157+
}
158+
153159
publicServiceBuilderdebug() {
154160
debugStream(System.out);
155161
returnthis;
@@ -161,7 +167,8 @@ public void checkPreconditions() {
161167
privateOAuthConfigcreateConfig() {
162168
checkPreconditions();
163169
returnnewOAuthConfig(apiKey,apiSecret,callback,signatureType,scope,debugStream,grantType,state,
164-
responseType,connectTimeout,readTimeout,asyncHttpClientConfig,asyncHttpProviderClassName);
170+
responseType,userAgent,connectTimeout,readTimeout,asyncHttpClientConfig,
171+
asyncHttpProviderClassName);
165172
}
166173

167174
/**

‎scribejava-core/src/main/java/com/github/scribejava/core/model/OAuthConfig.java‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class OAuthConfig {
1818
privatefinalOutputStreamdebugStream;
1919
privatefinalStringstate;
2020
privatefinalStringresponseType;
21+
privatefinalStringuserAgent;
2122

2223
//sync only version
2324
privatefinalIntegerconnectTimeout;
@@ -28,23 +29,25 @@ public class OAuthConfig {
2829
privatefinalStringasyncHttpProviderClassName;
2930

3031
publicOAuthConfig(Stringkey,Stringsecret) {
31-
this(key,secret,null,null,null,null,null,null,null,null,null,null,null);
32+
this(key,secret,null,null,null,null,null,null,null,null,null,null,null,null);
3233
}
3334

3435
publicOAuthConfig(StringapiKey,StringapiSecret,Stringcallback,SignatureTypesignatureType,Stringscope,
35-
OutputStreamdebugStream,StringgrantType,Stringstate,StringresponseType,IntegerconnectTimeout,
36-
IntegerreadTimeout,AsyncHttpClientConfigasyncHttpClientConfig,StringasyncHttpProviderClassName) {
36+
OutputStreamdebugStream,StringgrantType,Stringstate,StringresponseType,StringuserAgent,
37+
IntegerconnectTimeout,IntegerreadTimeout,AsyncHttpClientConfigasyncHttpClientConfig,
38+
StringasyncHttpProviderClassName) {
3739
this.apiKey =apiKey;
3840
this.apiSecret =apiSecret;
3941
this.callback =callback;
4042
this.signatureType =signatureType;
4143
this.scope =scope;
4244
this.debugStream =debugStream;
43-
this.connectTimeout =connectTimeout;
44-
this.readTimeout =readTimeout;
4545
this.grantType =grantType;
4646
this.state =state;
4747
this.responseType =responseType;
48+
this.userAgent =userAgent;
49+
this.connectTimeout =connectTimeout;
50+
this.readTimeout =readTimeout;
4851
this.asyncHttpClientConfig =asyncHttpClientConfig;
4952
this.asyncHttpProviderClassName =asyncHttpProviderClassName;
5053
}
@@ -89,6 +92,10 @@ public String getResponseType() {
8992
returnresponseType;
9093
}
9194

95+
publicStringgetUserAgent() {
96+
returnuserAgent;
97+
}
98+
9299
publicvoidlog(Stringmessage) {
93100
if (debugStream !=null) {
94101
message +='\n';
@@ -115,5 +122,4 @@ public AsyncHttpClientConfig getAsyncHttpClientConfig() {
115122
publicStringgetAsyncHttpProviderClassName() {
116123
returnasyncHttpProviderClassName;
117124
}
118-
119125
}

‎scribejava-core/src/main/java/com/github/scribejava/core/model/OAuthConstants.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,8 @@ public interface OAuthConstants {
3535
StringSTATE ="state";
3636
StringUSERNAME ="username";
3737
StringPASSWORD ="password";
38+
39+
//not OAuth specific
40+
StringUSER_AGENT_HEADER_NAME ="User-Agent";
41+
3842
}

‎scribejava-core/src/main/java/com/github/scribejava/core/model/OAuthRequest.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ void addHeaders() {
7070
for (Map.Entry<String,String>entry :getHeaders().entrySet()) {
7171
connection.setRequestProperty(entry.getKey(),entry.getValue());
7272
}
73+
finalStringuserAgent =getService().getConfig().getUserAgent();
74+
if (userAgent !=null) {
75+
connection.setRequestProperty(OAuthConstants.USER_AGENT_HEADER_NAME,userAgent);
76+
}
7377
}
7478

7579
voidaddBody(byte[]content)throwsIOException {

‎scribejava-core/src/main/java/com/github/scribejava/core/model/OAuthRequestAsync.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ public <T> Future<T> sendAsync(OAuthAsyncRequestCallback<T> callback, ResponseCo
4747
thrownewOAuthException("Cannot use async operations, only sync");
4848
}
4949
finalOAuthServiceservice =getService();
50+
finalOAuthConfigconfig =service.getConfig();
5051
if (ForceTypeOfHttpRequest.PREFER_SYNC_ONLY_HTTP_REQUESTS ==forceTypeOfHttpRequest) {
51-
service.getConfig().log("Cannot use async operations, only sync");
52+
config.log("Cannot use async operations, only sync");
5253
}
5354
finalStringcompleteUrl =getCompleteUrl();
5455
finalAsyncHttpClient.BoundRequestBuilderboundRequestBuilder;
@@ -72,6 +73,10 @@ public <T> Future<T> sendAsync(OAuthAsyncRequestCallback<T> callback, ResponseCo
7273
for (Map.Entry<String,String>header :headers.entrySet()) {
7374
boundRequestBuilder.addHeader(header.getKey(),header.getValue());
7475
}
76+
finalStringuserAgent =config.getUserAgent();
77+
if (userAgent !=null) {
78+
boundRequestBuilder.setHeader(OAuthConstants.USER_AGENT_HEADER_NAME,userAgent);
79+
}
7580

7681
if (proxyServer !=null) {
7782
boundRequestBuilder.setProxyServer(proxyServer);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp