Skip to content

Commit ad0af31

Browse files
tswastlesv
authored andcommitted
1 parent d20f66f commit ad0af31

File tree

5 files changed

+499
-1
lines changed

5 files changed

+499
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2016 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+
package com.example.appengine;
18+
19+
// [START cursors]
20+
import com.google.appengine.api.datastore.Cursor;
21+
import com.google.appengine.api.datastore.DatastoreService;
22+
import com.google.appengine.api.datastore.DatastoreServiceFactory;
23+
import com.google.appengine.api.datastore.Entity;
24+
import com.google.appengine.api.datastore.FetchOptions;
25+
import com.google.appengine.api.datastore.PreparedQuery;
26+
import com.google.appengine.api.datastore.Query;
27+
import com.google.appengine.api.datastore.Query.SortDirection;
28+
import com.google.appengine.api.datastore.QueryResultList;
29+
30+
import java.io.IOException;
31+
import java.io.PrintWriter;
32+
33+
import javax.servlet.ServletException;
34+
import javax.servlet.http.HttpServlet;
35+
import javax.servlet.http.HttpServletRequest;
36+
import javax.servlet.http.HttpServletResponse;
37+
38+
public class ListPeopleServlet extends HttpServlet {
39+
static final int PAGE_SIZE = 15;
40+
private final DatastoreService datastore;
41+
42+
public ListPeopleServlet() {
43+
datastore = DatastoreServiceFactory.getDatastoreService();
44+
}
45+
46+
@Override
47+
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
48+
throws ServletException, IOException {
49+
FetchOptions fetchOptions = FetchOptions.Builder.withLimit(PAGE_SIZE);
50+
51+
// If this servlet is passed a cursor parameter, let's use it.
52+
String startCursor = req.getParameter("cursor");
53+
if (startCursor != null) {
54+
fetchOptions.startCursor(Cursor.fromWebSafeString(startCursor));
55+
}
56+
57+
Query q = new Query("Person").addSort("name", SortDirection.ASCENDING);
58+
PreparedQuery pq = datastore.prepare(q);
59+
60+
QueryResultList<Entity> results;
61+
try {
62+
results = pq.asQueryResultList(fetchOptions);
63+
} catch (IllegalArgumentException e) {
64+
// IllegalArgumentException happens when an invalid cursor is used.
65+
// A user could have manually entered a bad cursor in the URL or there
66+
// may have been an internal implementation detail change in App Engine.
67+
// Redirect to the page without the cursor parameter to show something
68+
// rather than an error.
69+
resp.sendRedirect("/people");
70+
return;
71+
}
72+
73+
resp.setContentType("text/html");
74+
resp.setCharacterEncoding("UTF-8");
75+
PrintWriter w = resp.getWriter();
76+
w.println("<!DOCTYPE html>");
77+
w.println("<meta charset=\"utf-8\">");
78+
w.println("<title>Cloud Datastore Cursor Sample</title>");
79+
w.println("<ul>");
80+
for (Entity entity : results) {
81+
w.println("<li>" + entity.getProperty("name") + "</li>");
82+
}
83+
w.println("</ul>");
84+
85+
String cursorString = results.getCursor().toWebSafeString();
86+
87+
// This servlet lives at '/people'.
88+
w.println("<a href='/people?cursor=" + cursorString + "'>Next page</a>");
89+
}
90+
}
91+
// [END cursors]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright 2016 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+
package com.example.appengine;
18+
19+
import com.google.appengine.api.datastore.DatastoreService;
20+
import com.google.appengine.api.datastore.DatastoreServiceFactory;
21+
import com.google.appengine.api.datastore.Entity;
22+
import com.google.appengine.api.datastore.EntityNotFoundException;
23+
import com.google.appengine.api.datastore.Key;
24+
import com.google.appengine.api.datastore.KeyFactory;
25+
import com.google.common.collect.ImmutableList;
26+
27+
import java.io.IOException;
28+
29+
import javax.servlet.ServletException;
30+
import javax.servlet.http.HttpServlet;
31+
import javax.servlet.http.HttpServletRequest;
32+
import javax.servlet.http.HttpServletResponse;
33+
34+
/**
35+
* A startup handler to populate the datastore with example entities.
36+
*/
37+
public class StartupServlet extends HttpServlet {
38+
static final String IS_POPULATED_ENTITY = "IsPopulated";
39+
static final String IS_POPULATED_KEY_NAME = "is-populated";
40+
41+
private static final String PERSON_ENTITY = "Person";
42+
private static final String NAME_PROPERTY = "name";
43+
private static final ImmutableList<String> US_PRESIDENTS =
44+
ImmutableList.<String>builder()
45+
.add("George Washington")
46+
.add("John Adams")
47+
.add("Thomas Jefferson")
48+
.add("James Madison")
49+
.add("James Monroe")
50+
.add("John Quincy Adams")
51+
.add("Andrew Jackson")
52+
.add("Martin Van Buren")
53+
.add("William Henry Harrison")
54+
.add("John Tyler")
55+
.add("James K. Polk")
56+
.add("Zachary Taylor")
57+
.add("Millard Fillmore")
58+
.add("Franklin Pierce")
59+
.add("James Buchanan")
60+
.add("Abraham Lincoln")
61+
.add("Andrew Johnson")
62+
.add("Ulysses S. Grant")
63+
.add("Rutherford B. Hayes")
64+
.add("James A. Garfield")
65+
.add("Chester A. Arthur")
66+
.add("Grover Cleveland")
67+
.add("Benjamin Harrison")
68+
.add("Grover Cleveland")
69+
.add("William McKinley")
70+
.add("Theodore Roosevelt")
71+
.add("William Howard Taft")
72+
.add("Woodrow Wilson")
73+
.add("Warren G. Harding")
74+
.add("Calvin Coolidge")
75+
.add("Herbert Hoover")
76+
.add("Franklin D. Roosevelt")
77+
.add("Harry S. Truman")
78+
.add("Dwight D. Eisenhower")
79+
.add("John F. Kennedy")
80+
.add("Lyndon B. Johnson")
81+
.add("Richard Nixon")
82+
.add("Gerald Ford")
83+
.add("Jimmy Carter")
84+
.add("Ronald Reagan")
85+
.add("George H. W. Bush")
86+
.add("Bill Clinton")
87+
.add("George W. Bush")
88+
.add("Barack Obama")
89+
.build();
90+
91+
@Override
92+
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
93+
throws ServletException, IOException {
94+
resp.setContentType("text/plain");
95+
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
96+
97+
Key isPopulatedKey = KeyFactory.createKey(IS_POPULATED_ENTITY, IS_POPULATED_KEY_NAME);
98+
boolean isAlreadyPopulated;
99+
try {
100+
datastore.get(isPopulatedKey);
101+
isAlreadyPopulated = true;
102+
} catch (EntityNotFoundException expected) {
103+
isAlreadyPopulated = false;
104+
}
105+
if (isAlreadyPopulated) {
106+
resp.getWriter().println("ok");
107+
return;
108+
}
109+
110+
ImmutableList.Builder<Entity> people = ImmutableList.builder();
111+
for (String name : US_PRESIDENTS) {
112+
Entity person = new Entity(PERSON_ENTITY);
113+
person.setProperty(NAME_PROPERTY, name);
114+
people.add(person);
115+
}
116+
datastore.put(people.build());
117+
datastore.put(new Entity(isPopulatedKey));
118+
resp.getWriter().println("ok");
119+
}
120+
}

appengine/datastore/src/main/webapp/WEB-INF/web.xml

+19-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
</servlet>
2525
<servlet-mapping>
2626
<servlet-name>guestbook-strong</servlet-name>
27-
<url-pattern>/guestbook-strong</url-pattern>
27+
<url-pattern>/</url-pattern>
2828
</servlet-mapping>
2929
<servlet>
3030
<servlet-name>guestbook</servlet-name>
@@ -34,6 +34,14 @@
3434
<servlet-name>guestbook</servlet-name>
3535
<url-pattern>/guestbook</url-pattern>
3636
</servlet-mapping>
37+
<servlet>
38+
<servlet-name>people</servlet-name>
39+
<servlet-class>com.example.appengine.ListPeopleServlet</servlet-class>
40+
</servlet>
41+
<servlet-mapping>
42+
<servlet-name>people</servlet-name>
43+
<url-pattern>/people</url-pattern>
44+
</servlet-mapping>
3745
<servlet>
3846
<servlet-name>projection</servlet-name>
3947
<servlet-class>com.example.appengine.ProjectionServlet</servlet-class>
@@ -43,6 +51,16 @@
4351
<url-pattern>/projection</url-pattern>
4452
</servlet-mapping>
4553

54+
<!-- Special handler to populate Datastore with default values. -->
55+
<servlet>
56+
<servlet-name>startup</servlet-name>
57+
<servlet-class>com.example.appengine.StartupServlet</servlet-class>
58+
</servlet>
59+
<servlet-mapping>
60+
<servlet-name>startup</servlet-name>
61+
<url-pattern>/_ah/start</url-pattern>
62+
</servlet-mapping>
63+
4664
<security-constraint>
4765
<web-resource-collection>
4866
<web-resource-name>profile</web-resource-name>

0 commit comments

Comments
 (0)