// Path: src/main/java/dk/clanie/bitcoin/client/response/BitcoindErrorResponse.java
// @SuppressWarnings("serial")
// @RooJavaBean(settersByDefault = false)
// public class BitcoindErrorResponse extends BitcoindJsonRpcResponse<Object> {
// 
// }
// 
// Path: src/main/java/dk/clanie/bitcoin/exception/BitcoinException.java
// @SuppressWarnings("serial")
// public class BitcoinException extends AbstractRuntimeException {
// 
// 	private BitcoindErrorResponse errorResponse = null;
// 
// 
// 	protected BitcoinException(BitcoindErrorResponse errorResponse) {
// 		super(errorResponse.getError().getMessage());
// 		this.errorResponse = errorResponse;
// 	}
// 
// 
// 	public BitcoinException(Exception cause) {
// 		super(cause);
// 	}
// 	
// 
// 	public BitcoinException(String message, Exception cause) {
// 		super(message, cause);
// 	}
// 	
// 
// 	/**
// 	 * Gets the whole response received from bitcoind.
// 	 * 
// 	 * @return {@link BitcoindErrorResponse} - may be null.
// 	 */
// 	public BitcoindErrorResponse getErrorResponse() {
// 		return errorResponse;
// 	}
// 
// 
// 	/**
// 	 * Gets the error code received from bitcoind.
// 	 * 
// 	 * @return errorcode or null if no error code was received.
// 	 */
// 	public Integer getErrorCode() {
// 		if (errorResponse == null || errorResponse.getError() == null) return null;
// 		return errorResponse.getError().getCode();
// 	}
// 
// 
// 	@Override
// 	public String toString() {
// 		return ToStringBuilder.reflectionToString(this);
// 	}
// 
// 
// }
// 
// Path: src/main/java/dk/clanie/bitcoin/exception/client/BitcoinClientException.java
// @SuppressWarnings("serial")
// public class BitcoinClientException extends BitcoinException {
// 
// 	public BitcoinClientException(BitcoindErrorResponse errorResponse) {
// 		super(errorResponse);
// 	}
// 
// }
// 
// Path: src/main/java/dk/clanie/bitcoin/exception/client/MethodNotFoundException.java
// @SuppressWarnings("serial")
// public class MethodNotFoundException extends BitcoinClientException {
// 
// 
// 	public MethodNotFoundException(BitcoindErrorResponse errorResponse) {
// 		super(errorResponse);
// 	}
// 
// 	
// }
// 
// Path: src/main/java/dk/clanie/bitcoin/exception/server/BitcoinServerException.java
// @SuppressWarnings("serial")
// public class BitcoinServerException extends BitcoinException {
// 
// 	public BitcoinServerException(BitcoindErrorResponse errorResponse) {
// 		super(errorResponse);
// 	}
// 
// }
// 
// Path: src/main/java/dk/clanie/bitcoin/exception/server/InvalidAddressException.java
// @SuppressWarnings("serial")
// public class InvalidAddressException extends BitcoinServerException {
// 
// 
// 	public InvalidAddressException(BitcoindErrorResponse errorResponse) {
// 		super(errorResponse);
// 	}
// 
// 	
// }
// 
// Path: src/main/java/dk/clanie/bitcoin/exception/server/WalletEncryptionException.java
// @SuppressWarnings("serial")
// public class WalletEncryptionException extends BitcoinServerException {
// 
// 
// 	public WalletEncryptionException(BitcoindErrorResponse errorResponse) {
// 		super(errorResponse);
// 	}
// 
// 	
// }


import dk.clanie.bitcoin.client.response.BitcoindErrorResponse;
import dk.clanie.bitcoin.exception.BitcoinException;
import dk.clanie.bitcoin.exception.client.BitcoinClientException;
import dk.clanie.bitcoin.exception.client.MethodNotFoundException;
import dk.clanie.bitcoin.exception.server.BitcoinServerException;
import dk.clanie.bitcoin.exception.server.InvalidAddressException;
import dk.clanie.bitcoin.exception.server.WalletEncryptionException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.UnknownHttpStatusCodeException;
import com.fasterxml.jackson.databind.ObjectMapper;
/*
 * Copyright (C) 2013, Claus Nielsen, cn@cn-consult.dk
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package dk.clanie.bitcoin.client;





/**
 * Handles error responses from bitcoind.
 * <p>
 * When bitcoind returns an client- or server-error response (an HTTP 4xx
 * or 5xx response) this handler will throw an BitcoinClient- or
 * BitcoinServerException including the response body as an {@link
 * BitcoindErrorResponse}.<br>
 * If the response body isn't valid JSON, or if parsing it fails for
 * any reason, an BitcoinException is thrown. It will indicate which
 * HTTP status code was received.
 * <p>
 * In case of other HTTP errors an BitcoinException is also thrown. It
 * will <b>not</b> include the response body, but it will include
 * whatever exception Spring's {@link DefaultResponseErrorHandler} would
 * have thrown as it's cause.
 * 
 * @author Claus Nielsen
 */
public class BitcoindJsonRpcErrorHandler extends DefaultResponseErrorHandler {

	private static final ObjectMapper objectMapper = new ObjectMapper();

	public void handleError(ClientHttpResponse response) throws IOException {
		HttpStatus statusCode = getHttpStatusCode(response);
		switch (statusCode.series()) {
		case SERVER_ERROR: {
// Path: src/main/java/dk/clanie/bitcoin/client/response/BitcoindErrorResponse.java
// @SuppressWarnings("serial")
// @RooJavaBean(settersByDefault = false)
// public class BitcoindErrorResponse extends BitcoindJsonRpcResponse<Object> {
// 
// }
// 
// Path: src/main/java/dk/clanie/bitcoin/exception/BitcoinException.java
// @SuppressWarnings("serial")
// public class BitcoinException extends AbstractRuntimeException {
// 
// 	private BitcoindErrorResponse errorResponse = null;
// 
// 
// 	protected BitcoinException(BitcoindErrorResponse errorResponse) {
// 		super(errorResponse.getError().getMessage());
// 		this.errorResponse = errorResponse;
// 	}
// 
// 
// 	public BitcoinException(Exception cause) {
// 		super(cause);
// 	}
// 	
// 
// 	public BitcoinException(String message, Exception cause) {
// 		super(message, cause);
// 	}
// 	
// 
// 	/**
// 	 * Gets the whole response received from bitcoind.
// 	 * 
// 	 * @return {@link BitcoindErrorResponse} - may be null.
// 	 */
// 	public BitcoindErrorResponse getErrorResponse() {
// 		return errorResponse;
// 	}
// 
// 
// 	/**
// 	 * Gets the error code received from bitcoind.
// 	 * 
// 	 * @return errorcode or null if no error code was received.
// 	 */
// 	public Integer getErrorCode() {
// 		if (errorResponse == null || errorResponse.getError() == null) return null;
// 		return errorResponse.getError().getCode();
// 	}
// 
// 
// 	@Override
// 	public String toString() {
// 		return ToStringBuilder.reflectionToString(this);
// 	}
// 
// 
// }
// 
// Path: src/main/java/dk/clanie/bitcoin/exception/client/BitcoinClientException.java
// @SuppressWarnings("serial")
// public class BitcoinClientException extends BitcoinException {
// 
// 	public BitcoinClientException(BitcoindErrorResponse errorResponse) {
// 		super(errorResponse);
// 	}
// 
// }
// 
// Path: src/main/java/dk/clanie/bitcoin/exception/client/MethodNotFoundException.java
// @SuppressWarnings("serial")
// public class MethodNotFoundException extends BitcoinClientException {
// 
// 
// 	public MethodNotFoundException(BitcoindErrorResponse errorResponse) {
// 		super(errorResponse);
// 	}
// 
// 	
// }
// 
// Path: src/main/java/dk/clanie/bitcoin/exception/server/BitcoinServerException.java
// @SuppressWarnings("serial")
// public class BitcoinServerException extends BitcoinException {
// 
// 	public BitcoinServerException(BitcoindErrorResponse errorResponse) {
// 		super(errorResponse);
// 	}
// 
// }
// 
// Path: src/main/java/dk/clanie/bitcoin/exception/server/InvalidAddressException.java
// @SuppressWarnings("serial")
// public class InvalidAddressException extends BitcoinServerException {
// 
// 
// 	public InvalidAddressException(BitcoindErrorResponse errorResponse) {
// 		super(errorResponse);
// 	}
// 
// 	
// }
// 
// Path: src/main/java/dk/clanie/bitcoin/exception/server/WalletEncryptionException.java
// @SuppressWarnings("serial")
// public class WalletEncryptionException extends BitcoinServerException {
// 
// 
// 	public WalletEncryptionException(BitcoindErrorResponse errorResponse) {
// 		super(errorResponse);
// 	}
// 
// 	
// }


// Path: src/main/java/dk/clanie/bitcoin/client/BitcoindJsonRpcErrorHandler.java
import dk.clanie.bitcoin.client.response.BitcoindErrorResponse;
import dk.clanie.bitcoin.exception.BitcoinException;
import dk.clanie.bitcoin.exception.client.BitcoinClientException;
import dk.clanie.bitcoin.exception.client.MethodNotFoundException;
import dk.clanie.bitcoin.exception.server.BitcoinServerException;
import dk.clanie.bitcoin.exception.server.InvalidAddressException;
import dk.clanie.bitcoin.exception.server.WalletEncryptionException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.UnknownHttpStatusCodeException;
import com.fasterxml.jackson.databind.ObjectMapper;

/*
 * Copyright (C) 2013, Claus Nielsen, cn@cn-consult.dk
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package dk.clanie.bitcoin.client;





/**
 * Handles error responses from bitcoind.
 * <p>
 * When bitcoind returns an client- or server-error response (an HTTP 4xx
 * or 5xx response) this handler will throw an BitcoinClient- or
 * BitcoinServerException including the response body as an {@link
 * BitcoindErrorResponse}.<br>
 * If the response body isn't valid JSON, or if parsing it fails for
 * any reason, an BitcoinException is thrown. It will indicate which
 * HTTP status code was received.
 * <p>
 * In case of other HTTP errors an BitcoinException is also thrown. It
 * will <b>not</b> include the response body, but it will include
 * whatever exception Spring's {@link DefaultResponseErrorHandler} would
 * have thrown as it's cause.
 * 
 * @author Claus Nielsen
 */
public class BitcoindJsonRpcErrorHandler extends DefaultResponseErrorHandler {

	private static final ObjectMapper objectMapper = new ObjectMapper();

	public void handleError(ClientHttpResponse response) throws IOException {
		HttpStatus statusCode = getHttpStatusCode(response);
		switch (statusCode.series()) {
		case SERVER_ERROR: {
			BitcoindErrorResponse errorResponse = parseResponse(response, statusCode);