07-31-2023, 09:45 AM
I am trying to create REST apis to perform write operations using [Firebase Server SDK][1] and Jersey.
Here is what I am trying to do:
1. [Initializing Firebase SDK][2]
2. [Getting idToken from Client and Validating idToken][3]
3. Performing some write operations on few Firebase nodes
4. Sending api Response
I want to return Response only if Firebase write operation succeeds or fails.
That happens inside of Firebase Listeners. However, I try to return value from Listeners, I get error becuase Firebase listeners have void return type.
How can I return response from the listener?
Here is my code.
**MyEndpoint.java**
@Path("/restapi")
//BaseEndpoint does Authentication operations
public class MyEndpoint extends BaseEndpoint {
public void Authenticate(String token, AuthenticationListener authenticationResultListener) {
super.Authenticate(token, authenticationResultListener);
}
@POST
@Produces("application/json")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response createCommunity(@FormParam("idToken") String idToken) {
Authenticate(idToken, new AuthenticationListener() {
@Override
public void onAuthenticationSuccess() {
// The app only has access as defined in the Security Rules
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("/mysamplenode");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
String res = dataSnapshot.getKey();
//I will do some write operations on Firebase here
//If they complete successfuly only then I want to return api response
return Response.status(200).entity().build();
}
@Override
public void onCancelled(DatabaseError arg0) {
System.out.println("DatabaseError-------------" + arg0);
return Response.status(500).entity("DatabaseError").build();
}
});
}
@Override
public void onAuthenticationFailure() {
return Response.status(403).entity("Forbidden").build();
}
});
}
}
**BaseEndpoint.java**
public class BaseEndpoint {
public static String uid;
public void Authenticate(String token, AuthenticationListener authenticationResultListener) {
// idToken comes from the client app
FirebaseAuth.getInstance().verifyIdToken(token).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception arg0) {
System.out.println("Uid exp= " + arg0);
authenticationResultListener.onAuthenticationFailure();
}
}).addOnSuccessListener(new OnSuccessListener<FirebaseToken>() {
@Override
public void onSuccess(FirebaseToken decodedToken) {
uid = decodedToken.getUid();
System.out.println("Uid= " + uid);
authenticationResultListener.onAuthenticationSuccess();
}
});
}
}
[1]:
Here is what I am trying to do:
1. [Initializing Firebase SDK][2]
2. [Getting idToken from Client and Validating idToken][3]
3. Performing some write operations on few Firebase nodes
4. Sending api Response
I want to return Response only if Firebase write operation succeeds or fails.
That happens inside of Firebase Listeners. However, I try to return value from Listeners, I get error becuase Firebase listeners have void return type.
How can I return response from the listener?
Here is my code.
**MyEndpoint.java**
@Path("/restapi")
//BaseEndpoint does Authentication operations
public class MyEndpoint extends BaseEndpoint {
public void Authenticate(String token, AuthenticationListener authenticationResultListener) {
super.Authenticate(token, authenticationResultListener);
}
@POST
@Produces("application/json")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response createCommunity(@FormParam("idToken") String idToken) {
Authenticate(idToken, new AuthenticationListener() {
@Override
public void onAuthenticationSuccess() {
// The app only has access as defined in the Security Rules
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("/mysamplenode");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
String res = dataSnapshot.getKey();
//I will do some write operations on Firebase here
//If they complete successfuly only then I want to return api response
return Response.status(200).entity().build();
}
@Override
public void onCancelled(DatabaseError arg0) {
System.out.println("DatabaseError-------------" + arg0);
return Response.status(500).entity("DatabaseError").build();
}
});
}
@Override
public void onAuthenticationFailure() {
return Response.status(403).entity("Forbidden").build();
}
});
}
}
**BaseEndpoint.java**
public class BaseEndpoint {
public static String uid;
public void Authenticate(String token, AuthenticationListener authenticationResultListener) {
// idToken comes from the client app
FirebaseAuth.getInstance().verifyIdToken(token).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception arg0) {
System.out.println("Uid exp= " + arg0);
authenticationResultListener.onAuthenticationFailure();
}
}).addOnSuccessListener(new OnSuccessListener<FirebaseToken>() {
@Override
public void onSuccess(FirebaseToken decodedToken) {
uid = decodedToken.getUid();
System.out.println("Uid= " + uid);
authenticationResultListener.onAuthenticationSuccess();
}
});
}
}
[1]:
[To see links please register here]
[2]:[To see links please register here]
[3]:[To see links please register here]