18
18
19
19
import com .fasterxml .jackson .core .JsonProcessingException ;
20
20
import 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 ;
27
21
import 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 ;
28
29
29
30
import java .io .FileInputStream ;
30
31
import java .io .IOException ;
31
- import java .io .InputStream ;
32
32
import java .net .HttpURLConnection ;
33
33
import java .net .URL ;
34
34
import java .net .URLEncoder ;
35
35
import java .util .HashMap ;
36
36
import java .util .Map ;
37
- import java .util .Properties ;
38
37
import java .util .logging .Logger ;
39
38
40
39
public class FirebaseEventProxy {
41
40
42
41
private static final Logger log = Logger .getLogger (FirebaseEventProxy .class .getName ());
43
42
44
- private String firebaseAuthToken ;
45
-
46
43
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
+ }
49
60
}
50
61
51
62
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 ();
67
64
68
65
// Subscribe to value events. Depending on use case, you may want to subscribe to child events
69
66
// through childEventListener.
@@ -73,7 +70,7 @@ public void onDataChange(DataSnapshot snapshot) {
73
70
if (snapshot .exists ()) {
74
71
try {
75
72
// Convert value to JSON using Jackson
76
- String json = new ObjectMapper ().writeValueAsString (snapshot .getValue ());
73
+ String json = new ObjectMapper ().writeValueAsString (snapshot .getValue (false ));
77
74
78
75
// Replace the URL with the url of your own listener app.
79
76
URL dest = new URL ("http://gae-firebase-listener-python.appspot.com/log" );
@@ -109,36 +106,9 @@ public void onDataChange(DataSnapshot snapshot) {
109
106
}
110
107
111
108
@ Override
112
- public void onCancelled (FirebaseError error ) {
109
+ public void onCancelled (DatabaseError error ) {
113
110
log .severe ("Firebase connection cancelled: " + error .getMessage ());
114
111
}
115
112
});
116
113
}
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
- }
144
114
}
0 commit comments