Skip to content
This repository was archived by the owner on Oct 11, 2023. It is now read-only.

Commit fc0a722

Browse files
authored
Merge pull request #35 from Viascom/foxhttp-develop
Foxhttp develop
2 parents 2b325ee + 06d47d7 commit fc0a722

15 files changed

Lines changed: 207 additions & 59 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ Collection of useful libraries for Java and Android.
1111

1212
### FoxHttp
1313
The FoxHttp provides a fast and easy http client for java and android. It is part of the GroundWork Project by Viascom.<br/>
14-
[![master](https://img.shields.io/badge/master-v1.2-brightgreen.svg)](https://github.com/Viascom/groundwork/tree/master)
15-
[![develop](https://img.shields.io/badge/develop-v1.2-brightgreen.svg)](https://github.com/Viascom/groundwork/tree/foxhttp-develop)
14+
[![master](https://img.shields.io/badge/master-v1.2.1-brightgreen.svg)](https://github.com/Viascom/groundwork/tree/master)
15+
[![develop](https://img.shields.io/badge/develop-v1.2.1-brightgreen.svg)](https://github.com/Viascom/groundwork/tree/foxhttp-develop)
1616
[![Maven Central](https://img.shields.io/maven-central/v/ch.viascom.groundwork/foxhttp.svg)]()
1717
[![Bintray](https://img.shields.io/bintray/v/viascom/GroundWork/ch.viascom.groundwork%3Afoxhttp.svg)]()
1818

@@ -25,8 +25,8 @@ The POM module provides a parent pom which is also use in the GroundWork Project
2525

2626
### Service-Result
2727
The ServiceResult module provides a standard result for your REST applications. It is part of the GroundWork Project by Viascom.<br/>
28-
[![master](https://img.shields.io/badge/master-v1.2-brightgreen.svg)]()
29-
[![develop](https://img.shields.io/badge/develop-v1.2-brightgreen.svg)]()
28+
[![master](https://img.shields.io/badge/master-v1.2.1-brightgreen.svg)]()
29+
[![develop](https://img.shields.io/badge/develop-v1.2.1-brightgreen.svg)]()
3030
[![Maven Central](https://img.shields.io/maven-central/v/ch.viascom.groundwork/service-result.svg)]()
3131
[![Bintray](https://img.shields.io/bintray/v/viascom/GroundWork/ch.viascom.groundwork%3Aservice-result.svg)]()
3232

foxhttp/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
**FoxHttp provides a fast and easy http client for Java and Android. It's part of the GroundWork Project by Viascom.**
44

5-
[![release](https://img.shields.io/badge/release-v1.2-brightgreen.svg)](https://github.com/Viascom/groundwork/tree/master)
6-
[![develop](https://img.shields.io/badge/develop-v1.2-brightgreen.svg)](https://github.com/Viascom/groundwork/tree/foxhttp-develop)<br/>
5+
[![release](https://img.shields.io/badge/release-v1.2.1-brightgreen.svg)](https://github.com/Viascom/groundwork/tree/master)
6+
[![develop](https://img.shields.io/badge/develop-v1.2.1-brightgreen.svg)](https://github.com/Viascom/groundwork/tree/foxhttp-develop)<br/>
77
[![coverage](https://img.shields.io/badge/test--coverage-80%25-brightgreen.svg)](https://github.com/Viascom/groundwork/tree/foxhttp-develop)
88
[![Snap CI branch](https://img.shields.io/snap-ci/Viascom/groundwork/foxhttp-develop.svg)]()
99
[![Maven Central](https://img.shields.io/maven-central/v/ch.viascom.groundwork/foxhttp.svg)]()

foxhttp/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</parent>
1212

1313
<artifactId>foxhttp</artifactId>
14-
<version>1.2</version>
14+
<version>1.2.1</version>
1515

1616

1717
<name>GroundWork - FoxHttp</name>

foxhttp/src/main/java/ch/viascom/groundwork/foxhttp/FoxHttpRequest.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public class FoxHttpRequest {
3737
@Getter
3838
private URL url;
3939

40+
private FoxHttpAuthorizationScope authScope;
41+
4042
@Getter
4143
@Setter
4244
private FoxHttpRequestQuery requestQuery = new FoxHttpRequestQuery();
@@ -128,6 +130,9 @@ private FoxHttpResponse executeHttp(boolean isHttps) throws FoxHttpException {
128130
foxHttpClient.getFoxHttpLogger().log("setCookieStore(" + foxHttpClient.getFoxHttpCookieStore() + ")");
129131
CookieHandler.setDefault((CookieManager) foxHttpClient.getFoxHttpCookieStore());
130132

133+
// Create Scope
134+
authScope = FoxHttpAuthorizationScope.create(url.toString(), requestType);
135+
131136
foxHttpClient.getFoxHttpLogger().log("prepareQuery(" + getRequestQuery() + ")");
132137
prepareQuery();
133138

@@ -158,7 +163,7 @@ private FoxHttpResponse executeHttp(boolean isHttps) throws FoxHttpException {
158163

159164
//Set headers
160165
foxHttpClient.getFoxHttpLogger().log("prepareHeader(" + getRequestHeader() + ")");
161-
prepareHeader(connection);
166+
prepareHeader();
162167

163168
//Set User-Agent if not exist
164169
foxHttpClient.getFoxHttpLogger().log("setUserAgentIfNotExist(" + foxHttpClient.getFoxHttpUserAgent() + ")");
@@ -185,7 +190,7 @@ private FoxHttpResponse executeHttp(boolean isHttps) throws FoxHttpException {
185190

186191
//Process authorization strategy
187192
foxHttpClient.getFoxHttpLogger().log("processAuthorizationStrategy(" + foxHttpClient.getFoxHttpAuthorizationStrategy() + ")");
188-
processAuthorizationStrategy(connection);
193+
processAuthorizationStrategy();
189194

190195
//Execute interceptor
191196
foxHttpClient.getFoxHttpLogger().log("executeRequestHeaderInterceptor()");
@@ -198,7 +203,7 @@ private FoxHttpResponse executeHttp(boolean isHttps) throws FoxHttpException {
198203
setHeaderIfNotExist(HeaderTypes.CONTENT_TYPE, requestBody.getOutputContentType().toString(), connection);
199204
//Set request body
200205
foxHttpClient.getFoxHttpLogger().log("setRequestBodyStream(" + getRequestBody() + ")");
201-
setRequestBodyStream(connection);
206+
setRequestBodyStream();
202207
}
203208

204209
foxHttpClient.getFoxHttpLogger().log("sendRequest()");
@@ -231,7 +236,7 @@ private FoxHttpResponse executeHttp(boolean isHttps) throws FoxHttpException {
231236
foxHttpResponse = new FoxHttpResponse(is, this, responseCode, foxHttpClient);
232237
//Process response headers
233238
foxHttpClient.getFoxHttpLogger().log("processResponseHeader()");
234-
processResponseHeader(foxHttpResponse, connection);
239+
processResponseHeader();
235240

236241
//Execute interceptor
237242
foxHttpClient.getFoxHttpLogger().log("executeResponseInterceptor()");
@@ -265,14 +270,14 @@ private void checkPlaceholders() throws FoxHttpRequestException {
265270

266271
}
267272

268-
private void prepareHeader(URLConnection connection) {
273+
private void prepareHeader() {
269274
for (HeaderEntry headerField : getRequestHeader()) {
270275
connection.addRequestProperty(headerField.getName(), headerField.getValue());
271276
}
272277
}
273278

274-
private void setRequestBodyStream(URLConnection urlConnection) throws FoxHttpRequestException {
275-
requestBody.setBody(new FoxHttpRequestBodyContext(urlConnection, this, foxHttpClient));
279+
private void setRequestBodyStream() throws FoxHttpRequestException {
280+
requestBody.setBody(new FoxHttpRequestBodyContext(connection, this, foxHttpClient));
276281
}
277282

278283
private void prepareQuery() throws FoxHttpRequestException, MalformedURLException {
@@ -288,19 +293,16 @@ private void setHeaderIfNotExist(HeaderTypes type, String value, URLConnection c
288293
}
289294
}
290295

291-
private void processAuthorizationStrategy(URLConnection connection) throws FoxHttpRequestException {
292-
List<FoxHttpAuthorization> foxHttpAuthorizations = foxHttpClient.getFoxHttpAuthorizationStrategy().getAuthorization(connection, FoxHttpAuthorizationScope.create(
293-
url.toString(), RequestType.valueOf(((HttpURLConnection) connection).getRequestMethod())), foxHttpClient
296+
private void processAuthorizationStrategy() throws FoxHttpRequestException {
297+
List<FoxHttpAuthorization> foxHttpAuthorizations = foxHttpClient.getFoxHttpAuthorizationStrategy().getAuthorization(connection, authScope, foxHttpClient
294298
);
295299
for (FoxHttpAuthorization foxHttpAuthorization : foxHttpAuthorizations) {
296300
foxHttpClient.getFoxHttpLogger().log("-> doAuthorization(" + foxHttpAuthorization + ")");
297-
foxHttpAuthorization.doAuthorization(connection, FoxHttpAuthorizationScope.create(
298-
url.toString(), RequestType.valueOf(((HttpURLConnection) connection).getRequestMethod()))
299-
);
301+
foxHttpAuthorization.doAuthorization(connection, authScope);
300302
}
301303
}
302304

303-
private void processResponseHeader(FoxHttpResponse foxHttpResponse, URLConnection connection) {
305+
private void processResponseHeader() {
304306
FoxHttpHeader responseHeaders = new FoxHttpHeader();
305307
Map<String, List<String>> map = connection.getHeaderFields();
306308
for (Map.Entry<String, List<String>> entry : map.entrySet()) {

foxhttp/src/main/java/ch/viascom/groundwork/foxhttp/component/oauth2/OAuth2Component.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import ch.viascom.groundwork.foxhttp.FoxHttpClient;
44
import ch.viascom.groundwork.foxhttp.FoxHttpRequest;
5+
import ch.viascom.groundwork.foxhttp.authorization.FoxHttpAuthorizationScope;
56
import ch.viascom.groundwork.foxhttp.component.FoxHttpComponent;
67
import ch.viascom.groundwork.foxhttp.component.oauth2.authorization.OAuth2Authorization;
78
import ch.viascom.groundwork.foxhttp.component.oauth2.authorization.OAuth2BearerTokenAuthorization;
@@ -52,10 +53,12 @@ public OAuth2Component(OAuth2Store oAuth2Store) {
5253
public void initiation(FoxHttpClient foxHttpClient) throws FoxHttpException {
5354
this.foxHttpClient = foxHttpClient;
5455
foxHttpClient.getFoxHttpLogger().log("========= Initiate OAuth2Component =========");
55-
foxHttpClient.getFoxHttpLogger().log("-> Register authorization");
56+
foxHttpClient.getFoxHttpLogger().log("-> Register authorizations");
5657
//Register authorization
5758
oAuth2Authorization = new OAuth2BearerTokenAuthorization(oAuth2Store.getAccessToken());
58-
foxHttpClient.getFoxHttpAuthorizationStrategy().addAuthorization(oAuth2Store.getAuthScope(), oAuth2Authorization);
59+
for (FoxHttpAuthorizationScope scope : oAuth2Store.getAuthScopes()) {
60+
foxHttpClient.getFoxHttpAuthorizationStrategy().addAuthorization(scope, oAuth2Authorization);
61+
}
5962
foxHttpClient.getFoxHttpLogger().log("-> Register interceptor");
6063
//Register interceptor
6164
foxHttpClient.register(FoxHttpInterceptorType.REQUEST_CONNECTION, new OAuth2RequestInterceptor(this, 100));
@@ -67,4 +70,31 @@ public FoxHttpRequest generateRequestForGrantType(GrantType grantType) throws Ma
6770
OAuth2RequestGenerator oAuth2RequestGenerator = oAuth2RequestGenerators.get(grantType);
6871
return oAuth2RequestGenerator.getRequest(this);
6972
}
73+
74+
/**
75+
* Request a new token based on the configuration
76+
*
77+
* @param grantType grant type to use
78+
* @return access token from the response
79+
* @throws FoxHttpException
80+
* @throws MalformedURLException
81+
*/
82+
public String getNewToken(GrantType grantType) throws FoxHttpException, MalformedURLException {
83+
FoxHttpRequest request = this.generateRequestForGrantType(grantType);
84+
this.getOAuth2RequestExecutor().executeOAuth2Request(request, this);
85+
return getOAuth2Store().getAccessToken();
86+
}
87+
88+
/**
89+
* Request a new token based on the configuration
90+
*
91+
* @return access token from the response
92+
* @throws FoxHttpException
93+
* @throws MalformedURLException
94+
*/
95+
public String getNewToken() throws FoxHttpException, MalformedURLException {
96+
FoxHttpRequest request = this.generateRequestForGrantType(this.getOAuth2Store().getGrantType());
97+
this.getOAuth2RequestExecutor().executeOAuth2Request(request, this);
98+
return getOAuth2Store().getAccessToken();
99+
}
70100
}

foxhttp/src/main/java/ch/viascom/groundwork/foxhttp/component/oauth2/OAuth2Store.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import lombok.Data;
66
import org.joda.time.DateTime;
77

8+
import java.util.ArrayList;
89
import java.util.HashMap;
10+
import java.util.List;
911
import java.util.Map;
1012

1113
/**
@@ -34,7 +36,7 @@ public class OAuth2Store {
3436
/**
3537
* AuthorizationScope which needs a token
3638
*/
37-
private FoxHttpAuthorizationScope authScope;
39+
private List<FoxHttpAuthorizationScope> authScopes = new ArrayList<>();
3840
/**
3941
* Additional parameters which will be included in the token request
4042
*/

foxhttp/src/main/java/ch/viascom/groundwork/foxhttp/component/oauth2/OAuth2StoreBuilder.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import ch.viascom.groundwork.foxhttp.authorization.FoxHttpAuthorizationScope;
44
import ch.viascom.groundwork.foxhttp.type.RequestType;
55

6+
import java.util.List;
67
import java.util.Map;
78

89
/**
@@ -49,13 +50,24 @@ public OAuth2StoreBuilder setRequestScopes(String requestScopes) {
4950
}
5051

5152
/**
52-
* Set authorization scope which needs a token
53+
* Set authorization scopes which needs a token
54+
*
55+
* @param authScopes authorization scopes which needs a token
56+
* @return OAuth2StoreBuilder (this)
57+
*/
58+
public OAuth2StoreBuilder setFoxHttpAuthorizationScopes(List<FoxHttpAuthorizationScope> authScopes) {
59+
oAuth2Store.setAuthScopes(authScopes);
60+
return this;
61+
}
62+
63+
/**
64+
* Add authorization scope which needs a token
5365
*
5466
* @param authScope authorization scope which needs a token
5567
* @return OAuth2StoreBuilder (this)
5668
*/
57-
public OAuth2StoreBuilder setFoxHttpAuthorizationScope(FoxHttpAuthorizationScope authScope) {
58-
oAuth2Store.setAuthScope(authScope);
69+
public OAuth2StoreBuilder addFoxHttpAuthorizationScope(FoxHttpAuthorizationScope authScope) {
70+
oAuth2Store.getAuthScopes().add(authScope);
5971
return this;
6072
}
6173

foxhttp/src/main/java/ch/viascom/groundwork/foxhttp/component/oauth2/interceptor/OAuth2RequestInterceptor.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ch.viascom.groundwork.foxhttp.component.oauth2.interceptor;
22

33
import ch.viascom.groundwork.foxhttp.FoxHttpRequest;
4+
import ch.viascom.groundwork.foxhttp.authorization.FoxHttpAuthorizationScope;
45
import ch.viascom.groundwork.foxhttp.component.oauth2.GrantType;
56
import ch.viascom.groundwork.foxhttp.component.oauth2.OAuth2Component;
67
import ch.viascom.groundwork.foxhttp.exception.FoxHttpException;
@@ -30,9 +31,7 @@ public class OAuth2RequestInterceptor implements FoxHttpRequestConnectionInterce
3031
@Override
3132
public void onIntercept(FoxHttpRequestConnectionInterceptorContext context) throws FoxHttpException {
3233
try {
33-
if (RegexUtil.doesURLMatch(context.getRequest().getRequestType() + " " +
34-
context.getClient().getFoxHttpPlaceholderStrategy().processPlaceholders(context.getUrl().toString(), context.getClient()),
35-
context.getClient().getFoxHttpPlaceholderStrategy().processPlaceholders(oAuth2Component.getOAuth2Store().getAuthScope().toString(), context.getClient()))) {
34+
if (isScopePresent(context)) {
3635
context.getClient().getFoxHttpLogger().log(" -> OAuth2 is needed for this request");
3736
if (!isAccessTokenValid()) {
3837
context.getClient().getFoxHttpLogger().log(" -> New OAuth2 token is needed");
@@ -50,6 +49,18 @@ public void onIntercept(FoxHttpRequestConnectionInterceptorContext context) thro
5049
}
5150
}
5251

52+
private boolean isScopePresent(FoxHttpRequestConnectionInterceptorContext context) {
53+
boolean isPresent = false;
54+
for (FoxHttpAuthorizationScope scope : oAuth2Component.getOAuth2Store().getAuthScopes()) {
55+
isPresent = RegexUtil.doesURLMatch(context.getRequest().getRequestType() + " " + context.getClient().getFoxHttpPlaceholderStrategy().processPlaceholders(context.getUrl().toString(), context.getClient()),
56+
context.getClient().getFoxHttpPlaceholderStrategy().processPlaceholders(scope.toString(), context.getClient()));
57+
if (isPresent) {
58+
break;
59+
}
60+
}
61+
return isPresent;
62+
}
63+
5364
private boolean isAccessTokenValid() {
5465
if (oAuth2Component.getOAuth2Store().getAccessToken().isEmpty()) {
5566
return false;

foxhttp/src/main/java/ch/viascom/groundwork/foxhttp/component/oauth2/request/DefaultOAuth2RequestExecutor.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import ch.viascom.groundwork.foxhttp.FoxHttpRequest;
44
import ch.viascom.groundwork.foxhttp.FoxHttpResponse;
5+
import ch.viascom.groundwork.foxhttp.authorization.FoxHttpAuthorizationScope;
56
import ch.viascom.groundwork.foxhttp.component.oauth2.OAuth2Component;
67
import ch.viascom.groundwork.foxhttp.component.oauth2.response.OAuthTokenErrorResponse;
78
import ch.viascom.groundwork.foxhttp.component.oauth2.response.OAuthTokenResponse;
@@ -26,13 +27,11 @@ public void executeOAuth2Request(FoxHttpRequest foxHttpRequest, OAuth2Component
2627
);
2728
oAuth2Component.getOAuth2Store().setScopes(tokenResponse.getScope());
2829

29-
oAuth2Component.getFoxHttpClient().getFoxHttpAuthorizationStrategy().removeAuthorization(
30-
oAuth2Component.getOAuth2Store().getAuthScope(), oAuth2Component.getOAuth2Authorization()
31-
);
32-
oAuth2Component.getOAuth2Authorization().setValue(oAuth2Component.getOAuth2Store().getAccessToken());
33-
oAuth2Component.getFoxHttpClient().getFoxHttpAuthorizationStrategy().addAuthorization(
34-
oAuth2Component.getOAuth2Store().getAuthScope(), oAuth2Component.getOAuth2Authorization()
35-
);
30+
for (FoxHttpAuthorizationScope scope : oAuth2Component.getOAuth2Store().getAuthScopes()) {
31+
oAuth2Component.getFoxHttpClient().getFoxHttpAuthorizationStrategy().removeAuthorization(scope, oAuth2Component.getOAuth2Authorization());
32+
oAuth2Component.getOAuth2Authorization().setValue(oAuth2Component.getOAuth2Store().getAccessToken());
33+
oAuth2Component.getFoxHttpClient().getFoxHttpAuthorizationStrategy().addAuthorization(scope, oAuth2Component.getOAuth2Authorization());
34+
}
3635

3736
} else {
3837
OAuthTokenErrorResponse tokenErrorResponse = response.getParsedBody(OAuthTokenErrorResponse.class);

foxhttp/src/main/java/ch/viascom/groundwork/foxhttp/response/serviceresult/DefaultServiceResultFaultInterceptor.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@
1111
public class DefaultServiceResultFaultInterceptor implements FoxHttpResponseInterceptor {
1212

1313
/**
14-
* Set as last interceptors of all
14+
* Sets the weight to 1'000 of the DefaultServiceResultFaultInterceptor
15+
* <p>
16+
* Interceptors with a smaller weight number will be executed before this one
17+
* and interceptors with a higher weight number will be executed after this interceptor.
18+
* <p>
19+
* Override the weight if you wanna change the execution order of this interceptor.
1520
*
16-
* @return Integer.MAX_VALUE
21+
* @return 1000
1722
*/
1823
@Override
1924
public int getWeight() {
20-
return Integer.MAX_VALUE;
25+
return 1_000;
2126
}
2227

2328
@Override

0 commit comments

Comments
 (0)