org.vfdtech.models.ResponseWrapper Maven / Gradle / Ivy
Show all versions of utilities-and-generic-tools Show documentation
package org.vfdtech.models;
import lombok.Builder;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import java.util.Objects;
/**
* This class is a wrapper class around the Apache Pair lib
* and its purpose is to provide a container for a generic response
* for web interface interaction with libraries and service.
*
* The statusCode is basically a Http-Standard (200,201,400, ...) compatible Integer.
* The responseEntity is an Apache.Pair object, whose key is the message to show
* and the Value is the data to return to the client or in case of errors, the list of errors
*/
@Data
@Builder
public class ResponseWrapper {
private int statusCode;
private Pair responseEntity;
public static ResponseWrapper of(int statusCode, Pair responseEntity) {
return builder().statusCode(statusCode).responseEntity(responseEntity).build();
}
public boolean hasResponseEntityData() {
return Objects.nonNull(responseEntity) && StringUtils.isNotBlank(responseEntity.getRight());
}
public boolean isSuccessful() {
return this.statusCode >= 200 && this.statusCode <= 299;
}
public boolean isClientError() {
return this.statusCode >= 400 && this.statusCode <= 499;
}
public boolean isAuthError() {
return this.statusCode == 401;
}
}