Skip to content

Commit b4dce01

Browse files
authored
fix(appengine): removing obsolete symlink from java 8 on java 11 bundle services (#10272)
* fix(gae): fix broken symlinks for java11 bundle services" * refactor: remove redundant region tags, sanitize JSP output, and modernize Java stream usage * refactor: extract entity name property to local variable for readability in ListPeopleServlet
1 parent a630c7b commit b4dce01

33 files changed

Lines changed: 4094 additions & 32 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

appengine-java11-bundled-services/datastore/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@
126126
<version>1.4.0</version>
127127
<scope>test</scope>
128128
</dependency>
129+
<dependency>
130+
<groupId>jstl</groupId>
131+
<artifactId>jstl</artifactId>
132+
<version>1.2</version>
133+
</dependency>
129134
</dependencies>
130135

131136
<build>

appengine-java11-bundled-services/datastore/src/main/java/com/example/appengine/AbstractGuestbook.java

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2016 Google LLC
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.example.time.Clock;
20+
import com.google.appengine.api.datastore.DatastoreService;
21+
import com.google.appengine.api.datastore.DatastoreServiceFactory;
22+
import com.google.appengine.api.datastore.Entity;
23+
import com.google.appengine.api.users.User;
24+
import com.google.appengine.api.users.UserService;
25+
import com.google.appengine.api.users.UserServiceFactory;
26+
import com.google.common.collect.ImmutableList;
27+
import java.util.Date;
28+
import java.util.List;
29+
30+
/**
31+
* A log of notes left by users.
32+
*
33+
* <p>This is meant to be subclassed to demonstrate different storage structures in Datastore.
34+
*/
35+
abstract class AbstractGuestbook {
36+
37+
private final DatastoreService datastore;
38+
private final UserService userService;
39+
private final Clock clock;
40+
41+
AbstractGuestbook(Clock clock) {
42+
this.datastore = DatastoreServiceFactory.getDatastoreService();
43+
this.userService = UserServiceFactory.getUserService();
44+
this.clock = clock;
45+
}
46+
47+
/**
48+
* Appends a new greeting to the guestbook and returns the {@link Entity} that was created.
49+
**/
50+
public Greeting appendGreeting(String content) {
51+
return Greeting.create(
52+
createGreeting(datastore, userService.getCurrentUser(), Date.from(clock.now()), content));
53+
}
54+
55+
/**
56+
* Write a greeting to Datastore.
57+
*/
58+
protected abstract Entity createGreeting(
59+
DatastoreService datastore, User user, Date date, String content);
60+
61+
/**
62+
* Return a list of the most recent greetings.
63+
*/
64+
public List<Greeting> listGreetings() {
65+
return listGreetingEntities(datastore).stream()
66+
.map(Greeting::create)
67+
.collect(ImmutableList.toImmutableList());
68+
}
69+
70+
/**
71+
* Return a list of the most recent greetings.
72+
*/
73+
protected abstract List<Entity> listGreetingEntities(DatastoreService datastore);
74+
}

appengine-java11-bundled-services/datastore/src/main/java/com/example/appengine/AbstractGuestbookServlet.java

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2016 Google LLC
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 java.io.IOException;
20+
import javax.servlet.ServletException;
21+
import javax.servlet.http.HttpServlet;
22+
import javax.servlet.http.HttpServletRequest;
23+
import javax.servlet.http.HttpServletResponse;
24+
25+
abstract class AbstractGuestbookServlet extends HttpServlet {
26+
27+
private final AbstractGuestbook guestbook;
28+
29+
public AbstractGuestbookServlet(AbstractGuestbook guestbook) {
30+
this.guestbook = guestbook;
31+
}
32+
33+
private void renderGuestbook(HttpServletRequest req, HttpServletResponse resp)
34+
throws IOException, ServletException {
35+
resp.setContentType("text/html");
36+
resp.setCharacterEncoding("UTF-8");
37+
req.setAttribute("greetings", guestbook.listGreetings());
38+
req.getRequestDispatcher("/guestbook.jsp").forward(req, resp);
39+
}
40+
41+
@Override
42+
public void doGet(HttpServletRequest req, HttpServletResponse resp)
43+
throws IOException, ServletException {
44+
renderGuestbook(req, resp);
45+
}
46+
47+
@Override
48+
public void doPost(HttpServletRequest req, HttpServletResponse resp)
49+
throws IOException, ServletException {
50+
String content = req.getParameter("content");
51+
if (content == null || content.isEmpty()) {
52+
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "missing content");
53+
return;
54+
}
55+
guestbook.appendGreeting(content);
56+
renderGuestbook(req, resp);
57+
}
58+
}

appengine-java11-bundled-services/datastore/src/main/java/com/example/appengine/Greeting.java

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2016 Google LLC
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.Entity;
20+
import com.google.appengine.api.users.User;
21+
import com.google.auto.value.AutoValue;
22+
import java.time.Instant;
23+
import java.util.Date;
24+
import javax.annotation.Nullable;
25+
26+
@AutoValue
27+
public abstract class Greeting {
28+
29+
static Greeting create(Entity entity) {
30+
User user = (User) entity.getProperty("user");
31+
Instant date = ((Date) entity.getProperty("date")).toInstant();
32+
String content = (String) entity.getProperty("content");
33+
return new AutoValue_Greeting(user, date, content);
34+
}
35+
36+
@Nullable
37+
public abstract User getUser();
38+
39+
public abstract Instant getDate();
40+
41+
public abstract String getContent();
42+
}

appengine-java11-bundled-services/datastore/src/main/java/com/example/appengine/Guestbook.java

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2016 Google LLC
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.example.time.Clock;
20+
import com.google.appengine.api.datastore.DatastoreService;
21+
import com.google.appengine.api.datastore.Entity;
22+
import com.google.appengine.api.datastore.FetchOptions;
23+
import com.google.appengine.api.datastore.Query;
24+
import com.google.appengine.api.users.User;
25+
import java.util.Date;
26+
import java.util.List;
27+
28+
/**
29+
* A log of notes left by users.
30+
*
31+
* <p>This demonstrates the use of Google Cloud Datastore using the App Engine APIs. See the <a
32+
* href="https://cloud.google.com/appengine/docs/java/datastore/">documentation</a> for more
33+
* information.
34+
*/
35+
class Guestbook extends AbstractGuestbook {
36+
37+
Guestbook(Clock clock) {
38+
super(clock);
39+
}
40+
41+
@Override
42+
protected Entity createGreeting(
43+
DatastoreService datastore, User user, Date date, String content) {
44+
// No parent key specified, so Greeting is a root entity.
45+
Entity greeting = new Entity("Greeting");
46+
greeting.setProperty("user", user);
47+
greeting.setProperty("date", date);
48+
greeting.setProperty("content", content);
49+
50+
datastore.put(greeting);
51+
return greeting;
52+
}
53+
54+
@Override
55+
protected List<Entity> listGreetingEntities(DatastoreService datastore) {
56+
Query query = new Query("Greeting").addSort("date", Query.SortDirection.DESCENDING);
57+
return datastore.prepare(query).asList(FetchOptions.Builder.withLimit(10));
58+
}
59+
}

appengine-java11-bundled-services/datastore/src/main/java/com/example/appengine/GuestbookServlet.java

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)