Skip to content

Commit d450027

Browse files
dzlier-gcpkurtisvg
authored andcommitted
Added translate-pubsub sample from gcp/getting-started-java. (GoogleCloudPlatform#998)
1 parent 661edfa commit d450027

File tree

12 files changed

+735
-0
lines changed

12 files changed

+735
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Google Cloud API Showcase: Using Cloud Pub/Sub & Translate on App Engine Standard Java 8 Environment
2+
3+
This sample demonstrates how to use [Google Cloud Pub/Sub][pubsub] and [Google Translate][translate]
4+
from [Google App Engine standard environment][ae-docs].
5+
6+
[pubsub]: https://cloud.google.com/pubsub/docs/
7+
[translate]: https://cloud.google.com/translate/docs/
8+
[ae-docs]: https://cloud.google.com/appengine/docs/java/
9+
10+
The home page of this application provides a form to publish messages using Google/Cloud PubSub (though publishing in
11+
local development is currently not supported). The application then receives these published messages over a push
12+
subscription endpoint, translates it from a source language into a target language, and finally stores in Google Cloud
13+
Datastore.
14+
15+
The home page also provides a view of the most recently translated messages persisted in storage.
16+
17+
## Clone the sample app
18+
19+
Copy the sample apps to your local machine, and cd to the translate-pubsub directory:
20+
21+
```
22+
git clone https://github.com/GoogleCloudPlatform/java-docs-samples
23+
cd java-docs-samples/appengine-java8/translate-pubsub
24+
```
25+
26+
## Setup
27+
28+
- Make sure [`gcloud`](https://cloud.google.com/sdk/docs/) is installed and initialized:
29+
```
30+
gcloud init
31+
```
32+
- If this is the first time you are creating an App Engine project
33+
```
34+
gcloud app create
35+
```
36+
- For local development, [set up](https://cloud.google.com/docs/authentication/getting-started) authentication
37+
- Enable [Pub/Sub](https://console.cloud.google.com/launcher/details/google/pubsub.googleapis.com) and
38+
[Translate](https://console.cloud.google.com/launcher/details/google/translate.googleapis.com) APIs
39+
40+
- Choose a topic name and verification token.
41+
42+
- Set the following environment variables. The verification token is used to ensure that the end point only handles
43+
requests that are sent matching the verification token. You can use `uuidgen` on MacOS X, Windows, and Linux to
44+
generate a unique verification token. There are also online tools to generate UUIDs, such as
45+
[uuidgenerator.net][uuid].
46+
47+
```
48+
export PUBSUB_TOPIC=<your-topic-name>
49+
export PUBSUB_VERIFICATION_TOKEN=<your-verification-token>
50+
```
51+
52+
[uuid]: https://www.uuidgenerator.net/
53+
54+
- Create a topic
55+
```
56+
gcloud pubsub topics create $PUBSUB_TOPIC
57+
```
58+
59+
- Create a push subscription, to send messages to a Google Cloud Project URL such as
60+
https://<your-project-id>.appspot.com/push.
61+
62+
```
63+
gcloud pubsub subscriptions create <your-subscription-name> \
64+
--topic $PUBSUB_TOPIC \
65+
--push-endpoint \
66+
https://<your-project-id>.appspot.com/pubsub/push?token=$PUBSUB_VERIFICATION_TOKEN \
67+
--ack-deadline 30
68+
```
69+
70+
## Run locally
71+
Run using shown Maven command. You can then direct your browser to `http://localhost:8080/` to see translated messages
72+
created in the following step.
73+
74+
```
75+
mvn appengine:run
76+
```
77+
78+
## Send fake subscription push messages
79+
80+
```
81+
curl -H "Content-Type: application/json" -i --data @sample_message.json \
82+
"localhost:8080/pubsub/push?token=$PUBSUB_VERIFICATION_TOKEN"
83+
```
84+
85+
## Deploy
86+
87+
Update the environment variables `PUBSUB_TOPIC` and `PUBSUB_VERIFICATION_TOKEN` in
88+
[`appengine-web.xml`](src/main/webapp/WEB-INF/appengine-web.xml), then:
89+
90+
```
91+
mvn appengine:deploy
92+
```
93+
94+
Direct your browser to `https://<your-project-id>.appspot.com`.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<!--
2+
Copyright 2018 Google Inc.
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+
<!-- [START project] -->
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-translate-pubsub</artifactId>
23+
24+
<!--
25+
The parent pom defines common style checks and testing strategies for our samples.
26+
Removing or replacing it should not affect the execution of the samples in anyway.
27+
-->
28+
<parent>
29+
<groupId>com.google.cloud.samples</groupId>
30+
<artifactId>shared-configuration</artifactId>
31+
<version>1.0.8</version>
32+
</parent>
33+
34+
<properties>
35+
<maven.compiler.target>1.8</maven.compiler.target>
36+
<maven.compiler.source>1.8</maven.compiler.source>
37+
</properties>
38+
39+
<dependencies>
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+
48+
<!-- [START dependencies] -->
49+
<dependency>
50+
<groupId>com.google.cloud</groupId>
51+
<artifactId>google-cloud-pubsub</artifactId>
52+
<version>0.32.0-beta</version>
53+
</dependency>
54+
<dependency>
55+
<groupId>com.google.cloud</groupId>
56+
<artifactId>google-cloud-datastore</artifactId>
57+
<version>1.12.0</version>
58+
</dependency>
59+
<dependency>
60+
<groupId>com.google.cloud</groupId>
61+
<artifactId>google-cloud-translate</artifactId>
62+
<version>1.14.0</version>
63+
</dependency>
64+
<!-- [END dependencies] -->
65+
</dependencies>
66+
<build>
67+
<!-- for hot reload of the web application -->
68+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
69+
<plugins>
70+
<plugin>
71+
<groupId>com.google.cloud.tools</groupId>
72+
<artifactId>appengine-maven-plugin</artifactId>
73+
<version>1.3.1</version>
74+
<configuration>
75+
<deploy.promote>true</deploy.promote>
76+
<deploy.stopPreviousVersion>true</deploy.stopPreviousVersion>
77+
</configuration>
78+
</plugin>
79+
80+
<plugin>
81+
<groupId>org.apache.maven.plugins</groupId>
82+
<artifactId>maven-war-plugin</artifactId>
83+
<version>3.1.0</version>
84+
</plugin>
85+
86+
</plugins>
87+
</build>
88+
</project>
89+
<!-- [END project] -->
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"message":{"data":"eyJkYXRhIjoidHJhbnNsYXRlIiwic291cmNlTGFuZyI6ImVuIiwidGFyZ2V0TGFuZyI6ImVuIn0=","attributes":{"sourceLang":"en","targetLang":"es"},"messageId":"181789827785060","publishTime":"2018-01-06T00:41:01.839Z"}}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2018 Google Inc.
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.translatepubsub;
18+
19+
import com.google.protobuf.ByteString;
20+
21+
/**
22+
* A message captures information from the Pubsub message received over the push endpoint and is
23+
* persisted in storage.
24+
*/
25+
public class Message {
26+
private String messageId;
27+
private String publishTime;
28+
private String data;
29+
private String sourceLang = "en";
30+
private String targetLang = "en";
31+
32+
public Message(String messageId) {
33+
this.messageId = messageId;
34+
}
35+
36+
public String getMessageId() {
37+
return messageId;
38+
}
39+
40+
public void setMessageId(String messageId) {
41+
this.messageId = messageId;
42+
}
43+
44+
public String getPublishTime() {
45+
return publishTime;
46+
}
47+
48+
public void setPublishTime(String publishTime) {
49+
this.publishTime = publishTime;
50+
}
51+
52+
public String getData() {
53+
return data;
54+
}
55+
56+
public void setData(String data) {
57+
this.data = data;
58+
}
59+
60+
public String getSourceLang() {
61+
return sourceLang;
62+
}
63+
64+
public void setSourceLang(String sourceLang) {
65+
this.sourceLang = sourceLang;
66+
}
67+
68+
public String getTargetLang() {
69+
return targetLang;
70+
}
71+
72+
public void setTargetLang(String targetLang) {
73+
this.targetLang = targetLang;
74+
}
75+
76+
public String getTranslated() {
77+
return Translate.translateText(
78+
ByteString.copyFrom(data.getBytes()).toStringUtf8(), sourceLang, targetLang);
79+
}
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2018 Google Inc.
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.translatepubsub;
18+
19+
import java.util.List;
20+
21+
public interface MessageRepository {
22+
23+
/**
24+
* Save message to persistent storage.
25+
*/
26+
void save(Message message);
27+
28+
/**
29+
* Retrieve most recent stored messages.
30+
*
31+
* @param limit number of messages
32+
* @return list of messages
33+
*/
34+
List<Message> retrieve(int limit);
35+
}

0 commit comments

Comments
 (0)