package com.mv.hr.client;

import java.net.URI;

import javax.ws.rs.ProcessingException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;

import org.apache.log4j.Logger;

import com.mv.base.exception.ThirdPartyBadResponseException;
import com.mv.base.exception.ThirdPartyConnectivityFailureException;

public abstract class ActionPerformer {
	private static final Logger LOG = Logger.getLogger(ActionPerformer.class);

	protected URI urlPath;
	protected Builder request;

	public ActionPerformer(Client webClient, UriBuilder uriBuilder, Object... uriParams) {
		this.urlPath = uriBuilder.build(uriParams);
		this.request = getRequest(webClient);

	}

	private Builder getRequest(Client webClient) {
		WebTarget webResource = webClient.target(urlPath);
		return webResource.request(MediaType.APPLICATION_JSON_TYPE);
	}

	public <T> T getResponse(Class<T> resultClass) throws ThirdPartyBadResponseException, ThirdPartyConnectivityFailureException {
		Response response = this.act();
		assertResponseValidity(response);
		return parseResponse(response, resultClass);
	}

	protected abstract Response act() throws ThirdPartyConnectivityFailureException;

	private <T> T parseResponse(Response response, Class<T> resultClass) throws ThirdPartyBadResponseException {
		try {
			response.bufferEntity();
			return response.readEntity(resultClass);
		} catch (ProcessingException e) {
			String message = e.getMessage();

			Throwable cause = e.getCause();
			if (cause != null) {
				message += "; " + cause.getMessage();
			}

			String headers = response.getHeaders().toString();
			String body = response.readEntity(String.class);
			int status = response.getStatus();

			LOG.error("Failed parsing the response from " + urlPath + ", message: " + message);
			LOG.debug(urlPath);
			LOG.debug(status);
			LOG.debug(headers);
			LOG.debug(body);

			throw new ThirdPartyBadResponseException(message, response, e);
		}
	}

	private void assertResponseValidity(Response response) throws ThirdPartyBadResponseException {
		if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
			String responseString = response.readEntity(String.class);
			String errorMessage = urlPath + " returned unsuccessful response, status: " + response.getStatus();

			LOG.error(errorMessage + "; response: " + responseString);
			throw new ThirdPartyBadResponseException(errorMessage, response);
		}
	}
}

@@ -1695,151 +1695,403 @@
 %0A%09%09%09
-String headers = response.getHeaders().toString();%0A%09%09%09String body = response.readEntity(String.class);%0A%09%09%09int status = response.getStatus();%0A%0A%09
+logDebugInfo(response, message);%0A%0A%09%09%09throw new ThirdPartyBadResponseException(message, response, e);%0A%09%09%7D%0A%09%7D%0A%0A%09private void logDebugInfo(Response response, String message) %7B%0A%09%09String headers = response.getHeaders().toString();%0A%09%09int status = response.getStatus();%0A%0A%09%09String body;%0A%09%09try %7B%0A%09%09%09body = response.readEntity(String.class);%0A%09%09%7D catch (Exception e)%7B%0A%09%09%09body = %22Can't read the body%22;%0A%09%09%7D%0A%0A
 %09%09LO
@@ -2172,17 +2172,16 @@
 ssage);%0A
-%09
 %09%09LOG.de
@@ -2192,25 +2192,24 @@
 urlPath);%0A%09%09
-%09
 LOG.debug(st
@@ -2211,25 +2211,24 @@
 ug(status);%0A
-%09
 %09%09LOG.debug(
@@ -2237,17 +2237,16 @@
 aders);%0A
-%09
 %09%09LOG.de
@@ -2259,80 +2259,8 @@
 dy);
-%0A%0A%09%09%09throw new ThirdPartyBadResponseException(message, response, e);%0A%09%09%7D
 %0A%09%7D%0A

Fix logging breaking in some cases.