Skip to content

Commit 026b974

Browse files
committed
Added files via upload
Moved examples from https://cloud.google.com/appengine/docs/java/requests to Github.
1 parent 0dd0927 commit 026b974

File tree

8 files changed

+438
-0
lines changed

8 files changed

+438
-0
lines changed

appengine/requests/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Request Handling sample for Google App Engine
2+
3+
This sample provides Java code samples in support of the "Handling Requests" description [Requests][requests-doc] on [Google App
4+
Engine][ae-docs].
5+
6+
[requests-doc]: https://cloud.google.com/appengine/docs/java/requests
7+
[ae-docs]: https://cloud.google.com/appengine/docs/java/
8+
9+
## Running locally
10+
This example uses the
11+
[Maven gcloud plugin](https://cloud.google.com/appengine/docs/java/managed-vms/maven).
12+
To run this sample locally:
13+
14+
$ mvn appengine:devserver
15+
16+
To see the results of the RequestsServlet, open localhost:8080 in a WWW browser.
17+
18+
To see the results of the LoggingServlet, open localhost:8080/logs in a WWW browser
19+
and examine the logs to see the actual messages.
20+
21+
## Deploying
22+
In the following command, replace YOUR-PROJECT-ID with your
23+
[Google Cloud Project ID](https://developers.google.com/console/help/new/#projectnumber)
24+
and SOME-VERSION with a valid version number.
25+
26+
$ mvn appengine:update -Dappengine.appId=YOUR-PROJECT-ID -Dappengine.version=SOME-VERSION
27+
28+
## Setup
29+
To save your project settings so that you don't need to enter the
30+
parameters, you can:
31+
32+
1. Update the <application> tag in src/main/webapp/WEB-INF/appengine-web.xml
33+
with your project name.
34+
35+
2. Update the <version> tag in src/main/webapp/WEB-INF/appengine-web.xml
36+
with a valid version number.
37+
38+
39+
You will now be able to run
40+
41+
$ mvn appengine:update
42+
43+
without the need for any additional parameters.

appengine/requests/pom.xml

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2016 Google Inc. All Rights Reserved.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<project>
18+
<modelVersion>4.0.0</modelVersion>
19+
<packaging>war</packaging>
20+
<version>1.0-SNAPSHOT</version>
21+
<groupId>com.example.appengine</groupId>
22+
<artifactId>appengine-requests</artifactId>
23+
<!-- Parent POM defines ${appengine.sdk.version} (updates frequently). -->
24+
<parent>
25+
<groupId>com.google.cloud</groupId>
26+
<artifactId>doc-samples</artifactId>
27+
<version>1.0.0</version>
28+
<relativePath>../..</relativePath>
29+
</parent>
30+
<dependencies>
31+
<dependency>
32+
<groupId>com.google.appengine</groupId>
33+
<artifactId>appengine-maven-plugin</artifactId>
34+
<version>${appengine.sdk.version}</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>com.google.guava</groupId>
38+
<artifactId>guava</artifactId>
39+
<version>19.0</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>javax.servlet</groupId>
43+
<artifactId>servlet-api</artifactId>
44+
<version>2.5</version>
45+
<type>jar</type>
46+
<scope>provided</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.json</groupId>
50+
<artifactId>json</artifactId>
51+
<version>20151123</version>
52+
</dependency>
53+
<!-- Test Dependencies -->
54+
<dependency>
55+
<groupId>junit</groupId>
56+
<artifactId>junit</artifactId>
57+
<version>4.10</version>
58+
<scope>test</scope>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.mockito</groupId>
62+
<artifactId>mockito-all</artifactId>
63+
<version>1.10.19</version>
64+
<scope>test</scope>
65+
</dependency>
66+
<dependency>
67+
<groupId>com.google.appengine</groupId>
68+
<artifactId>appengine-testing</artifactId>
69+
<version>${appengine.sdk.version}</version>
70+
<scope>test</scope>
71+
</dependency>
72+
<dependency>
73+
<groupId>com.google.appengine</groupId>
74+
<artifactId>appengine-api-stubs</artifactId>
75+
<version>${appengine.sdk.version}</version>
76+
<scope>test</scope>
77+
</dependency>
78+
<dependency>
79+
<groupId>com.google.appengine</groupId>
80+
<artifactId>appengine-tools-sdk</artifactId>
81+
<version>${appengine.sdk.version}</version>
82+
<scope>test</scope>
83+
</dependency>
84+
<dependency>
85+
<groupId>com.google.truth</groupId>
86+
<artifactId>truth</artifactId>
87+
<version>0.28</version>
88+
<scope>test</scope>
89+
</dependency>
90+
91+
</dependencies>
92+
<build>
93+
<!-- for hot reload of the web application -->
94+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
95+
<plugins>
96+
<plugin>
97+
<groupId>org.apache.maven.plugins</groupId>
98+
<version>3.3</version>
99+
<artifactId>maven-compiler-plugin</artifactId>
100+
<configuration>
101+
<source>1.7</source>
102+
<target>1.7</target>
103+
</configuration>
104+
</plugin>
105+
<plugin>
106+
<groupId>com.google.appengine</groupId>
107+
<artifactId>appengine-maven-plugin</artifactId>
108+
<version>${appengine.sdk.version}</version>
109+
</plugin>
110+
</plugins>
111+
</build>
112+
</project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* Copyright 2016 Google Inc. All Rights Reserved.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
package com.example.appengine.requests;
16+
17+
import java.io.IOException;
18+
import java.util.logging.Logger;
19+
20+
import javax.servlet.http.HttpServlet;
21+
import javax.servlet.http.HttpServletRequest;
22+
import javax.servlet.http.HttpServletResponse;
23+
24+
// [START simple_logging_example]
25+
public class LoggingServlet extends HttpServlet {
26+
private static final Logger log = Logger.getLogger(LoggingServlet.class.getName());
27+
28+
@Override
29+
public void doGet(HttpServletRequest req, HttpServletResponse resp)
30+
throws IOException {
31+
log.info("An informational message.");
32+
log.warning("A warning message.");
33+
log.severe("An error message.");
34+
// [START_EXCLUDE]
35+
resp.setContentType("text/plain");
36+
resp.getWriter().println("Check logs for results");
37+
// [END_EXCLUDE]
38+
}
39+
}
40+
// [END simple_logging_example]
41+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* Copyright 2016 Google Inc. All Rights Reserved.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
package com.example.appengine.requests;
16+
17+
import java.io.IOException;
18+
19+
import javax.servlet.http.HttpServlet;
20+
import javax.servlet.http.HttpServletRequest;
21+
import javax.servlet.http.HttpServletResponse;
22+
23+
// [START simple_request_example]
24+
public class RequestsServlet extends HttpServlet {
25+
@Override
26+
public void doGet(HttpServletRequest req, HttpServletResponse resp)
27+
throws IOException {
28+
resp.setContentType("text/plain");
29+
resp.getWriter().println("Hello, world");
30+
}
31+
}
32+
// [END simple_request_example]
33+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
3+
<application>YOUR-PROJECT-ID</application>
4+
<version>YOUR-VERSION-ID</version>
5+
<threadsafe>true</threadsafe>
6+
</appengine-web-app>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
3+
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
4+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
5+
version="2.5">
6+
<servlet>
7+
<servlet-name>requests</servlet-name>
8+
<servlet-class>com.example.appengine.requests.RequestsServlet</servlet-class>
9+
</servlet>
10+
<servlet>
11+
<servlet-name>logging</servlet-name>
12+
<servlet-class>com.example.appengine.requests.LoggingServlet</servlet-class>
13+
</servlet>
14+
<servlet-mapping>
15+
<servlet-name>requests</servlet-name>
16+
<url-pattern>/</url-pattern>
17+
</servlet-mapping>
18+
<servlet-mapping>
19+
<servlet-name>requests</servlet-name>
20+
<url-pattern>/requests</url-pattern>
21+
</servlet-mapping>
22+
<servlet-mapping>
23+
<servlet-name>logging</servlet-name>
24+
<url-pattern>/logs</url-pattern>
25+
</servlet-mapping>
26+
</web-app>
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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.requests;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static org.mockito.Mockito.when;
21+
22+
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
23+
24+
import org.junit.After;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.junit.runners.JUnit4;
29+
import org.mockito.Mock;
30+
import org.mockito.MockitoAnnotations;
31+
32+
import java.io.ByteArrayOutputStream;
33+
import java.io.PrintStream;
34+
import java.io.PrintWriter;
35+
import java.io.StringWriter;
36+
37+
import javax.servlet.http.HttpServletRequest;
38+
import javax.servlet.http.HttpServletResponse;
39+
40+
/**
41+
* Unit tests for {@link LoggingServlet}.
42+
*/
43+
@RunWith(JUnit4.class)
44+
public class LoggingServletTest {
45+
// Set up a helper so that the ApiProxy returns a valid environment for local testing.
46+
private final LocalServiceTestHelper helper = new LocalServiceTestHelper();
47+
48+
// To capture and restore stderr
49+
private final ByteArrayOutputStream stderr = new ByteArrayOutputStream();
50+
private static final PrintStream REAL_ERR = System.err;
51+
52+
@Mock private HttpServletRequest mockRequest;
53+
@Mock private HttpServletResponse mockResponse;
54+
private StringWriter responseWriter;
55+
private LoggingServlet servletUnderTest;
56+
57+
@Before
58+
public void setUp() throws Exception {
59+
// Capture stderr to examine messages written to it
60+
System.setErr(new PrintStream(stderr));
61+
62+
MockitoAnnotations.initMocks(this);
63+
helper.setUp();
64+
65+
// Set up a fake HTTP response.
66+
responseWriter = new StringWriter();
67+
when(mockResponse.getWriter()).thenReturn(new PrintWriter(responseWriter));
68+
69+
servletUnderTest = new LoggingServlet();
70+
}
71+
72+
@After
73+
public void tearDown() {
74+
// Restore stderr
75+
System.setErr(LoggingServletTest.REAL_ERR);
76+
77+
helper.tearDown();
78+
}
79+
80+
@Test
81+
public void testListLogs() throws Exception {
82+
servletUnderTest.doGet(mockRequest, mockResponse);
83+
84+
String out = stderr.toString();
85+
86+
// We expect three log messages to be created
87+
// with the following messages.
88+
assertThat(out).contains("An informational message.");
89+
assertThat(out).contains("A warning message.");
90+
assertThat(out).contains("An error message.");
91+
92+
// We expect three log messages to be created
93+
// with the following severities.
94+
// Since there's no guarantee of case, use lowercase
95+
String lcOut = out.toLowerCase();
96+
assertThat(lcOut).contains("info");
97+
assertThat(lcOut).contains("warning");
98+
assertThat(lcOut).contains("severe");
99+
}
100+
101+
}

0 commit comments

Comments
 (0)