Skip to content

Commit

Permalink
feat: migrate to ReactiveVaadinChatApplication app naming
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriidenisenko committed Nov 5, 2024
1 parent 82dda7b commit 9a9ed56
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 0 deletions.
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()));
}

}
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()));
}
}
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);
}
}
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) {
}
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);
}
}


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);
}

}

0 comments on commit 9a9ed56

Please sign in to comment.