From bb14fde12494b50c1e6c42f57d5a3a95ca874c90 Mon Sep 17 00:00:00 2001 From: Marco Collovati Date: Fri, 9 Apr 2021 18:17:29 +0200 Subject: [PATCH] Upgrade to vert.x 4.0.x for vaadin 8 --- .../com/github/mcollovati/vertx/Sync.java | 9 ++-- .../mcollovati/vertx/vaadin/CookieUtils.java | 6 +-- .../vertx/vaadin/FirstVerticle.java | 46 ------------------- .../vertx/vaadin/VaadinVerticle.java | 7 +-- .../mcollovati/vertx/vaadin/VertxVaadin.java | 6 +-- .../vertx/vaadin/VertxVaadinRequest.java | 4 +- .../vertx/vaadin/SessionStoreAdapterIT.java | 12 +++-- .../vertx/vaadin/VertxVaadinRequestUT.java | 15 ++++-- .../vertx/vaadin/VertxVaadinResponseUT.java | 6 +-- 9 files changed, 38 insertions(+), 73 deletions(-) delete mode 100644 vertx-vaadin-8-parent/vertx-vaadin8/src/main/java/com/github/mcollovati/vertx/vaadin/FirstVerticle.java diff --git a/vertx-vaadin-8-parent/vertx-vaadin8/src/main/java/com/github/mcollovati/vertx/Sync.java b/vertx-vaadin-8-parent/vertx-vaadin8/src/main/java/com/github/mcollovati/vertx/Sync.java index 40653d48..96b58544 100644 --- a/vertx-vaadin-8-parent/vertx-vaadin8/src/main/java/com/github/mcollovati/vertx/Sync.java +++ b/vertx-vaadin-8-parent/vertx-vaadin8/src/main/java/com/github/mcollovati/vertx/Sync.java @@ -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; @@ -41,21 +42,21 @@ public final class Sync { public static T await(Consumer>> task) { CountDownLatch countDownLatch = new CountDownLatch(1); try { - Future f = Future.future().setHandler(ar -> { + Promise p = Promise.promise(); + Future 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); } - } - } diff --git a/vertx-vaadin-8-parent/vertx-vaadin8/src/main/java/com/github/mcollovati/vertx/vaadin/CookieUtils.java b/vertx-vaadin-8-parent/vertx-vaadin8/src/main/java/com/github/mcollovati/vertx/vaadin/CookieUtils.java index 92d9e647..33b3159d 100644 --- a/vertx-vaadin-8-parent/vertx-vaadin8/src/main/java/com/github/mcollovati/vertx/vaadin/CookieUtils.java +++ b/vertx-vaadin-8-parent/vertx-vaadin8/src/main/java/com/github/mcollovati/vertx/vaadin/CookieUtils.java @@ -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); @@ -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()); diff --git a/vertx-vaadin-8-parent/vertx-vaadin8/src/main/java/com/github/mcollovati/vertx/vaadin/FirstVerticle.java b/vertx-vaadin-8-parent/vertx-vaadin8/src/main/java/com/github/mcollovati/vertx/vaadin/FirstVerticle.java deleted file mode 100644 index 07398fd9..00000000 --- a/vertx-vaadin-8-parent/vertx-vaadin8/src/main/java/com/github/mcollovati/vertx/vaadin/FirstVerticle.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * The MIT License - * Copyright © 2016-2020 Marco Collovati (mcollovati@gmail.com) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package com.github.mcollovati.vertx.vaadin; - -import io.vertx.core.AbstractVerticle; -import io.vertx.core.Future; - -/** - * Created by marco on 16/07/16. - */ -public class FirstVerticle extends AbstractVerticle { - - @Override - public void start(Future fut) throws Exception { - - vertx.createHttpServer() - .requestHandler(req -> req.response().end("Mandi")) - .listen(8080, result -> { - if (result.succeeded()) { - fut.complete(); - } else { - fut.fail(result.cause()); - } - }); - } -} diff --git a/vertx-vaadin-8-parent/vertx-vaadin8/src/main/java/com/github/mcollovati/vertx/vaadin/VaadinVerticle.java b/vertx-vaadin-8-parent/vertx-vaadin8/src/main/java/com/github/mcollovati/vertx/vaadin/VaadinVerticle.java index c243d14c..ca1d44ef 100644 --- a/vertx-vaadin-8-parent/vertx-vaadin8/src/main/java/com/github/mcollovati/vertx/vaadin/VaadinVerticle.java +++ b/vertx-vaadin-8-parent/vertx-vaadin8/src/main/java/com/github/mcollovati/vertx/vaadin/VaadinVerticle.java @@ -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; @@ -54,7 +55,7 @@ public class VaadinVerticle extends AbstractVerticle { private VertxVaadinService vaadinService; @Override - public void start(Future startFuture) throws Exception { + public void start(Promise startFuture) throws Exception { log.info("Starting vaadin verticle " + getClass().getName()); VaadinVerticleConfiguration vaadinVerticleConfiguration = getClass().getAnnotation(VaadinVerticleConfiguration.class); @@ -82,7 +83,7 @@ public void start(Future 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); @@ -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 diff --git a/vertx-vaadin-8-parent/vertx-vaadin8/src/main/java/com/github/mcollovati/vertx/vaadin/VertxVaadin.java b/vertx-vaadin-8-parent/vertx-vaadin8/src/main/java/com/github/mcollovati/vertx/vaadin/VertxVaadin.java index fa09748b..47cd8c41 100644 --- a/vertx-vaadin-8-parent/vertx-vaadin8/src/main/java/com/github/mcollovati/vertx/vaadin/VertxVaadin.java +++ b/vertx-vaadin-8-parent/vertx-vaadin8/src/main/java/com/github/mcollovati/vertx/vaadin/VertxVaadin.java @@ -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; @@ -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 @@ -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); @@ -254,7 +252,7 @@ private static ExtendedSessionStore withSessionExpirationHandler( MessageProducer sessionExpiredProducer = sessionExpiredProducer(service); store.expirationHandler(res -> { if (res.succeeded()) { - sessionExpiredProducer.send(res.result()); + sessionExpiredProducer.write(res.result()); } else { res.cause().printStackTrace(); } diff --git a/vertx-vaadin-8-parent/vertx-vaadin8/src/main/java/com/github/mcollovati/vertx/vaadin/VertxVaadinRequest.java b/vertx-vaadin-8-parent/vertx-vaadin8/src/main/java/com/github/mcollovati/vertx/vaadin/VertxVaadinRequest.java index b0a8302d..de8d9655 100644 --- a/vertx-vaadin-8-parent/vertx-vaadin8/src/main/java/com/github/mcollovati/vertx/vaadin/VertxVaadinRequest.java +++ b/vertx-vaadin-8-parent/vertx-vaadin8/src/main/java/com/github/mcollovati/vertx/vaadin/VertxVaadinRequest.java @@ -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; } @@ -250,7 +250,7 @@ public BufferedReader getReader() throws IOException { @Override public String getMethod() { - return request.rawMethod(); + return request.method().name(); } @Override diff --git a/vertx-vaadin-8-parent/vertx-vaadin8/src/test/java/com/github/mcollovati/vertx/vaadin/SessionStoreAdapterIT.java b/vertx-vaadin-8-parent/vertx-vaadin8/src/test/java/com/github/mcollovati/vertx/vaadin/SessionStoreAdapterIT.java index c11a1fb6..aadec982 100644 --- a/vertx-vaadin-8-parent/vertx-vaadin8/src/test/java/com/github/mcollovati/vertx/vaadin/SessionStoreAdapterIT.java +++ b/vertx-vaadin-8-parent/vertx-vaadin8/src/test/java/com/github/mcollovati/vertx/vaadin/SessionStoreAdapterIT.java @@ -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; @@ -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) diff --git a/vertx-vaadin-8-parent/vertx-vaadin8/src/test/java/com/github/mcollovati/vertx/vaadin/VertxVaadinRequestUT.java b/vertx-vaadin-8-parent/vertx-vaadin8/src/test/java/com/github/mcollovati/vertx/vaadin/VertxVaadinRequestUT.java index 2fe8d90b..5a13a5a0 100644 --- a/vertx-vaadin-8-parent/vertx-vaadin8/src/test/java/com/github/mcollovati/vertx/vaadin/VertxVaadinRequestUT.java +++ b/vertx-vaadin-8-parent/vertx-vaadin8/src/test/java/com/github/mcollovati/vertx/vaadin/VertxVaadinRequestUT.java @@ -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; @@ -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; @@ -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; @@ -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); @@ -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 diff --git a/vertx-vaadin-8-parent/vertx-vaadin8/src/test/java/com/github/mcollovati/vertx/vaadin/VertxVaadinResponseUT.java b/vertx-vaadin-8-parent/vertx-vaadin8/src/test/java/com/github/mcollovati/vertx/vaadin/VertxVaadinResponseUT.java index 49792a15..4f529a8f 100644 --- a/vertx-vaadin-8-parent/vertx-vaadin8/src/test/java/com/github/mcollovati/vertx/vaadin/VertxVaadinResponseUT.java +++ b/vertx-vaadin-8-parent/vertx-vaadin8/src/test/java/com/github/mcollovati/vertx/vaadin/VertxVaadinResponseUT.java @@ -190,7 +190,7 @@ public void shouldDelegateSendError() throws Exception { @Test public void shouldDelegateAadCookie() throws Exception { - Set cookies = new LinkedHashSet<>(); + Set cookies = new LinkedHashSet<>(); Cookie cookie = new Cookie("name", "value"); cookie.setMaxAge(10); cookie.setSecure(true); @@ -200,9 +200,9 @@ public void shouldDelegateAadCookie() throws Exception { vaadinResponse.addCookie(cookie); - ArgumentCaptor cookieCaptor = ArgumentCaptor.forClass(io.vertx.ext.web.Cookie.class); + ArgumentCaptor 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();