Skip to content

Commit

Permalink
[WIP] Java 11 Region Tags (#1405)
Browse files Browse the repository at this point in the history
* Guestbook region tags

* simple jetty main region tags

* rename hello world

* fix license

* renamed

* Fix license

* Task region tags

* fix errors

* Add parent to pom.xml

* Add oauth region tags

* Update firestore instance

* Fix styling

* fix exception
  • Loading branch information
averikitsch authored May 7, 2019
1 parent 427bf71 commit d3d5ec4
Show file tree
Hide file tree
Showing 27 changed files with 109 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.example.appengine.demo.jettymain;

// [START gae_java11_server]
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.Configuration.ClassList;
import org.eclipse.jetty.webapp.WebAppContext;
Expand Down Expand Up @@ -61,3 +62,4 @@ public static void main(String[] args) throws Exception {
server.join();
}
}
// [END gae_java11_server]
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Google Inc.
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,6 +14,7 @@
* limitations under the License.
*/

// [START gae_java11_simple_server]
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.io.OutputStream;
Expand All @@ -38,3 +39,4 @@ public static void main(String[] args) throws IOException {
server.start();
}
}
// [END gae_java11_simple_server]
File renamed without changes.
18 changes: 18 additions & 0 deletions appengine-java11/custom-entrypoint/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2019 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.

# [START gae_java11_custom_entrypoint]
runtime: java11
entrypoint: java Main.java
# [END gae_java11_custom_entrypoint]
2 changes: 1 addition & 1 deletion appengine-java11/gaeinfo/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
Copyright 2019 Google Inc.
Copyright 2019 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<!--
Copyright 2019 Google Inc.
Copyright 2019 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
14 changes: 12 additions & 2 deletions appengine-java11/guestbook-cloud-firestore/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@
<groupId>com.example.guestbook</groupId>
<artifactId>guestbook-cloud-firestore</artifactId>

<!--
The parent pom defines common style checks and testing strategies for our samples.
Removing or replacing it should not affect the execution of the samples in anyway.
-->
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.0.11</version>
</parent>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
Expand All @@ -36,14 +46,14 @@
<version>1</version>
<scope>provided</scope>
</dependency>

<!-- [START gae_java11_firestore_pom] -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-firestore</artifactId>
<version>0.81.0-beta</version>
<scope>provided</scope>
</dependency>

<!-- [END gae_java11_firestore_pom] -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START gae_java11_firestore_yaml]
runtime: java11
instance_class: F2
entrypoint: 'java -cp * com.example.appengine.demo.jettymain.Main guestbook.war'
# [END gae_java11_firestore_yaml]
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.example.guestbook;


import com.google.common.base.MoreObjects;
import java.util.Date;
import java.util.HashMap;
Expand Down Expand Up @@ -51,6 +50,7 @@ public Greeting(String book, String content, String name) {

/** Save the Greeting in the guestbook */
public void save() {
// [START gae_java11_firestore_greeting]
// Construct a Greeting.
Map<String, Object> greetingData = new HashMap<>();
greetingData.put("date", date);
Expand All @@ -59,6 +59,7 @@ public void save() {

// Add Greeting to Guestbook with random id.
book.getBookRef().collection("Greetings").add(greetingData);
// [END gae_java11_firestore_greeting]
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,19 @@ public Guestbook(String book) {
// Construct the Guestbook data.
Map<String, Object> bookData = new HashMap<>();
bookData.put("name", this.book);

// [START gae_java11_firestore_book]
// Get Guestbook reference in the collection.
bookRef = getFirestore().collection("Guestbooks").document(this.book);
// Add Guestbook to collection.
bookRef.set(bookData);
// [END gae_java11_firestore_book]
}

public DocumentReference getBookRef() {
return bookRef;
}

// [START gae_java11_firestore_get_greetings]
/** Get greetings for the Guestbook */
public List<Greeting> getGreetings() {
// Initialize a List for Greetings.
Expand All @@ -71,12 +73,13 @@ public List<Greeting> getGreetings() {
for (QueryDocumentSnapshot greeting : querySnapshot.getDocuments()) {
greetings.add(greeting.toObject(Greeting.class));
}
} catch (Exception InterruptedException) {
System.out.println("Nothing to query.");
} catch (Exception e) {
System.out.println(e.getMessage());
}

return greetings.build();
}
// [END gae_java11_firestore_get_greetings]

@Override
public boolean equals(Object obj) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,31 @@

package com.example.guestbook;

// [START gae_java11_firestore_dependencies]
import com.google.cloud.firestore.Firestore;
import com.google.cloud.firestore.FirestoreOptions;
import java.util.concurrent.atomic.AtomicReference;
// [END gae_java11_firestore_dependencies]

/** Create a persistence connection to your Firestore instance. */
public class Persistence {

private static AtomicReference<Firestore> firestore = new AtomicReference<>();
private static Firestore firestore;

@SuppressWarnings("JavadocMethod")
public static Firestore getFirestore() {
if (firestore.get() == null) {
if (firestore == null) {
// Authorized Firestore service
firestore.set(
FirestoreOptions.newBuilder().setProjectId("YOUR-PROJECT-ID").build().getService());
// [START gae_java11_firestore]
Firestore db =
FirestoreOptions.newBuilder().setProjectId("YOUR-PROJECT-ID").build().getService();
// [END gae_java11_firestore]
firestore = db;
}

return firestore.get();
return firestore;
}

public static void setFirestore(Firestore firestore) {
Persistence.firestore.set(firestore);
Persistence.firestore = firestore;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@

package com.example.guestbook;

// [START gae_java11_form_data]
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** Servlet to sace the form data to your Firestore instance. */
@SuppressWarnings("serial")
@WebServlet(name = "SignGuestbookServlet", value="/sign")
@WebServlet(name = "SignGuestbookServlet", value = "/sign")
public class SignGuestbookServlet extends HttpServlet {

// Process the HTTP POST of the form
Expand All @@ -44,3 +45,4 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp)
resp.sendRedirect("/index.jsp?guestbookName=" + guestbookName);
}
}
// [END gae_java11_form_data]
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ limitations under the License.
<div><input type="submit" value="Switch Guestbook"/></div>
</form>

<h3>Write Your Greeting</h3>

<%-- Guestbook form --%>
<h3>Write Your Greeting</h3>
<%-- [START gae_java11_form] --%>
<form method="POST" action="/sign">
<div>
<label for="name">Name</label>
Expand All @@ -72,8 +72,9 @@ limitations under the License.
<div><input type="submit" value="Sign"/></div>
<input type="hidden" name="guestbookName" value="${fn:escapeXml(guestbookName)}"/>
</form>
<%-- [END gae_java11_form] --%>


<%-- List greetings --%>
<h3>Greetings:</h3>

<% if (greetings.isEmpty()) { %>
Expand Down
2 changes: 0 additions & 2 deletions appengine-java11/hello-world/app.yaml

This file was deleted.

14 changes: 14 additions & 0 deletions appengine-java11/http-server/src/main/appengine/app.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
# Copyright 2019 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.

runtime: java11
# No need for an entrypoint with single fatjar with correct manifest class-path entry.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Google Inc.
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.appengine;

import com.sun.net.httpserver.HttpServer;
Expand Down
2 changes: 2 additions & 0 deletions appengine-java11/oauth2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
</dependency>

<!-- OAuth 2.0 packages -->
<!-- [START gae_java11_oauth2_dependencies] -->
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client</artifactId>
Expand All @@ -72,6 +73,7 @@
<version>v2-rev131-1.23.0</version>
<scope>provided</scope>
</dependency>
<!-- [END gae_java11_oauth2_dependencies] -->

<dependency>
<groupId>javax.servlet</groupId>
Expand Down
2 changes: 2 additions & 0 deletions appengine-java11/oauth2/src/main/appengine/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ runtime: java11
instance_class: F2
entrypoint: 'java -cp * com.example.appengine.demo.jettymain.Main oauth2.war'

# [START gae_java11_oauth2_yaml]
env_variables:
CLIENT_ID: '<YOUR_CLIENT_ID>'
CLIENT_SECRET: '<YOUR_CLIENT_SECRET>'
# [END gae_java11_oauth2_yaml]
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.example.appengine;

// [START gae_java11_oauth2_login]
import com.google.api.client.auth.oauth2.AuthorizationCodeFlow;
import com.google.api.client.extensions.servlet.auth.oauth2.AbstractAuthorizationCodeServlet;
import java.io.IOException;
Expand Down Expand Up @@ -49,3 +50,4 @@ protected String getUserId(HttpServletRequest req) throws ServletException, IOEx
return Utils.getUserId(req);
}
}
// [START gae_java11_oauth2_login]
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.example.appengine;

// [START gae_java11_oauth2_callback]
import com.google.api.client.auth.oauth2.AuthorizationCodeFlow;
import com.google.api.client.auth.oauth2.AuthorizationCodeResponseUrl;
import com.google.api.client.auth.oauth2.Credential;
Expand Down Expand Up @@ -75,3 +76,4 @@ protected String getUserId(HttpServletRequest req) throws ServletException, IOEx
return Utils.getUserId(req);
}
}
// [END gae_java11_oauth2_callback]
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ static String getRedirectUri(HttpServletRequest req) {
return url.build();
}

// [START gae_java11_oauth2_code_flow]
/**
* Loads the authorization code flow to be used across all HTTP servlet requests. It is only
* called during the first HTTP servlet request.
Expand All @@ -79,6 +80,7 @@ public static GoogleAuthorizationCodeFlow newFlow() throws IOException {
.setAccessType("offline")
.build();
}
// [END gae_java11_oauth2_code_flow]

/**
* Returns the user ID for the given HTTP servlet request. This identifies your application's user
Expand All @@ -89,6 +91,7 @@ static String getUserId(HttpServletRequest req) throws ServletException, IOExcep
return req.getSession().getId();
}

// [START gae_java11_oauth2_get_user]
/** Obtain end-user authorization grant for Google APIs and return username */
public static String getUserInfo(Credential credential) throws IOException {
Oauth2 oauth2Client =
Expand All @@ -101,4 +104,5 @@ public static String getUserInfo(Credential credential) throws IOException {
String username = userInfo.getGivenName();
return username;
}
// [END gae_java11_oauth2_get_user]
}
2 changes: 1 addition & 1 deletion appengine-java11/oauth2/src/main/webapp/index.jsp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<!--
Copyright 2019 Google Inc.
Copyright 2019 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019 Google Inc.
Copyright 2019 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Loading

0 comments on commit d3d5ec4

Please sign in to comment.