io.vrap.rmf.base.client.ApiHttpResponse Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rmf-java-base Show documentation
Show all versions of rmf-java-base Show documentation
The e-commerce SDK from commercetools Composable Commerce for Java
package io.vrap.rmf.base.client;
import org.apache.commons.lang3.builder.ToStringBuilder;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
public class ApiHttpResponse extends Base {
private int statusCode;
private ApiHttpHeaders headers;
private U body;
private String message;
public ApiHttpResponse(final int statusCode, final ApiHttpHeaders headers, final U body) {
this(statusCode, headers, body, null);
}
public ApiHttpResponse(final int statusCode, final ApiHttpHeaders headers, final U body, final String message) {
this.statusCode = statusCode;
this.headers = headers;
this.body = body;
this.message = message;
}
public ApiHttpResponse(final ApiHttpResponse response) {
this.statusCode = response.statusCode;
this.headers = response.headers;
this.body = response.body;
this.message = response.message;
}
public int getStatusCode() {
return statusCode;
}
public ApiHttpResponse withStatusCode(final int statusCode) {
ApiHttpResponse response = copy();
response.statusCode = statusCode;
return response;
}
public ApiHttpHeaders getHeaders() {
return headers;
}
public ApiHttpResponse withHeaders(final ApiHttpHeaders headers) {
ApiHttpResponse response = copy();
response.headers = headers;
return response;
}
public U getBody() {
return body;
}
public ApiHttpResponse withBody(final U body) {
ApiHttpResponse response = copy();
response.body = body;
return response;
}
public String getMessage() {
return message;
}
public ApiHttpResponse withMessage(final String message) {
ApiHttpResponse response = copy();
response.message = message;
return response;
}
@Override
public String toString() {
return new ToStringBuilder(this)
.append("statusCode", statusCode)
.append("headers", headers)
.append("textInterpretedBody", getSecuredBody())
.toString();
}
static String tryToFilter(final String input) {
return input.replaceAll("(\"\\w*([Pp]ass|access_token|refresh_token)\\w*\"):\"[^\"]*\"", "$1:\"**removed from output**\"");
}
public Optional getBodyAsString()
{
if (body instanceof byte[]) {
return Optional.of(body).map(b -> tryToFilter(new String((byte[])b, StandardCharsets.UTF_8)));
} else {
return Optional.ofNullable(body.toString());
}
}
public String getSecuredBody()
{
return getBodyAsString().orElse("empty body");
}
private ApiHttpResponse copy()
{
return new ApiHttpResponse<>(this);
}
}