Google App Engine tutorial

35
Google App Engine tutorial - 2013 1 Google App Engine tutorial

description

Google App Engine tutorial for Java. Demonstrates how to open an account, setup a connection between your server and an Android app and some more features of GAE.

Transcript of Google App Engine tutorial

Page 1: Google App Engine tutorial

Google App Engine tutorial - 2013 1

Google App Engine tutorial

Page 2: Google App Engine tutorial

Google App Engine tutorial - 2013 2

• Open an account • Installing GAE plugin for eclipse

• Installing JRE and JDK

• Register & Login example • GAE Junit • More features of GAE

• Example source code

Note: As for now, it is possible to write your server side code in Python, Java or PHP. This slideshow demonstrates an example using Java.

Table of contents

Page 3: Google App Engine tutorial

Google App Engine tutorial - 2013 3

Open an account

Suppose we have a Gmail account: [email protected]. Go to the address: https://developers.google.com/appengine/ and click Sign Up.

Page 4: Google App Engine tutorial

Google App Engine tutorial - 2013 4

Open an account

Fill in the project name. The Project ID is your unique server name. You can use the one that was generate for you or set one as you like as long as no one else is already using it. Your server address will be http://<your_project_id>.appspot.com. Click Get Started.

Page 5: Google App Engine tutorial

Google App Engine tutorial - 2013 5

Open an account

In the next window click your project name.

Page 6: Google App Engine tutorial

Google App Engine tutorial - 2013 6

Open an account

Here you can view all of your projects. You can open up to 10 projects per Gmail account. Click your project name.

Page 7: Google App Engine tutorial

Google App Engine tutorial - 2013 7

Open an account

Now click App Engine.

Page 8: Google App Engine tutorial

Google App Engine tutorial - 2013 8

Open an account

You made it. That’s your main dashboard. Here you manage your app.

Page 9: Google App Engine tutorial

Google App Engine tutorial - 2013 9

Installing GAE plugin for eclipse

Depends on your eclipse version, follow the instructions in the relevant link: https://developers.google.com/eclipse/docs/install-eclipse-4.3 https://developers.google.com/eclipse/docs/install-eclipse-4.2 https://developers.google.com/eclipse/docs/install-eclipse-3.7 When finished you’ll see a blue Google symbol ( ) in your upper toolbar. Now sign in to your account: In the lower right corner click Sign in to Google:

Page 10: Google App Engine tutorial

Google App Engine tutorial - 2013 10

You may have problems with your JDK or JRE, so download the latest versions from the java site: http://www.oracle.com/technetwork/java/javase/downloads/jre7-downloads-1880261.html http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html

Installing JRE and JDK

Page 11: Google App Engine tutorial

Google App Engine tutorial - 2013 11

Register & Login example

A simple example only to understand how to communicate between your Android app and your GAE server. We will use POST request. Usually Get request is for getting back data from the sever, while POST request is for storing data in the server. In Get request the parameters are passed in the url, while in POST request a package is being sent to the server with the parameters encoded inside. The following example uses POST request both for getting back data from the server and for storing data in the server. The reason is that in order to get back the data from the server, our password is being sent on the web and we don’t want it to appear in the url. Note: Using POST request is not a sufficient protection for sending password on the web. An SSL protection is needed as will be shown next.

Page 12: Google App Engine tutorial

Google App Engine tutorial - 2013 12

Register & Login example – Server side

Click the Google symbol ( ) in your eclipse. Then choose New Web Application Project…

Page 13: Google App Engine tutorial

Google App Engine tutorial - 2013 13

Fill in your server side app name and also a package name. Click Finish.

Register & Login example – Server side

Page 14: Google App Engine tutorial

Google App Engine tutorial - 2013 14

Your server side project should look like in the image. Predictable problems: - If you are missing the App Engine SDK or the JRE System Library then: Right click your project name > properties > Java Build Path > choose Libraries tab > in the menu on the right choose Add Library… > choose Google App Engine > Next > Finish. In the same way for the JRE but instead of choosing Google App Engine choose JRE System Library.

- If the war\WEB-INF\lib is empty then try: Right click your project name > properties > expand Google > App Engine > uncheck Use Google App Engine box > OK. Then do it again but now check the Use Google App Engine box and your libraries should be added. Note: In general Project > Clean do magic.

Register & Login example – Server side

Page 15: Google App Engine tutorial

Google App Engine tutorial - 2013 15

Now Go to appengine-web.xml and set your project id. That’s the connection between your code and the Google App Engine project you opened earlier.

Register & Login example – Server side

Page 17: Google App Engine tutorial

Google App Engine tutorial - 2013 17

Define the two classes we’ve just created as servlets, and map them to the corresponding urls. For example, according to the written on the right the url https://clean-abacus-384.appspot.com/login is mapped to the servlet Login which is referred to the com.serversideexample.Login class, which we defined in the previous slide. In lines 34-42 you can see the definition of the security constraint for using SSL. For further reading about applying SSL protection in GAE: https://developers.google.com/appengine/docs/java/config/webxml#Secure_URLs

Register & Login example – Server side

Page 18: Google App Engine tutorial

Google App Engine tutorial - 2013 18

Register class extends HttpServlet in order to define the Register servlet. The datastore may contain many kinds of entities all together in the same table (there is only one table). Here we create a Person kind entity by specifying it in the entity’s constructor.

Register & Login example – Server side

Page 19: Google App Engine tutorial

Google App Engine tutorial - 2013 19

As before Login class extends HttpServlet in order to define the Login servlet. We should specify which entity we’re looking for (line 47). Note: in CompositeFilter also “or” operator may be used. In FilterOperator also “gt”, ”gte”, ”lt”, ”lte”, ”ne”, ”in” may be used.

Register & Login example – Server side

Page 20: Google App Engine tutorial

Google App Engine tutorial - 2013 20

Deploy your project: Right click your project name > Google > Deploy to App Engine and then click Deploy.

Register & Login example – Server side

Page 21: Google App Engine tutorial

Google App Engine tutorial - 2013 21

Register & Login example – Client side

Suppose you have a little experience in Android. Create a new app with the following permissions:

Now set main_activity.xml to look like that:

Page 22: Google App Engine tutorial

Google App Engine tutorial - 2013 22

Register & Login example – Client side A simple activity which listens the two buttons and calls the execute function in the ClientAsync class in order to perform the POST request to the GAE server.

Page 23: Google App Engine tutorial

Google App Engine tutorial - 2013 23

Register & Login example – Client side

ClientAsync extends AsyncTask in order to work in background, sets a POST request with the encoded parameters which the server is expecting for. ClientAsync uses HttpsURLConnection for supporting GAE’s SSL security constraint. Finally displays the server’s answer with the TextView object.

Page 24: Google App Engine tutorial

Google App Engine tutorial - 2013 24

Register & Login example – Testing

Starting point: Datastore is empty. Fill in some details and click Register.

Page 25: Google App Engine tutorial

Google App Engine tutorial - 2013 25

Register & Login example – Testing

After clicking Register the Person will be shown in the datastore. Note: The App engine gives every entity an unique id (key). For further reading about keys: https://developers.google.com/appengine/docs/java/datastore/entities#Java_Generating_keys

Page 26: Google App Engine tutorial

Google App Engine tutorial - 2013 26

Register & Login example – Testing

Further Login Testing:

Page 27: Google App Engine tutorial

Google App Engine tutorial - 2013 27

GAE Junit Test your code in a convenient way. Setting up the framework: • Right click your project name > properties > Java Build Path > choose Libraries tab > in the menu on the right choose Add External Jars… > go to eclipse\plugins\com.google.appengine.eclipse.sdkbundle_1.8.6\appengine-java-sdk-1.8.6\lib\impl and select appengine-api.jar, appengine-api-labs.jar, appengine-api-stubs.jar > Open > OK. • Right click your project name > properties > Java Build Path > choose Libraries tab > in the menu on the right choose Add External Jars… > go to eclipse\plugins\com.google.appengine.eclipse.sdkbundle_1.8.6\appengine-java-sdk-1.8.6\lib\testing and select appengine-testing.jar > Open > OK.

• Right click your project name > properties > Java Build Path > choose Libraries tab > in the menu on the right choose Add Library… > choose Juint > Next > choose Junit 4 > Finish.

For further reading about Junit: https://developers.google.com/appengine/docs/java/tools/localunittesting

Page 28: Google App Engine tutorial

Google App Engine tutorial - 2013 28

GAE Junit Junit: To run the tests do Run > Run As… > Junit Test Note: The changes are local and have no effect on your GAE datastore.

Page 29: Google App Engine tutorial

Google App Engine tutorial - 2013 29

More features of GAE

Jackson/Gson: An easy way to transfer objects threw the web. User class should be at the server side and at the client side. In the server side we convert user to json string into the response and in the client side we read the json string and convert it back to user.

For further reading about Gson: https://code.google.com/p/google-gson/

Server side:

Client side:

For further reading about Jackson: http://wiki.fasterxml.com/JacksonInFiveMinutes http://wiki.fasterxml.com/JacksonDownload

Page 30: Google App Engine tutorial

Google App Engine tutorial - 2013 30

More features of GAE

Objectify/JDO/JPA: It’s possible to define an entity as a class. More comfortable, less dirty code.

For further reading about Objectify: https://code.google.com/p/objectify-appengine/ For further reading about JDO: https://developers.google.com/appengine/docs/java/datastore/jdo/overview-dn2 For further reading about JPA: https://developers.google.com/appengine/docs/java/datastore/jpa/overview

Page 31: Google App Engine tutorial

Google App Engine tutorial - 2013 31

More features of GAE

Sending mail from your server: You can send and receive emails on behalf of your project ID. Great for password recovery, confirm registration etc.

For further reading about mailing options: https://developers.google.com/appengine/docs/java/mail/

Page 32: Google App Engine tutorial

Google App Engine tutorial - 2013 32

More features of GAE

Logging messages to Logs console: You can log out notes of some categories: severe, warning and info. You can view your logs in the Logs section in your app’s console. Great for debugging

For further reading about logging: https://developers.google.com/appengine/docs/java/#Java_Logging

Page 33: Google App Engine tutorial

Google App Engine tutorial - 2013 33

More features of GAE

Running on local host: You can run your app locally. Do Run As > Web Application. Your server’s url now is http://localhost:8888/. You can get to your app’s console in: http://localhost:8888/_ah/admin. Note: you may need to remove your SSL protection (use http://...) when testing locally.

Page 34: Google App Engine tutorial

Google App Engine tutorial - 2013 34

More features of GAE More relevant topics: • Transactions: atomic operations in the datastore. Obligatory for concurrent writes to datastore. https://developers.google.com/appengine/docs/java/datastore/transactions • ThreadSafe: allowing your server to run your server side in parallel. https://developers.google.com/appengine/docs/java/config/appconfig#Java_appengine_web_xml_Using_concurrent_requests • Backends: background services for heavy computations. https://developers.google.com/appengine/docs/java/backends/ •Blobstore: storing large files like images, videos etc. https://developers.google.com/appengine/docs/java/blobstore/ • Quotas: limits for free datastore usage. https://developers.google.com/appengine/docs/quotas#Datastore