Skip to content

Commit 4105f47

Browse files
committed
Moved the examples to the services root
(cherry picked from commit e6d700f)
1 parent e3c16d1 commit 4105f47

File tree

22 files changed

+903
-42
lines changed

22 files changed

+903
-42
lines changed

service-proxy-examples/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<modules>
1414
<module>service-provider</module>
1515
<module>service-consumer</module>
16+
<module>sockjs-proxies</module>
1617
</modules>
1718

1819
<profiles>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
= Vert.x SockJS service proxies
2+
3+
== Sock.js Proxy Service (TypeScript)
4+
5+
This example shows how you can work with Sock.js Proxy services. Sock.js Proxy Services allow you to do RPC like
6+
operations on services deployed on the eventbus, directly from your client browser.
7+
8+
There are some steps required before this example works. You will need to install the `NPM` dependencies in order to get
9+
webpack installed, once that is done e.g.: `npm install`, `webpack` will drive the typescript compiler and bundle the
10+
application to a single js bundle.
11+
12+
Important files to note:
13+
14+
* link:src/main/java/io/vertx/example/web/proxies/Server.java[Java Vert.x server]
15+
* link:src/main/java/io/vertx/example/web/proxies/client/main.ts[TypeScript app example]
16+
* link:src/main/java/io/vertx/example/web/proxies/client/my_service-proxy.ts[Vert.x generated TypeScript proxy]
17+
18+
Minimal webpack config:
19+
20+
* link:src/main/java/io/vertx/example/web/proxies/package.json[dependencies for webpack]
21+
* link:src/main/java/io/vertx/example/web/proxies/webpack.config.js[Webpack config]
22+
* link:src/main/java/io/vertx/example/web/proxies/tsconfig.json[Typescript compiler config]
23+
24+
Run the server either in your IDE or on the command line, then open your browser and hit
25+
link:http://localhost:8080 and chat with a couple of browser windows!
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"excludes": [
3+
4+
// Template examples
5+
// Only MVEL examples are polyglot
6+
{
7+
"package": "io.vertx.example.web.templating.handlebars"
8+
},
9+
{
10+
"package": "io.vertx.example.web.templating.jade"
11+
},
12+
{
13+
"package": "io.vertx.example.web.templating.thymeleaf"
14+
},
15+
16+
// Angular
17+
{
18+
"package": "io.vertx.example.web.angularjs"
19+
},
20+
{
21+
"package": "io.vertx.example.web.angular_realtime"
22+
},
23+
24+
// React
25+
{
26+
"package": "io.vertx.example.web.react"
27+
},
28+
29+
// JDBC and Mongo
30+
{
31+
"package": "io.vertx.example.web.jdbc"
32+
},
33+
{
34+
"package": "io.vertx.example.web.mongo"
35+
},
36+
37+
// Form
38+
{
39+
"package": "io.vertx.example.web.form"
40+
},
41+
42+
// vert.x bus
43+
{
44+
"package": "io.vertx.example.web.vertxbus"
45+
},
46+
47+
{
48+
"package": "io.vertx.example.web.realtime"
49+
},
50+
51+
// Rest, JS only
52+
{
53+
"package": "io.vertx.example.web.rest",
54+
"langs": ["js"]
55+
}
56+
57+
]
58+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>io.vertx</groupId>
8+
<artifactId>sockjs-proxies-examples</artifactId>
9+
<version>3.5.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
13+
<dependency>
14+
<groupId>io.vertx</groupId>
15+
<artifactId>vertx-core</artifactId>
16+
<version>${project.version}</version>
17+
</dependency>
18+
19+
<dependency>
20+
<groupId>io.vertx</groupId>
21+
<artifactId>vertx-web</artifactId>
22+
<version>${project.version}</version>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>io.vertx</groupId>
27+
<artifactId>examples-utils</artifactId>
28+
<version>${project.version}</version>
29+
<optional>true</optional>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>io.vertx</groupId>
34+
<artifactId>vertx-sockjs-service-proxy</artifactId>
35+
<version>${project.version}</version>
36+
</dependency>
37+
38+
</dependencies>
39+
40+
<build>
41+
<pluginManagement>
42+
<plugins>
43+
<!-- We specify the Maven compiler plugin as we need to set it to Java 1.8 -->
44+
<plugin>
45+
<artifactId>maven-compiler-plugin</artifactId>
46+
<version>3.1</version>
47+
<configuration>
48+
<source>1.8</source>
49+
<target>1.8</target>
50+
<compilerArgs>
51+
<arg>-Acodetrans.output=${project.basedir}/src/main</arg>
52+
<arg>-Acodetrans.config=${basedir}/codetrans-config.json</arg>
53+
</compilerArgs>
54+
<annotationProcessors>
55+
<annotationProcessor>io.vertx.codegen.CodeGenProcessor</annotationProcessor>
56+
</annotationProcessors>
57+
<generatedSourcesDirectory>
58+
${project.basedir}/src/main/generated
59+
</generatedSourcesDirectory>
60+
<compilerArgs>
61+
<arg>-AoutputDirectory=${project.basedir}/src/main</arg>
62+
</compilerArgs>
63+
</configuration>
64+
</plugin>
65+
</plugins>
66+
</pluginManagement>
67+
</build>
68+
69+
<profiles>
70+
<profile>
71+
<id>staging</id>
72+
<repositories>
73+
<repository>
74+
<id>staging</id>
75+
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
76+
</repository>
77+
</repositories>
78+
</profile>
79+
</profiles>
80+
81+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Copyright 2014 Red Hat, Inc.
3+
*
4+
* Red Hat licenses this file to you under the Apache License, version 2.0
5+
* (the "License"); you may not use this file except in compliance with the
6+
* License. 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, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
package io.vertx.example.web.proxies;
18+
19+
import io.vertx.example.web.proxies.MyService;
20+
import io.vertx.core.eventbus.DeliveryOptions;
21+
import io.vertx.core.Vertx;
22+
import io.vertx.core.Future;
23+
import io.vertx.core.json.JsonObject;
24+
import io.vertx.core.json.JsonArray;
25+
import java.util.ArrayList;
26+
import java.util.HashSet;
27+
import java.util.List;
28+
import java.util.Map;
29+
import java.util.Set;
30+
import java.util.stream.Collectors;
31+
import java.util.function.Function;
32+
import io.vertx.serviceproxy.ProxyHelper;
33+
import io.vertx.serviceproxy.ServiceException;
34+
import io.vertx.serviceproxy.ServiceExceptionMessageCodec;
35+
import io.vertx.example.web.proxies.MyService;
36+
import io.vertx.core.AsyncResult;
37+
import io.vertx.core.Handler;
38+
39+
/*
40+
Generated Proxy code - DO NOT EDIT
41+
@author Roger the Robot
42+
*/
43+
@SuppressWarnings({"unchecked", "rawtypes"})
44+
public class MyServiceVertxEBProxy implements MyService {
45+
46+
private Vertx _vertx;
47+
private String _address;
48+
private DeliveryOptions _options;
49+
private boolean closed;
50+
51+
public MyServiceVertxEBProxy(Vertx vertx, String address) {
52+
this(vertx, address, null);
53+
}
54+
55+
public MyServiceVertxEBProxy(Vertx vertx, String address, DeliveryOptions options) {
56+
this._vertx = vertx;
57+
this._address = address;
58+
this._options = options;
59+
try {
60+
this._vertx.eventBus().registerDefaultCodec(ServiceException.class,
61+
new ServiceExceptionMessageCodec());
62+
} catch (IllegalStateException ex) {}
63+
}
64+
65+
public MyService sayHello(String name, Handler<AsyncResult<String>> handler) {
66+
if (closed) {
67+
handler.handle(Future.failedFuture(new IllegalStateException("Proxy is closed")));
68+
return this;
69+
}
70+
JsonObject _json = new JsonObject();
71+
_json.put("name", name);
72+
DeliveryOptions _deliveryOptions = (_options != null) ? new DeliveryOptions(_options) : new DeliveryOptions();
73+
_deliveryOptions.addHeader("action", "sayHello");
74+
_vertx.eventBus().<String>send(_address, _json, _deliveryOptions, res -> {
75+
if (res.failed()) {
76+
handler.handle(Future.failedFuture(res.cause()));
77+
} else {
78+
handler.handle(Future.succeededFuture(res.result().body()));
79+
}
80+
});
81+
return this;
82+
}
83+
84+
85+
private List<Character> convertToListChar(JsonArray arr) {
86+
List<Character> list = new ArrayList<>();
87+
for (Object obj: arr) {
88+
Integer jobj = (Integer)obj;
89+
list.add((char)(int)jobj);
90+
}
91+
return list;
92+
}
93+
94+
private Set<Character> convertToSetChar(JsonArray arr) {
95+
Set<Character> set = new HashSet<>();
96+
for (Object obj: arr) {
97+
Integer jobj = (Integer)obj;
98+
set.add((char)(int)jobj);
99+
}
100+
return set;
101+
}
102+
103+
private <T> Map<String, T> convertMap(Map map) {
104+
if (map.isEmpty()) {
105+
return (Map<String, T>) map;
106+
}
107+
108+
Object elem = map.values().stream().findFirst().get();
109+
if (!(elem instanceof Map) && !(elem instanceof List)) {
110+
return (Map<String, T>) map;
111+
} else {
112+
Function<Object, T> converter;
113+
if (elem instanceof List) {
114+
converter = object -> (T) new JsonArray((List) object);
115+
} else {
116+
converter = object -> (T) new JsonObject((Map) object);
117+
}
118+
return ((Map<String, T>) map).entrySet()
119+
.stream()
120+
.collect(Collectors.toMap(Map.Entry::getKey, converter::apply));
121+
}
122+
}
123+
private <T> List<T> convertList(List list) {
124+
if (list.isEmpty()) {
125+
return (List<T>) list;
126+
}
127+
128+
Object elem = list.get(0);
129+
if (!(elem instanceof Map) && !(elem instanceof List)) {
130+
return (List<T>) list;
131+
} else {
132+
Function<Object, T> converter;
133+
if (elem instanceof List) {
134+
converter = object -> (T) new JsonArray((List) object);
135+
} else {
136+
converter = object -> (T) new JsonObject((Map) object);
137+
}
138+
return (List<T>) list.stream().map(converter).collect(Collectors.toList());
139+
}
140+
}
141+
private <T> Set<T> convertSet(List list) {
142+
return new HashSet<T>(convertList(list));
143+
}
144+
}

0 commit comments

Comments
 (0)