Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Commit

Permalink
Merge pull request #36 from Viascom/develop
Browse files Browse the repository at this point in the history
foxhttp 1.2.1 & service-result 1.2.1
  • Loading branch information
nikolastankovic authored Feb 24, 2017
2 parents 3755041 + f21ddb2 commit 79efa09
Show file tree
Hide file tree
Showing 17 changed files with 214 additions and 64 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ GroundWork Project
Collection of useful libraries for Java and Android.

## Build-Status
[![Build Status](https://travis-ci.org/Viascom/groundwork.svg?branch=master)](https://travis-ci.org/Viascom/groundwork)
Master: [![Build Status](https://travis-ci.org/Viascom/groundwork.svg?branch=master)](https://travis-ci.org/Viascom/groundwork)

Develop: [![Build Status](https://travis-ci.org/Viascom/groundwork.svg?branch=develop)](https://travis-ci.org/Viascom/groundwork)

## Projects:

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

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

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

Expand Down
4 changes: 2 additions & 2 deletions foxhttp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

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

[![release](https://img.shields.io/badge/release-v1.2-brightgreen.svg)](https://github.com/Viascom/groundwork/tree/master)
[![develop](https://img.shields.io/badge/develop-v1.2-brightgreen.svg)](https://github.com/Viascom/groundwork/tree/foxhttp-develop)<br/>
[![release](https://img.shields.io/badge/release-v1.2.1-brightgreen.svg)](https://github.com/Viascom/groundwork/tree/master)
[![develop](https://img.shields.io/badge/develop-v1.2.1-brightgreen.svg)](https://github.com/Viascom/groundwork/tree/foxhttp-develop)<br/>
[![coverage](https://img.shields.io/badge/test--coverage-80%25-brightgreen.svg)](https://github.com/Viascom/groundwork/tree/foxhttp-develop)
[![Snap CI branch](https://img.shields.io/snap-ci/Viascom/groundwork/foxhttp-develop.svg)]()
[![Maven Central](https://img.shields.io/maven-central/v/ch.viascom.groundwork/foxhttp.svg)]()
Expand Down
2 changes: 1 addition & 1 deletion foxhttp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</parent>

<artifactId>foxhttp</artifactId>
<version>1.2</version>
<version>1.2.1</version>


<name>GroundWork - FoxHttp</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class FoxHttpRequest {
@Getter
private URL url;

private FoxHttpAuthorizationScope authScope;

@Getter
@Setter
private FoxHttpRequestQuery requestQuery = new FoxHttpRequestQuery();
Expand Down Expand Up @@ -128,6 +130,9 @@ private FoxHttpResponse executeHttp(boolean isHttps) throws FoxHttpException {
foxHttpClient.getFoxHttpLogger().log("setCookieStore(" + foxHttpClient.getFoxHttpCookieStore() + ")");
CookieHandler.setDefault((CookieManager) foxHttpClient.getFoxHttpCookieStore());

// Create Scope
authScope = FoxHttpAuthorizationScope.create(url.toString(), requestType);

foxHttpClient.getFoxHttpLogger().log("prepareQuery(" + getRequestQuery() + ")");
prepareQuery();

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

//Set headers
foxHttpClient.getFoxHttpLogger().log("prepareHeader(" + getRequestHeader() + ")");
prepareHeader(connection);
prepareHeader();

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

//Process authorization strategy
foxHttpClient.getFoxHttpLogger().log("processAuthorizationStrategy(" + foxHttpClient.getFoxHttpAuthorizationStrategy() + ")");
processAuthorizationStrategy(connection);
processAuthorizationStrategy();

//Execute interceptor
foxHttpClient.getFoxHttpLogger().log("executeRequestHeaderInterceptor()");
Expand All @@ -198,7 +203,7 @@ private FoxHttpResponse executeHttp(boolean isHttps) throws FoxHttpException {
setHeaderIfNotExist(HeaderTypes.CONTENT_TYPE, requestBody.getOutputContentType().toString(), connection);
//Set request body
foxHttpClient.getFoxHttpLogger().log("setRequestBodyStream(" + getRequestBody() + ")");
setRequestBodyStream(connection);
setRequestBodyStream();
}

foxHttpClient.getFoxHttpLogger().log("sendRequest()");
Expand Down Expand Up @@ -231,7 +236,7 @@ private FoxHttpResponse executeHttp(boolean isHttps) throws FoxHttpException {
foxHttpResponse = new FoxHttpResponse(is, this, responseCode, foxHttpClient);
//Process response headers
foxHttpClient.getFoxHttpLogger().log("processResponseHeader()");
processResponseHeader(foxHttpResponse, connection);
processResponseHeader();

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

}

private void prepareHeader(URLConnection connection) {
private void prepareHeader() {
for (HeaderEntry headerField : getRequestHeader()) {
connection.addRequestProperty(headerField.getName(), headerField.getValue());
}
}

private void setRequestBodyStream(URLConnection urlConnection) throws FoxHttpRequestException {
requestBody.setBody(new FoxHttpRequestBodyContext(urlConnection, this, foxHttpClient));
private void setRequestBodyStream() throws FoxHttpRequestException {
requestBody.setBody(new FoxHttpRequestBodyContext(connection, this, foxHttpClient));
}

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

private void processAuthorizationStrategy(URLConnection connection) throws FoxHttpRequestException {
List<FoxHttpAuthorization> foxHttpAuthorizations = foxHttpClient.getFoxHttpAuthorizationStrategy().getAuthorization(connection, FoxHttpAuthorizationScope.create(
url.toString(), RequestType.valueOf(((HttpURLConnection) connection).getRequestMethod())), foxHttpClient
private void processAuthorizationStrategy() throws FoxHttpRequestException {
List<FoxHttpAuthorization> foxHttpAuthorizations = foxHttpClient.getFoxHttpAuthorizationStrategy().getAuthorization(connection, authScope, foxHttpClient
);
for (FoxHttpAuthorization foxHttpAuthorization : foxHttpAuthorizations) {
foxHttpClient.getFoxHttpLogger().log("-> doAuthorization(" + foxHttpAuthorization + ")");
foxHttpAuthorization.doAuthorization(connection, FoxHttpAuthorizationScope.create(
url.toString(), RequestType.valueOf(((HttpURLConnection) connection).getRequestMethod()))
);
foxHttpAuthorization.doAuthorization(connection, authScope);
}
}

private void processResponseHeader(FoxHttpResponse foxHttpResponse, URLConnection connection) {
private void processResponseHeader() {
FoxHttpHeader responseHeaders = new FoxHttpHeader();
Map<String, List<String>> map = connection.getHeaderFields();
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ch.viascom.groundwork.foxhttp.FoxHttpClient;
import ch.viascom.groundwork.foxhttp.FoxHttpRequest;
import ch.viascom.groundwork.foxhttp.authorization.FoxHttpAuthorizationScope;
import ch.viascom.groundwork.foxhttp.component.FoxHttpComponent;
import ch.viascom.groundwork.foxhttp.component.oauth2.authorization.OAuth2Authorization;
import ch.viascom.groundwork.foxhttp.component.oauth2.authorization.OAuth2BearerTokenAuthorization;
Expand Down Expand Up @@ -52,10 +53,12 @@ public OAuth2Component(OAuth2Store oAuth2Store) {
public void initiation(FoxHttpClient foxHttpClient) throws FoxHttpException {
this.foxHttpClient = foxHttpClient;
foxHttpClient.getFoxHttpLogger().log("========= Initiate OAuth2Component =========");
foxHttpClient.getFoxHttpLogger().log("-> Register authorization");
foxHttpClient.getFoxHttpLogger().log("-> Register authorizations");
//Register authorization
oAuth2Authorization = new OAuth2BearerTokenAuthorization(oAuth2Store.getAccessToken());
foxHttpClient.getFoxHttpAuthorizationStrategy().addAuthorization(oAuth2Store.getAuthScope(), oAuth2Authorization);
for (FoxHttpAuthorizationScope scope : oAuth2Store.getAuthScopes()) {
foxHttpClient.getFoxHttpAuthorizationStrategy().addAuthorization(scope, oAuth2Authorization);
}
foxHttpClient.getFoxHttpLogger().log("-> Register interceptor");
//Register interceptor
foxHttpClient.register(FoxHttpInterceptorType.REQUEST_CONNECTION, new OAuth2RequestInterceptor(this, 100));
Expand All @@ -67,4 +70,31 @@ public FoxHttpRequest generateRequestForGrantType(GrantType grantType) throws Ma
OAuth2RequestGenerator oAuth2RequestGenerator = oAuth2RequestGenerators.get(grantType);
return oAuth2RequestGenerator.getRequest(this);
}

/**
* Request a new token based on the configuration
*
* @param grantType grant type to use
* @return access token from the response
* @throws FoxHttpException
* @throws MalformedURLException
*/
public String getNewToken(GrantType grantType) throws FoxHttpException, MalformedURLException {
FoxHttpRequest request = this.generateRequestForGrantType(grantType);
this.getOAuth2RequestExecutor().executeOAuth2Request(request, this);
return getOAuth2Store().getAccessToken();
}

/**
* Request a new token based on the configuration
*
* @return access token from the response
* @throws FoxHttpException
* @throws MalformedURLException
*/
public String getNewToken() throws FoxHttpException, MalformedURLException {
FoxHttpRequest request = this.generateRequestForGrantType(this.getOAuth2Store().getGrantType());
this.getOAuth2RequestExecutor().executeOAuth2Request(request, this);
return getOAuth2Store().getAccessToken();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import lombok.Data;
import org.joda.time.DateTime;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -34,7 +36,7 @@ public class OAuth2Store {
/**
* AuthorizationScope which needs a token
*/
private FoxHttpAuthorizationScope authScope;
private List<FoxHttpAuthorizationScope> authScopes = new ArrayList<>();
/**
* Additional parameters which will be included in the token request
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ch.viascom.groundwork.foxhttp.authorization.FoxHttpAuthorizationScope;
import ch.viascom.groundwork.foxhttp.type.RequestType;

import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -49,13 +50,24 @@ public OAuth2StoreBuilder setRequestScopes(String requestScopes) {
}

/**
* Set authorization scope which needs a token
* Set authorization scopes which needs a token
*
* @param authScopes authorization scopes which needs a token
* @return OAuth2StoreBuilder (this)
*/
public OAuth2StoreBuilder setFoxHttpAuthorizationScopes(List<FoxHttpAuthorizationScope> authScopes) {
oAuth2Store.setAuthScopes(authScopes);
return this;
}

/**
* Add authorization scope which needs a token
*
* @param authScope authorization scope which needs a token
* @return OAuth2StoreBuilder (this)
*/
public OAuth2StoreBuilder setFoxHttpAuthorizationScope(FoxHttpAuthorizationScope authScope) {
oAuth2Store.setAuthScope(authScope);
public OAuth2StoreBuilder addFoxHttpAuthorizationScope(FoxHttpAuthorizationScope authScope) {
oAuth2Store.getAuthScopes().add(authScope);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.viascom.groundwork.foxhttp.component.oauth2.interceptor;

import ch.viascom.groundwork.foxhttp.FoxHttpRequest;
import ch.viascom.groundwork.foxhttp.authorization.FoxHttpAuthorizationScope;
import ch.viascom.groundwork.foxhttp.component.oauth2.GrantType;
import ch.viascom.groundwork.foxhttp.component.oauth2.OAuth2Component;
import ch.viascom.groundwork.foxhttp.exception.FoxHttpException;
Expand Down Expand Up @@ -30,9 +31,7 @@ public class OAuth2RequestInterceptor implements FoxHttpRequestConnectionInterce
@Override
public void onIntercept(FoxHttpRequestConnectionInterceptorContext context) throws FoxHttpException {
try {
if (RegexUtil.doesURLMatch(context.getRequest().getRequestType() + " " +
context.getClient().getFoxHttpPlaceholderStrategy().processPlaceholders(context.getUrl().toString(), context.getClient()),
context.getClient().getFoxHttpPlaceholderStrategy().processPlaceholders(oAuth2Component.getOAuth2Store().getAuthScope().toString(), context.getClient()))) {
if (isScopePresent(context)) {
context.getClient().getFoxHttpLogger().log(" -> OAuth2 is needed for this request");
if (!isAccessTokenValid()) {
context.getClient().getFoxHttpLogger().log(" -> New OAuth2 token is needed");
Expand All @@ -50,6 +49,18 @@ public void onIntercept(FoxHttpRequestConnectionInterceptorContext context) thro
}
}

private boolean isScopePresent(FoxHttpRequestConnectionInterceptorContext context) {
boolean isPresent = false;
for (FoxHttpAuthorizationScope scope : oAuth2Component.getOAuth2Store().getAuthScopes()) {
isPresent = RegexUtil.doesURLMatch(context.getRequest().getRequestType() + " " + context.getClient().getFoxHttpPlaceholderStrategy().processPlaceholders(context.getUrl().toString(), context.getClient()),
context.getClient().getFoxHttpPlaceholderStrategy().processPlaceholders(scope.toString(), context.getClient()));
if (isPresent) {
break;
}
}
return isPresent;
}

private boolean isAccessTokenValid() {
if (oAuth2Component.getOAuth2Store().getAccessToken().isEmpty()) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ch.viascom.groundwork.foxhttp.FoxHttpRequest;
import ch.viascom.groundwork.foxhttp.FoxHttpResponse;
import ch.viascom.groundwork.foxhttp.authorization.FoxHttpAuthorizationScope;
import ch.viascom.groundwork.foxhttp.component.oauth2.OAuth2Component;
import ch.viascom.groundwork.foxhttp.component.oauth2.response.OAuthTokenErrorResponse;
import ch.viascom.groundwork.foxhttp.component.oauth2.response.OAuthTokenResponse;
Expand All @@ -26,13 +27,11 @@ public void executeOAuth2Request(FoxHttpRequest foxHttpRequest, OAuth2Component
);
oAuth2Component.getOAuth2Store().setScopes(tokenResponse.getScope());

oAuth2Component.getFoxHttpClient().getFoxHttpAuthorizationStrategy().removeAuthorization(
oAuth2Component.getOAuth2Store().getAuthScope(), oAuth2Component.getOAuth2Authorization()
);
oAuth2Component.getOAuth2Authorization().setValue(oAuth2Component.getOAuth2Store().getAccessToken());
oAuth2Component.getFoxHttpClient().getFoxHttpAuthorizationStrategy().addAuthorization(
oAuth2Component.getOAuth2Store().getAuthScope(), oAuth2Component.getOAuth2Authorization()
);
for (FoxHttpAuthorizationScope scope : oAuth2Component.getOAuth2Store().getAuthScopes()) {
oAuth2Component.getFoxHttpClient().getFoxHttpAuthorizationStrategy().removeAuthorization(scope, oAuth2Component.getOAuth2Authorization());
oAuth2Component.getOAuth2Authorization().setValue(oAuth2Component.getOAuth2Store().getAccessToken());
oAuth2Component.getFoxHttpClient().getFoxHttpAuthorizationStrategy().addAuthorization(scope, oAuth2Component.getOAuth2Authorization());
}

} else {
OAuthTokenErrorResponse tokenErrorResponse = response.getParsedBody(OAuthTokenErrorResponse.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@
public class DefaultServiceResultFaultInterceptor implements FoxHttpResponseInterceptor {

/**
* Set as last interceptors of all
* Sets the weight to 1'000 of the DefaultServiceResultFaultInterceptor
* <p>
* Interceptors with a smaller weight number will be executed before this one
* and interceptors with a higher weight number will be executed after this interceptor.
* <p>
* Override the weight if you wanna change the execution order of this interceptor.
*
* @return Integer.MAX_VALUE
* @return 1000
*/
@Override
public int getWeight() {
return Integer.MAX_VALUE;
return 1_000;
}

@Override
Expand Down
Loading

0 comments on commit 79efa09

Please sign in to comment.