1818
1919import com .fasterxml .jackson .core .JsonProcessingException ;
2020import com .fasterxml .jackson .databind .ObjectMapper ;
21- import com .firebase .client .AuthData ;
22- import com .firebase .client .DataSnapshot ;
23- import com .firebase .client .Firebase ;
24- import com .firebase .client .FirebaseError ;
25- import com .firebase .client .ValueEventListener ;
26- import com .firebase .security .token .TokenGenerator ;
2721import com .google .appengine .api .utils .SystemProperty ;
22+ import com .google .firebase .FirebaseApp ;
23+ import com .google .firebase .FirebaseOptions ;
24+ import com .google .firebase .database .DataSnapshot ;
25+ import com .google .firebase .database .DatabaseError ;
26+ import com .google .firebase .database .DatabaseReference ;
27+ import com .google .firebase .database .FirebaseDatabase ;
28+ import com .google .firebase .database .ValueEventListener ;
2829
2930import java .io .FileInputStream ;
3031import java .io .IOException ;
31- import java .io .InputStream ;
3232import java .net .HttpURLConnection ;
3333import java .net .URL ;
3434import java .net .URLEncoder ;
3535import java .util .HashMap ;
3636import java .util .Map ;
37- import java .util .Properties ;
3837import java .util .logging .Logger ;
3938
4039public class FirebaseEventProxy {
4140
4241 private static final Logger log = Logger .getLogger (FirebaseEventProxy .class .getName ());
4342
44- private String firebaseAuthToken ;
45-
4643 public FirebaseEventProxy () {
47- // Store Firebase authentication token as an instance variable.
48- this .firebaseAuthToken = this .getFirebaseAuthToken (this .getFirebaseSecret ());
44+ String firebaseLocation = "https://crackling-torch-392.firebaseio.com" ;
45+ Map <String , Object > databaseAuthVariableOverride = new HashMap <String , Object >();
46+ // uid and provider will have to match what you have in your firebase security rules
47+ databaseAuthVariableOverride .put ("uid" , "gae-firebase-event-proxy" );
48+ databaseAuthVariableOverride .put ("provider" , "com.example" );
49+ try {
50+ FirebaseOptions options = new FirebaseOptions .Builder ()
51+ .setServiceAccount (new FileInputStream ("gae-firebase-secrets.json" ))
52+ .setDatabaseUrl (firebaseLocation )
53+ .setDatabaseAuthVariableOverride (databaseAuthVariableOverride ).build ();
54+ FirebaseApp .initializeApp (options );
55+ } catch (IOException e ) {
56+ throw new RuntimeException (
57+ "Error reading firebase secrets from file: src/main/webapp/gae-firebase-secrets.json: "
58+ + e .getMessage ());
59+ }
4960 }
5061
5162 public void start () {
52- String firebaseLocation = "https://gae-fb-proxy.firebaseio.com/" ;
53- Firebase firebase = new Firebase (firebaseLocation );
54-
55- // Authenticate with Firebase
56- firebase .authWithCustomToken (this .firebaseAuthToken , new Firebase .AuthResultHandler () {
57- @ Override
58- public void onAuthenticationError (FirebaseError error ) {
59- log .severe ("Firebase login error: " + error .getMessage ());
60- }
61-
62- @ Override
63- public void onAuthenticated (AuthData auth ) {
64- log .info ("Firebase login successful" );
65- }
66- });
63+ DatabaseReference firebase = FirebaseDatabase .getInstance ().getReference ();
6764
6865 // Subscribe to value events. Depending on use case, you may want to subscribe to child events
6966 // through childEventListener.
@@ -73,7 +70,7 @@ public void onDataChange(DataSnapshot snapshot) {
7370 if (snapshot .exists ()) {
7471 try {
7572 // Convert value to JSON using Jackson
76- String json = new ObjectMapper ().writeValueAsString (snapshot .getValue ());
73+ String json = new ObjectMapper ().writeValueAsString (snapshot .getValue (false ));
7774
7875 // Replace the URL with the url of your own listener app.
7976 URL dest = new URL ("http://gae-firebase-listener-python.appspot.com/log" );
@@ -109,36 +106,9 @@ public void onDataChange(DataSnapshot snapshot) {
109106 }
110107
111108 @ Override
112- public void onCancelled (FirebaseError error ) {
109+ public void onCancelled (DatabaseError error ) {
113110 log .severe ("Firebase connection cancelled: " + error .getMessage ());
114111 }
115112 });
116113 }
117-
118- private String getFirebaseSecret () {
119- Properties props = new Properties ();
120- try {
121- // Read from src/main/webapp/firebase-secrets.properties
122- InputStream inputStream = new FileInputStream ("firebase-secret.properties" );
123- props .load (inputStream );
124- return props .getProperty ("firebaseSecret" );
125- } catch (java .net .MalformedURLException e ) {
126- throw new RuntimeException (
127- "Error reading firebase secrets from file: src/main/webapp/firebase-sercrets.properties: "
128- + e .getMessage ());
129- } catch (IOException e ) {
130- throw new RuntimeException (
131- "Error reading firebase secrets from file: src/main/webapp/firebase-sercrets.properties: "
132- + e .getMessage ());
133- }
134- }
135-
136- private String getFirebaseAuthToken (String firebaseSecret ) {
137- Map <String , Object > authPayload = new HashMap <String , Object >();
138- // uid and provider will have to match what you have in your firebase security rules
139- authPayload .put ("uid" , "gae-firebase-event-proxy" );
140- authPayload .put ("provider" , "com.example" );
141- TokenGenerator tokenGenerator = new TokenGenerator (firebaseSecret );
142- return tokenGenerator .createToken (authPayload );
143- }
144114}
0 commit comments