Skip to content

Commit

Permalink
Added translate-pubsub sample from gcp/getting-started-java. (#998)
Browse files Browse the repository at this point in the history
  • Loading branch information
dzlier-gcp authored and kurtisvg committed Jan 19, 2018
1 parent 661edfa commit d450027
Show file tree
Hide file tree
Showing 12 changed files with 735 additions and 0 deletions.
94 changes: 94 additions & 0 deletions appengine-java8/translate-pubsub/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Google Cloud API Showcase: Using Cloud Pub/Sub & Translate on App Engine Standard Java 8 Environment

This sample demonstrates how to use [Google Cloud Pub/Sub][pubsub] and [Google Translate][translate]
from [Google App Engine standard environment][ae-docs].

[pubsub]: https://cloud.google.com/pubsub/docs/
[translate]: https://cloud.google.com/translate/docs/
[ae-docs]: https://cloud.google.com/appengine/docs/java/

The home page of this application provides a form to publish messages using Google/Cloud PubSub (though publishing in
local development is currently not supported). The application then receives these published messages over a push
subscription endpoint, translates it from a source language into a target language, and finally stores in Google Cloud
Datastore.

The home page also provides a view of the most recently translated messages persisted in storage.

## Clone the sample app

Copy the sample apps to your local machine, and cd to the translate-pubsub directory:

```
git clone https://github.com/GoogleCloudPlatform/java-docs-samples
cd java-docs-samples/appengine-java8/translate-pubsub
```

## Setup

- Make sure [`gcloud`](https://cloud.google.com/sdk/docs/) is installed and initialized:
```
gcloud init
```
- If this is the first time you are creating an App Engine project
```
gcloud app create
```
- For local development, [set up](https://cloud.google.com/docs/authentication/getting-started) authentication
- Enable [Pub/Sub](https://console.cloud.google.com/launcher/details/google/pubsub.googleapis.com) and
[Translate](https://console.cloud.google.com/launcher/details/google/translate.googleapis.com) APIs

- Choose a topic name and verification token.

- Set the following environment variables. The verification token is used to ensure that the end point only handles
requests that are sent matching the verification token. You can use `uuidgen` on MacOS X, Windows, and Linux to
generate a unique verification token. There are also online tools to generate UUIDs, such as
[uuidgenerator.net][uuid].

```
export PUBSUB_TOPIC=<your-topic-name>
export PUBSUB_VERIFICATION_TOKEN=<your-verification-token>
```

[uuid]: https://www.uuidgenerator.net/

- Create a topic
```
gcloud pubsub topics create $PUBSUB_TOPIC
```

- Create a push subscription, to send messages to a Google Cloud Project URL such as
https://<your-project-id>.appspot.com/push.

```
gcloud pubsub subscriptions create <your-subscription-name> \
--topic $PUBSUB_TOPIC \
--push-endpoint \
https://<your-project-id>.appspot.com/pubsub/push?token=$PUBSUB_VERIFICATION_TOKEN \
--ack-deadline 30
```

## Run locally
Run using shown Maven command. You can then direct your browser to `http://localhost:8080/` to see translated messages
created in the following step.

```
mvn appengine:run
```

## Send fake subscription push messages

```
curl -H "Content-Type: application/json" -i --data @sample_message.json \
"localhost:8080/pubsub/push?token=$PUBSUB_VERIFICATION_TOKEN"
```

## Deploy

Update the environment variables `PUBSUB_TOPIC` and `PUBSUB_VERIFICATION_TOKEN` in
[`appengine-web.xml`](src/main/webapp/WEB-INF/appengine-web.xml), then:

```
mvn appengine:deploy
```

Direct your browser to `https://<your-project-id>.appspot.com`.
89 changes: 89 additions & 0 deletions appengine-java8/translate-pubsub/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<!--
Copyright 2018 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- [START project] -->
<project>
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<groupId>com.example.appengine</groupId>
<artifactId>appengine-translate-pubsub</artifactId>

<!--
The parent pom defines common style checks and testing strategies for our samples.
Removing or replacing it should not affect the execution of the samples in anyway.
-->
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.0.8</version>
</parent>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<type>jar</type>
<scope>provided</scope>
</dependency>

<!-- [START dependencies] -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-pubsub</artifactId>
<version>0.32.0-beta</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-datastore</artifactId>
<version>1.12.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-translate</artifactId>
<version>1.14.0</version>
</dependency>
<!-- [END dependencies] -->
</dependencies>
<build>
<!-- for hot reload of the web application -->
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
<plugins>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>1.3.1</version>
<configuration>
<deploy.promote>true</deploy.promote>
<deploy.stopPreviousVersion>true</deploy.stopPreviousVersion>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
</plugin>

</plugins>
</build>
</project>
<!-- [END project] -->
1 change: 1 addition & 0 deletions appengine-java8/translate-pubsub/sample_message.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"message":{"data":"eyJkYXRhIjoidHJhbnNsYXRlIiwic291cmNlTGFuZyI6ImVuIiwidGFyZ2V0TGFuZyI6ImVuIn0=","attributes":{"sourceLang":"en","targetLang":"es"},"messageId":"181789827785060","publishTime":"2018-01-06T00:41:01.839Z"}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.appengine.translatepubsub;

import com.google.protobuf.ByteString;

/**
* A message captures information from the Pubsub message received over the push endpoint and is
* persisted in storage.
*/
public class Message {
private String messageId;
private String publishTime;
private String data;
private String sourceLang = "en";
private String targetLang = "en";

public Message(String messageId) {
this.messageId = messageId;
}

public String getMessageId() {
return messageId;
}

public void setMessageId(String messageId) {
this.messageId = messageId;
}

public String getPublishTime() {
return publishTime;
}

public void setPublishTime(String publishTime) {
this.publishTime = publishTime;
}

public String getData() {
return data;
}

public void setData(String data) {
this.data = data;
}

public String getSourceLang() {
return sourceLang;
}

public void setSourceLang(String sourceLang) {
this.sourceLang = sourceLang;
}

public String getTargetLang() {
return targetLang;
}

public void setTargetLang(String targetLang) {
this.targetLang = targetLang;
}

public String getTranslated() {
return Translate.translateText(
ByteString.copyFrom(data.getBytes()).toStringUtf8(), sourceLang, targetLang);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.appengine.translatepubsub;

import java.util.List;

public interface MessageRepository {

/**
* Save message to persistent storage.
*/
void save(Message message);

/**
* Retrieve most recent stored messages.
*
* @param limit number of messages
* @return list of messages
*/
List<Message> retrieve(int limit);
}
Loading

0 comments on commit d450027

Please sign in to comment.