Skip to content

Commit f34c5a8

Browse files
authored
feat: Hilla push implementation behind a feature flag (#13350)
* feat: Hilla push implementation behind a feature flag This adds support for endpoints methods to return a Flux<T> which will be streamed to the client side to the subscribers that have registered using TheEndpoint.theMethod().onNext(value => ...); The implementation is based on https://socket.io/ but this is not considered to be part of the public API. We should be free to change the underlying implementation if needed. Fixes vaadin/hilla#56 * Fix lift * Fix tests * Use correct JSON library * Upgrade engine io to fix long polling send issue * Move JS code to @hilla/frontend * Remove unnecessary boolean * format * format and remove unnecessary changes * Unit test for TS generation * format * Ensure anonymous user is not interpreted as logged in * Flux push IT * TS ignore Subscription import as it is mostly unused * Clean up engine io server and project reactor threads on shutdown * Remove commented code * Upgrade Spring Security to 5.6.3 * Fix cleanup when not using Spring Boot * fix merge issue
1 parent 25a60c8 commit f34c5a8

71 files changed

Lines changed: 1496 additions & 63 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎flow-server/src/main/java/com/vaadin/experimental/FeatureFlags.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ public class FeatureFlags implements Serializable {
6464
"Spreadsheet component (Pro)", "spreadsheetComponent",
6565
"https://github.com/vaadin/platform/issues/2027", true,
6666
"com.vaadin.flow.component.spreadsheet.Spreadsheet");
67+
public static final Feature HILLA_PUSH = new Feature(
68+
"Push support in Hilla", "hillaPush",
69+
"https://github.com/vaadin/hilla/issues/56", true, null);
6770
private List<Feature> features = new ArrayList<>();
6871

6972
File propertiesFolder = null;
@@ -84,6 +87,7 @@ public FeatureFlags(Lookup lookup) {
8487
features.add(new Feature(VITE));
8588
features.add(new Feature(MAP_COMPONENT));
8689
features.add(new Feature(SPREADSHEET_COMPONENT));
90+
features.add(new Feature(HILLA_PUSH));
8791
loadProperties();
8892
}
8993

@@ -326,6 +330,13 @@ private boolean isDevelopmentMode() {
326330
return configuration != null && !configuration.isProductionMode();
327331
}
328332

333+
public String getEnableHelperMessage(Feature feature) {
334+
return feature.getTitle()
335+
+ " is not enabled. Enable it in the debug window or by adding "
336+
+ getPropertyName(feature.getId())
337+
+ "=true to src/main/resources/" + PROPERTIES_FILENAME;
338+
}
339+
329340
private Logger getLogger() {
330341
return LoggerFactory.getLogger(FeatureFlags.class);
331342
}

‎flow-tests/vaadin-spring-tests/test-endpoints/frontend/src/test-component.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ class TestComponent extends PolymerElement {
2020
<button id="getObjectWithNullValues" on-click="getObjectWithNullValues">Get Object With Null Values From Endpoint</button><br/>
2121
<button id="denied" on-click="denied">endpoint denied</button><br/>
2222
<button id="logout" on-click="logout">logout</button><br/>
23-
<form method="post" action="login">
24-
<input id="username" name="username"></input>
25-
<input id="password" name="password"></input>
26-
<input id="login" type="submit"></input>
23+
<form method="POST" action="login">
24+
<input id="username" name="username">
25+
<input id="password" name="password">
26+
<input id="login" type="submit">
2727
</form>
2828
<div id="content"></div>
2929
`;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { Subscription } from '@hilla/frontend';
2+
import { LitElement, html } from 'lit';
3+
import { customElement, query } from 'lit/decorators.js';
4+
import { FluxEndpoint } from '../generated/endpoints';
5+
6+
@customElement('test-flux')
7+
export class TestFlux extends LitElement {
8+
@query('#content')
9+
private content!: HTMLElement;
10+
11+
render() {
12+
return html`
13+
<div style="display:flex;flex-direction:column">
14+
<form method="POST" action="login">
15+
<input id="username" name="username" />
16+
<input id="password" name="password" />
17+
<input id="login" type="submit" />
18+
</form>
19+
20+
<button id="denied" @click="${this.denied}">denied()</button>
21+
<button id="hello" @click="${this.hello}">hello()</button>
22+
<button id="helloAnonymous" @click="${this.helloAnonymous}">helloAnonymous()</button>
23+
<button id="helloAdmin" @click="${this.helloAdmin}">helloAdmin()</button>
24+
<button id="checkUser" @click="${this.checkUser}">checkUser()</button>
25+
<button id="countTo" @click=${() => this.countTo(5)}>countTo(5)</button>
26+
<button id="countEvenTo" @click="${() => this.countEvenTo(10)}">countEvenTo(10)</button>
27+
<button id="countThrowError" @click="${() => this.countThrowError(10)}">countThrowError(10)</button>
28+
29+
<pre id="content"></pre>
30+
</div>
31+
`;
32+
}
33+
34+
private testEndpoint(sub: Subscription<any>) {
35+
this.content.textContent = '';
36+
sub
37+
.onNext((value) => {
38+
this.content.textContent += 'Value: ' + value + '\n';
39+
})
40+
.onComplete(() => {
41+
this.content.textContent += 'Completed' + '\n';
42+
})
43+
.onError(() => {
44+
this.content.textContent += 'Error' + '\n';
45+
});
46+
}
47+
private denied() {
48+
this.testEndpoint(FluxEndpoint.denied());
49+
}
50+
private helloAnonymous() {
51+
this.testEndpoint(FluxEndpoint.helloAnonymous());
52+
}
53+
private hello() {
54+
this.testEndpoint(FluxEndpoint.hello('John', 'Manager'));
55+
}
56+
private helloAdmin() {
57+
this.testEndpoint(FluxEndpoint.helloAdmin());
58+
}
59+
private checkUser() {
60+
this.testEndpoint(FluxEndpoint.checkUser());
61+
}
62+
private countTo(to: number) {
63+
this.testEndpoint(FluxEndpoint.countTo(to));
64+
}
65+
private countEvenTo(to: number) {
66+
this.testEndpoint(FluxEndpoint.countEvenTo(to));
67+
}
68+
private countThrowError(to: number) {
69+
this.testEndpoint(FluxEndpoint.countThrowError(to));
70+
}
71+
}

‎flow-tests/vaadin-spring-tests/test-endpoints/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<version>23.1-SNAPSHOT</version>
99
</parent>
1010
<artifactId>test-endpoints</artifactId>
11-
<name>Integration tests for V15+ endpoints</name>
11+
<name>Integration tests for endpoints</name>
1212
<packaging>jar</packaging>
1313
<properties>
1414
<maven.deploy.skip>true</maven.deploy.skip>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2000-2018 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* 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 under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.connect;
17+
18+
import com.vaadin.flow.component.Component;
19+
import com.vaadin.flow.component.Tag;
20+
import com.vaadin.flow.component.dependency.JsModule;
21+
22+
/**
23+
* A Test Web Component.
24+
*/
25+
@Tag("test-flux")
26+
@JsModule("./src/test-flux.ts")
27+
public class FluxComponent extends Component {
28+
29+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.vaadin.flow.connect;
2+
3+
import java.time.Duration;
4+
5+
import javax.annotation.Nullable;
6+
import javax.annotation.security.PermitAll;
7+
import javax.annotation.security.RolesAllowed;
8+
9+
import com.vaadin.flow.server.auth.AnonymousAllowed;
10+
11+
import org.springframework.security.core.Authentication;
12+
import org.springframework.security.core.context.SecurityContextHolder;
13+
14+
import dev.hilla.Endpoint;
15+
import dev.hilla.Nonnull;
16+
import reactor.core.publisher.Flux;
17+
18+
@Endpoint
19+
public class FluxEndpoint {
20+
21+
// DenyAll by default
22+
@Nonnull
23+
public Flux<String> denied() {
24+
return Flux.just("Will never be accessible");
25+
}
26+
27+
@PermitAll
28+
@Nonnull
29+
public Flux<String> hello(String name, @Nullable String title) {
30+
return Flux.just(
31+
"Hello, " + (title != null ? title + " " : "") + name + "!");
32+
}
33+
34+
@AnonymousAllowed
35+
@Nonnull
36+
public Flux<String> helloAnonymous() {
37+
return Flux.just("Hello, stranger!");
38+
}
39+
40+
@RolesAllowed("ADMIN")
41+
@Nonnull
42+
public Flux<String> helloAdmin() {
43+
return Flux.just("Hello, admin!");
44+
}
45+
46+
@AnonymousAllowed
47+
@Nonnull
48+
public Flux<String> checkUser() {
49+
Authentication auth = SecurityContextHolder.getContext()
50+
.getAuthentication();
51+
return Flux.just(auth == null ? "Not authenticated" : auth.getName());
52+
}
53+
54+
@AnonymousAllowed
55+
@Nonnull
56+
public Flux<Integer> countTo(int n) {
57+
return Flux.range(1, n).delayElements(Duration.ofMillis(200));
58+
}
59+
60+
@AnonymousAllowed
61+
@Nonnull
62+
public Flux<Integer> countEvenTo(int n) {
63+
return Flux.range(1, n).delayElements(Duration.ofMillis(200))
64+
.filter(number -> number % 2 == 0);
65+
}
66+
67+
@AnonymousAllowed
68+
@Nonnull
69+
public Flux<Integer> countThrowError(int n) {
70+
return Flux.range(1, n).delayElements(Duration.ofMillis(200))
71+
.filter(number -> {
72+
if (number == 3) {
73+
throw new RuntimeException(
74+
"Intentionally failing Flux");
75+
}
76+
return true;
77+
});
78+
}
79+
80+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2000-2018 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* 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 under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.connect;
17+
18+
import com.vaadin.flow.component.html.Div;
19+
import com.vaadin.flow.router.Route;
20+
import com.vaadin.flow.server.auth.AnonymousAllowed;
21+
22+
@Route(value = "flux")
23+
@AnonymousAllowed
24+
public class FluxView extends Div {
25+
26+
public FluxView() {
27+
add(new FluxComponent());
28+
}
29+
30+
}

‎flow-tests/vaadin-spring-tests/test-endpoints/src/main/java/com/vaadin/flow/connect/MainView.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
import com.vaadin.flow.router.HasUrlParameter;
2121
import com.vaadin.flow.router.Route;
2222
import com.vaadin.flow.router.WildcardParameter;
23+
import com.vaadin.flow.server.auth.AnonymousAllowed;
2324

2425
@Route(value = "")
26+
@AnonymousAllowed
2527
public class MainView extends Div implements HasUrlParameter<String> {
2628

2729
public MainView() {
Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
11
package com.vaadin.flow.connect;
22

3+
import com.vaadin.flow.spring.security.VaadinWebSecurityConfigurerAdapter;
4+
35
import org.springframework.context.annotation.Configuration;
46
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
57
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
6-
import org.springframework.security.config.annotation.web.builders.WebSecurity;
78
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
8-
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
99

1010
@EnableWebSecurity
1111
@Configuration
12-
public class SecurityConfig extends WebSecurityConfigurerAdapter {
12+
public class SecurityConfig extends VaadinWebSecurityConfigurerAdapter {
1313

1414
@Override
1515
protected void configure(HttpSecurity http) throws Exception {
16-
// Use default spring login form
17-
http.formLogin();
18-
// Vaadin already handles csrf.
19-
http.csrf().disable();
20-
}
21-
22-
@Override
23-
public void configure(WebSecurity web) {
24-
// Access to static resources, bypassing Spring security.
25-
web.ignoring().antMatchers("/VAADIN/**");
16+
super.configure(http);
17+
http.csrf().ignoringAntMatchers("/login");
18+
setLoginView(http, "/login");
2619
}
2720

2821
@Override
@@ -33,4 +26,5 @@ protected void configure(AuthenticationManagerBuilder auth)
3326
.roles("USER").and().withUser("admin").password("{noop}admin")
3427
.roles("ADMIN", "USER");
3528
}
36-
}
29+
30+
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
server.servlet.context-path=/foo
21
server.port=8888
32
vaadin.devmode.liveReload.enabled=false

0 commit comments

Comments
 (0)