googleAppEngineContacts
Folders and files
| Name | Name | Last commit date | ||
|---|---|---|---|---|
parent directory.. | ||||
This is sample code for Chapter 7 of Wiley's Enterprise Android. The sample creates a RESTful Contacts service with backend persistence support from Google App Engine. This service provides an operational backend for the examples in chapter 5. The example assumes that: * You are familiar with the code in syncAdapterContacts and on springServiceContacts. You can use any of the three major operating systems for PCs to develop the web backend: Linux, Mac OS, or Windows. The OS is not particularly relevant for backend services. The code is likely to work on a variety of software versions, but was specifically tested on: Java (1.7.0_25), Appengine-java-sdk(1.8.0), EclipseEE (Kepler) === Prepare: The App Engine example also relies on Spring, but not on Tomcat, and of course, requires a Google account for App Engine use, so create one if needed. Log in to your Google account and then go to: https://appengine.google.com/ 1. Create a Google App Engine Service Click the Create Application button, and then follow the subsequent instructions to create a Google App Engine application. Start by creating a Google "application identifier", which is a unique string you pick between 6 and 30 lowercase characters; be forewarned, many strings are already taken, we used wileyenterpriseandroidea. Pick a title, select an access level, and sign the terms of use. Your application should now be registered. 2. Install the App Engine SDK To work with App Engine, you'll need to download the SDK for Java from Google's developer site at the following location: https://developers.google.com/appengine/downloads#Google_App_Engine_SDK_for_Java Note: Make sure that you download the App Engine SDK for Java, not for Python or another language. It’s easy to pick the wrong one for the example on the download page. Also choose a version that matches your OS and Java binary version, 32- or 64-bit. Google has significant App Engine documentation online: https://developers.google.com/appengine/ 3. Import the Project into Eclipse Just like you have done for the other service projects in the book so far, import the project into Eclipse: cd $CODE/googleAppEngineContacts/ ant eclipse Then import the project folder into Eclipse. 4. Install the Google Plugin for Eclipse Follow the instructions below to install the Google plugin for Eclipse: https://developers.google.com/appengine/docs/java/tools/eclipse Edit the build properties file: $CODE/googleAppEngineContacts/build.properties to enter the path where you installed the App Engine SDK. 5. Configure an Application Identifier You need to put the application identifier you created in Step 1 into the $CODE/war/WEB-INF/appengine-web.xml, as follows, on line 3: 1 <?xml version="1.0" encoding="utf-8"?> 2 <appengine-web-app xmlns="http://appengine.google.com/ns/1.0"> 3 <application>Add_your_application_id_here</application> 4 <version>1</version> 6. Create an Application-Specific Password You won’t be able to deploy updates to your app engine service using your normal account password; instead you must create an application-specific password using the following instructions: * Enable two step verification on your account (if not done already): Two step verification: Application-specific passwords require you to have two-step verification in place, so if you don’t have it on your Google account, you’ll have to turn it on and authorize your computer before completing the process. Be sure to have a phone or a device on which you can receive text messages available. The included link provides a YouTube tutorial on this process. When invoking update requests from ant or in eclipse, use the following credentials: https://www.google.com/settings/security "2 Step Verification" -> Settings; Enable two step verification - you'll need a mobile phone for this step. * Enable less secure apps at the previous link using: * Select settings under "Less secure apps", then select "Enable" Then visit the following link to add an application password: * https://accounts.google.com/IssuedAuthSubTokens?hide_authsub=1#acccess_codes * E-mail address—Your account e-mail address * Password—An application-specific password === Deploy the code As with other projects, you can run the code from the command line or in Eclipse. == Deploy the code with ant: With Ant, you can deploy the contact service to the Google cloud, or you can run a local instance: Execute steps 1-6 above. 1. Deploy the project: cd $CODE/googleAppEngineContacts ant 2. To run the code locally on port 8080: ant runserver The local endpoint is, which is slight different from the other examples: http://localhost:8080/Contacts 3. To deploy to the google cloud, run the command: ant deploy-app Enter credentials as specified previously for your application specific password. Testing the local service: To test the service, run the following commands in a shell (cygwin for Windows). Create contacts: curl -H "Content-Type: application/json" -X POST -d '{"firstName":"John","lastName":"Smith", "phone":2345678901, "email":"jsmith@nosuchhost.com" }' http://localhost:8080/Contacts Get contacts: curl -H "Content-Type: application/json" -X GET http://localhost:8080/Contacts === Deploying with Eclipse Execute steps 1-4 of Prepare. 1. Setup the Eclipse build files: cd $CODE/googleAppEngineContacts ant eclipse Note: In this project, this command also resolves ivy dependencies. 2. As shown in springServiceContacts, import the project directory in Eclipse: $CODE/googleAppEngineContacts 3. Add the AppEngine SDK to the classpath. a. Right click the googleAppEngineContacts project b. Go to Project menu and select properties c. Click the "Java Build Path" on the left. When the dialog opens, click the library tab. d. Click the "Add Library..." button e. Select "Google App Engine", and then click the "Next" button f. Select the App Engine SDK, choose use default if you've just installed a single copy, and then and then click "Finish". Click Ok and Eclipse will build the project. 4. To run the sample code locally, right click the googleAppEngineContact and select the "Run As->Web Application" menu. 5. To deploy the app to Google: a. Right click googleAppEngineContacts b. Select "Google -> Deploy to App Engine" Note: You can view log messages by selecting your_app_id from the list of apps listed from: http://appengine.google.com/ Google provides a large arrays of service statistics here as well. Testing the deployed service: Client commands for cloud use (from Eclipse or command line): Create contacts: curl -H "Content-Type: application/json" -X POST -d '{"firstName":"John", "lastName":"Smith", "phone":2345678901, "email":"jsmith@nosuchhost.com" }' http://<your_ae_id>.appspot.com>/Contacts Get contacts: curl -X GET http://<your_ae_id>.appspot.com/Contacts Replace <your_ae_id> with your application ID. Congratulations! You can now change the Chapter 5 endpoint again to point in $CODE/syncAdapterContacts to your new service URL on Google. If the contacts service was a product, you would now, thanks to its flexible architecture, be able to deploy it on your own hardware, the AWS cloud, or Google’s App Engine cloud.