Skip to content

Commit 85c23c2

Browse files
committed
Oauth2 updates, Guestbook (#172)
Oauth2 - Tim and Brett’s comments - Mostly nits - HelloServlet now uses User GuestBook - Initial checkin, updated README remove .gitignore * Check style changes * rename guestbook -> guestbook-objectify
1 parent e486daf commit 85c23c2

File tree

17 files changed

+554
-27
lines changed

17 files changed

+554
-27
lines changed
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# appengine/guestbook-objectify
2+
3+
An App Engine guestbook using Java, Maven, and Objectify.
4+
5+
Data access using [Objectify](https://github.com/objectify/objectify)
6+
7+
Please ask questions on [Stackoverflow](http://stackoverflow.com/questions/tagged/google-app-engine)
8+
9+
## Running Locally
10+
11+
How do I, as a developer, start working on the project?
12+
13+
1. `mvn clean appengine:devserver`
14+
15+
## Deploying
16+
17+
1. `mvn clean appengine:update -Dappengine.appId=PROJECT -Dappengine.version=VERSION`

appengine/guestbook-objectify/pom.xml

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
4+
<modelVersion>4.0.0</modelVersion>
5+
<packaging>war</packaging>
6+
<version>1.0-SNAPSHOT</version>
7+
8+
<groupId>com.example.appengine</groupId>
9+
<artifactId>guestbook</artifactId>
10+
<properties>
11+
<objectify.version>5.1.5</objectify.version>
12+
<guava.version>18.0</guava.version>
13+
</properties>
14+
<parent>
15+
<groupId>com.google.cloud</groupId>
16+
<artifactId>doc-samples</artifactId>
17+
<version>1.0.0</version>
18+
<relativePath>../..</relativePath>
19+
</parent>
20+
21+
<!-- [START set_versions] -->
22+
<prerequisites>
23+
<maven>3.3.9</maven>
24+
</prerequisites>
25+
<!-- [END set_versions] -->
26+
27+
<dependencies>
28+
<!-- Compile/runtime dependencies -->
29+
<dependency>
30+
<groupId>com.google.appengine</groupId>
31+
<artifactId>appengine-api-1.0-sdk</artifactId>
32+
<version>${appengine.sdk.version}</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>javax.servlet</groupId>
36+
<artifactId>servlet-api</artifactId>
37+
<version>2.5</version>
38+
<scope>provided</scope>
39+
</dependency>
40+
<dependency>
41+
<groupId>jstl</groupId>
42+
<artifactId>jstl</artifactId>
43+
<version>1.2</version>
44+
</dependency>
45+
46+
<!-- [START Objectify_Dependencies] -->
47+
<dependency>
48+
<groupId>com.google.guava</groupId>
49+
<artifactId>guava</artifactId>
50+
<version>${guava.version}</version>
51+
</dependency>
52+
<dependency>
53+
<groupId>com.googlecode.objectify</groupId>
54+
<artifactId>objectify</artifactId>
55+
<version>${objectify.version}</version>
56+
</dependency>
57+
<!-- [END Objectify_Dependencies] -->
58+
59+
<!-- Test Dependencies -->
60+
<dependency>
61+
<groupId>com.google.appengine</groupId>
62+
<artifactId>appengine-testing</artifactId>
63+
<version>${appengine.sdk.version}</version>
64+
<scope>test</scope>
65+
</dependency>
66+
<dependency>
67+
<groupId>com.google.appengine</groupId>
68+
<artifactId>appengine-api-stubs</artifactId>
69+
<version>${appengine.sdk.version}</version>
70+
<scope>test</scope>
71+
</dependency>
72+
</dependencies>
73+
74+
<build>
75+
<!-- for hot reload of the web application-->
76+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
77+
<plugins>
78+
<plugin>
79+
<groupId>com.google.appengine</groupId>
80+
<artifactId>appengine-maven-plugin</artifactId>
81+
<version>${appengine.sdk.version}</version>
82+
<configuration>
83+
<enableJarClasses>false</enableJarClasses>
84+
<!-- Comment in the below snippet to bind to all IPs instead of just localhost -->
85+
<!-- address>0.0.0.0</address>
86+
<port>8080</port -->
87+
<!-- Comment in the below snippet to enable local debugging with a remote debugger
88+
like those included with Eclipse or IntelliJ -->
89+
<!-- jvmFlags>
90+
<jvmFlag>-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n</jvmFlag>
91+
</jvmFlags -->
92+
</configuration>
93+
</plugin>
94+
</plugins>
95+
</build>
96+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* Copyright 2014-2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
//[START all]
18+
package com.example.guestbook;
19+
20+
import com.googlecode.objectify.Key;
21+
import com.googlecode.objectify.annotation.Entity;
22+
import com.googlecode.objectify.annotation.Id;
23+
import com.googlecode.objectify.annotation.Index;
24+
import com.googlecode.objectify.annotation.Parent;
25+
26+
import java.lang.String;
27+
import java.util.Date;
28+
29+
/**
30+
* The @Entity tells Objectify about our entity. We also register it in {@link OfyHelper}
31+
* Our primary key @Id is set automatically by the Google Datastore for us.
32+
*
33+
* We add a @Parent to tell the object about its ancestor. We are doing this to support many
34+
* guestbooks. Objectify, unlike the AppEngine library requires that you specify the fields you
35+
* want to index using @Index. Only indexing the fields you need can lead to substantial gains in
36+
* performance -- though if not indexing your data from the start will require indexing it later.
37+
*
38+
* NOTE - all the properties are PUBLIC so that can keep the code simple.
39+
**/
40+
@Entity
41+
public class Greeting {
42+
@Parent Key<Guestbook> theBook;
43+
@Id public Long id;
44+
45+
public String authorEmail;
46+
public String authorId;
47+
public String content;
48+
@Index public Date date;
49+
50+
/**
51+
* Simple constructor just sets the date.
52+
**/
53+
public Greeting() {
54+
date = new Date();
55+
}
56+
57+
/**
58+
* A convenience constructor.
59+
**/
60+
public Greeting(String book, String content) {
61+
this();
62+
if ( book != null ) {
63+
theBook = Key.create(Guestbook.class, book); // Creating the Ancestor key
64+
} else {
65+
theBook = Key.create(Guestbook.class, "default");
66+
}
67+
this.content = content;
68+
}
69+
70+
/**
71+
* Takes all important fields.
72+
**/
73+
public Greeting(String book, String content, String id, String email) {
74+
this(book, content);
75+
authorEmail = email;
76+
authorId = id;
77+
}
78+
79+
}
80+
//[END all]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright 2014-2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
//[START all]
18+
package com.example.guestbook;
19+
20+
import com.googlecode.objectify.annotation.Entity;
21+
import com.googlecode.objectify.annotation.Id;
22+
23+
/**
24+
* The @Entity tells Objectify about our entity. We also register it in
25+
* OfyHelper.java -- very important.
26+
*
27+
* This is never actually created, but gives a hint to Objectify about our Ancestor key.
28+
*/
29+
@Entity
30+
public class Guestbook {
31+
@Id public String book;
32+
}
33+
//[END all]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright 2014-2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software distributed under the License
9+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10+
* or implied. See the License for the specific language governing permissions and limitations under
11+
* the License.
12+
*/
13+
//[START all]
14+
package com.example.guestbook;
15+
16+
import com.googlecode.objectify.ObjectifyService;
17+
18+
import javax.servlet.ServletContextEvent;
19+
import javax.servlet.ServletContextListener;
20+
21+
/**
22+
* OfyHelper, a ServletContextListener, is setup in web.xml to run before a JSP is run. This is
23+
* required to let JSP's access Ofy.
24+
**/
25+
public class OfyHelper implements ServletContextListener {
26+
public void contextInitialized(ServletContextEvent event) {
27+
// This will be invoked as part of a warmup request, or the first user request if no warmup
28+
// request.
29+
ObjectifyService.register(Guestbook.class);
30+
ObjectifyService.register(Greeting.class);
31+
}
32+
33+
public void contextDestroyed(ServletContextEvent event) {
34+
// App Engine does not currently invoke this method.
35+
}
36+
}
37+
//[END all]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Copyright 2014-2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
//[START all]
18+
package com.example.guestbook;
19+
20+
import com.google.appengine.api.users.User;
21+
import com.google.appengine.api.users.UserService;
22+
import com.google.appengine.api.users.UserServiceFactory;
23+
24+
import com.googlecode.objectify.ObjectifyService;
25+
26+
import java.io.IOException;
27+
28+
import javax.servlet.http.HttpServlet;
29+
import javax.servlet.http.HttpServletRequest;
30+
import javax.servlet.http.HttpServletResponse;
31+
32+
/**
33+
* Form Handling Servlet - most of the action for this sample is in webapp/guestbook.jsp,
34+
* which displays the {@link Greeting}'s.
35+
*/
36+
public class SignGuestbookServlet extends HttpServlet {
37+
38+
// Process the http POST of the form
39+
@Override
40+
public void doPost(HttpServletRequest req, HttpServletResponse resp)
41+
throws IOException {
42+
Greeting greeting;
43+
44+
UserService userService = UserServiceFactory.getUserService();
45+
User user = userService.getCurrentUser(); // Find out who the user is.
46+
47+
String guestbookName = req.getParameter("guestbookName");
48+
String content = req.getParameter("content");
49+
if (user != null) {
50+
greeting = new Greeting(guestbookName, content, user.getUserId(), user.getEmail());
51+
} else {
52+
greeting = new Greeting(guestbookName, content);
53+
}
54+
55+
// Use Objectify to save the greeting and now() is used to make the call synchronously as we
56+
// will immediately get a new page using redirect and we want the data to be present.
57+
ObjectifyService.ofy().save().entity(greeting).now();
58+
59+
resp.sendRedirect("/guestbook.jsp?guestbookName=" + guestbookName);
60+
}
61+
}
62+
//[END all]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
3+
<application>your-app-id</application>
4+
<version>${appengine.app.version}</version>
5+
<threadsafe>true</threadsafe>
6+
7+
<system-properties>
8+
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
9+
</system-properties>
10+
</appengine-web-app>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# A default java.util.logging configuration.
2+
# (All App Engine logging is through java.util.logging by default).
3+
#
4+
# To use this configuration, copy it into your application's WEB-INF
5+
# folder and add the following to your appengine-web.xml:
6+
#
7+
# <system-properties>
8+
# <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
9+
# </system-properties>
10+
#
11+
12+
# Set the default logging level for all loggers to WARNING
13+
.level = WARNING
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
5+
6+
<!-- [START standard_mappings] -->
7+
<servlet>
8+
<servlet-name>sign</servlet-name>
9+
<servlet-class>com.example.guestbook.SignGuestbookServlet</servlet-class>
10+
</servlet>
11+
12+
<servlet-mapping>
13+
<servlet-name>sign</servlet-name>
14+
<url-pattern>/sign</url-pattern>
15+
</servlet-mapping>
16+
17+
<welcome-file-list>
18+
<welcome-file>guestbook.jsp</welcome-file>
19+
</welcome-file-list>
20+
<!-- [END standard_mappings] -->
21+
22+
<!-- [START Objectify] -->
23+
<filter>
24+
<filter-name>ObjectifyFilter</filter-name>
25+
<filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class>
26+
</filter>
27+
<filter-mapping>
28+
<filter-name>ObjectifyFilter</filter-name>
29+
<url-pattern>/*</url-pattern>
30+
</filter-mapping>
31+
<listener>
32+
<listener-class>com.example.guestbook.OfyHelper</listener-class>
33+
</listener>
34+
<!-- [END Objectify] -->
35+
</web-app>

0 commit comments

Comments
 (0)