Skip to content

Commit c9cc40d

Browse files
author
Les Vogel
committed
Add URLFetch sample
1 parent c56f553 commit c9cc40d

File tree

8 files changed

+203
-0
lines changed

8 files changed

+203
-0
lines changed

appengine/urlfetch/.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Eclipse files
2+
.project
3+
.classpath
4+
.settings
5+
6+
# Intellij
7+
.idea/
8+
9+
# Target folders
10+
target/

appengine/urlfetch/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Google App Engine Standard Environment URL Fetch Sample
2+
3+
This sample demonstrates how to deploy an application on Google App Engine.
4+
5+
See the [Google App Engine standard environment documentation][ae-docs] for more
6+
detailed instructions.
7+
8+
[ae-docs]: https://cloud.google.com/appengine/docs/java/
9+
10+
## Setup
11+
1. Update the `<application>` tag in `src/main/webapp/WEB-INF/appengine-web.xml`
12+
with your project name.
13+
1. Update the `<version>` tag in `src/main/webapp/WEB-INF/appengine-web.xml`
14+
with your version name.
15+
16+
## Running locally
17+
$ mvn appengine:devserver
18+
19+
## Deploying
20+
$ mvn appengine:update
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3+
<component name="FacetManager">
4+
<facet type="google-app-engine" name="Google App Engine">
5+
<configuration>
6+
<sdk-home-path>$MAVEN_REPOSITORY$/com/google/appengine/appengine-java-sdk/1.9.34/appengine-java-sdk/appengine-java-sdk-1.9.34</sdk-home-path>
7+
</configuration>
8+
</facet>
9+
<facet type="web" name="Web">
10+
<configuration>
11+
<descriptors>
12+
<deploymentDescriptor name="web.xml" url="file://$MODULE_DIR$/src/main/webapp/WEB-INF/web.xml" />
13+
</descriptors>
14+
<webroots>
15+
<root url="file://$MODULE_DIR$/src/main/webapp" relative="/" />
16+
</webroots>
17+
</configuration>
18+
</facet>
19+
</component>
20+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="false">
21+
<output url="file://$MODULE_DIR$/target/appengine-URLFetch-1.0-SNAPSHOT/WEB-INF/classes" />
22+
<output-test url="file://$MODULE_DIR$/target/test-classes" />
23+
<content url="file://$MODULE_DIR$">
24+
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
25+
<excludeFolder url="file://$MODULE_DIR$/target" />
26+
</content>
27+
<orderEntry type="inheritedJdk" />
28+
<orderEntry type="sourceFolder" forTests="false" />
29+
<orderEntry type="library" scope="PROVIDED" name="Maven: javax.servlet:servlet-api:2.5" level="project" />
30+
<orderEntry type="library" name="Maven: org.json:json:20160212" level="project" />
31+
</component>
32+
</module>

appengine/urlfetch/pom.xml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<!--
2+
Copyright 2015 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+
<project>
17+
<modelVersion>4.0.0</modelVersion>
18+
<packaging>war</packaging>
19+
<version>1.0-SNAPSHOT</version>
20+
<groupId>com.example.appengine</groupId>
21+
<artifactId>appengine-URLFetch</artifactId>
22+
<parent>
23+
<groupId>com.google.cloud</groupId>
24+
<artifactId>doc-samples</artifactId>
25+
<version>1.0.0</version>
26+
<relativePath>../..</relativePath>
27+
</parent>
28+
<dependencies>
29+
<dependency>
30+
<groupId>javax.servlet</groupId>
31+
<artifactId>servlet-api</artifactId>
32+
<type>jar</type>
33+
<scope>provided</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.json</groupId>
37+
<artifactId>json</artifactId>
38+
<version>20160212</version>
39+
</dependency>
40+
</dependencies>
41+
<build>
42+
<!-- for hot reload of the web application -->
43+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
44+
<plugins>
45+
<plugin>
46+
<groupId>org.apache.maven.plugins</groupId>
47+
<version>3.3</version>
48+
<artifactId>maven-compiler-plugin</artifactId>
49+
<configuration>
50+
<source>1.7</source>
51+
<target>1.7</target>
52+
</configuration>
53+
</plugin>
54+
<!-- Parent POM defines ${appengine.sdk.version} (updates frequently). -->
55+
<plugin>
56+
<groupId>com.google.appengine</groupId>
57+
<artifactId>appengine-maven-plugin</artifactId>
58+
<version>${appengine.sdk.version}</version>
59+
</plugin>
60+
</plugins>
61+
</build>
62+
</project>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright 2015 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 org.json.JSONObject;
20+
21+
import javax.servlet.http.HttpServlet;
22+
import javax.servlet.http.HttpServletRequest;
23+
import javax.servlet.http.HttpServletResponse;
24+
import java.io.BufferedReader;
25+
import java.io.IOException;
26+
import java.io.InputStreamReader;
27+
import java.io.PrintWriter;
28+
import java.net.URL;
29+
30+
// [START example]
31+
@SuppressWarnings("serial")
32+
public class UrlFetchServlet extends HttpServlet {
33+
34+
@Override
35+
public void doGet(HttpServletRequest req, HttpServletResponse resp)
36+
throws IOException {
37+
PrintWriter out = resp.getWriter();
38+
out.println("<html><body>");
39+
40+
// [START example]
41+
URL url = new URL("http://api.icndb.com/jokes/random");
42+
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
43+
String json = "";
44+
String line;
45+
46+
while ((line = reader.readLine()) != null) {
47+
json += line;
48+
}
49+
reader.close();
50+
// [END example]
51+
JSONObject jo = new JSONObject(json);
52+
out.println("<h2>"
53+
+jo.getJSONObject("value").getString("joke")
54+
+"</h2>");
55+
out.println("</body></html>");
56+
}
57+
}
58+
// [END example]
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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>hello</servlet-name>
8+
<servlet-class>com.example.appengine.UrlFetchServlet</servlet-class>
9+
</servlet>
10+
<servlet-mapping>
11+
<servlet-name>hello</servlet-name>
12+
<url-pattern>/</url-pattern>
13+
</servlet-mapping>
14+
</web-app>

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
<module>appengine/sendgrid</module>
5353
<module>appengine/static-files</module>
5454
<module>appengine/twilio</module>
55+
<module>appengine/urlfetch</module>
5556
<module>bigquery</module>
5657
<module>datastore</module>
5758
<module>logging</module>

0 commit comments

Comments
 (0)