Skip to content

Instantly share code, notes, and snippets.

@timjonesdev
timjonesdev / CorsWebFilter.java
Created August 21, 2019 17:21
A simple CORS filter
@Bean
CorsWebFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
// Possibly...
// config.applyPermitDefaultValues()
config.setAllowCredentials(true);
// allow access to my dev Angular instance
@timjonesdev
timjonesdev / StaticWebFilter.java
Created August 21, 2019 17:17
A static web filter
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;
@Component
public class StaticWebFilter implements WebFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
@timjonesdev
timjonesdev / RandomizeScore.java
Created August 21, 2019 16:45
The zip function, in action.
public Mono<ServerResponse> randomizeScore(ServerRequest request) {
String countString = request.pathVariable("count");
int count = Integer.parseInt(countString);
if (count < 0 || count > 40) {
return ServerResponse.badRequest().body(BodyInserters.fromObject("Count must be between 0 and 40"));
}
Flux<String> playerNames = this.teamRepository.findAll()
.map(Team::getPlayers)
/**
* Subscribe to watch updates to a particular team
*
* @param request - must be of the form /{name}
* @return a subscription to a Server Sent Event which
* fires every time the requested team is updated in the fantasy_db.teams collection.
* The subscription is watching a change stream in MongoDB
*/
public Mono<ServerResponse> watchTeam(ServerRequest request) {
String teamName = request.pathVariable("name");
@Bean
public RouterFunction<ServerResponse> route(TeamHandler teamHandler) {
return RouterFunctions
.route(RequestPredicates.GET("/teams"), teamHandler::getTeams)
.andRoute(RequestPredicates.GET("/teams/watch"), teamHandler::watchTeams)
.andRoute(RequestPredicates.GET("/team/{name}"), teamHandler::watchTeam);
}
@timjonesdev
timjonesdev / TeamHandler_1.java
Created August 21, 2019 00:02
The first team handler
/**
* Return all teams from the fantasy_db.teams collection
*
* @param request - the request (unused in this operation)
* @return a list of all teams
*/
public Mono<ServerResponse> getTeams(ServerRequest request) {
return ServerResponse.ok()
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromPublisher(this.teamRepository.findAll(), Team.class));
@timjonesdev
timjonesdev / TeamRouter_1.java
Created August 21, 2019 00:01
The first router function
@Bean
public RouterFunction<ServerResponse> route(TeamHandler teamHandler) {
return RouterFunctions
.route(RequestPredicates.GET("/teams"), teamHandler::getTeams);
}
@timjonesdev
timjonesdev / TeamChangeWatch.java
Created August 20, 2019 22:47
The main implementation for watching changes on a MongoDB Collection
/**
* Watch for changes to the teams collection
*
* @return a subscription to the change stream
*/
public Flux<Team> watchForTeamCollectionChanges() {
// set changestream options to watch for any changes to the businesses collection
ChangeStreamOptions options = ChangeStreamOptions.builder()
.filter(Aggregation.newAggregation(Team.class,
Aggregation.match(
@timjonesdev
timjonesdev / ReactiveMongoConfig.java
Created August 20, 2019 22:31
Extend the AbstractReactiveMongoConfiguration
import com.mongodb.reactivestreams.client.MongoClient;
import com.mongodb.reactivestreams.client.MongoClients;
import org.springframework.context.annotation.Bean;
import org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;
/**
* Config class to set up necessary components for watching the MongoDB change stream
*/
@timjonesdev
timjonesdev / TeamRepository.java
Created August 20, 2019 22:07
Team Reactive Mongo Repository for Reactive Mongo Example
import dev.timjones.reactive.data.model.Team;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import reactor.core.publisher.Mono;
public interface TeamRepository extends ReactiveMongoRepository<Team, ObjectId> {
@Query(value = "{ 'players.name' : ?0 }")
Mono<Team> findDistinctByPlayerName(String playerName);