Skip to content
Snippets Groups Projects
Commit 9cf3995c authored by Andrej Rasevic's avatar Andrej Rasevic
Browse files

completed back-end app

parent d9fde98c
No related branches found
No related tags found
No related merge requests found
...@@ -7,3 +7,4 @@ ...@@ -7,3 +7,4 @@
/build /build
/captures /captures
.externalNativeBuild .externalNativeBuild
backend/src/main/webapp/WEB-INF/*.json
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.andrejrasevic.firebasedemo"> package="com.example.andrejrasevic.firebasedemo">
<uses-permission android:name="android.permission.INTERNET" />
<application <application
android:allowBackup="true" android:allowBackup="true"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
......
// If you would like more information on the gradle-appengine-plugin please refer to the github page
// https://github.com/GoogleCloudPlatform/gradle-appengine-plugin
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.google.appengine:gradle-appengine-plugin:1.9.42'
}
}
repositories {
jcenter();
}
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'appengine'
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
dependencies {
appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.42'
compile 'javax.servlet:servlet-api:2.5'
compile 'com.google.appengine:appengine-api-1.0-sdk:1.9.58'
compile 'com.google.firebase:firebase-server-sdk:3.0.3'
compile 'org.apache.httpcomponents:httpclient:4.5.3'
}
appengine {
downloadSdk = true
appcfg {
oauth2 = true
}
}
package com.example.andrej.rasevic.myapplication.backend;
import com.google.api.client.googleapis.auth.clientlogin.ClientLogin;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Properties;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.*;
import javax.xml.ws.Response;
public class MyServlet extends HttpServlet {
static Logger Log = Logger.getLogger("com.example.andrej.rasevic.myapplication.backend.MyServlet");
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
Log.info("Sending the todo list email.");
String outString;
outString = "<p>Sending the todo list email.</p><p><strong>Note:</strong> ";
outString = outString.concat("the servlet must be deployed to App Engine in order to ");
outString = outString.concat("send the email. Running the server locally writes a message ");
outString = outString.concat("to the log file instead of sending an email message.</p>");
resp.getWriter().println(outString);
// Note: Ensure that the [PRIVATE_KEY_FILENAME].json has read
// permissions set.
FirebaseOptions options = new FirebaseOptions.Builder()
.setServiceAccount(getServletContext().getResourceAsStream("/WEB-INF/FirebaseDemo-a6320e903952.json"))
.setDatabaseUrl("https://fir-demo-d5acb.firebaseio.com/")
.build();
try {
FirebaseApp.getInstance();
Log.info("Got to here!");
}
catch (Exception error){
Log.info("doesn't exist...");
}
try {
FirebaseApp.initializeApp(options);
Log.info("now I got to here!!");
}
catch(Exception error){
Log.info("already exists...");
}
// As an admin, the app has access to read and write all data, regardless of Security Rules
DatabaseReference ref = FirebaseDatabase
.getInstance()
.getReference("todoItems");
// This fires when the servlet first runs, returning all the existing values
// only runs once, until the servlet starts up again.
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Object document = dataSnapshot.getValue();
Log.info("new value: "+ document);
String todoText = "Don't forget to...\n\n";
Iterator<DataSnapshot> children = dataSnapshot.getChildren().iterator();
while(children.hasNext()){
DataSnapshot childSnapshot = (DataSnapshot) children.next();
todoText = todoText + " * " + childSnapshot.getValue().toString() + "\n";
}
// Now send the email
// Note: When an application running in the development server calls the Mail
// service to send an email message, the message is printed to the log.
// The Java development server does not send the email message.
// You can test the email without waiting for the cron job to run by
// loading http://[FIREBASE_PROJECT_ID].appspot.com/send-email in your browser.
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
try {
Message msg = new MimeMessage(session);
//Make sure you substitute your project-id in the email From field
msg.setFrom(new InternetAddress("reminder@fir-demo-d5acb.appspotmail.com",
"FirebaseDemoApp Admin"));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress("andrej@rasevicengineering.com", "Recipient"));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress("arasevic@cs.umd.edu", "Recipient"));
msg.setSubject("Things to do today");
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress("aporter@cs.umd.edu", "Recipient"));
msg.setText(todoText);
Transport.send(msg);
} catch (MessagingException | UnsupportedEncodingException e) {
Log.warning(e.getMessage());
}
// Note: in a production application you should replace the hard-coded email address
// above with code that populates msg.addRecipient with the app user's email address.
}
@Override
public void onCancelled(DatabaseError error){
System.out.println("Error: "+error);
}
});
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>fir-demo-d5acb</application>
<version>1</version>
<threadsafe>true</threadsafe>
<manual-scaling>
<instances>1</instances>
</manual-scaling>
<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties" />
</system-properties>
</appengine-web-app>
\ No newline at end of file
# A default java.util.logging configuration.
# (All App Engine logging is through java.util.logging by default).
#
# To use this configuration, copy it into your application's WEB-INF
# folder and add the following to your appengine-web.xml:
#
# <system-properties>
# <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
# </system-properties>
#
# Set the default logging level for all loggers to WARNING
.level = WARNING
<?xml version="1.0" encoding="utf-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.example.andrej.rasevic.myapplication.backend.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/send-email</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<title>Hello, App Engine!</title>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<link rel="stylesheet"
href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css">
</head>
<body role="document" style="padding-top: 70px;">
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">Hello, App Engine!</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="https://developers.google.com/appengine/docs/java/">Google App Engine
documentation</a></li>
<li><a href="https://console.developers.google.com">Google Developers Console</a>
</li>
</ul>
</div>
</div>
</div>
<div class="container theme-showcase" role="main">
<div class="jumbotron">
<div class="row">
<div class="col-lg-12">
<h1>Hello, App Engine!</h1>
<p>Enter your name and press the button below to call <code>MyServlet</code>.</p>
<form action="/hello" method="POST">
<div class="input-group">
<input type="text" class="form-control input-lg" placeholder="Name"
name="name"/>
<span class="input-group-btn">
<button class="btn btn-default btn-primary btn-group btn-lg"
type="submit" id="helloButton">Say "Hello";</button>
</span>
</div>
</form>
<br/>
<p>For more information about Google App Engine for Java, check out the <a
href="https://developers.google.com/appengine/docs/java/">App Engine
documentation</a>.</p>
<p>If you need step-by-step instructions for connecting your Android application to
this backend module, see <a
href="https://github.com/GoogleCloudPlatform/gradle-appengine-templates/tree/master/HelloWorld">"App
Engine Java Servlet Module" template documentation</a>.</p>
</div>
</div>
</div>
</div>
</body>
</html>
include ':app' include ':app', ':backend'
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment