From 1367860bbaceea78e3c3d1d3c72d025ca9bca87c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=A1=E1=86=AB=E1=84=82=E1=85=A1=E1=84=8B?= =?UTF-8?q?=E1=85=A7=E1=86=BC?= Date: Fri, 7 Jun 2024 14:29:54 +0900 Subject: [PATCH] refactor:fcm fix --- .../common/firebase/FirebaseConfig.java | 23 +++++++------- .../common/firebase/FirebaseConfigTest.java | 30 +++++++++++++++++++ 2 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 src/test/java/com/hanaro/triptogether/common/firebase/FirebaseConfigTest.java diff --git a/src/main/java/com/hanaro/triptogether/common/firebase/FirebaseConfig.java b/src/main/java/com/hanaro/triptogether/common/firebase/FirebaseConfig.java index 0e378d7..b851f22 100644 --- a/src/main/java/com/hanaro/triptogether/common/firebase/FirebaseConfig.java +++ b/src/main/java/com/hanaro/triptogether/common/firebase/FirebaseConfig.java @@ -4,15 +4,14 @@ import com.google.firebase.FirebaseApp; import com.google.firebase.FirebaseOptions; import com.google.firebase.messaging.FirebaseMessaging; +import com.google.gson.JsonParser; +import com.google.gson.stream.JsonReader; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.util.ResourceUtils; -import java.io.ByteArrayInputStream; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; import java.nio.charset.StandardCharsets; @Configuration @@ -20,17 +19,21 @@ public class FirebaseConfig { @Bean public FirebaseApp firebaseApp() throws IOException { - InputStream resource = new ClassPathResource("./triptogether-e7bac-firebase-adminsdk-peiki-127517aa66.json").getInputStream(); - String jsonString = new String(resource.readAllBytes(), StandardCharsets.UTF_8); + InputStream resource = new ClassPathResource("triptogether-e7bac-firebase-adminsdk-peiki-127517aa66.json").getInputStream(); - // \n을 실제 줄 바꿈으로 변환 - String formattedJson = jsonString.replace("\\n", "\n"); + JsonReader jsonReader = new JsonReader(new InputStreamReader(resource)); + jsonReader.setLenient(true); - InputStream formattedJsonStream = new ByteArrayInputStream(formattedJson.getBytes(StandardCharsets.UTF_8)); + // JsonParser를 사용하여 JSON 데이터를 파싱 + var jsonElement = JsonParser.parseReader(jsonReader); + String jsonString = jsonElement.toString(); + + // 파싱된 JSON 데이터를 다시 InputStream으로 변환 + InputStream jsonInputStream = new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8)); FirebaseOptions options = FirebaseOptions .builder() - .setCredentials(GoogleCredentials.fromStream(formattedJsonStream)) + .setCredentials(GoogleCredentials.fromStream(jsonInputStream)) .build(); return FirebaseApp.initializeApp(options); } diff --git a/src/test/java/com/hanaro/triptogether/common/firebase/FirebaseConfigTest.java b/src/test/java/com/hanaro/triptogether/common/firebase/FirebaseConfigTest.java new file mode 100644 index 0000000..468be5d --- /dev/null +++ b/src/test/java/com/hanaro/triptogether/common/firebase/FirebaseConfigTest.java @@ -0,0 +1,30 @@ +package com.hanaro.triptogether.common.firebase; + +import org.junit.jupiter.api.Test; +import org.springframework.core.io.ClassPathResource; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; + +import static org.junit.jupiter.api.Assertions.*; + +class FirebaseConfigTest { + + @Test + public void test() throws IOException { + + InputStream resource = new ClassPathResource("./triptogether-e7bac-firebase-adminsdk-peiki-127517aa66.json").getInputStream(); + BufferedReader br = new BufferedReader(new InputStreamReader(resource, StandardCharsets.UTF_8)); + + String line; + while ((line = br.readLine()) != null) { + System.out.println(line); + } + } + + + +} \ No newline at end of file