Skip to content

Commit 8946d9b

Browse files
Release 4.0.8
1 parent 30752dc commit 8946d9b

File tree

8 files changed

+100
-10
lines changed

8 files changed

+100
-10
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Add this dependency to your project's POM:
2323
<dependency>
2424
<groupId>ch.postfinance</groupId>
2525
<artifactId>postfinancecheckout-java-sdk</artifactId>
26-
<version>4.0.7</version>
26+
<version>4.0.8</version>
2727
<scope>compile</scope>
2828
</dependency>
2929
```
@@ -33,7 +33,7 @@ Add this dependency to your project's POM:
3333
Add this dependency to your project's build file:
3434

3535
```groovy
36-
compile "ch.postfinance:postfinancecheckout-java-sdk:4.0.7"
36+
compile "ch.postfinance:postfinancecheckout-java-sdk:4.0.8"
3737
```
3838

3939
### Others
@@ -46,7 +46,7 @@ mvn clean package
4646

4747
Then manually install the following JARs:
4848

49-
* `target/postfinancecheckout-java-sdk-4.0.7.jar`
49+
* `target/postfinancecheckout-java-sdk-4.0.8.jar`
5050
* `target/lib/*.jar`
5151

5252
## Usage

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'idea'
22
apply plugin: 'eclipse'
33

44
group = 'ch.postfinance'
5-
version = '4.0.7'
5+
version = '4.0.8'
66

77
buildscript {
88
repositories {

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ lazy val root = (project in file(".")).
22
settings(
33
organization := "ch.postfinance",
44
name := "postfinancecheckout-java-sdk",
5-
version := "4.0.7",
5+
version := "4.0.8",
66
scalaVersion := "2.11.4",
77
scalacOptions ++= Seq("-feature"),
88
javacOptions in compile ++= Seq("-Xlint:deprecation"),

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<artifactId>postfinancecheckout-java-sdk</artifactId>
66
<packaging>jar</packaging>
77
<name>postfinancecheckout-java-sdk</name>
8-
<version>4.0.7</version>
8+
<version>4.0.8</version>
99
<url>https://postfinance.ch/en/business/products/e-commerce/postfinance-checkout-all-in-one.html</url>
1010
<description>The SDK for simplifying the integration with PostFinance Checkout API.</description>
1111
<scm>

src/main/java/ch/postfinance/sdk/ApiClient.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package ch.postfinance.sdk;
22

33
import ch.postfinance.sdk.service.*;
4+
import java.util.HashMap;
5+
import java.util.Map;
46
import com.fasterxml.jackson.annotation.JsonInclude;
57
import com.fasterxml.jackson.databind.DeserializationFeature;
68
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -12,6 +14,7 @@
1214
import com.google.api.client.http.HttpRequestInitializer;
1315
import com.google.api.client.http.javanet.NetHttpTransport;
1416
import com.google.api.client.json.Json;
17+
import com.google.api.client.http.HttpHeaders;
1518

1619
import java.io.IOException;
1720
import java.io.OutputStream;
@@ -23,6 +26,7 @@ public class ApiClient {
2326
private final ObjectMapper objectMapper;
2427
private final long userId;
2528
private final String applicationKey;
29+
private final Map<String, String> defaultHeaders;
2630
public final static int READ_TIMEOUT = 20 * 1000;
2731

2832
// A reasonable default object mapper. Client can pass in a chosen ObjectMapper anyway, this is just for reasonable defaults.
@@ -36,6 +40,7 @@ private static ObjectMapper createDefaultObjectMapper() {
3640
return objectMapper;
3741
}
3842

43+
3944
/**
4045
* Constructor for ApiClient
4146
*
@@ -45,7 +50,7 @@ private static ObjectMapper createDefaultObjectMapper() {
4550
public ApiClient(long userId, String applicationKey) {
4651
this(userId, applicationKey, "https://checkout.postfinance.ch:443/api");
4752
}
48-
53+
4954
/**
5055
* Constructor for ApiClient
5156
*
@@ -66,6 +71,7 @@ public ApiClient(long userId, String applicationKey, String basePath) {
6671
this.basePath = basePath;
6772
this.userId = userId;
6873
this.applicationKey = applicationKey;
74+
this.defaultHeaders = new HashMap<>();
6975
this.httpRequestFactory = this.createRequestFactory();
7076
this.objectMapper = createDefaultObjectMapper();
7177
}
@@ -75,15 +81,19 @@ public HttpRequestFactory getHttpRequestFactory() {
7581
}
7682

7783
public HttpRequestFactory createRequestFactory() {
78-
final Auth signer = new Auth(this.userId, this.applicationKey);
84+
final RequestInterceptor interceptor = new RequestInterceptor(this.userId, this.applicationKey, this.defaultHeaders);
7985
NetHttpTransport transport = new NetHttpTransport();
8086
return transport.createRequestFactory(new HttpRequestInitializer() {
8187
public void initialize(HttpRequest request) {
82-
request.setInterceptor(signer);
88+
request.setInterceptor(interceptor);
8389
}
8490
});
8591
}
8692

93+
public void addDefaultHeader (String key, String value) {
94+
this.defaultHeaders.put(key, value);
95+
}
96+
8797
public String getBasePath() {
8898
return basePath;
8999
}

src/main/java/ch/postfinance/sdk/Auth.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/**
2020
* Auth
2121
*/
22-
public class Auth implements HttpExecuteInterceptor {
22+
public class Auth {
2323

2424
private long userId;
2525
private String applicationKey;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package ch.postfinance.sdk;
2+
3+
import com.google.api.client.http.HttpExecuteInterceptor;
4+
import com.google.api.client.http.HttpHeaders;
5+
import com.google.api.client.http.HttpRequest;
6+
7+
import java.io.IOException;
8+
import java.util.Map;
9+
10+
/**
11+
* Default headers interceptor
12+
*/
13+
public class DefaultHeaders {
14+
15+
private final Map<String, String> defaultHeaders;
16+
17+
/**
18+
*
19+
* @param defaultHeaders default headers
20+
*/
21+
public DefaultHeaders(Map<String, String> defaultHeaders) {
22+
this.defaultHeaders = defaultHeaders;
23+
}
24+
25+
/**
26+
* Intercept given http request
27+
* @param request
28+
* @throws IOException
29+
*/
30+
public void intercept(HttpRequest request) throws IOException {
31+
request.getHeaders().putAll(getDefaultHeaders());
32+
defaultHeaders.forEach((key, value) -> request.getHeaders().putIfAbsent(key, value));
33+
}
34+
35+
private HttpHeaders getDefaultHeaders() {
36+
HttpHeaders headers = new HttpHeaders();
37+
headers.put("x-meta-sdk-version", "4.0.8");
38+
headers.put("x-meta-sdk-language", "java");
39+
headers.put("x-meta-sdk-provider", "PostFinance Checkout");
40+
headers.put("x-meta-sdk-language-version", System.getProperty("java.version"));
41+
return headers;
42+
}
43+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package ch.postfinance.sdk;
2+
3+
import com.google.api.client.http.HttpExecuteInterceptor;
4+
import com.google.api.client.http.HttpRequest;
5+
6+
import java.io.IOException;
7+
import java.util.Map;
8+
9+
/**
10+
* Common http request interceptor
11+
*/
12+
public class RequestInterceptor implements HttpExecuteInterceptor {
13+
private final DefaultHeaders headerModifier;
14+
private final Auth signer;
15+
16+
/**
17+
*
18+
* @param userId user id
19+
* @param applicationKey application key
20+
* @param defaultHeaders default headers
21+
*/
22+
public RequestInterceptor(long userId, String applicationKey, Map<String, String> defaultHeaders) {
23+
this.signer = new Auth(userId, applicationKey);
24+
this.headerModifier = new DefaultHeaders(defaultHeaders);
25+
}
26+
27+
/**
28+
* Intercept given http request
29+
* @param request request
30+
* @throws IOException IOException
31+
*/
32+
@Override
33+
public void intercept(HttpRequest request) throws IOException {
34+
headerModifier.intercept(request);
35+
signer.intercept(request);
36+
}
37+
}

0 commit comments

Comments
 (0)