Skip to content
Draft
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
5 changes: 5 additions & 0 deletions appengine-java17-bundled-services/datastore/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@
<version>1.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>

<build>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2016 Google LLC
*
* 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) {
return Greeting.create(
createGreeting(datastore, userService.getCurrentUser(), Date.from(clock.now()), content));
}

/**
* 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);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2016 Google LLC
*
* 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);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2016 Google LLC
*
* 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 java.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 = ((Date) entity.getProperty("date")).toInstant();
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();
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2016 Google LLC
*
* 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));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2016 Google LLC
*
* 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.SystemClock;

public class GuestbookServlet extends AbstractGuestbookServlet {

public GuestbookServlet() {
super(new Guestbook(new SystemClock()));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2016 Google LLC
*
* 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.Key;
import com.google.appengine.api.datastore.KeyFactory;
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 GuestbookStrong extends AbstractGuestbook {

private final String guestbookName;

GuestbookStrong(String guestbookName, Clock clock) {
super(clock);
this.guestbookName = guestbookName;
}

@Override
protected Entity createGreeting(
DatastoreService datastore, User user, Date date, String content) {
// String guestbookName = "my guestbook"; -- Set elsewhere (injected to the constructor).
Key guestbookKey = KeyFactory.createKey("Guestbook", guestbookName);

// Place greeting in the same entity group as guestbook.
Entity greeting = new Entity("Greeting", guestbookKey);
greeting.setProperty("user", user);
greeting.setProperty("date", date);
greeting.setProperty("content", content);

datastore.put(greeting);
return greeting;
}

@Override
protected List<Entity> listGreetingEntities(DatastoreService datastore) {
Key guestbookKey = KeyFactory.createKey("Guestbook", guestbookName);
Query query =
new Query("Greeting", guestbookKey)
.setAncestor(guestbookKey)
.addSort("date", Query.SortDirection.DESCENDING);
return datastore.prepare(query).asList(FetchOptions.Builder.withLimit(10));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2016 Google LLC
*
* 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.SystemClock;

public class GuestbookStrongServlet extends AbstractGuestbookServlet {

public static final String GUESTBOOK_ID = "my guestbook";

public GuestbookStrongServlet() {
super(new GuestbookStrong(GUESTBOOK_ID, new SystemClock()));
}
}
Loading