Slides - Gustavus Adolphus College

Download Report

Transcript Slides - Gustavus Adolphus College

MCS 270 Spring 2014
Object-Oriented Software Development
MCS 270 Object-Oriented Software Development
Today’s schedule
GAE – Google App Engine
Modules
User Service
Blob Service
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Overview
Google App Engine is:
- runtime platform that provides web application
hosting, data storage, and high-speed networking
- infrastructure (servers + storage ) at Google
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Overview
GAE Advantages:
Does one thing well: running web apps
App Engine handles HTTP(S) requests, nothing else
Simple (relatively) app configuration
Automatic Scaling to Application Needs
Secure
Easy on budget: Pay only for what you use (small apps
are free)
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
GAE Runtime Environment
Java: Java 7 Virtual Machine (JVM)
Java Servlet interface for client-server
Python: Python 2.7
Go
Java or Python?
Python: powerful python syntax, library, shorter code
Java: can use Java Data Objects (JDO) and Java
Persistance API (JPA)
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
GAE Sandbox
Applications run in a secure environment - limited access
to underlying OS
Plus - App will NEVER affect other applications on the same server
Minuses - Cannot spawn additional processes or threads
Cannot make arbitrary network connections
Only read its own code and resource files and cannot create or
modify files
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
GAE Sandbox Restrictions
- App can only access other computers on the Internet
through URL fetch and email services.
- App interaction to server only by HTTP (or HTTPS)
requests on the standard ports
- Applications cannot write to the file system
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
GAE Services
URLFetch – fetch web resources/services (servlets)
Images – manipulate images: resize, rotate, flip, crop
User Services: Google Accounts
Mail
XMPP – instant messages
Task Queue – message queue; allow integration with non-GAPPs
Datastore – managing data objects
Blobstore – large files, much larger than objects in datastore
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
GAE User Services
Google App Engine service based on Google infrastructure
Accessible by applications using libraries included with GAE SDK.
Provides integration with Google user accounts.
Users use existing Google accounts to sign in to Application
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
GAE User Services
Demo
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Program Flow – GusList.java
public void onModuleLoad() {
glView.setController(GusList.this);
homeURL = Window.Location.getHref();
clientModelService.setAppBaseURL(homeURL,
new AsyncCallback<String>() {
public void onFailure(Throwable caught) {return;}
public void onSuccess(String result) {}});
clientModelService.isUserLoggedIn(
new AsyncCallback<Boolean>() {
public void onFailure(Throwable caught) {return;}
public void onSuccess(Boolean result) {
if(result) glView.viewWelcomePage();
else glView.setWindow("../GusListWelcome.html");
}});}
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Program Flow – GusListView.java
public void setWindow(String url) {
Window.Location.replace(url);
}
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Program Flow – GusListWelcome.html
<p style="text-align: center;">
Welcome to GusList - the Gustavus on-line classified ad system.&nbsp; To
start checking out the latest ads from the coolest students, please
<a href="guslist/loginservice">login</a>.
&nbsp;
If you are new to the system, you will need to
<a href="https://accounts.google.com/SignUp">create
a Google account</a> to login. </p>
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Program Flow – web.xml
<servlet>
<servlet-name>LoginService</servlet-name>
<servletclass>edu.gac.mcs270.hvidsten.guslistgae.server.LoginService</servletclass>
</servlet>
<servlet-mapping>
<servlet-name>LoginService</servlet-name>
<url-pattern>/guslist/loginservice</url-pattern>
</servlet-mapping>
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Program Flow - LoginService
public class LoginService extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws
IOException {
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
if (user != null) {
resp.setStatus(HttpServletResponse.SC_SEE_OTHER);
resp.setHeader("Location", "../GusList.html”);
} else {
String logInLink = userService.createLoginURL("../GusList.html”);
resp.setStatus(HttpServletResponse.SC_SEE_OTHER);
resp.setHeader("Location", logInLink);
}}}
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Login by GAE
Note:
Login and logout pages are handle by GAE automatically,
but the workflow is different :
Run on local – It will simulate Google Accounts sign-in
page (no password authentication).
Run on GAE – It will redirect to actual Google Account
login screen.
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Program Flow – After Login returns to GusList.html
<body>
Basically Empty – filled in by GusListView
</body>
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
RPC vs HTTP Servlets
HTTP – Simplest way to retrieve data from server. Data
can be anything (text, HTML, XML, binary data, etc).
Programmer must make sense of data.
RPC – Easiest way to tranfer Java objects from server<>client. Data can only contain Serializable objects.
Programmer knows exactly what form data is in.
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
RPC
“A remote procedure call (RPC) is an inter-process
communication that allows a program to cause a
procedure to execute in another address space
(commonly on another computer on a shared network)
without the programmer explicitly coding the details for
this remote interaction. When the software in question
uses object-oriented principles, RPC is called remote
invocation or remote method invocation.”
(Wikipedia)
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
HTTP Request / Response
Cycle
Web Server
HTTP
Request
HTTP
Response
Browser
IE, FireFox,
Chrome,Safari
http://www.oreilly.com/openbook/cgi/ch04_02.html
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
HTTP Request / Response
Cycle
Web Server
GET /index.html
HTTP
Request
<head> .. </head>
<body>
HTTP <h1>Welcome to my
Response application</h1>
....
</body>
Browser
IE, FireFox,
Chrome,Safari
http://www.oreilly.com/openbook/cgi/ch04_02.html
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
RPC vs HTTP Servlets
LoginService - Transfers actual URL’s, comes from GET
request. Must be HTTP servlet.
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Program Flow - LoginService
public class LoginService extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws
IOException {
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
if (user != null) {
resp.setStatus(HttpServletResponse.SC_SEE_OTHER);
resp.setHeader("Location", "../GusList.html”);
} else {
String logInLink = userService.createLoginURL("../GusList.html”);
resp.setStatus(HttpServletResponse.SC_SEE_OTHER);
resp.setHeader("Location", logInLink);
}}}
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
GAE Blob Service
Blobstore API - mechanism to store “blobs” of information
Blob can be up to 50MB in size (typically large files –video, images)
Datastore vs Blob Service
Datastore – data size limited to 1mb, Access only through a GAE
app.
Blob Service – Size up to 50mb. Cheaper than Datastore. Data can
be served directly as url access. Designed for images – can do image
transformations in place.
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
GAE Blob Service
Demo
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Program Flow - GusListView
FormPanel submitFormPanel
// The submitFormPanel, when submitted, will trigger an HTTP call to the
// servlet. The following parameters must be set
submitFormPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
submitFormPanel.setMethod(FormPanel.METHOD_POST);
// Set Names for the text boxes so that they can be retrieved from the
// HTTP call as parameters
nameTextbox.setName("name");
titleTextbox.setName("title");
descrText.setName("description");
priceTextbox.setName("price");
upload.setName("upload");
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Program Flow - GusListView
submitButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
control.handlePostFromSubmitForm(submitFormPanel);
}});
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Program Flow - GusList
public void handlePostFromSubmitForm(final FormPanel submitFormPanel) {
blobService.getBlobStoreUploadUrl( new AsyncCallback<String>() {
public void onSuccess(String result) {
// Set the form action to the newly created blobstore upload URL
submitFormPanel.setAction(result.toString());
// Submit the form to complete the upload
// This causes activation of an upload HTTP servlet
submitFormPanel.submit();
}
public void onFailure(Throwable caught) {
glView.sendErrorMessage("Upload Failed");
}});
}
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Program Flow - BlobService
public class BlobServiceImpl extends RemoteServiceServlet implements BlobService {
//Start a GAE BlobstoreService session
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
//Generate a Blobstore Upload URL
public String getBlobStoreUploadUrl() {
// Map the BlobService UploadURL to the
// HTTP servlet which will be called when
//
submitting the FormPanel (see web.xml for servlet def)
return blobstoreService.createUploadUrl("/guslist/uploadservice");
}
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Program Flow - SubmitPostHTTPService
public class SubmitPostHTTPServiceImpl extends HttpServlet {
//Start Blobstore
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
//Override the doPost method to store the Blob's meta-data
@Override
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
BlobKey blobKey = blobs.get("upload");
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu
MCS 270 Object-Oriented Software Development
Program Flow - SubmitPostHTTPService
//Get the parameters from the request to post the ad
String name = req.getParameter("name");
String title = req.getParameter("title");
String descr = req.getParameter("description");
double price = Double.valueOf(req.getParameter("price"));
//Map the ImageURL to the blobservice servlet, which will serve the image
String url = "/guslist/blobservice?blob-key=" + blobKey.getKeyString();
PostData post = new PostData(title,descr,price, url, new Seller(name), null);
GusListModel.storePost(post);
}}
GUSTAVUS ADOLPHUS COLLEGE
gustavus.edu