All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ksoot.common.spring.rest.response.ReactiveResponseEntityBuilder Maven / Gradle / Ivy

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 com.ksoot.common.spring.rest.response.builder.StatusBuilder;
import java.net.URI;
import java.util.List;
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 ReactiveResponseEntityBuilder {

  public static  StatusBuilder>> of(final T body) {
    Assert.notNull(body, "Body must not be null");
    return new MonoResponseEntityBuilder(body);
  }

  public static  HeaderBuilder>> of(final Optional body) {
    Assert.notNull(body, "Body must not be null");
    return of(Mono.justOrEmpty(body));
  }

  public static  HeaderBuilder>> of(final Mono body) {
    Assert.notNull(body, "Body must not be null");
    return body.map(content -> new MonoResponseEntityBuilder(content, 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.collectList()
        .map(content -> new FluxResponseEntityBuilder(content, HttpStatus.OK))
        .defaultIfEmpty(new FluxResponseEntityBuilder<>(HttpStatus.NOT_FOUND))
        .block();
  }

  public static  HeaderBuilder>> accepted() {
    return new MonoResponseEntityBuilder<>(HttpStatus.ACCEPTED);
  }

  public static  HeaderBuilder>> badRequest() {
    return new MonoResponseEntityBuilder<>(HttpStatus.BAD_REQUEST);
  }

  public static  HeaderBuilder>> noContent() {
    return new MonoResponseEntityBuilder<>(HttpStatus.NO_CONTENT);
  }

  public static  HeaderBuilder>> notFound() {
    return new MonoResponseEntityBuilder<>(HttpStatus.NOT_FOUND);
  }

  public static  HeaderBuilder>> created(final URI location) {
    return new MonoResponseEntityBuilder<>(location);
  }

  public static  HeaderBuilder>> created(
      final URI location, final T body) {
    return new MonoResponseEntityBuilder<>(location, body);
  }

  public static  HeaderBuilder>> ok(final T body) {
    return new MonoResponseEntityBuilder<>(body, HttpStatus.OK);
  }

  public static  HeaderBuilder>> ok() {
    return new MonoResponseEntityBuilder<>(HttpStatus.OK);
  }

  public static class MonoResponseEntityBuilder
      extends AbstractResponseBuilder>> {

    private URI location;

    MonoResponseEntityBuilder(final HttpStatus status) {
      super(status);
    }

    MonoResponseEntityBuilder(final T body) {
      super(body);
    }

    MonoResponseEntityBuilder(final T 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 T 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 List body) {
      super(body);
    }

    FluxResponseEntityBuilder(final List 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 ReactiveResponseEntityBuilder() {
    throw new IllegalStateException("Just a utility class, not supposed to be instantiated");
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy