Skip to content

Commit 0fd32f9

Browse files
committed
Fixed readme dependencies version
2 parents 3f8d87e + ab5d006 commit 0fd32f9

File tree

7 files changed

+66
-38
lines changed

7 files changed

+66
-38
lines changed

build.gradle

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ plugins {
88
}
99

1010
group = 'io.github.waileong'
11-
version = '1.0.1'
12-
11+
version = '1.0.2'
1312
java {
1413
toolchain {
1514
languageVersion = JavaLanguageVersion.of(17)

readme.adoc

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Add the following to your `build.gradle`:
5757

5858
----
5959
dependencies {
60-
implementation 'io.github.waileong:spring-boot-fcm:1.0.1
60+
implementation 'io.github.waileong:spring-boot-fcm:1.0.2
6161
}
6262
----
6363

@@ -70,7 +70,7 @@ Insert this dependency in your `pom.xml`:
7070
<dependency>
7171
<groupId>io.github.waileong</groupId>
7272
<artifactId>spring-boot-fcm</artifactId>
73-
<version>1.0.1</version>
73+
<version>1.0.2</version>
7474
</dependency>
7575
</dependencies>
7676
----

src/main/java/io/github/waileong/fcm/authentication/FcmJwtTokenPooledObjectFactory.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package io.github.waileong.fcm.authentication;
22

3+
import io.github.waileong.fcm.config.FcmProperties;
4+
import io.github.waileong.fcm.util.RSAKeyPairUtil;
35
import io.jsonwebtoken.Jwts;
46
import io.jsonwebtoken.SignatureAlgorithm;
57
import org.apache.commons.pool2.BasePooledObjectFactory;
68
import org.apache.commons.pool2.PooledObject;
79
import org.apache.commons.pool2.impl.DefaultPooledObject;
8-
import io.github.waileong.fcm.config.FcmProperties;
9-
import io.github.waileong.fcm.util.RSAKeyPairUtil;
1010
import org.slf4j.Logger;
1111
import org.slf4j.LoggerFactory;
1212

@@ -52,7 +52,7 @@ public FcmJwtTokenPooledObjectFactory(FcmProperties.Credential credential) {
5252
@Override
5353
public FcmJwtToken create() throws Exception {
5454
Instant now = Instant.now();
55-
Instant expiration = now.plusSeconds(3600);
55+
Instant expiration = now.plus(credential.getJwtExpireDuration());
5656
String privateKeyId = credential.getPrivateKeyId();
5757
String clientEmail = credential.getClientEmail();
5858
String privateKey = credential.getPrivateKey();

src/main/java/io/github/waileong/fcm/config/FcmAutoConfiguration.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.github.waileong.fcm.config;
22

3+
import io.github.waileong.fcm.authentication.FcmJwtToken;
34
import io.github.waileong.fcm.service.impl.FcmServiceImpl;
5+
import org.apache.commons.pool2.impl.GenericObjectPool;
46
import org.springframework.beans.factory.annotation.Qualifier;
57
import org.springframework.boot.autoconfigure.AutoConfiguration;
68
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
@@ -42,7 +44,8 @@ public class FcmAutoConfiguration {
4244
* @return An instance of {@link FcmServiceImpl}, ready to be used for FCM operations within the application.
4345
*/
4446
@Bean
45-
public FcmServiceImpl fcmService(@Qualifier("fcmRestClient") RestClient fcmRestClient) {
46-
return new FcmServiceImpl(fcmRestClient);
47+
public FcmServiceImpl fcmService(@Qualifier("fcmRestClient") RestClient fcmRestClient,
48+
@Qualifier("fcmJwtTokenPool") GenericObjectPool<FcmJwtToken> googleFcmJwtTokenPool) {
49+
return new FcmServiceImpl(fcmRestClient, googleFcmJwtTokenPool);
4750
}
4851
}

src/main/java/io/github/waileong/fcm/config/FcmConnectionConfiguration.java

+2-27
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import com.fasterxml.jackson.annotation.JsonInclude;
44
import com.fasterxml.jackson.databind.ObjectMapper;
5-
import io.github.waileong.fcm.authentication.FcmJwtToken;
6-
import org.apache.commons.pool2.impl.GenericObjectPool;
75
import io.github.waileong.fcm.exception.FcmRestClientResponseErrorHandler;
86
import org.springframework.beans.factory.ObjectProvider;
97
import org.springframework.beans.factory.annotation.Qualifier;
@@ -121,13 +119,11 @@ public FcmRestClientResponseErrorHandler fcmRestClientResponseErrorHandler(
121119

122120
/**
123121
* Configures and provides a {@link RestClient} tailored for FCM communication. This REST client is configured
124-
* with FCM-specific headers, message converters, error handlers, and a token-based authentication mechanism
125-
* leveraging a JWT token pool. It is designed for sending messages to FCM services, using a base URL constructed
122+
* with FCM-specific headers, message converters, and error handlers. It is designed for sending messages to FCM services, using a base URL constructed
126123
* from the provided FCM project ID.
127124
*
128125
* @param fcmClientHttpRequestFactory The HTTP request factory for FCM.
129126
* @param fcmMessageConverter The message converter for FCM.
130-
* @param googleFcmJwtTokenPool A pool of JWT tokens for FCM authentication.
131127
* @param fcmRestClientResponseErrorHandler The error handler for FCM responses.
132128
* @param fcmProperties Properties containing the FCM credential and project ID.
133129
* @return A configured {@link RestClient} instance for FCM communication.
@@ -136,7 +132,6 @@ public FcmRestClientResponseErrorHandler fcmRestClientResponseErrorHandler(
136132
public RestClient fcmRestClient(
137133
@Qualifier("fcmClientHttpRequestFactory") ClientHttpRequestFactory fcmClientHttpRequestFactory,
138134
@Qualifier("fcmMessageConverter") MappingJackson2HttpMessageConverter fcmMessageConverter,
139-
@Qualifier("fcmJwtTokenPool") GenericObjectPool<FcmJwtToken> googleFcmJwtTokenPool,
140135
@Qualifier("fcmRestClientResponseErrorHandler") FcmRestClientResponseErrorHandler fcmRestClientResponseErrorHandler,
141136
FcmProperties fcmProperties) {
142137
String projectId = fcmProperties.getCredential().getProjectId();
@@ -149,7 +144,6 @@ public RestClient fcmRestClient(
149144
.baseUrl("https://fcm.googleapis.com/v1/projects/" + projectId + "/messages:send")
150145
.defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
151146
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
152-
.defaultHeaders(httpHeaders -> httpHeaders.setBearerAuth(getAccessToken(googleFcmJwtTokenPool)))
153147
.requestFactory(fcmClientHttpRequestFactory)
154148
.messageConverters(httpMessageConverters -> {
155149
// override the default message converters
@@ -160,24 +154,5 @@ public RestClient fcmRestClient(
160154
.build();
161155
}
162156

163-
/**
164-
* Retrieves an access token from the JWT token pool for authenticating FCM requests. This method ensures
165-
* that a valid token is always used for authentication by borrowing from and returning tokens to the pool.
166-
*
167-
* @param pool The pool of FCM JWT tokens.
168-
* @return A valid JWT token string for FCM authentication.
169-
*/
170-
private String getAccessToken(GenericObjectPool<FcmJwtToken> pool) {
171-
FcmJwtToken fcmJwtToken = null;
172-
try {
173-
fcmJwtToken = pool.borrowObject();
174-
return fcmJwtToken.token();
175-
} catch (Exception e) {
176-
throw new RuntimeException(e);
177-
} finally {
178-
if (fcmJwtToken != null) {
179-
pool.returnObject(fcmJwtToken);
180-
}
181-
}
182-
}
157+
183158
}

src/main/java/io/github/waileong/fcm/config/FcmProperties.java

+22
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ public static class Credential {
193193
* The client email associated with the Firebase service account, used for authentication.
194194
*/
195195
private String clientEmail;
196+
/**
197+
* The time period a JWT token remains valid, set to 1 hour by default.
198+
*/
199+
private Duration jwtExpireDuration = Duration.ofHours(1);
196200

197201
/**
198202
* Gets the project ID of the Firebase service account.
@@ -265,6 +269,24 @@ public String getClientEmail() {
265269
public void setClientEmail(String clientEmail) {
266270
this.clientEmail = clientEmail;
267271
}
272+
273+
/**
274+
* Gets the time period a JWT token remains valid.
275+
*
276+
* @return The duration a JWT token remains valid.
277+
*/
278+
public Duration getJwtExpireDuration() {
279+
return jwtExpireDuration;
280+
}
281+
282+
/**
283+
* Sets the time period a JWT token remains valid.
284+
*
285+
* @param jwtExpireDuration The duration a JWT token remains valid.
286+
*/
287+
public void setJwtExpireDuration(Duration jwtExpireDuration) {
288+
this.jwtExpireDuration = jwtExpireDuration;
289+
}
268290
}
269291

270292
/**

src/main/java/io/github/waileong/fcm/service/impl/FcmServiceImpl.java

+31-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package io.github.waileong.fcm.service.impl;
22

3+
import io.github.waileong.fcm.authentication.FcmJwtToken;
34
import io.github.waileong.fcm.service.FcmService;
45
import io.github.waileong.fcm.service.domain.FcmError;
6+
import io.github.waileong.fcm.service.domain.FcmErrorResponse;
57
import io.github.waileong.fcm.service.domain.FcmMessage;
68
import io.github.waileong.fcm.service.domain.FcmSendRequest;
9+
import org.apache.commons.pool2.impl.GenericObjectPool;
710
import org.slf4j.Logger;
811
import org.slf4j.LoggerFactory;
912
import org.springframework.aot.hint.annotation.RegisterReflectionForBinding;
@@ -23,21 +26,47 @@
2326
*
2427
* @author Wai Leong
2528
*/
26-
@RegisterReflectionForBinding({FcmSendRequest.class, FcmMessage.class, FcmError.class})
29+
@RegisterReflectionForBinding({FcmSendRequest.class, FcmMessage.class, FcmError.class, FcmErrorResponse.class})
2730
public class FcmServiceImpl implements FcmService {
2831
private final Logger logger = LoggerFactory.getLogger(this.getClass());
2932
private final RestClient fcmRestClient;
33+
private final GenericObjectPool<FcmJwtToken> fcmJwtTokenPool;
3034

31-
public FcmServiceImpl(RestClient fcmRestClient) {
35+
public FcmServiceImpl(RestClient fcmRestClient,
36+
GenericObjectPool<FcmJwtToken> fcmJwtTokenPool) {
3237
this.fcmRestClient = fcmRestClient;
38+
this.fcmJwtTokenPool = fcmJwtTokenPool;
3339
}
3440

3541

3642
@Override
3743
public FcmMessage send(FcmSendRequest sendRequest) {
3844
return this.fcmRestClient.post()
45+
.headers(httpHeaders -> httpHeaders.setBearerAuth(getAccessToken(fcmJwtTokenPool)))
3946
.body(sendRequest)
4047
.retrieve()
4148
.body(FcmMessage.class);
4249
}
50+
51+
/**
52+
* Retrieves an access token from the JWT token pool for authenticating FCM requests. This method ensures
53+
* that a valid token is always used for authentication by borrowing from and returning tokens to the pool.
54+
*
55+
* @param pool The pool of FCM JWT tokens.
56+
* @return A valid JWT token string for FCM authentication.
57+
*/
58+
private String getAccessToken(GenericObjectPool<FcmJwtToken> pool) {
59+
FcmJwtToken fcmJwtToken = null;
60+
try {
61+
fcmJwtToken = pool.borrowObject();
62+
return fcmJwtToken.token();
63+
} catch (Exception e) {
64+
throw new RuntimeException(e);
65+
} finally {
66+
if (fcmJwtToken != null) {
67+
pool.returnObject(fcmJwtToken);
68+
}
69+
}
70+
}
71+
4372
}

0 commit comments

Comments
 (0)