Skip to content

Commit 847bcc5

Browse files
committed
Merge pull request GoogleCloudPlatform#133 from GoogleCloudPlatform/copy-to-ae
Add appengine versions of managed vm samples
2 parents 5b8ba30 + 47907e8 commit 847bcc5

File tree

27 files changed

+1023
-5
lines changed

27 files changed

+1023
-5
lines changed

appengine/analytics/pom.xml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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-analytics</artifactId>
22+
23+
<parent>
24+
<artifactId>doc-samples</artifactId>
25+
<groupId>com.google.cloud</groupId>
26+
<version>1.0.0</version>
27+
<relativePath>../..</relativePath>
28+
</parent>
29+
<!-- Parent POM defines ${appengine.sdk.version} (updates frequently). -->
30+
<dependencies>
31+
<dependency>
32+
<groupId>com.google.appengine</groupId>
33+
<artifactId>appengine-api-1.0-sdk</artifactId>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.apache.httpcomponents</groupId>
37+
<artifactId>httpclient</artifactId>
38+
<version>4.5.1</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>javax.servlet</groupId>
42+
<artifactId>javax.servlet-api</artifactId>
43+
<version>3.1.0</version>
44+
<type>jar</type>
45+
<scope>provided</scope>
46+
</dependency>
47+
</dependencies>
48+
<build>
49+
<!-- for hot reload of the web application -->
50+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
51+
<plugins>
52+
<!-- Parent POM defines ${appengine.sdk.version} (updates frequently). -->
53+
<plugin>
54+
<groupId>com.google.appengine</groupId>
55+
<artifactId>appengine-maven-plugin</artifactId>
56+
<version>${appengine.sdk.version}</version>
57+
</plugin>
58+
<plugin>
59+
<groupId>org.apache.maven.plugins</groupId>
60+
<version>3.3</version>
61+
<artifactId>maven-compiler-plugin</artifactId>
62+
<configuration>
63+
<source>1.7</source>
64+
<target>1.7</target>
65+
</configuration>
66+
</plugin>
67+
</plugins>
68+
</build>
69+
</project>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.analytics;
18+
19+
import com.google.appengine.api.urlfetch.URLFetchService;
20+
import com.google.appengine.api.urlfetch.URLFetchServiceFactory;
21+
22+
import org.apache.http.client.utils.URIBuilder;
23+
24+
import java.io.IOException;
25+
import java.net.URI;
26+
import java.net.URISyntaxException;
27+
import java.net.URL;
28+
29+
import javax.servlet.ServletException;
30+
import javax.servlet.http.HttpServlet;
31+
import javax.servlet.http.HttpServletRequest;
32+
import javax.servlet.http.HttpServletResponse;
33+
34+
// [START example]
35+
@SuppressWarnings("serial")
36+
public class AnalyticsServlet extends HttpServlet {
37+
38+
@Override
39+
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException,
40+
ServletException {
41+
String trackingId = System.getenv("GA_TRACKING_ID");
42+
URIBuilder builder = new URIBuilder();
43+
builder.setScheme("http").setHost("www.google-analytics.com").setPath("/collect")
44+
.addParameter("v", "1") // API Version.
45+
.addParameter("tid", trackingId) // Tracking ID / Property ID.
46+
// Anonymous Client Identifier. Ideally, this should be a UUID that
47+
// is associated with particular user, device, or browser instance.
48+
.addParameter("cid", "555")
49+
.addParameter("t", "event") // Event hit type.
50+
.addParameter("ec", "example") // Event category.
51+
.addParameter("ea", "test action"); // Event action.
52+
URI uri = null;
53+
try {
54+
uri = builder.build();
55+
} catch (URISyntaxException e) {
56+
throw new ServletException("Problem building URI", e);
57+
}
58+
URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
59+
URL url = uri.toURL();
60+
fetcher.fetch(url);
61+
resp.getWriter().println("Event tracked.");
62+
}
63+
}
64+
// [END example]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- [START_EXCLUDE] -->
3+
<!--
4+
Copyright 2015 Google Inc. All Rights Reserved.
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+
http://www.apache.org/licenses/LICENSE-2.0
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+
<!-- [END_EXCLUDE] -->
16+
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
17+
<application>YOUR-PROJECT-ID</application>
18+
<version>YOUR-VERSION-ID</version>
19+
<threadsafe>true</threadsafe>
20+
<env-variables>
21+
<env-var name="GA_TRACKING_ID" value="YOUR-GA-TRACKING-ID" />
22+
</env-variables>
23+
</appengine-web-app>
24+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- [START_EXCLUDE] -->
3+
<!--
4+
Copyright 2015 Google Inc. All Rights Reserved.
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+
http://www.apache.org/licenses/LICENSE-2.0
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+
<!-- [END_EXCLUDE] -->
16+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
17+
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
18+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
19+
version="2.5">
20+
<servlet>
21+
<servlet-name>analytics</servlet-name>
22+
<servlet-class>com.example.appengine.analytics.AnalyticsServlet</servlet-class>
23+
</servlet>
24+
<servlet-mapping>
25+
<servlet-name>analytics</servlet-name>
26+
<url-pattern>/</url-pattern>
27+
</servlet-mapping>
28+
</web-app>
29+

appengine/helloworld/pom.xml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
21
<!--
32
Copyright 2015 Google Inc. All Rights Reserved.
43
@@ -14,14 +13,12 @@ Copyright 2015 Google Inc. All Rights Reserved.
1413
See the License for the specific language governing permissions and
1514
limitations under the License.
1615
-->
17-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
16+
<project>
1917
<modelVersion>4.0.0</modelVersion>
2018
<packaging>war</packaging>
2119
<version>1.0-SNAPSHOT</version>
2220
<groupId>com.example.appengine</groupId>
2321
<artifactId>appengine-helloworld</artifactId>
24-
<!-- Parent POM defines ${appengine.sdk.version} (updates frequently). -->
2522
<parent>
2623
<groupId>com.google.cloud</groupId>
2724
<artifactId>doc-samples</artifactId>
@@ -32,7 +29,6 @@ Copyright 2015 Google Inc. All Rights Reserved.
3229
<dependency>
3330
<groupId>javax.servlet</groupId>
3431
<artifactId>servlet-api</artifactId>
35-
<version>2.5</version>
3632
<type>jar</type>
3733
<scope>provided</scope>
3834
</dependency>
@@ -50,6 +46,7 @@ Copyright 2015 Google Inc. All Rights Reserved.
5046
<target>1.7</target>
5147
</configuration>
5248
</plugin>
49+
<!-- Parent POM defines ${appengine.sdk.version} (updates frequently). -->
5350
<plugin>
5451
<groupId>com.google.appengine</groupId>
5552
<artifactId>appengine-maven-plugin</artifactId>

appengine/memcache/pom.xml

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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-memcache</artifactId>
22+
23+
<parent>
24+
<artifactId>doc-samples</artifactId>
25+
<groupId>com.google.cloud</groupId>
26+
<version>1.0.0</version>
27+
<relativePath>../..</relativePath>
28+
</parent>
29+
30+
<dependencies>
31+
<dependency>
32+
<groupId>javax.servlet</groupId>
33+
<artifactId>javax.servlet-api</artifactId>
34+
<version>3.1.0</version>
35+
<type>jar</type>
36+
<scope>provided</scope>
37+
</dependency>
38+
39+
<!-- [START dependencies] -->
40+
<!-- Parent POM defines ${appengine.sdk.version} (updates frequently). -->
41+
<dependency>
42+
<groupId>com.google.appengine</groupId>
43+
<artifactId>appengine-api-1.0-sdk</artifactId>
44+
</dependency>
45+
<dependency>
46+
<groupId>com.googlecode.xmemcached</groupId>
47+
<artifactId>xmemcached</artifactId>
48+
<version>2.0.0</version>
49+
</dependency>
50+
<!-- [END dependencies] -->
51+
</dependencies>
52+
<build>
53+
<!-- for hot reload of the web application -->
54+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
55+
<plugins>
56+
<!-- Parent POM defines ${appengine.sdk.version} (updates frequently). -->
57+
<plugin>
58+
<groupId>com.google.appengine</groupId>
59+
<artifactId>appengine-maven-plugin</artifactId>
60+
<version>${appengine.sdk.version}</version>
61+
</plugin>
62+
<plugin>
63+
<groupId>org.apache.maven.plugins</groupId>
64+
<artifactId>maven-compiler-plugin</artifactId>
65+
<version>3.3</version>
66+
<configuration>
67+
<source>1.7</source>
68+
<target>1.7</target>
69+
</configuration>
70+
</plugin>
71+
</plugins>
72+
</build>
73+
</project>
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+
17+
package com.example.appengine.memcache;
18+
19+
import com.google.appengine.api.memcache.ErrorHandlers;
20+
import com.google.appengine.api.memcache.MemcacheService;
21+
import com.google.appengine.api.memcache.MemcacheServiceFactory;
22+
23+
import java.io.IOException;
24+
import java.math.BigInteger;
25+
import java.util.logging.Level;
26+
27+
import javax.servlet.ServletException;
28+
import javax.servlet.http.HttpServlet;
29+
import javax.servlet.http.HttpServletRequest;
30+
import javax.servlet.http.HttpServletResponse;
31+
32+
// [START example]
33+
@SuppressWarnings("serial")
34+
public class MemcacheServlet extends HttpServlet {
35+
36+
@Override
37+
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException,
38+
ServletException {
39+
MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
40+
syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO));
41+
String key = "count";
42+
byte[] value;
43+
long count = 1;
44+
value = (byte[]) syncCache.get(key);
45+
if (value == null) {
46+
value = BigInteger.valueOf(count).toByteArray();
47+
syncCache.put(key, value);
48+
} else {
49+
// Increment value
50+
count = new BigInteger(value).longValue();
51+
count++;
52+
value = BigInteger.valueOf(count).toByteArray();
53+
// Put back in cache
54+
syncCache.put(key, value);
55+
}
56+
57+
// Output content
58+
resp.setContentType("text/plain");
59+
resp.getWriter().print("Value is " + count + "\n");
60+
}
61+
}
62+
// [END example]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- [START_EXCLUDE] -->
3+
<!--
4+
Copyright 2015 Google Inc. All Rights Reserved.
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+
http://www.apache.org/licenses/LICENSE-2.0
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+
<!-- [END_EXCLUDE] -->
16+
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
17+
<application>YOUR-PROJECT-ID</application>
18+
<version>YOUR-VERSION-ID</version>
19+
<threadsafe>true</threadsafe>
20+
</appengine-web-app>
21+

0 commit comments

Comments
 (0)