Search in sources :

Example 1 with Firestore

use of com.google.cloud.firestore.Firestore in project getting-started-java by GoogleCloudPlatform.

the class UserJourneyTestIT method tearDownClass.

@AfterClass
public static void tearDownClass() throws ExecutionException, InterruptedException {
    // Clear the firestore list if we're not using the local emulator
    Firestore firestore = FirestoreOptions.getDefaultInstance().getService();
    for (QueryDocumentSnapshot docSnapshot : firestore.collection("translations").get().get().getDocuments()) {
        try {
            docSnapshot.getReference().delete().get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }
    service.stop();
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) Firestore(com.google.cloud.firestore.Firestore) ExecutionException(java.util.concurrent.ExecutionException) AfterClass(org.junit.AfterClass)

Example 2 with Firestore

use of com.google.cloud.firestore.Firestore in project getting-started-java by GoogleCloudPlatform.

the class TranslateServlet method doGet.

// [START getting_started_background_app_list]
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    Firestore firestore = (Firestore) this.getServletContext().getAttribute("firestore");
    CollectionReference translations = firestore.collection("translations");
    QuerySnapshot snapshot;
    try {
        snapshot = translations.limit(10).get().get();
    } catch (InterruptedException | ExecutionException e) {
        throw new ServletException("Exception retrieving documents from Firestore.", e);
    }
    List<TranslateMessage> translateMessages = Lists.newArrayList();
    List<QueryDocumentSnapshot> documents = Lists.newArrayList(snapshot.getDocuments());
    documents.sort(Comparator.comparing(DocumentSnapshot::getCreateTime));
    for (DocumentSnapshot document : Lists.reverse(documents)) {
        String encoded = gson.toJson(document.getData());
        TranslateMessage message = gson.fromJson(encoded, TranslateMessage.class);
        message.setData(decode(message.getData()));
        translateMessages.add(message);
    }
    req.setAttribute("messages", translateMessages);
    req.setAttribute("page", "list");
    req.getRequestDispatcher("/base.jsp").forward(req, resp);
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) CollectionReference(com.google.cloud.firestore.CollectionReference) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot) ServletException(javax.servlet.ServletException) QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) Firestore(com.google.cloud.firestore.Firestore) TranslateMessage(com.getstarted.background.objects.TranslateMessage) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with Firestore

use of com.google.cloud.firestore.Firestore in project getting-started-java by GoogleCloudPlatform.

the class TranslateServlet method doPost.

// [END getting_started_background_app_list]
/**
 * Handle a posted message from Pubsub.
 *
 * @param req The message Pubsub posts to this process.
 * @param resp Not used.
 */
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    // Block requests that don't contain the proper verification token.
    String pubsubVerificationToken = PUBSUB_VERIFICATION_TOKEN;
    if (req.getParameter("token").compareTo(pubsubVerificationToken) != 0) {
        resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    // [START getting_started_background_translate_string]
    String body = req.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
    PubSubMessage pubsubMessage = gson.fromJson(body, PubSubMessage.class);
    TranslateMessage message = pubsubMessage.getMessage();
    // Use Translate service client to translate the message.
    Translate translate = (Translate) this.getServletContext().getAttribute("translate");
    message.setData(decode(message.getData()));
    Translation translation = translate.translate(message.getData(), Translate.TranslateOption.sourceLanguage(message.getAttributes().getSourceLang()), Translate.TranslateOption.targetLanguage(message.getAttributes().getTargetLang()));
    // [END getting_started_background_translate_string]
    message.setTranslatedText(translation.getTranslatedText());
    try {
        // [START getting_started_background_translate]
        // Use Firestore service client to store the translation in Firestore.
        Firestore firestore = (Firestore) this.getServletContext().getAttribute("firestore");
        CollectionReference translations = firestore.collection("translations");
        ApiFuture<WriteResult> setFuture = translations.document().set(message, SetOptions.merge());
        setFuture.get();
        resp.getWriter().write(translation.getTranslatedText());
    // [END getting_started_background_translate]
    } catch (InterruptedException | ExecutionException e) {
        throw new ServletException("Exception storing data in Firestore.", e);
    }
}
Also used : Translation(com.google.cloud.translate.Translation) CollectionReference(com.google.cloud.firestore.CollectionReference) ServletException(javax.servlet.ServletException) WriteResult(com.google.cloud.firestore.WriteResult) Firestore(com.google.cloud.firestore.Firestore) PubSubMessage(com.getstarted.background.objects.PubSubMessage) TranslateMessage(com.getstarted.background.objects.TranslateMessage) ExecutionException(java.util.concurrent.ExecutionException) Translate(com.google.cloud.translate.Translate)

Example 4 with Firestore

use of com.google.cloud.firestore.Firestore in project getting-started-java by GoogleCloudPlatform.

the class BackgroundContextListener method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent event) {
    String firestoreProjectId = System.getenv("FIRESTORE_CLOUD_PROJECT");
    Firestore firestore = (Firestore) event.getServletContext().getAttribute("firestore");
    if (firestore == null) {
        firestore = FirestoreOptions.getDefaultInstance().toBuilder().setProjectId(firestoreProjectId).build().getService();
        event.getServletContext().setAttribute("firestore", firestore);
    }
    Translate translate = (Translate) event.getServletContext().getAttribute("translate");
    if (translate == null) {
        translate = TranslateOptions.getDefaultInstance().getService();
        event.getServletContext().setAttribute("translate", translate);
    }
    String topicId = System.getenv("PUBSUB_TOPIC");
    TopicName topicName = TopicName.of(firestoreProjectId, topicId);
    Publisher publisher = (Publisher) event.getServletContext().getAttribute("publisher");
    if (publisher == null) {
        try {
            publisher = Publisher.newBuilder(topicName).build();
            event.getServletContext().setAttribute("publisher", publisher);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : Firestore(com.google.cloud.firestore.Firestore) Publisher(com.google.cloud.pubsub.v1.Publisher) IOException(java.io.IOException) Translate(com.google.cloud.translate.Translate) TopicName(com.google.pubsub.v1.TopicName)

Aggregations

Firestore (com.google.cloud.firestore.Firestore)4 ExecutionException (java.util.concurrent.ExecutionException)3 TranslateMessage (com.getstarted.background.objects.TranslateMessage)2 CollectionReference (com.google.cloud.firestore.CollectionReference)2 QueryDocumentSnapshot (com.google.cloud.firestore.QueryDocumentSnapshot)2 Translate (com.google.cloud.translate.Translate)2 ServletException (javax.servlet.ServletException)2 PubSubMessage (com.getstarted.background.objects.PubSubMessage)1 DocumentSnapshot (com.google.cloud.firestore.DocumentSnapshot)1 QuerySnapshot (com.google.cloud.firestore.QuerySnapshot)1 WriteResult (com.google.cloud.firestore.WriteResult)1 Publisher (com.google.cloud.pubsub.v1.Publisher)1 Translation (com.google.cloud.translate.Translation)1 TopicName (com.google.pubsub.v1.TopicName)1 IOException (java.io.IOException)1 AfterClass (org.junit.AfterClass)1