-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate to ReactiveVaadinChatApplication app naming
- Loading branch information
1 parent
82dda7b
commit 9a9ed56
Showing
6 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/main/java/com/iqkv/incubator/sample/reactivevaadinchat/ChatService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.iqkv.incubator.sample.reactivevaadinchat; | ||
|
||
import java.time.Instant; | ||
|
||
import com.vaadin.flow.spring.security.AuthenticationContext; | ||
import org.springframework.stereotype.Service; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Sinks; | ||
|
||
@Service | ||
class ChatService { | ||
|
||
private final Sinks.Many<Message> messages = Sinks.many().multicast().directBestEffort(); | ||
|
||
private final Flux<Message> messagesFlux = messages.asFlux(); | ||
|
||
private final AuthenticationContext ctx; | ||
|
||
ChatService(AuthenticationContext ctx) { | ||
this.ctx = ctx; | ||
} | ||
|
||
Flux<Message> join() { | ||
return this.messagesFlux; | ||
} | ||
|
||
void add(String message) { | ||
var username = this.ctx.getPrincipalName().orElse("Anonymous"); | ||
this.messages.tryEmitNext(new Message(username, message, Instant.now())); | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/iqkv/incubator/sample/reactivevaadinchat/ChatView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.iqkv.incubator.sample.reactivevaadinchat; | ||
|
||
import jakarta.annotation.security.PermitAll; | ||
import java.util.ArrayList; | ||
|
||
import com.vaadin.flow.component.messages.MessageInput; | ||
import com.vaadin.flow.component.messages.MessageList; | ||
import com.vaadin.flow.component.messages.MessageListItem; | ||
import com.vaadin.flow.component.orderedlayout.VerticalLayout; | ||
import com.vaadin.flow.router.Route; | ||
import com.vaadin.flow.server.Command; | ||
|
||
@Route("") | ||
@PermitAll | ||
class ChatView extends VerticalLayout { | ||
|
||
ChatView(ChatService service) { | ||
|
||
var messageList = new MessageList(); | ||
var textInput = new MessageInput(); | ||
|
||
setSizeFull(); | ||
add(messageList, textInput); | ||
expand(messageList); | ||
textInput.setWidthFull(); | ||
|
||
service.join().subscribe(message -> { | ||
var nl = new ArrayList<>(messageList.getItems()); | ||
nl.add(new MessageListItem(message.text(), message.time(), message.username())); | ||
getUI().ifPresent(ui -> ui.access((Command) () -> messageList.setItems(nl))); | ||
}); | ||
textInput.addSubmitListener(event -> service.add(event.getValue())); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/iqkv/incubator/sample/reactivevaadinchat/LoginView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.iqkv.incubator.sample.reactivevaadinchat; | ||
|
||
import com.vaadin.flow.component.login.LoginForm; | ||
import com.vaadin.flow.component.orderedlayout.VerticalLayout; | ||
import com.vaadin.flow.router.Route; | ||
|
||
@Route("login") | ||
class LoginView extends VerticalLayout { | ||
|
||
LoginView() { | ||
var form = new LoginForm(); | ||
form.setAction("login"); | ||
add(form); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/iqkv/incubator/sample/reactivevaadinchat/Message.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.iqkv.incubator.sample.reactivevaadinchat; | ||
|
||
import java.time.Instant; | ||
|
||
record Message(String username, String text, Instant time) { | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/iqkv/incubator/sample/reactivevaadinchat/ReactiveVaadinChatApplication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.iqkv.incubator.sample.reactivevaadinchat; | ||
|
||
import com.vaadin.flow.component.page.AppShellConfigurator; | ||
import com.vaadin.flow.component.page.Push; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.boot.context.metrics.buffering.BufferingApplicationStartup; | ||
|
||
@SpringBootApplication | ||
@Push | ||
public class ReactiveVaadinChatApplication implements AppShellConfigurator { | ||
public static void main(String[] args) { | ||
SpringApplication springApplication = new SpringApplication(ReactiveVaadinChatApplication.class); | ||
springApplication.setApplicationStartup(new BufferingApplicationStartup(2048)); | ||
springApplication.run(args); | ||
} | ||
} | ||
|
||
|
31 changes: 31 additions & 0 deletions
31
src/main/java/com/iqkv/incubator/sample/reactivevaadinchat/SecurityConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.iqkv.incubator.sample.reactivevaadinchat; | ||
|
||
import java.util.Set; | ||
|
||
import com.vaadin.flow.spring.security.VaadinWebSecurity; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.core.userdetails.User; | ||
import org.springframework.security.provisioning.InMemoryUserDetailsManager; | ||
import org.springframework.security.provisioning.UserDetailsManager; | ||
|
||
@Configuration | ||
class SecurityConfiguration extends VaadinWebSecurity { | ||
|
||
@Override | ||
protected void configure(HttpSecurity http) throws Exception { | ||
super.configure(http); | ||
setLoginView(http, LoginView.class); | ||
} | ||
|
||
@Bean | ||
UserDetailsManager userDetailsManager() { | ||
var users = Set.of("tony", "steve", "bruce", "natasha") | ||
.stream() | ||
.map(name -> User.withDefaultPasswordEncoder().username(name).password("pwd").roles("USER").build()) | ||
.toList(); | ||
return new InMemoryUserDetailsManager(users); | ||
} | ||
|
||
} |