-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #171 from GoogleCloudPlatform/datastore
Add App Engine Datastore raw APIs sample.
- Loading branch information
Showing
21 changed files
with
1,330 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Google Cloud Datastore Sample | ||
|
||
This sample demonstrates how to use [Google Cloud Datastore][java-datastore] | ||
from [Google App Engine standard environment][ae-docs]. | ||
|
||
[java-datastore]: https://cloud.google.com/appengine/docs/java/datastore/ | ||
[ae-docs]: https://cloud.google.com/appengine/docs/java/ | ||
|
||
## Setup | ||
1. Update the `<application>` tag in `src/main/webapp/WEB-INF/appengine-web.xml` | ||
with your project name. | ||
1. Update the `<version>` tag in `src/main/webapp/WEB-INF/appengine-web.xml` | ||
with your version name. | ||
|
||
## Running locally | ||
$ mvn appengine:devserver | ||
|
||
## Deploying | ||
$ mvn appengine:update |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<!-- | ||
Copyright 2016 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. | ||
--> | ||
<project> | ||
<modelVersion>4.0.0</modelVersion> | ||
<packaging>war</packaging> | ||
<version>1.0-SNAPSHOT</version> | ||
<groupId>com.example.appengine</groupId> | ||
<artifactId>appengine-datastore</artifactId> | ||
<parent> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>doc-samples</artifactId> | ||
<version>1.0.0</version> | ||
<relativePath>../..</relativePath> | ||
</parent> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-api-1.0-sdk</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.auto.value</groupId> | ||
<artifactId>auto-value</artifactId> | ||
<version>1.2</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.code.findbugs</groupId> | ||
<artifactId>jsr305</artifactId> <!-- @Nullable annotations --> | ||
<version>3.0.1</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.google.guava</groupId> | ||
<artifactId>guava</artifactId> | ||
<version>19.0</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>servlet-api</artifactId> | ||
<type>jar</type> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>joda-time</groupId> | ||
<artifactId>joda-time</artifactId> | ||
<version>2.9.3</version> | ||
</dependency> | ||
|
||
<!-- Test Dependencies --> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.10</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-all</artifactId> | ||
<version>1.10.19</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<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> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-tools-sdk</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.truth</groupId> | ||
<artifactId>truth</artifactId> | ||
<version>0.28</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> | ||
<!-- Parent POM defines ${appengine.sdk.version} (updates frequently). --> | ||
<plugin> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-maven-plugin</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
83 changes: 83 additions & 0 deletions
83
appengine/datastore/src/main/java/com/example/appengine/AbstractGuestbook.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2016 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. | ||
*/ | ||
|
||
package com.example.appengine; | ||
|
||
import com.example.time.Clock; | ||
|
||
import com.google.appengine.api.datastore.DatastoreService; | ||
import com.google.appengine.api.datastore.DatastoreServiceFactory; | ||
import com.google.appengine.api.datastore.Entity; | ||
import com.google.appengine.api.users.User; | ||
import com.google.appengine.api.users.UserService; | ||
import com.google.appengine.api.users.UserServiceFactory; | ||
import com.google.common.collect.ImmutableList; | ||
|
||
import java.util.Date; | ||
import java.util.List; | ||
|
||
/** | ||
* A log of notes left by users. | ||
* | ||
* <p>This is meant to be subclassed to demonstrate different storage structures in Datastore. | ||
*/ | ||
abstract class AbstractGuestbook { | ||
private final DatastoreService datastore; | ||
private final UserService userService; | ||
private final Clock clock; | ||
|
||
AbstractGuestbook(Clock clock) { | ||
this.datastore = DatastoreServiceFactory.getDatastoreService(); | ||
this.userService = UserServiceFactory.getUserService(); | ||
this.clock = clock; | ||
} | ||
|
||
/** | ||
* Appends a new greeting to the guestbook and returns the {@link Entity} that was created. | ||
*/ | ||
public Greeting appendGreeting(String content) { | ||
Greeting greeting = | ||
Greeting.create( | ||
createGreeting( | ||
datastore, | ||
userService.getCurrentUser(), | ||
clock.now().toDate(), | ||
content)); | ||
return greeting; | ||
} | ||
|
||
/** | ||
* Write a greeting to Datastore. | ||
*/ | ||
protected abstract Entity createGreeting( | ||
DatastoreService datastore, User user, Date date, String content); | ||
|
||
/** | ||
* Return a list of the most recent greetings. | ||
*/ | ||
public List<Greeting> listGreetings() { | ||
ImmutableList.Builder<Greeting> greetings = ImmutableList.builder(); | ||
for (Entity entity : listGreetingEntities(datastore)) { | ||
greetings.add(Greeting.create(entity)); | ||
} | ||
return greetings.build(); | ||
} | ||
|
||
/** | ||
* Return a list of the most recent greetings. | ||
*/ | ||
protected abstract List<Entity> listGreetingEntities(DatastoreService datastore); | ||
} |
59 changes: 59 additions & 0 deletions
59
appengine/datastore/src/main/java/com/example/appengine/AbstractGuestbookServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2016 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. | ||
*/ | ||
|
||
package com.example.appengine; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
abstract class AbstractGuestbookServlet extends HttpServlet { | ||
private final AbstractGuestbook guestbook; | ||
|
||
public AbstractGuestbookServlet(AbstractGuestbook guestbook) { | ||
this.guestbook = guestbook; | ||
} | ||
|
||
private void renderGuestbook(HttpServletRequest req, HttpServletResponse resp) | ||
throws IOException, ServletException { | ||
resp.setContentType("text/html"); | ||
resp.setCharacterEncoding("UTF-8"); | ||
req.setAttribute("greetings", guestbook.listGreetings()); | ||
req.getRequestDispatcher("/guestbook.jsp").forward(req, resp); | ||
} | ||
|
||
@Override | ||
public void doGet(HttpServletRequest req, HttpServletResponse resp) | ||
throws IOException, ServletException { | ||
renderGuestbook(req, resp); | ||
} | ||
|
||
@Override | ||
public void doPost(HttpServletRequest req, HttpServletResponse resp) | ||
throws IOException, ServletException { | ||
String content = req.getParameter("content"); | ||
if (content == null || content.isEmpty()) { | ||
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "missing content"); | ||
return; | ||
} | ||
guestbook.appendGreeting(content); | ||
renderGuestbook(req, resp); | ||
} | ||
} | ||
|
43 changes: 43 additions & 0 deletions
43
appengine/datastore/src/main/java/com/example/appengine/Greeting.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2016 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. | ||
*/ | ||
|
||
package com.example.appengine; | ||
|
||
import com.google.appengine.api.datastore.Entity; | ||
import com.google.appengine.api.users.User; | ||
import com.google.auto.value.AutoValue; | ||
import org.joda.time.Instant; | ||
|
||
import java.util.Date; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
@AutoValue | ||
public abstract class Greeting { | ||
static Greeting create(Entity entity) { | ||
User user = (User) entity.getProperty("user"); | ||
Instant date = new Instant((Date) entity.getProperty("date")); | ||
String content = (String) entity.getProperty("content"); | ||
return new AutoValue_Greeting(user, date, content); | ||
} | ||
|
||
@Nullable | ||
public abstract User getUser(); | ||
|
||
public abstract Instant getDate(); | ||
|
||
public abstract String getContent(); | ||
} |
66 changes: 66 additions & 0 deletions
66
appengine/datastore/src/main/java/com/example/appengine/Guestbook.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2016 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. | ||
*/ | ||
|
||
package com.example.appengine; | ||
|
||
import com.example.time.Clock; | ||
|
||
import com.google.appengine.api.datastore.DatastoreService; | ||
import com.google.appengine.api.datastore.Entity; | ||
import com.google.appengine.api.datastore.FetchOptions; | ||
import com.google.appengine.api.datastore.Query; | ||
import com.google.appengine.api.users.User; | ||
|
||
import java.util.Date; | ||
import java.util.List; | ||
|
||
/** | ||
* A log of notes left by users. | ||
* | ||
* <p>This demonstrates the use of Google Cloud Datastore using the App Engine | ||
* APIs. See the | ||
* <a href="https://cloud.google.com/appengine/docs/java/datastore/">documentation</a> | ||
* for more information. | ||
*/ | ||
class Guestbook extends AbstractGuestbook { | ||
Guestbook(Clock clock) { | ||
super(clock); | ||
} | ||
|
||
@Override | ||
protected Entity createGreeting( | ||
DatastoreService datastore, User user, Date date, String content) { | ||
// No parent key specified, so Greeting is a root entity. | ||
Entity greeting = new Entity("Greeting"); | ||
greeting.setProperty("user", user); | ||
greeting.setProperty("date", date); | ||
greeting.setProperty("content", content); | ||
|
||
datastore.put(greeting); | ||
return greeting; | ||
} | ||
|
||
@Override | ||
protected List<Entity> listGreetingEntities(DatastoreService datastore) { | ||
Query query = | ||
new Query("Greeting") | ||
.addSort("date", Query.SortDirection.DESCENDING); | ||
return datastore.prepare(query) | ||
.asList(FetchOptions.Builder.withLimit(10)); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.