1616
1717package com .example .appengine .firetactoe ;
1818
19+ import com .google .api .client .auth .oauth2 .Credential ;
1920import com .google .api .client .extensions .appengine .http .UrlFetchTransport ;
2021import com .google .api .client .googleapis .auth .oauth2 .GoogleCredential ;
2122import com .google .api .client .http .ByteArrayContent ;
3132
3233import java .io .FileInputStream ;
3334import java .io .IOException ;
35+ import java .io .InputStream ;
3436import java .io .InputStreamReader ;
3537import java .nio .charset .StandardCharsets ;
3638import java .util .Arrays ;
4648 */
4749public class FirebaseChannel {
4850 private static final String FIREBASE_SNIPPET_PATH = "WEB-INF/view/firebase_config.jspf" ;
51+ static InputStream firebaseConfigStream = null ;
4952 private static final Collection FIREBASE_SCOPES = Arrays .asList (
5053 "https://www.googleapis.com/auth/firebase.database" ,
5154 "https://www.googleapis.com/auth/userinfo.email"
5255 );
5356 private static final String IDENTITY_ENDPOINT =
5457 "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit" ;
55- static final HttpTransport HTTP_TRANSPORT = new UrlFetchTransport ();
5658
5759 private String firebaseDbUrl ;
5860 private GoogleCredential credential ;
61+ // Keep this a package-private member variable, so that it can be mocked for unit tests
62+ HttpTransport httpTransport ;
5963
6064 private static FirebaseChannel instance ;
6165
@@ -79,11 +83,17 @@ public static FirebaseChannel getInstance() {
7983 */
8084 private FirebaseChannel () {
8185 try {
86+ // This variables exist primarily so it can be stubbed out in unit tests.
87+ if (null == firebaseConfigStream ) {
88+ firebaseConfigStream = new FileInputStream (FIREBASE_SNIPPET_PATH );
89+ }
90+
8291 String firebaseSnippet = CharStreams .toString (new InputStreamReader (
83- new FileInputStream ( FIREBASE_SNIPPET_PATH ) , StandardCharsets .UTF_8 ));
92+ firebaseConfigStream , StandardCharsets .UTF_8 ));
8493 firebaseDbUrl = parseFirebaseUrl (firebaseSnippet );
8594
8695 credential = GoogleCredential .getApplicationDefault ().createScoped (FIREBASE_SCOPES );
96+ httpTransport = UrlFetchTransport .getDefaultInstance ();
8797 } catch (IOException e ) {
8898 throw new RuntimeException (e );
8999 }
@@ -109,7 +119,7 @@ private static String parseFirebaseUrl(String firebaseSnippet) {
109119 public void sendFirebaseMessage (String channelKey , Game game )
110120 throws IOException {
111121 // Make requests auth'ed using Application Default Credentials
112- HttpRequestFactory requestFactory = HTTP_TRANSPORT .createRequestFactory (credential );
122+ HttpRequestFactory requestFactory = httpTransport .createRequestFactory (credential );
113123 GenericUrl url = new GenericUrl (
114124 String .format ("%s/channels/%s.json" , firebaseDbUrl , channelKey ));
115125 HttpResponse response = null ;
@@ -163,4 +173,63 @@ public String createFirebaseToken(Game game, String userId) {
163173 AppIdentityService .SigningResult result = appIdentity .signForApp (toSign .getBytes ());
164174 return String .format ("%s.%s" , toSign , base64 .encode (result .getSignature ()));
165175 }
176+
177+ // The following methods are to illustrate making various calls to Firebase from App Engine
178+ // Standard
179+
180+ public HttpResponse firebasePut (String path , Object object ) throws IOException {
181+ // Make requests auth'ed using Application Default Credentials
182+ Credential credential = GoogleCredential .getApplicationDefault ().createScoped (FIREBASE_SCOPES );
183+ HttpRequestFactory requestFactory = httpTransport .createRequestFactory (credential );
184+
185+ String json = new Gson ().toJson (object );
186+ GenericUrl url = new GenericUrl (path );
187+
188+ return requestFactory .buildPutRequest (
189+ url , new ByteArrayContent ("application/json" , json .getBytes ())).execute ();
190+ }
191+
192+ public HttpResponse firebasePatch (String path , Object object ) throws IOException {
193+ // Make requests auth'ed using Application Default Credentials
194+ Credential credential = GoogleCredential .getApplicationDefault ().createScoped (FIREBASE_SCOPES );
195+ HttpRequestFactory requestFactory = httpTransport .createRequestFactory (credential );
196+
197+ String json = new Gson ().toJson (object );
198+ GenericUrl url = new GenericUrl (path );
199+
200+ return requestFactory .buildPatchRequest (
201+ url , new ByteArrayContent ("application/json" , json .getBytes ())).execute ();
202+ }
203+
204+ public HttpResponse firebasePost (String path , Object object ) throws IOException {
205+ // Make requests auth'ed using Application Default Credentials
206+ Credential credential = GoogleCredential .getApplicationDefault ().createScoped (FIREBASE_SCOPES );
207+ HttpRequestFactory requestFactory = httpTransport .createRequestFactory (credential );
208+
209+ String json = new Gson ().toJson (object );
210+ GenericUrl url = new GenericUrl (path );
211+
212+ return requestFactory .buildPostRequest (
213+ url , new ByteArrayContent ("application/json" , json .getBytes ())).execute ();
214+ }
215+
216+ public HttpResponse firebaseGet (String path ) throws IOException {
217+ // Make requests auth'ed using Application Default Credentials
218+ Credential credential = GoogleCredential .getApplicationDefault ().createScoped (FIREBASE_SCOPES );
219+ HttpRequestFactory requestFactory = httpTransport .createRequestFactory (credential );
220+
221+ GenericUrl url = new GenericUrl (path );
222+
223+ return requestFactory .buildGetRequest (url ).execute ();
224+ }
225+
226+ public HttpResponse firebaseDelete (String path ) throws IOException {
227+ // Make requests auth'ed using Application Default Credentials
228+ Credential credential = GoogleCredential .getApplicationDefault ().createScoped (FIREBASE_SCOPES );
229+ HttpRequestFactory requestFactory = httpTransport .createRequestFactory (credential );
230+
231+ GenericUrl url = new GenericUrl (path );
232+
233+ return requestFactory .buildDeleteRequest (url ).execute ();
234+ }
166235}
0 commit comments