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

Commit 5cc31fc

Browse files
committed
Add getNewToken to OAuth2Component
1 parent b424ee2 commit 5cc31fc

2 files changed

Lines changed: 31 additions & 6 deletions

File tree

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,31 @@ public FoxHttpRequest generateRequestForGrantType(GrantType grantType) throws Ma
7070
OAuth2RequestGenerator oAuth2RequestGenerator = oAuth2RequestGenerators.get(grantType);
7171
return oAuth2RequestGenerator.getRequest(this);
7272
}
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+
}
73100
}

foxhttp/src/test/java/ch/viascom/groundwork/foxhttp/FoxHttpOAuth2Test.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
import ch.viascom.groundwork.foxhttp.component.oauth2.GrantType;
77
import ch.viascom.groundwork.foxhttp.component.oauth2.OAuth2Component;
88
import ch.viascom.groundwork.foxhttp.component.oauth2.OAuth2StoreBuilder;
9-
import ch.viascom.groundwork.foxhttp.component.oauth2.response.OAuthTokenErrorResponse;
10-
import ch.viascom.groundwork.foxhttp.exception.FoxHttpRequestException;
9+
import ch.viascom.groundwork.foxhttp.interceptor.FoxHttpInterceptorType;
1110
import ch.viascom.groundwork.foxhttp.log.SystemOutFoxHttpLogger;
1211
import ch.viascom.groundwork.foxhttp.models.OAuth2Response;
1312
import ch.viascom.groundwork.foxhttp.parser.GsonParser;
13+
import ch.viascom.groundwork.foxhttp.response.serviceresult.DefaultServiceResultFaultInterceptor;
1414
import ch.viascom.groundwork.foxhttp.response.serviceresult.FoxHttpServiceResultResponse;
1515
import ch.viascom.groundwork.foxhttp.type.RequestType;
1616
import org.joda.time.DateTime;
@@ -36,7 +36,7 @@ public class FoxHttpOAuth2Test {
3636
static OAuth2Component oAuth2Component;
3737
private static String solaraURL = "http://localhost:8080/solara-webservice-1.0-SNAPSHOT";
3838
private static String solaraTestUser = "fox@viascom.ch";
39-
private static String solaraTestPassword = "password1234";
39+
private static String solaraTestPassword = "password1234-";
4040
private static String solaraClient = "AluxApp/1.0-SNAPSHOT";
4141
private static String solaraClientSecret = "7OVm8OeTPf6EZq3C";
4242

@@ -45,6 +45,7 @@ public static void prepareTestInstance() throws Exception {
4545
httpClient = new FoxHttpClientBuilder(new GsonParser(), new GsonParser())
4646
.setFoxHttpLogger(new SystemOutFoxHttpLogger(true, "OAuth2"))
4747
.addFoxHttpPlaceholderEntry("solara", solaraURL)
48+
.registerFoxHttpInterceptor(FoxHttpInterceptorType.RESPONSE,new DefaultServiceResultFaultInterceptor())
4849
.build();
4950

5051

@@ -87,9 +88,6 @@ public static void prepareTestInstance() throws Exception {
8788
oAuth2Component1.getFoxHttpClient().getFoxHttpAuthorizationStrategy().addAuthorization(scope, oAuth2Component1.getOAuth2Authorization());
8889
}
8990

90-
} else {
91-
OAuthTokenErrorResponse tokenErrorResponse = response.getParsedBody(OAuthTokenErrorResponse.class);
92-
throw new FoxHttpRequestException(tokenErrorResponse.getError() + " : " + tokenErrorResponse.getErrorDescription());
9391
}
9492
});
9593
httpClient.activateComponent(oAuth2Component);

0 commit comments

Comments
 (0)