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

com.ksoot.common.spring.rest.response.ResponseEntityBuilder 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.BodyBuilder;
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.Optional;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;

/**
 * @author Rajveer Singh
 */
public class ResponseEntityBuilder {

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

  public static  HeaderBuilder> of(final Optional body) {
    Assert.notNull(body, "Body must not be null");
    return body.isPresent()
        ? new Builder<>(body.get(), HttpStatus.OK)
        : new Builder<>(HttpStatus.NOT_FOUND);
  }

  public static  BodyBuilder> accepted() {
    return new Builder<>(HttpStatus.ACCEPTED);
  }

  public static  BodyBuilder> badRequest() {
    return new Builder<>(HttpStatus.BAD_REQUEST);
  }

  public static  BodyBuilder> noContent() {
    return new Builder<>(HttpStatus.NO_CONTENT);
  }

  public static  BodyBuilder> notFound() {
    return new Builder<>(HttpStatus.NOT_FOUND);
  }

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

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

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

  public static  BodyBuilder> ok() {
    return new Builder<>(HttpStatus.OK);
  }

  public static class Builder extends AbstractResponseBuilder> {

    private URI location;

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

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

    Builder(final T body, final HttpStatus status) {
      super(body, status);
    }

    Builder(final URI location) {
      super(HttpStatus.CREATED);
      Assert.notNull(location, "location must not be null");
      this.location = location;
    }

    Builder(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 ResponseEntity 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 ? bodyBuilder.body(this.body) : bodyBuilder.build();
    }
  }

  private ResponseEntityBuilder() {
    throw new IllegalStateException("Just a utility class, not supposed to be instantiated");
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy