Skip to content

Oauth2 updates, Guestbook #172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions appengine/guestbook-objectify/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# appengine/guestbook-objectify

An App Engine guestbook using Java, Maven, and Objectify.

Data access using [Objectify](https://github.com/objectify/objectify)

Please ask questions on [Stackoverflow](http://stackoverflow.com/questions/tagged/google-app-engine)

## Running Locally

How do I, as a developer, start working on the project?

1. `mvn clean appengine:devserver`

## Deploying

1. `mvn clean appengine:update -Dappengine.appId=PROJECT -Dappengine.version=VERSION`
96 changes: 96 additions & 0 deletions appengine/guestbook-objectify/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?xml version="1.0" encoding="UTF-8"?>
Copy link
Contributor

@tswast tswast Apr 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

License Header.
Also, xmlns attributes are optional, so can be removed.

<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">

<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>

<groupId>com.example.appengine</groupId>
<artifactId>guestbook</artifactId>
<properties>
<objectify.version>5.1.5</objectify.version>
<guava.version>18.0</guava.version>
</properties>
<parent>
<groupId>com.google.cloud</groupId>
<artifactId>doc-samples</artifactId>
<version>1.0.0</version>
<relativePath>../..</relativePath>
</parent>

<!-- [START set_versions] -->
<prerequisites>
<maven>3.3.9</maven>
</prerequisites>
<!-- [END set_versions] -->

<dependencies>
<!-- Compile/runtime dependencies -->
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-1.0-sdk</artifactId>
<version>${appengine.sdk.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<!-- [START Objectify_Dependencies] -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>com.googlecode.objectify</groupId>
<artifactId>objectify</artifactId>
<version>${objectify.version}</version>
</dependency>
<!-- [END Objectify_Dependencies] -->

<!-- Test Dependencies -->
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-testing</artifactId>
<version>${appengine.sdk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-stubs</artifactId>
<version>${appengine.sdk.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<!-- for hot reload of the web application-->
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
<plugins>
<plugin>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>${appengine.sdk.version}</version>
<configuration>
<enableJarClasses>false</enableJarClasses>
<!-- Comment in the below snippet to bind to all IPs instead of just localhost -->
<!-- address>0.0.0.0</address>
<port>8080</port -->
<!-- Comment in the below snippet to enable local debugging with a remote debugger
like those included with Eclipse or IntelliJ -->
<!-- jvmFlags>
<jvmFlag>-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n</jvmFlag>
</jvmFlags -->
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Copyright 2014-2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

//[START all]
package com.example.guestbook;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Index;
import com.googlecode.objectify.annotation.Parent;

import java.lang.String;
import java.util.Date;

/**
* The @Entity tells Objectify about our entity. We also register it in {@link OfyHelper}
* Our primary key @Id is set automatically by the Google Datastore for us.
*
* We add a @Parent to tell the object about its ancestor. We are doing this to support many
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing <p> tag.

* guestbooks. Objectify, unlike the AppEngine library requires that you specify the fields you
* want to index using @Index. Only indexing the fields you need can lead to substantial gains in
* performance -- though if not indexing your data from the start will require indexing it later.
*
* NOTE - all the properties are PUBLIC so that can keep the code simple.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing <p> tag.

**/
@Entity
public class Greeting {
@Parent Key<Guestbook> theBook;
@Id public Long id;

public String authorEmail;
public String authorId;
public String content;
@Index public Date date;

/**
* Simple constructor just sets the date.
**/
public Greeting() {
date = new Date();
}

/**
* A convenience constructor.
**/
public Greeting(String book, String content) {
this();
if ( book != null ) {
theBook = Key.create(Guestbook.class, book); // Creating the Ancestor key
} else {
theBook = Key.create(Guestbook.class, "default");
}
this.content = content;
}

/**
* Takes all important fields.
**/
public Greeting(String book, String content, String id, String email) {
this(book, content);
authorEmail = email;
authorId = id;
}

}
//[END all]
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright 2014-2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

//[START all]
package com.example.guestbook;

import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;

/**
* The @Entity tells Objectify about our entity. We also register it in
* OfyHelper.java -- very important.
*
* This is never actually created, but gives a hint to Objectify about our Ancestor key.
*/
@Entity
public class Guestbook {
@Id public String book;
}
//[END all]
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright 2014-2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
//[START all]
package com.example.guestbook;

import com.googlecode.objectify.ObjectifyService;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
* OfyHelper, a ServletContextListener, is setup in web.xml to run before a JSP is run. This is
* required to let JSP's access Ofy.
**/
public class OfyHelper implements ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
// This will be invoked as part of a warmup request, or the first user request if no warmup
// request.
ObjectifyService.register(Guestbook.class);
ObjectifyService.register(Greeting.class);
}

public void contextDestroyed(ServletContextEvent event) {
// App Engine does not currently invoke this method.
}
}
//[END all]
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright 2014-2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

//[START all]
package com.example.guestbook;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import com.googlecode.objectify.ObjectifyService;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Form Handling Servlet - most of the action for this sample is in webapp/guestbook.jsp,
* which displays the {@link Greeting}'s.
*/
public class SignGuestbookServlet extends HttpServlet {

// Process the http POST of the form
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
Greeting greeting;

UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser(); // Find out who the user is.

String guestbookName = req.getParameter("guestbookName");
String content = req.getParameter("content");
if (user != null) {
greeting = new Greeting(guestbookName, content, user.getUserId(), user.getEmail());
} else {
greeting = new Greeting(guestbookName, content);
}

// Use Objectify to save the greeting and now() is used to make the call synchronously as we
// will immediately get a new page using redirect and we want the data to be present.
ObjectifyService.ofy().save().entity(greeting).now();

resp.sendRedirect("/guestbook.jsp?guestbookName=" + guestbookName);
}
}
//[END all]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>your-app-id</application>
<version>${appengine.app.version}</version>
<threadsafe>true</threadsafe>

<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
</system-properties>
</appengine-web-app>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# 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
35 changes: 35 additions & 0 deletions appengine/guestbook-objectify/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- [START standard_mappings] -->
<servlet>
<servlet-name>sign</servlet-name>
<servlet-class>com.example.guestbook.SignGuestbookServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>sign</servlet-name>
<url-pattern>/sign</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>guestbook.jsp</welcome-file>
</welcome-file-list>
<!-- [END standard_mappings] -->

<!-- [START Objectify] -->
<filter>
<filter-name>ObjectifyFilter</filter-name>
<filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ObjectifyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>com.example.guestbook.OfyHelper</listener-class>
</listener>
<!-- [END Objectify] -->
</web-app>
Loading