Skip to content

Commit f9edd7d

Browse files
misterwilliamlesv
authored andcommitted
Updated firebase-event-proxy sample code to latest Firebase SDK (GoogleCloudPlatform#244)
* Updating firebase-event-proxy/pom.xml so that it is more like other pom's and references the parent pom.xml. Adding firebase-event-proxy to parent pom.xml * Fixing checkstyle issues * Update firebase-event-proxy sample code to use latest Firebase SDK * Linting
1 parent 0a90ad5 commit f9edd7d

File tree

2 files changed

+32
-62
lines changed

2 files changed

+32
-62
lines changed

appengine/firebase-event-proxy/gae-firebase-event-proxy/pom.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,19 @@
5555
<version>1.2</version>
5656
</dependency>
5757
<dependency>
58-
<groupId>com.firebase</groupId>
59-
<artifactId>firebase-client-jvm</artifactId>
60-
<version>[1.0.8,)</version>
58+
<groupId>com.google.firebase</groupId>
59+
<artifactId>firebase-server-sdk</artifactId>
60+
<version>[3.0.0,)</version>
6161
</dependency>
6262
<dependency>
6363
<groupId>com.fasterxml.jackson.core</groupId>
6464
<artifactId>jackson-core</artifactId>
6565
<version>2.7.3</version>
6666
</dependency>
6767
<dependency>
68-
<groupId>com.firebase</groupId>
69-
<artifactId>firebase-token-generator</artifactId>
70-
<version>2.0.0</version>
68+
<groupId>com.fasterxml.jackson.core</groupId>
69+
<artifactId>jackson-databind</artifactId>
70+
<version>2.5.3</version>
7171
</dependency>
7272

7373
<!-- Test Dependencies -->

appengine/firebase-event-proxy/gae-firebase-event-proxy/src/main/java/com/example/GaeFirebaseEventProxy/FirebaseEventProxy.java

Lines changed: 26 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,52 +18,49 @@
1818

1919
import com.fasterxml.jackson.core.JsonProcessingException;
2020
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;
2721
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;
2829

2930
import java.io.FileInputStream;
3031
import java.io.IOException;
31-
import java.io.InputStream;
3232
import java.net.HttpURLConnection;
3333
import java.net.URL;
3434
import java.net.URLEncoder;
3535
import java.util.HashMap;
3636
import java.util.Map;
37-
import java.util.Properties;
3837
import java.util.logging.Logger;
3938

4039
public 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

Comments
 (0)