Skip to content

Commit

Permalink
Upgrade to vert.x 4.0.x for vaadin 8
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollovati committed Apr 9, 2021
1 parent 4f0eba2 commit bb14fde
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Promise;
import io.vertx.core.VertxException;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
Expand All @@ -41,21 +42,21 @@ public final class Sync {
public static <T> T await(Consumer<Handler<AsyncResult<T>>> task) {
CountDownLatch countDownLatch = new CountDownLatch(1);
try {
Future<T> f = Future.<T>future().setHandler(ar -> {
Promise<T> p = Promise.promise();
Future<T> f = p.future();
f.onComplete(ar -> {
countDownLatch.countDown();
if (ar.failed()) {
throw new VertxException(ar.cause());
}
});
task.accept(f.completer());
task.accept(p);
countDownLatch.await();
return f.result();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new VertxException(e);
}

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
class CookieUtils {


static Cookie fromVertxCookie(io.vertx.ext.web.Cookie cookie) {
static Cookie fromVertxCookie(io.vertx.core.http.Cookie cookie) {
io.netty.handler.codec.http.cookie.Cookie decoded = ClientCookieDecoder.STRICT.decode(cookie.encode());
Cookie out = new Cookie(decoded.name(), decoded.value());
Optional.ofNullable(decoded.domain()).ifPresent(out::setDomain);
Expand All @@ -48,8 +48,8 @@ static Cookie fromVertxCookie(io.vertx.ext.web.Cookie cookie) {
return out;
}

public static io.vertx.ext.web.Cookie toVertxCookie(Cookie cookie) {
return io.vertx.ext.web.Cookie.cookie(cookie.getName(), cookie.getValue())
public static io.vertx.core.http.Cookie toVertxCookie(Cookie cookie) {
return io.vertx.core.http.Cookie.cookie(cookie.getName(), cookie.getValue())
.setMaxAge(cookie.getMaxAge()).setSecure(cookie.getSecure())
.setHttpOnly(cookie.isHttpOnly()).setPath(cookie.getPath())
.setDomain(cookie.getDomain());
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.vaadin.ui.UI;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.VertxException;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerOptions;
Expand All @@ -54,7 +55,7 @@ public class VaadinVerticle extends AbstractVerticle {
private VertxVaadinService vaadinService;

@Override
public void start(Future<Void> startFuture) throws Exception {
public void start(Promise<Void> startFuture) throws Exception {
log.info("Starting vaadin verticle " + getClass().getName());

VaadinVerticleConfiguration vaadinVerticleConfiguration = getClass().getAnnotation(VaadinVerticleConfiguration.class);
Expand Down Expand Up @@ -82,7 +83,7 @@ public void start(Future<Void> startFuture) throws Exception {
router.mountSubRouter(mountPoint, vertxVaadin.router());

Integer httpPort = httpPort();
httpServer.requestHandler(router::accept).listen(httpPort);
httpServer.requestHandler(router).listen(httpPort);

serviceInitialized(vaadinService, router);

Expand Down Expand Up @@ -155,7 +156,7 @@ private void readConfigurationAnnotation(JsonObject vaadinConfig) {

if (VaadinServlet.PARAMETER_WIDGETSET.equals(name.value())
&& method.getDefaultValue().equals(stringValue)) {
// Do not set the widgetset to anything so that the
// Do not set the VertxVaadinRequestUTwidgetset to anything so that the
// framework can fallback to the default. Setting
// anything to the init parameter will force that into
// use and e.g. AppWidgetset will not be used even
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;
import io.vertx.ext.web.handler.CookieHandler;
import io.vertx.ext.web.handler.SessionHandler;
import io.vertx.ext.web.handler.StaticHandler;
import io.vertx.ext.web.handler.sockjs.SockJSHandler;
Expand Down Expand Up @@ -178,7 +177,6 @@ private Router initRouter() {
.setStatusCode(302).end()
);

vaadinRouter.route().handler(CookieHandler.create());
vaadinRouter.route().handler(BodyHandler.create());

// Disable SessionHandler for /VAADIN/ static resources
Expand All @@ -192,7 +190,7 @@ private Router initRouter() {
Objects.toString(ctx.request().getParam("compressed"), "")
)
));
vaadinRouter.route("/VAADIN/*").handler(StaticHandler.create("VAADIN", getClass().getClassLoader()));
vaadinRouter.route("/VAADIN/*").handler(StaticHandler.create("VAADIN"));

initSockJS(vaadinRouter, sessionHandler);

Expand Down Expand Up @@ -254,7 +252,7 @@ private static ExtendedSessionStore withSessionExpirationHandler(
MessageProducer<String> sessionExpiredProducer = sessionExpiredProducer(service);
store.expirationHandler(res -> {
if (res.succeeded()) {
sessionExpiredProducer.send(res.result());
sessionExpiredProducer.write(res.result());
} else {
res.cause().printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public VertxVaadinService getService() {
@Override
public Cookie[] getCookies() {
if (routingContext.cookieCount() > 0) {
return routingContext.cookies().stream().map(CookieUtils::fromVertxCookie).toArray(Cookie[]::new);
return routingContext.cookieMap().values().stream().map(CookieUtils::fromVertxCookie).toArray(Cookie[]::new);
}
return null;
}
Expand Down Expand Up @@ -250,7 +250,7 @@ public BufferedReader getReader() throws IOException {

@Override
public String getMethod() {
return request.rawMethod();
return request.method().name();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.UI;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpClientResponse;
import io.vertx.core.http.HttpMethod;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
Expand Down Expand Up @@ -67,12 +70,13 @@ public void testVerticle(TestContext context) {

final Async async = context.async();

vertx.createHttpClient().getNow(PORT, "localhost", "/",
response -> response.handler(b -> {
vertx.createHttpClient().request(HttpMethod.GET, PORT, "localhost", "/")
.flatMap(HttpClientRequest::send)
.flatMap(HttpClientResponse::body)
.onComplete(b -> {
context.assertTrue(b.toString().contains("Done"));
async.complete();
})
);
});
}

@VaadinServletConfiguration(productionMode = false, ui = MyUi.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.github.mcollovati.vertx.utils.RandomStringGenerator;
import com.github.mcollovati.vertx.web.ExtendedSession;
Expand All @@ -46,11 +47,12 @@
import io.vertx.core.MultiMap;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.json.JsonObject;
import io.vertx.core.net.impl.SocketAddressImpl;
import io.vertx.ext.auth.User;
import io.vertx.ext.web.Cookie;
import io.vertx.core.http.Cookie;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.Session;
import io.vertx.ext.web.impl.ParsableLanguageValue;
Expand All @@ -66,11 +68,13 @@
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.Collections.list;
import static java.util.function.Function.identity;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -294,7 +298,7 @@ public void shouldDelegateGetCookies() {
.setMaxAge(90L).setPath("path").setSecure(true);
Cookie cookie2 = Cookie.cookie("cookie2", "value2");
when(routingContext.cookieCount()).thenReturn(0).thenReturn(2);
when(routingContext.cookies()).thenReturn(new LinkedHashSet<>(Arrays.asList(cookie1, cookie2)));
when(routingContext.cookieMap()).thenReturn(Stream.of(cookie1, cookie2).collect(Collectors.toMap(Cookie::getName, identity())));
assertThat(vaadinRequest.getCookies()).isNull();
javax.servlet.http.Cookie[] cookies = vaadinRequest.getCookies();
assertThat(cookies).hasSize(2);
Expand Down Expand Up @@ -394,8 +398,11 @@ public void shouldDelegateGetCharacterEncoding() {

@Test
public void shouldDelegateGetMethod() {
vaadinRequest.getMethod();
verify(httpServerRequest).rawMethod();
HttpMethod.values().forEach(met -> {
when(httpServerRequest.method()).thenReturn(met);
assertThat(vaadinRequest.getMethod()).isEqualTo(met.name());
});
verify(httpServerRequest,times(HttpMethod.values().size())).method();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public void shouldDelegateSendError() throws Exception {

@Test
public void shouldDelegateAadCookie() throws Exception {
Set<io.vertx.ext.web.Cookie> cookies = new LinkedHashSet<>();
Set<io.vertx.core.http.Cookie> cookies = new LinkedHashSet<>();
Cookie cookie = new Cookie("name", "value");
cookie.setMaxAge(10);
cookie.setSecure(true);
Expand All @@ -200,9 +200,9 @@ public void shouldDelegateAadCookie() throws Exception {
vaadinResponse.addCookie(cookie);


ArgumentCaptor<io.vertx.ext.web.Cookie> cookieCaptor = ArgumentCaptor.forClass(io.vertx.ext.web.Cookie.class);
ArgumentCaptor<io.vertx.core.http.Cookie> cookieCaptor = ArgumentCaptor.forClass(io.vertx.core.http.Cookie.class);
verify(routingContext).addCookie(cookieCaptor.capture());
String expectedCookie = io.vertx.ext.web.Cookie.cookie(cookie.getName(), cookie.getValue())
String expectedCookie = io.vertx.core.http.Cookie.cookie(cookie.getName(), cookie.getValue())
.setMaxAge(cookie.getMaxAge()).setSecure(cookie.getSecure())
.setHttpOnly(cookie.isHttpOnly()).setPath(cookie.getPath())
.setDomain(cookie.getDomain()).encode();
Expand Down

0 comments on commit bb14fde

Please sign in to comment.