All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xins.client.UnsuccessfulXINSCallException Maven / Gradle / Ivy

The newest version!
/*
 * $Id: UnsuccessfulXINSCallException.java,v 1.38 2012/02/28 18:10:54 agoubard Exp $
 *
 * See the COPYRIGHT file for redistribution and use restrictions.
 */
package org.xins.client;

import java.util.Map;

import org.xins.common.MandatoryArgumentChecker;
import org.xins.common.service.TargetDescriptor;
import org.xins.common.spec.ErrorCodeSpec;
import org.w3c.dom.Element;

/**
 * Exception that indicates that a result code was returned by the API call.
 *
 * @version $Revision: 1.38 $ $Date: 2012/02/28 18:10:54 $
 * @author Ernst de Haan
 *
 * @since XINS 1.0.0
 */
public class UnsuccessfulXINSCallException
extends XINSCallException
implements XINSCallResultData {

   /**
    * The result data. The value of this field cannot be null.
    */
   private final XINSCallResultData _result;

   /**
    * The type of the error.
    */
   private ErrorCodeSpec.Type _type;

   /**
    * Constructs a new UnsuccessfulXINSCallException based on a
    * XINSCallResultData instance.
    *
    * @param request
    *    the original request, cannot be null.
    *
    * @param target
    *    descriptor for the target that was attempted to be called, cannot be
    *    null.
    *
    * @param duration
    *    the call duration in milliseconds, must be >= 0.
    *
    * @param resultData
    *    the result data, cannot be null.
    *
    * @param detail
    *    detail message, or null.
    *
    * @throws IllegalArgumentException
    *    if request     == null
    *          || target      == null
    *          || duration  < 0
    *          || resultData  == null
    *          || resultData.{@link XINSCallResult#getErrorCode() getErrorCode()} == null.
    */
   UnsuccessfulXINSCallException(XINSCallRequest    request,
                                 TargetDescriptor   target,
                                 long               duration,
                                 XINSCallResultData resultData,
                                 String             detail)
   throws IllegalArgumentException {

      super("Unsuccessful XINS call result", request, target, duration,
            determineDetail(resultData, detail), null);

      // Check additional precondition
      MandatoryArgumentChecker.check("resultData", resultData);

      // Result object must be unsuccessful
      String errorCode = resultData.getErrorCode();
      if (errorCode == null) {
         throw new IllegalArgumentException("resultData.getErrorCode() == null");
      }

      // Store details
      _result = resultData;
   }

   /**
    * Delegate for the constructor that determines the detail message based on
    * a XINSCallResultData object and an optional detailed
    * description.
    *
    * @param result
    *    the {@link XINSCallResultData} instance, should not be
    *    null.
    *
    * @param detail
    *    detailed description to include, or null if unavailable.
    *
    * @return
    *    the detail message for the constructor to use, never
    *    null.
    *
    * @throws IllegalArgumentException
    *    if result == null
    *          || result.{@link XINSCallResultData#getErrorCode() getErrorCode()} == null.
    */
   private static final String determineDetail(XINSCallResultData result,
                                               String             detail)
   throws IllegalArgumentException {

      // Argument cannot be null
      MandatoryArgumentChecker.check("result", result);

      // Result must be unsuccessful
      String errorCode = result.getErrorCode();
      if (errorCode == null) {
         throw new IllegalArgumentException("result.getErrorCode() == null");
      }

      if (detail == null || detail.length() < 1) {
         return "Error code \"" + errorCode + "\".";
      } else {
         return "Error code \"" + errorCode + "\": " + detail;
      }
   }

   /**
    * Returns the error code.
    *
    * @return
    *    the error code, never null.
    */
   public final String getErrorCode() {
      return _result.getErrorCode();
   }

   /**
    * Gets all returned parameters.
    *
    * @return
    *    a {@link Map} containing all parameters, or
    *    null if there are none.
    */
   public final Map getParameters() {
      return _result.getParameters();
   }

   /**
    * Gets the value of the specified returned parameter.
    *
    * @param name
    *    the parameter name, not null.
    *
    * @return
    *    the value of the parameter, or null if there is no values.
    *
    * @throws IllegalArgumentException
    *    if name == null.
    */
   public final String getParameter(String name)
   throws IllegalArgumentException {
      Map p = getParameters();
      if (p == null) {
         return null;
      } else {
         return p.get(name);
      }
   }

   /**
    * Returns the optional extra data.
    *
    * @return
    *    the extra data as an {@link Element}, can be null;
    */
   public final Element getDataElement() {
      return _result.getDataElement();
   }

   /**
    * Sets the type of the error code.
    *
    * @param type
    *    the type of the error (functionnal or technical).
    */
   void setType(ErrorCodeSpec.Type type) {
      _type = type;
   }

   /**
    * Returns the type of the error code.
    *
    * @return
    *    the type as a {@link ErrorCodeSpec ErrorCodeSpec.Type}, can be null if it's unknown.
    *
    * @since XINS 1.4.0
    */
   public final ErrorCodeSpec.Type getType() {
      return _type;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy