package org.onap.holmes.common.utils;
-import org.eclipse.jetty.http.HttpStatus;
import org.onap.holmes.common.exception.HttpException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private Map<String, Object> parameters = new HashMap();
private List<String> paths = new ArrayList();
+ public static JerseyClient newInstance() {
+ return new JerseyClient();
+ }
+
+ public static JerseyClient newInstance(long timeout) {
+ return new JerseyClient(timeout);
+ }
- public JerseyClient() {
+ private JerseyClient() {
this(DEFAULT_TIMEOUT);
}
- public JerseyClient(long timeout) {
+ private JerseyClient(long timeout) {
this.client = ClientBuilder.newBuilder()
.connectTimeout(timeout, TimeUnit.MILLISECONDS)
.readTimeout(timeout, TimeUnit.MILLISECONDS)
Response response = builder.get();
- if (isSuccessful(response)) {
+ if (isSuccessful(response, url)) {
return response2Target(response, clazz);
}
Response response = builder.post(entity);
- if (isSuccessful(response)) {
+ if (isSuccessful(response, url)) {
return response2Target(response, clazz);
}
Response response = builder.put(entity);
- if (isSuccessful(response)) {
+ if (isSuccessful(response, url)) {
return response2Target(response, clazz);
}
Response response = builder.delete();
- if (isSuccessful(response)) {
+ if (isSuccessful(response, url)) {
return response2Target(response, clazz);
}
return null;
}
- private boolean isSuccessful(Response response) {
- int status = response.getStatus();
- if (!HttpStatus.isSuccess(status)) {
- throw new HttpException(status, String.format("Failed to get response from the server. Info: %s",
- response.readEntity(String.class)));
+ private boolean isSuccessful(Response response, String url) {
+ Response.StatusType statusInfo = response.getStatusInfo();
+ if (statusInfo.getFamily() != Response.Status.Family.SUCCESSFUL) {
+ logger.error(
+ String.format("Failed to get response from the server <%d>. " +
+ "\nURL: %s\nCause: %s\nResponse body: %s",
+ statusInfo.getStatusCode(),
+ url,
+ statusInfo.getReasonPhrase(),
+ response.readEntity(String.class)));
+ return false;
}
return true;
}