Search in sources :

Example 1 with FirebaseOptions

use of com.google.firebase.FirebaseOptions in project curiostack by curioswitch.

the class FirebaseAuthModule method firebaseApp.

@Provides
@Singleton
static FirebaseApp firebaseApp(FirebaseAuthConfig config) {
    final FirebaseOptions options;
    try {
        options = new FirebaseOptions.Builder().setCredentials(GoogleCredentials.fromStream(new ByteArrayInputStream(Base64.getDecoder().decode(config.getServiceAccountBase64())))).setDatabaseUrl("https://" + config.getProjectId() + ".firebaseio.com").build();
    } catch (IOException e) {
        throw new UncheckedIOException("Could not read certificate.", e);
    }
    FirebaseApp.initializeApp(options);
    return checkNotNull(FirebaseApp.getInstance());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) FirebaseOptions(com.google.firebase.FirebaseOptions) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 2 with FirebaseOptions

use of com.google.firebase.FirebaseOptions in project geofire-java by firebase.

the class SFVehiclesActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sfvehicles);
    // setup map and camera position
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    this.map = mapFragment.getMap();
    LatLng latLngCenter = new LatLng(INITIAL_CENTER.latitude, INITIAL_CENTER.longitude);
    this.searchCircle = this.map.addCircle(new CircleOptions().center(latLngCenter).radius(1000));
    this.searchCircle.setFillColor(Color.argb(66, 255, 0, 255));
    this.searchCircle.setStrokeColor(Color.argb(66, 0, 0, 0));
    this.map.moveCamera(CameraUpdateFactory.newLatLngZoom(latLngCenter, INITIAL_ZOOM_LEVEL));
    this.map.setOnCameraChangeListener(this);
    FirebaseOptions options = new FirebaseOptions.Builder().setApplicationId("geofire").setDatabaseUrl(GEO_FIRE_DB).build();
    FirebaseApp app = FirebaseApp.initializeApp(this, options);
    // setup GeoFire
    this.geoFire = new GeoFire(FirebaseDatabase.getInstance(app).getReferenceFromUrl(GEO_FIRE_REF));
    // radius in km
    this.geoQuery = this.geoFire.queryAtLocation(INITIAL_CENTER, 1);
    // setup markers
    this.markers = new HashMap<String, Marker>();
}
Also used : SupportMapFragment(com.google.android.gms.maps.SupportMapFragment) GeoFire(com.firebase.geofire.GeoFire) FirebaseOptions(com.google.firebase.FirebaseOptions) FirebaseApp(com.google.firebase.FirebaseApp)

Example 3 with FirebaseOptions

use of com.google.firebase.FirebaseOptions in project iosched by google.

the class LoadSessionsServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
    // Check that only admins or other allowed users can make this call.
    if (!performBasicChecking(req, resp)) {
        return;
    }
    // Pull existing session and room data from Google Cloud Storage.
    JsonParser jsonParser = new JsonParser();
    String manifestStr = IOUtils.toString(new URL(Config.CLOUD_STORAGE_BASE_URL + MANIFEST_FILENAME).openStream());
    JsonElement jManifest = jsonParser.parse(manifestStr);
    JsonArray jDataFiles = jManifest.getAsJsonObject().get("data_files").getAsJsonArray();
    String sessionDataFileName = null;
    for (int i = 0; i < jDataFiles.size(); i++) {
        String filename = jDataFiles.get(i).getAsString();
        if (filename.startsWith("session_data")) {
            sessionDataFileName = filename;
            break;
        }
    }
    if (sessionDataFileName == null) {
        // Unable to find session data to load.
        resp.setContentType("text/plain");
        resp.getWriter().println("Unable to find session data to load.");
        return;
    }
    // Get session and room data from file in GCS.
    String sessionDataStr = IOUtils.toString(new URL(Config.CLOUD_STORAGE_BASE_URL + sessionDataFileName).openStream());
    JsonElement jSessionData = jsonParser.parse(sessionDataStr);
    // Extract rooms and sessions
    final JsonArray jRooms = jSessionData.getAsJsonObject().get(ROOMS_KEY).getAsJsonArray();
    final JsonArray jSessions = jSessionData.getAsJsonObject().get(SESSIONS_KEY).getAsJsonArray();
    // Only sessions that are of type TYPE_SESSIONS can be reserved so remove those that do not
    // have this type.
    List<JsonElement> sessionsToRemove = new ArrayList<>();
    for (JsonElement jSession : jSessions) {
        // TODO: keynotes have a better type.
        if (jSession.getAsJsonObject().get("id").getAsString().startsWith("__keynote")) {
            sessionsToRemove.add(jSession);
            continue;
        }
        JsonArray jTags = jSession.getAsJsonObject().get("tags").getAsJsonArray();
        boolean isReservable = false;
        for (JsonElement jTag : jTags) {
            if (jTag.getAsString().equals("TYPE_SESSIONS")) {
                isReservable = true;
                break;
            }
        }
        if (!isReservable) {
            sessionsToRemove.add(jSession);
        }
    }
    for (JsonElement jsonElement : sessionsToRemove) {
        jSessions.remove(jsonElement);
    }
    log.info("Non-Reservable session count: " + sessionsToRemove.size());
    log.info("Reservable session count: " + jSessions.size());
    resp.setContentType("text/plain");
    resp.getWriter().println("Room and session data retrieved.");
    // Initialize Firebase app with service account credentials.
    FirebaseOptions options = new FirebaseOptions.Builder().setCredential(FirebaseCredentials.fromCertificate(getServletContext().getResourceAsStream("/WEB-INF/io2017-backend-dev-serv-cred.json"))).setDatabaseUrl("https://io2017-backend-dev.firebaseio.com/").build();
    try {
        FirebaseApp.initializeApp(options);
        log.info("Initialized Firebase");
    } catch (Exception e) {
        // Firebase Instance already exists.
        log.info("Firebase already initialized");
    }
    // Session retrieval task.
    final TaskCompletionSource<Map<String, Session>> sessionsTaskCompletionSource = new TaskCompletionSource<>();
    final Task<Map<String, Session>> sessionsTask = sessionsTaskCompletionSource.getTask();
    // Get Firebase Database reference to sessions path, add listener for single event.
    final FirebaseDatabase defaultDatabase = FirebaseDatabase.getInstance();
    defaultDatabase.getReference(PATH_SESSIONS).addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Map<String, Session> sessions = new HashMap<>();
            for (DataSnapshot sessionSnapshot : dataSnapshot.getChildren()) {
                Session s = sessionSnapshot.getValue(Session.class);
                sessions.put(sessionSnapshot.getKey(), s);
            }
            sessionsTaskCompletionSource.setResult(sessions);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            log.warning("RTDB error: " + databaseError.getMessage());
        }
    });
    try {
        // Wait for the sessions from RTDB.
        Map<String, Session> rtdbSessions = Tasks.await(sessionsTask);
        // Update sessions in RTDB with values from GCS.
        for (String sessionId : rtdbSessions.keySet()) {
            // Update session task.
            final TaskCompletionSource<Void> updateSessionTCS = new TaskCompletionSource<>();
            final Task<Void> updateSessionTCSTask = updateSessionTCS.getTask();
            // Check that GCS has a matching session and room as the one from RTDB.
            JsonObject jSession = getSession(sessionId, jSessions);
            if (jSession != null) {
                JsonObject jRoom = getRoom(jSession, jRooms);
                if (jRoom != null) {
                    final int gcsCap = Long.valueOf(Math.round(jRoom.get(CAPACITY_KEY).getAsInt() * RESERVABLE_CAPACITY_PERCENTAGE)).intValue();
                    final String gcsRoomName = jRoom.get(NAME_KEY).getAsString();
                    final long gcsStartTime = getTimeInMillis(jSession, START_TIME_KEY);
                    final long gcsEndTime = getTimeInMillis(jSession, END_TIME_KEY);
                    final String gcsTitle = jSession.get(TITLE_KEY).getAsString();
                    // Update session in a transaction.
                    defaultDatabase.getReference().child(PATH_SESSIONS).child(sessionId).runTransaction(new Handler() {

                        @Override
                        public Result doTransaction(MutableData mutableData) {
                            Session session = mutableData.getValue(Session.class);
                            if (session != null) {
                                // Update start and end times of session in RTDB.
                                session.time_start = gcsStartTime;
                                session.time_end = gcsEndTime;
                                // Update session title.
                                session.title = gcsTitle;
                                // Update session room name.
                                session.room_name = gcsRoomName;
                                int currResCount = session.seats.reserved;
                                boolean currHasSeats = session.seats.seats_available;
                                if (currResCount > gcsCap) {
                                    // If there are to many reservations move extras to waitlist.
                                    int resToMove = currResCount - gcsCap;
                                    moveReservationsToWaitList(resToMove, session);
                                } else if (currResCount < gcsCap && !currHasSeats) {
                                    // If there is space and a waitlist, promote as many as possible from the
                                    // waitlist.
                                    int numSeatsAvailable = gcsCap - currResCount;
                                    promoteFromWaitList(currResCount, numSeatsAvailable, session);
                                }
                                // Update session capacity.
                                session.seats.capacity = gcsCap;
                                mutableData.setValue(session);
                            }
                            return success(mutableData);
                        }

                        @Override
                        public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
                            // Signal that session update is complete.
                            updateSessionTCS.setResult(null);
                        }
                    });
                    // Wait for session update to complete.
                    Tasks.await(updateSessionTCSTask);
                }
                // Remove updated sessions from list of sessions to be added.
                jSessions.remove(jSession);
            }
        }
    } catch (ExecutionException | InterruptedException e) {
        e.printStackTrace();
    }
    // Add all sessions that were retrieved from GCS but did not already in RTDB.
    for (int i = 0; i < jSessions.size(); i++) {
        JsonObject jSession = jSessions.get(i).getAsJsonObject();
        String sessionId = jSession.get(ID_KEY).getAsString();
        String sessionTitle = jSession.get(TITLE_KEY).getAsString();
        String roomId = jSession.get(ROOM_KEY).getAsString();
        JsonObject jRoom = getRoom(roomId, jRooms);
        int capacity = Long.valueOf(Math.round(jRoom.get(CAPACITY_KEY).getAsInt() * RESERVABLE_CAPACITY_PERCENTAGE)).intValue();
        String sessionRoomName = jRoom.get(NAME_KEY).getAsString();
        long startTime = getTimeInMillis(jSession, START_TIME_KEY);
        long endTime = getTimeInMillis(jSession, END_TIME_KEY);
        Session session = new Session();
        session.title = sessionTitle;
        session.room_name = sessionRoomName;
        session.time_end = endTime;
        session.time_start = startTime;
        Seats seats = new Seats();
        seats.capacity = capacity;
        seats.reserved = 0;
        seats.seats_available = true;
        seats.waitlisted = false;
        session.seats = seats;
        defaultDatabase.getReference(PATH_SESSIONS).child(sessionId).setValue(session);
    }
    resp.getWriter().println("Sessions added to RTDB.");
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) DataSnapshot(com.google.firebase.database.DataSnapshot) URL(java.net.URL) Result(com.google.firebase.database.Transaction.Result) TaskCompletionSource(com.google.firebase.tasks.TaskCompletionSource) ValueEventListener(com.google.firebase.database.ValueEventListener) MutableData(com.google.firebase.database.MutableData) ExecutionException(java.util.concurrent.ExecutionException) JsonParser(com.google.gson.JsonParser) Handler(com.google.firebase.database.Transaction.Handler) FirebaseOptions(com.google.firebase.FirebaseOptions) ServletException(javax.servlet.ServletException) ParseException(java.text.ParseException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Seats(com.google.samples.apps.iosched.server.schedule.reservations.model.Seats) JsonArray(com.google.gson.JsonArray) DatabaseError(com.google.firebase.database.DatabaseError) JsonElement(com.google.gson.JsonElement) HashMap(java.util.HashMap) Map(java.util.Map) Session(com.google.samples.apps.iosched.server.schedule.reservations.model.Session)

Example 4 with FirebaseOptions

use of com.google.firebase.FirebaseOptions in project openremote by openremote.

the class PushNotificationHandler method init.

public void init(Container container) throws Exception {
    this.managerIdentityService = container.getService(ManagerIdentityService.class);
    this.assetStorageService = container.getService(AssetStorageService.class);
    this.gatewayService = container.getService(GatewayService.class);
    container.getService(MessageBrokerService.class).getContext().addRoutes(this);
    String firebaseConfigFilePath = container.getConfig().get(FIREBASE_CONFIG_FILE);
    if (TextUtil.isNullOrEmpty(firebaseConfigFilePath)) {
        LOG.warning(FIREBASE_CONFIG_FILE + " not defined, can not send FCM notifications");
        return;
    }
    if (!Files.isReadable(Paths.get(firebaseConfigFilePath))) {
        LOG.warning(FIREBASE_CONFIG_FILE + " invalid path or file not readable: " + firebaseConfigFilePath);
        return;
    }
    try (InputStream is = Files.newInputStream(Paths.get(firebaseConfigFilePath))) {
        FirebaseOptions options = new FirebaseOptions.Builder().setCredentials(GoogleCredentials.fromStream(is)).setConnectTimeout(CONNECT_TIMEOUT_MILLIS).setReadTimeout(READ_TIMEOUT_MILLIS).build();
        FirebaseApp.initializeApp(options);
        valid = true;
    } catch (Exception ex) {
        LOG.severe("Exception occurred whilst initialising FCM");
    }
}
Also used : ManagerIdentityService(org.openremote.manager.security.ManagerIdentityService) AssetStorageService(org.openremote.manager.asset.AssetStorageService) InputStream(java.io.InputStream) RouteBuilder(org.apache.camel.builder.RouteBuilder) FirebaseOptions(com.google.firebase.FirebaseOptions) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) GatewayService(org.openremote.manager.gateway.GatewayService)

Example 5 with FirebaseOptions

use of com.google.firebase.FirebaseOptions in project pratilipi by Pratilipi.

the class FirebaseApi method initialiseFirebase.

private static synchronized void initialiseFirebase() {
    if (hasBeenInitialized)
        return;
    logger.log(Level.INFO, "Firebase Apps: " + FirebaseApp.getApps());
    logger.log(Level.INFO, "Initialising Firebase...");
    String serviceAccountKey = DataAccessorFactory.getDataAccessor().getAppProperty(AppProperty.SERVICE_ACCOUNT_FIREBASE).getValue();
    FirebaseOptions options = new FirebaseOptions.Builder().setServiceAccount(IOUtils.toInputStream(serviceAccountKey, StandardCharsets.UTF_8)).setDatabaseUrl(DATABASE_URL).build();
    FirebaseApp.initializeApp(options);
    hasBeenInitialized = true;
    logger.log(Level.INFO, "Firebase has been initialised successfully!");
}
Also used : FirebaseOptions(com.google.firebase.FirebaseOptions)

Aggregations

FirebaseOptions (com.google.firebase.FirebaseOptions)8 IOException (java.io.IOException)3 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 GeoFire (com.firebase.geofire.GeoFire)1 SupportMapFragment (com.google.android.gms.maps.SupportMapFragment)1 FirebaseApp (com.google.firebase.FirebaseApp)1 DataSnapshot (com.google.firebase.database.DataSnapshot)1 DatabaseError (com.google.firebase.database.DatabaseError)1 FirebaseDatabase (com.google.firebase.database.FirebaseDatabase)1 MutableData (com.google.firebase.database.MutableData)1 Handler (com.google.firebase.database.Transaction.Handler)1 Result (com.google.firebase.database.Transaction.Result)1 ValueEventListener (com.google.firebase.database.ValueEventListener)1 TaskCompletionSource (com.google.firebase.tasks.TaskCompletionSource)1 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 Seats (com.google.samples.apps.iosched.server.schedule.reservations.model.Seats)1