Skip to content

Commit c1bdd10

Browse files
committed
Merge pull request #158 from GoogleCloudPlatform/urlfetch
Add URLFetch sample
2 parents c56f553 + a00fa21 commit c1bdd10

File tree

7 files changed

+171
-0
lines changed

7 files changed

+171
-0
lines changed

appengine/urlfetch/.gitignore

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Eclipse files
2+
.project
3+
.classpath
4+
.settings
5+
6+
# Intellij
7+
.idea/
8+
*.iml
9+
10+
# Target folders
11+
target/

appengine/urlfetch/README.md

+20
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

appengine/urlfetch/pom.xml

+62
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>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 java.io.BufferedReader;
22+
import java.io.IOException;
23+
import java.io.InputStreamReader;
24+
import java.io.PrintWriter;
25+
import java.net.URL;
26+
27+
import javax.servlet.http.HttpServlet;
28+
import javax.servlet.http.HttpServletRequest;
29+
import javax.servlet.http.HttpServletResponse;
30+
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+
}
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>
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

+1
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)