Package com.globant.google.mendoza.malbec

Source Code of com.globant.google.mendoza.malbec.MalbecHttpErrorCodeException

/* vim: set ts=2 et sw=2 cindent fo=qroca: */
package com.globant.google.mendoza.malbec;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.globant.google.mendoza.malbec.schema._2.ErrorResponse;

/**
* Exception created when an error code is recieved from the server through
* http.
*/
public class MalbecHttpErrorCodeException extends RuntimeException {

  /** The serial version id to serialize the class.
   */
  private static final long serialVersionUID = 5679804334708415373L;

  /** The class logger.
   */
  private static Log log = LogFactory.getLog(
      MalbecHttpErrorCodeException.class);

  /**
   * The error message obtained in the response from Malbec.
   */
  private String responseMessage = null;

  /**
   * The error code number.
   */
  private int responseCode = 0;

  /**
   * Creates a {@link MalbecHttpErrorCodeException}.
   * @param message The message for the exception.
   * @param errorCode The error code number.
   * @param response The response obtained for the http request.
   */
  public MalbecHttpErrorCodeException(final String message, final int errorCode,
      final String response) {
    super(message);
    responseCode = errorCode;
    processResponse(response);
  }

  /**
   * Creates a {@link MalbecHttpErrorCodeException}.
   * @param message The message for the exception.
   * @param errorCode The error code number.
   * @param response The response obtained for the http request.
   * @param cause A {@link Throwable} object that caused this exception.
   */
  public MalbecHttpErrorCodeException(final String message, final int errorCode,
      final String response, final Throwable cause) {
    super(message, cause);
    responseCode = errorCode;
    processResponse(response);
  }

  /**
   * Transforms the xml response into a jaxb object, and then store it's
   * contents.
   * @param response the xml response content.
   */
  private void processResponse(final String response) {
    try {
      ErrorResponse errorResponse =
        (ErrorResponse) CartUtils.unmarshal(response).getValue();
      this.responseMessage = errorResponse.getErrorMessage();
    } catch (Exception e) {
      // In case the message cannot be parsed, assign a default value to it.
      //log.warn("Could not parse message", e);
      log.warn("Could not parse message");
      log.warn("Using default value for response message");
      this.responseMessage = "The message could not be parsed.";
    }
  }

  /**
   * Returns the error response message obtained. Null means no message
   * obtained.
   * @return the error response message obtained.
   */
  public final String getResponseMessage() {
    return responseMessage;
  }

  /**
   * Returns the error response code. 0 means no error code obtained.
   * @return the error response code.
   */
  public final int getResponseCode() {
    return responseCode;
  }
}
TOP

Related Classes of com.globant.google.mendoza.malbec.MalbecHttpErrorCodeException

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.