org.infinispan.client.rest.impl.okhttp.RestResponseOkHttp Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of infinispan-client-rest-jakarta Show documentation
Show all versions of infinispan-client-rest-jakarta Show documentation
Infinispan REST Client Jakarta
The newest version!
package org.infinispan.client.rest.impl.okhttp;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import org.infinispan.client.rest.RestResponse;
import org.infinispan.client.rest.configuration.Protocol;
import org.infinispan.commons.dataconversion.MediaType;
import okhttp3.Response;
/**
* @author Tristan Tarrant <[email protected]>
* @since 10.0
**/
public class RestResponseOkHttp implements RestResponse {
private final Response response;
RestResponseOkHttp(Response response) {
this.response = response;
}
@Override
public int getStatus() {
return response.code();
}
@Override
public Map> headers() {
return response.headers().toMultimap();
}
@Override
public String getHeader(String header) {
List values = response.headers(header);
if (values.isEmpty()) return null;
return String.join(",", values);
}
@Override
public String getBody() {
try {
return response.body().string();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public InputStream getBodyAsStream() {
return response.body().byteStream();
}
@Override
public byte[] getBodyAsByteArray() {
try {
return response.body().bytes();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public MediaType contentType() {
okhttp3.MediaType mediaType = response.body().contentType();
return mediaType == null ? null : MediaType.fromString(mediaType.toString());
}
@Override
public Protocol getProtocol() {
switch (response.protocol()) {
case H2_PRIOR_KNOWLEDGE:
case HTTP_2:
return Protocol.HTTP_20;
case HTTP_1_1:
return Protocol.HTTP_11;
default:
throw new IllegalStateException("Unknown protocol " + response.protocol());
}
}
@Override
public void close() {
response.close();
}
@Override
public boolean usedAuthentication() {
return !response.request().headers("Authorization").isEmpty();
}
}