From 0810e1203e1f4fc7d36af4d4c2b742efd43a0e67 Mon Sep 17 00:00:00 2001 From: Fernando Ribeiro Date: Sat, 19 Mar 2016 23:22:45 -0300 Subject: [PATCH] Added samples in multiple languages This patch adds samples in Java, JavaScript and Python. Change-Id: I70cd618d88bb905d6d71a08730b83015f6aaf009 Closes-Bug: #1559613 --- .../receive_message/JsonDecoder.java | 43 ++++++++++++++ .../receive_message/SampleZaqarEndpoint.java | 57 +++++++++++++++++++ .../send_message/SampleZaqarEndpoint.java | 45 +++++++++++++++ .../receive_message/zaqar_sample.js | 32 +++++++++++ .../javascript/send_message/zaqar_sample.js | 25 ++++++++ .../javascript}/websocket.html | 0 .../receive_message/SampleZaqarServlet.java | 55 ++++++++++++++++++ .../send_message/SampleZaqarServlet.java | 52 +++++++++++++++++ .../nodejs/receive_message/zaqar_sample.js | 34 +++++++++++ samples/nodejs/send_message/zaqar_sample.js | 27 +++++++++ .../receive_message/zaqar_sample.py | 30 ++++++++++ .../send_message/zaqar_sample.py | 27 +++++++++ 12 files changed, 427 insertions(+) create mode 100755 samples/java-api-for-websocket/receive_message/JsonDecoder.java create mode 100755 samples/java-api-for-websocket/receive_message/SampleZaqarEndpoint.java create mode 100755 samples/java-api-for-websocket/send_message/SampleZaqarEndpoint.java create mode 100755 samples/javascript/receive_message/zaqar_sample.js create mode 100755 samples/javascript/send_message/zaqar_sample.js rename {examples => samples/javascript}/websocket.html (100%) create mode 100755 samples/jaxrs/receive_message/SampleZaqarServlet.java create mode 100755 samples/jaxrs/send_message/SampleZaqarServlet.java create mode 100755 samples/nodejs/receive_message/zaqar_sample.js create mode 100755 samples/nodejs/send_message/zaqar_sample.js create mode 100755 samples/python-zaqarclient/receive_message/zaqar_sample.py create mode 100755 samples/python-zaqarclient/send_message/zaqar_sample.py diff --git a/samples/java-api-for-websocket/receive_message/JsonDecoder.java b/samples/java-api-for-websocket/receive_message/JsonDecoder.java new file mode 100755 index 000000000..ced97f223 --- /dev/null +++ b/samples/java-api-for-websocket/receive_message/JsonDecoder.java @@ -0,0 +1,43 @@ +/* + * 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 org.openstack.zaqar.sample; + +import java.io.StringReader; + +import javax.json.Json; +import javax.json.JsonObject; +import javax.websocket.Decoder; +import javax.websocket.EndpointConfig; + +public final class JsonDecoder implements Decoder.Text { + + @Override + public JsonObject decode(final String s) { + return Json.createReader(new StringReader(s)).readObject(); + } + + @Override + public void destroy() { + } + + @Override + public void init(final EndpointConfig config) { + } + + @Override + public boolean willDecode(final String s) { + return true; + } + +} \ No newline at end of file diff --git a/samples/java-api-for-websocket/receive_message/SampleZaqarEndpoint.java b/samples/java-api-for-websocket/receive_message/SampleZaqarEndpoint.java new file mode 100755 index 000000000..3d1787320 --- /dev/null +++ b/samples/java-api-for-websocket/receive_message/SampleZaqarEndpoint.java @@ -0,0 +1,57 @@ +/* + * 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. + */ +import static java.lang.System.out; + +import java.io.IOException; + +import javax.json.JsonObject; +import javax.websocket.ClientEndpoint; +import javax.websocket.OnMessage; +import javax.websocket.OnOpen; +import javax.websocket.RemoteEndpoint; +import javax.websocket.Session; + +@ClientEndpoint(decoders = JsonDecoder.class) +public final class SampleZaqarEndpoint { + + @OnMessage + public void onMessage(final JsonObject msg) { + + if (msg.getJsonObject("body").getJsonArray("messages") != null) + out.println(msg.getJsonObject("body").getJsonArray("messages") + .getJsonObject(0).getString("body")); + + } + + @OnOpen + public void onOpen(final Session sess) throws IOException { + final RemoteEndpoint.Basic remote = sess.getBasicRemote(); + + final String authenticateMsg = "{\"action\":\"authenticate\"," + + "\"headers\":{\"X-Auth-Token\":" + + "\"8444886dd9b04a1b87ddb502b508261c\",\"X-Project-ID\":" + + "\"7530fad032ca431e9dc8ed4a5de5d99c\"}}"; // refer to bug + // #1553398 + + remote.sendText(authenticateMsg); + + final String claimCreateMsg = "{\"action\":\"claim_create\",\"body\":" + + "{\"queue_name\":\"SampleQueue\"},\"headers\":{\"Client-ID\":" + + "\"355186cd-d1e8-4108-a3ac-a2183697232a\",\"X-Project-ID\":" + + "\"7530fad032ca431e9dc8ed4a5de5d99c\"}}"; + + remote.sendText(claimCreateMsg); + } + +} diff --git a/samples/java-api-for-websocket/send_message/SampleZaqarEndpoint.java b/samples/java-api-for-websocket/send_message/SampleZaqarEndpoint.java new file mode 100755 index 000000000..d9d608a85 --- /dev/null +++ b/samples/java-api-for-websocket/send_message/SampleZaqarEndpoint.java @@ -0,0 +1,45 @@ +/* + * 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. + */ +import java.io.IOException; + +import javax.websocket.ClientEndpoint; +import javax.websocket.OnOpen; +import javax.websocket.RemoteEndpoint; +import javax.websocket.Session; + +@ClientEndpoint +public final class SampleZaqarEndpoint { + + @OnOpen + public void onOpen(final Session sess) throws IOException { + final RemoteEndpoint.Basic remote = sess.getBasicRemote(); + + final String authenticateMsg = "{\"action\":\"authenticate\"," + + "\"headers\":{\"X-Auth-Token\":" + + "\"8444886dd9b04a1b87ddb502b508261c\",\"X-Project-ID\":" + + "\"7530fad032ca431e9dc8ed4a5de5d99c\"}}"; // refer to bug + // #1553398 + + remote.sendText(authenticateMsg); + + final String messagePostMsg = "{\"action\":\"message_post\",\"body\":" + + "{\"messages\":[{\"body\":\"Zaqar Sample\"}],\"queue_name\":" + + "\"SampleQueue\"},\"headers\":{\"Client-ID\":" + + "\"355186cd-d1e8-4108-a3ac-a2183697232a\",\"X-Project-ID\":" + + "\"7530fad032ca431e9dc8ed4a5de5d99c\"}}"; + + remote.sendText(messagePostMsg); + } + +} diff --git a/samples/javascript/receive_message/zaqar_sample.js b/samples/javascript/receive_message/zaqar_sample.js new file mode 100755 index 000000000..3c41e2056 --- /dev/null +++ b/samples/javascript/receive_message/zaqar_sample.js @@ -0,0 +1,32 @@ +/* + * 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. + */ +const ws = new WebSocket('ws://localhost:9000'); + +ws.onmessage = (e) => { + const msg = JSON.parse(e.data); + + if (msg.body.messages) + console.log(msg.body.messages[0].body); + +}; + +ws.onopen = () => { + ws.send('{"action": "authenticate", "headers": {"X-Auth-Token": \ + "8444886dd9b04a1b87ddb502b508261c", "X-Project-ID": \ + "7530fad032ca431e9dc8ed4a5de5d99c"}}'); // refer to bug #1553398 + + ws.send('{"action": "claim_create", "body": {"queue_name": "SampleQueue"}, \ + "headers": {"Client-ID": "355186cd-d1e8-4108-a3ac-a2183697232a", \ + "X-Project-ID": "7530fad032ca431e9dc8ed4a5de5d99c"}}'); +}; diff --git a/samples/javascript/send_message/zaqar_sample.js b/samples/javascript/send_message/zaqar_sample.js new file mode 100755 index 000000000..eee7850f4 --- /dev/null +++ b/samples/javascript/send_message/zaqar_sample.js @@ -0,0 +1,25 @@ +/* + * 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. + */ +const ws = new WebSocket('ws://localhost:9000'); + +ws.onopen = () => { + ws.send('{"action": "authenticate", "headers": {"X-Auth-Token": \ + "8444886dd9b04a1b87ddb502b508261c", "X-Project-ID": \ + "7530fad032ca431e9dc8ed4a5de5d99c"}}'); // refer to bug #1553398 + + ws.send('{"action": "message_post", "body": {"messages": [{"body": \ + "Zaqar Sample"}], "queue_name": "SampleQueue"}, "headers": \ + {"Client-ID": "355186cd-d1e8-4108-a3ac-a2183697232a", "X-Project-ID": \ + "7530fad032ca431e9dc8ed4a5de5d99c"}}'); +}; diff --git a/examples/websocket.html b/samples/javascript/websocket.html similarity index 100% rename from examples/websocket.html rename to samples/javascript/websocket.html diff --git a/samples/jaxrs/receive_message/SampleZaqarServlet.java b/samples/jaxrs/receive_message/SampleZaqarServlet.java new file mode 100755 index 000000000..844e2acff --- /dev/null +++ b/samples/jaxrs/receive_message/SampleZaqarServlet.java @@ -0,0 +1,55 @@ +/* + * 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. + */ +import java.io.IOException; + +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.Response; + +@SuppressWarnings("serial") +@WebServlet(name = "SampleServlet", value = "/") +public final class SampleZaqarServlet extends HttpServlet { + + @Override + protected void doGet(final HttpServletRequest req, + final HttpServletResponse resp) throws IOException { + final Client client = ClientBuilder.newClient(); + + final MultivaluedMap headers = + new MultivaluedHashMap(); + + headers.putSingle("Client-ID", "355186cd-d1e8-4108-a3ac-a2183697232a"); + + headers.putSingle("X-Auth-Token", "8444886dd9b04a1b87ddb502b508261c"); + + headers.putSingle("X-Project-Id", "7530fad032ca431e9dc8ed4a5de5d99c"); + + final Response res = client + .target("http://localhost:8888/v2/queues/SampleQueue/claims") + .request().headers(headers).post(Entity.json("")); + + resp.getWriter().println(res.readEntity(String.class)); + + client.close(); + } + +} diff --git a/samples/jaxrs/send_message/SampleZaqarServlet.java b/samples/jaxrs/send_message/SampleZaqarServlet.java new file mode 100755 index 000000000..64690f474 --- /dev/null +++ b/samples/jaxrs/send_message/SampleZaqarServlet.java @@ -0,0 +1,52 @@ +/* + * 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. + */ +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; + +@SuppressWarnings("serial") +@WebServlet(name = "SampleZaqarServlet", value = "/") +public final class SampleZaqarServlet extends HttpServlet { + + @Override + protected void doGet(final HttpServletRequest req, + final HttpServletResponse resp) { + final Client client = ClientBuilder.newClient(); + + final MultivaluedMap headers = + new MultivaluedHashMap(); + + headers.putSingle("Client-ID", "355186cd-d1e8-4108-a3ac-a2183697232a"); + + headers.putSingle("X-Auth-Token", "8444886dd9b04a1b87ddb502b508261c"); + + headers.putSingle("X-Project-Id", "7530fad032ca431e9dc8ed4a5de5d99c"); + + client.target("http://localhost:8888/v2/queues/SampleQueue/messages") + .request(MediaType.APPLICATION_JSON_TYPE).headers(headers) + .post(Entity + .json("{\"messages\":[{\"body\":\"Zaqar Sample\"}]}")); + + client.close(); + } + +} diff --git a/samples/nodejs/receive_message/zaqar_sample.js b/samples/nodejs/receive_message/zaqar_sample.js new file mode 100755 index 000000000..62526a235 --- /dev/null +++ b/samples/nodejs/receive_message/zaqar_sample.js @@ -0,0 +1,34 @@ +/* + * 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. + */ +const WebSocket = require('ws'); + +const ws = new WebSocket('ws://localhost:9000'); + +ws.on('message', (data, flags) => { + const msg = JSON.parse(data); + + if (msg.body.messages) + console.log(msg.body.messages[0].body); + +}); + +ws.on('open', () => { + ws.send('{"action": "authenticate", "headers": {"X-Auth-Token": \ + "8444886dd9b04a1b87ddb502b508261c", "X-Project-ID": \ + "7530fad032ca431e9dc8ed4a5de5d99c"}}'); // refer to bug #1553398 + + ws.send('{"action": "claim_create", "body": {"queue_name": "SampleQueue"}, \ + "headers": {"Client-ID": "355186cd-d1e8-4108-a3ac-a2183697232a", \ + "X-Project-ID": "7530fad032ca431e9dc8ed4a5de5d99c"}}'); +}); diff --git a/samples/nodejs/send_message/zaqar_sample.js b/samples/nodejs/send_message/zaqar_sample.js new file mode 100755 index 000000000..45df9dbc1 --- /dev/null +++ b/samples/nodejs/send_message/zaqar_sample.js @@ -0,0 +1,27 @@ +/* + * 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. + */ +const WebSocket = require('ws'); + +const ws = new WebSocket('ws://localhost:9000'); + +ws.on('open', () => { + ws.send('{"action": "authenticate", "headers": {"X-Auth-Token": \ + "8444886dd9b04a1b87ddb502b508261c", "X-Project-ID": \ + "7530fad032ca431e9dc8ed4a5de5d99c"}}'); // refer to bug #1553398 + + ws.send('{"action": "message_post", "body": {"messages": [{"body": \ + "Zaqar Sample"}], "queue_name": "SampleQueue"}, "headers": \ + {"Client-ID": "355186cd-d1e8-4108-a3ac-a2183697232a", "X-Project-ID": \ + "7530fad032ca431e9dc8ed4a5de5d99c"}}'); +}); diff --git a/samples/python-zaqarclient/receive_message/zaqar_sample.py b/samples/python-zaqarclient/receive_message/zaqar_sample.py new file mode 100755 index 000000000..20faf08e9 --- /dev/null +++ b/samples/python-zaqarclient/receive_message/zaqar_sample.py @@ -0,0 +1,30 @@ +# 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. +from zaqarclient.queues.v1 import client + +client = client.Client('http://localhost:8888', conf={ + 'auth_opts': { + 'options': { + 'client_uuid': '355186cd-d1e8-4108-a3ac-a2183697232a', + 'os_auth_token': '8444886dd9b04a1b87ddb502b508261c', + 'os_auth_url': 'http://localhost:5000/v3.0/', + 'os_project_id': '7530fad032ca431e9dc8ed4a5de5d99c' + } + } +}, version=2) + +queue = client.queue('SampleQueue') + +claim = queue.claim(ttl=600, grace=600) # refer to bug #1553387 + +for msg in claim: + print(msg) diff --git a/samples/python-zaqarclient/send_message/zaqar_sample.py b/samples/python-zaqarclient/send_message/zaqar_sample.py new file mode 100755 index 000000000..5a406a476 --- /dev/null +++ b/samples/python-zaqarclient/send_message/zaqar_sample.py @@ -0,0 +1,27 @@ +# 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. +from zaqarclient.queues.v1 import client + +client = client.Client('http://localhost:8888', conf={ + 'auth_opts': { + 'options': { + 'client_uuid': '355186cd-d1e8-4108-a3ac-a2183697232a', + 'os_auth_token': '8444886dd9b04a1b87ddb502b508261c', + 'os_auth_url': 'http://localhost:5000/v3.0/', + 'os_project_id': '7530fad032ca431e9dc8ed4a5de5d99c' + } + } +}, version=2) + +queue = client.queue('SampleQueue') + +queue.post([{'body': 'Zaqar Sample'}])