com.ksoot.common.spring.rest.response.PublisherResponseEntityBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-boot-commons Show documentation
Show all versions of spring-boot-commons Show documentation
Commons Spring boot components and Utilities
The newest version!
package com.ksoot.common.spring.rest.response;
import com.ksoot.common.spring.rest.response.builder.AbstractResponseBuilder;
import com.ksoot.common.spring.rest.response.builder.HeaderBuilder;
import java.net.URI;
import java.util.Optional;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
/**
* @author Rajveer Singh
*/
public class PublisherResponseEntityBuilder {
public static HeaderBuilder, Mono>>> of(
final Optional body) {
Assert.notNull(body, "Body must not be null");
return of(Mono.justOrEmpty(body));
}
public static HeaderBuilder, Mono>>> of(final Mono body) {
Assert.notNull(body, "Body must not be null");
return body.hasElement()
.map(notEmpty -> new MonoResponseEntityBuilder(body, HttpStatus.OK))
.defaultIfEmpty(new MonoResponseEntityBuilder<>(HttpStatus.NOT_FOUND))
.block();
}
public static HeaderBuilder, Mono>>> of(final Flux body) {
Assert.notNull(body, "Body must not be null");
return body.hasElements()
.map(notEmpty -> new FluxResponseEntityBuilder(body, HttpStatus.OK))
.defaultIfEmpty(new FluxResponseEntityBuilder<>(HttpStatus.NOT_FOUND))
.block();
}
public static HeaderBuilder, Mono>>> accepted() {
return new MonoResponseEntityBuilder<>(HttpStatus.ACCEPTED);
}
public static HeaderBuilder, Mono>>> badRequest() {
return new MonoResponseEntityBuilder<>(HttpStatus.BAD_REQUEST);
}
public static HeaderBuilder, Mono>>> noContent() {
return new MonoResponseEntityBuilder<>(HttpStatus.NO_CONTENT);
}
public static HeaderBuilder, Mono>>> notFound() {
return new MonoResponseEntityBuilder<>(HttpStatus.NOT_FOUND);
}
public static HeaderBuilder, Mono>>> created(
final URI location) {
return new MonoResponseEntityBuilder<>(location);
}
public static HeaderBuilder, Mono>>> created(
final URI location, final Mono body) {
return new MonoResponseEntityBuilder<>(location, body);
}
public static HeaderBuilder, Mono>>> ok(final T body) {
return new MonoResponseEntityBuilder<>(Mono.just(body), HttpStatus.OK);
}
public static HeaderBuilder, Mono>>> ok() {
return new MonoResponseEntityBuilder<>(HttpStatus.OK);
}
public static class MonoResponseEntityBuilder
extends AbstractResponseBuilder, Mono>>> {
private URI location;
MonoResponseEntityBuilder(final HttpStatus status) {
super(status);
}
MonoResponseEntityBuilder(final Mono body) {
super(body);
}
MonoResponseEntityBuilder(final Mono body, final HttpStatus status) {
super(body, status);
}
MonoResponseEntityBuilder(final URI location) {
super(HttpStatus.CREATED);
Assert.notNull(location, "location must not be null");
this.location = location;
}
MonoResponseEntityBuilder(final URI location, final Mono body) {
super(body, HttpStatus.CREATED);
Assert.notNull(location, "location must not be null");
Assert.notNull(body, "body must not be null");
this.location = location;
}
@Override
public Mono>> build() {
org.springframework.http.ResponseEntity.BodyBuilder bodyBuilder =
ResponseEntity.status(this.status);
if (this.location != null) {
bodyBuilder.location(this.location);
}
if (this.headers != null) {
bodyBuilder.headers(this.headers);
}
return this.body != null
? Mono.just(bodyBuilder.body(this.body))
: Mono.just(bodyBuilder.build());
}
}
public static class FluxResponseEntityBuilder
extends AbstractResponseBuilder, Mono>>> {
private URI location;
FluxResponseEntityBuilder(final HttpStatus status) {
super(status);
}
FluxResponseEntityBuilder(final Flux body) {
super(body);
}
FluxResponseEntityBuilder(final Flux body, final HttpStatus status) {
super(body, status);
}
FluxResponseEntityBuilder(final URI location) {
super(HttpStatus.CREATED);
Assert.notNull(location, "location must not be null");
this.location = location;
}
@Override
public Mono>> build() {
org.springframework.http.ResponseEntity.BodyBuilder bodyBuilder =
ResponseEntity.status(this.status);
if (this.location != null) {
bodyBuilder.location(this.location);
}
if (this.headers != null) {
bodyBuilder.headers(this.headers);
}
return this.body != null
? Mono.just(bodyBuilder.body(this.body))
: Mono.just(bodyBuilder.build());
}
}
private PublisherResponseEntityBuilder() {
throw new IllegalStateException("Just a utility class, not supposed to be instantiated");
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy