Пример #1
0
  public void handleMessage(Message msg) {
    Fault fault = (Fault) msg.getContent(Exception.class);

    if (fault.getDetail() != null
        && !MessageUtils.getContextualBoolean(msg, DISABLE_FAULT_MAPPING, false)) {
      processFaultDetail(fault, msg);
      setStackTrace(fault, msg);
    }

    FaultMode faultMode = FaultMode.UNCHECKED_APPLICATION_FAULT;

    // Check if the raised exception is declared in the WSDL or by the JAX-RS resource
    Method m = msg.getExchange().get(Method.class);
    if (m != null) {
      Exception e = msg.getContent(Exception.class);
      for (Class<?> cl : m.getExceptionTypes()) {
        if (cl.isInstance(e)) {
          faultMode = FaultMode.CHECKED_APPLICATION_FAULT;
          break;
        }
      }
    }

    msg.getExchange().put(FaultMode.class, faultMode);
  }
Пример #2
0
 private void setStackTrace(Fault fault, Message msg) {
   Throwable cause = null;
   Map<String, String> ns = new HashMap<String, String>();
   XPathUtils xu = new XPathUtils(ns);
   ns.put("s", Fault.STACKTRACE_NAMESPACE);
   String ss =
       (String)
           xu.getValue(
               "//s:" + Fault.STACKTRACE + "/text()", fault.getDetail(), XPathConstants.STRING);
   List<StackTraceElement> stackTraceList = new ArrayList<StackTraceElement>();
   if (!StringUtils.isEmpty(ss)) {
     Iterator<String> linesIterator = Arrays.asList(CAUSE_SUFFIX_SPLITTER.split(ss)).iterator();
     while (linesIterator.hasNext()) {
       String oneLine = linesIterator.next();
       if (oneLine.startsWith("Caused by:")) {
         cause = getCause(linesIterator, oneLine);
         break;
       }
       stackTraceList.add(parseStackTrackLine(oneLine));
     }
     if (stackTraceList.size() > 0 || cause != null) {
       Exception e = msg.getContent(Exception.class);
       if (!stackTraceList.isEmpty()) {
         StackTraceElement[] stackTraceElement = new StackTraceElement[stackTraceList.size()];
         e.setStackTrace(stackTraceList.toArray(stackTraceElement));
       }
       if (cause != null) {
         e.initCause(cause);
       }
     }
   }
 }
Пример #3
0
  protected void processFaultDetail(Fault fault, Message msg) {
    Element exDetail = (Element) DOMUtils.getChild(fault.getDetail(), Node.ELEMENT_NODE);
    if (exDetail == null) {
      return;
    }
    QName qname = new QName(exDetail.getNamespaceURI(), exDetail.getLocalName());
    FaultInfo faultWanted = null;
    MessagePartInfo part = null;
    BindingOperationInfo boi = msg.getExchange().get(BindingOperationInfo.class);
    if (boi == null) {
      return;
    }
    if (boi.isUnwrapped()) {
      boi = boi.getWrappedOperation();
    }
    for (FaultInfo faultInfo : boi.getOperationInfo().getFaults()) {
      for (MessagePartInfo mpi : faultInfo.getMessageParts()) {
        if (qname.equals(mpi.getConcreteName())) {
          faultWanted = faultInfo;
          part = mpi;
          break;
        }
      }
      if (faultWanted != null) {
        break;
      }
    }
    if (faultWanted == null) {
      // did not find it using the proper qualified names, we'll try again with just the localpart
      for (FaultInfo faultInfo : boi.getOperationInfo().getFaults()) {
        for (MessagePartInfo mpi : faultInfo.getMessageParts()) {
          if (qname.getLocalPart().equals(mpi.getConcreteName().getLocalPart())) {
            faultWanted = faultInfo;
            part = mpi;
            break;
          }
        }
        if (faultWanted != null) {
          break;
        }
      }
    }
    if (faultWanted == null) {
      return;
    }
    Service s = msg.getExchange().get(Service.class);
    DataBinding dataBinding = s.getDataBinding();

    Object e = null;
    if (isDOMSupported(dataBinding)) {
      DataReader<Node> reader = this.getNodeDataReader(msg);
      reader.setProperty(DataReader.FAULT, fault);
      e = reader.read(part, exDetail);
    } else {
      DataReader<XMLStreamReader> reader = this.getDataReader(msg);
      XMLStreamReader xsr = new W3CDOMStreamReader(exDetail);
      try {
        xsr.nextTag();
      } catch (XMLStreamException e1) {
        throw new Fault(e1);
      }
      reader.setProperty(DataReader.FAULT, fault);
      e = reader.read(part, xsr);
    }

    if (!(e instanceof Exception)) {

      try {
        Class<?> exClass = faultWanted.getProperty(Class.class.getName(), Class.class);
        if (exClass == null) {
          return;
        }
        if (e == null) {
          Constructor<?> constructor = exClass.getConstructor(new Class[] {String.class});
          e = constructor.newInstance(new Object[] {fault.getMessage()});
        } else {

          try {
            Constructor<?> constructor = getConstructor(exClass, e);
            e = constructor.newInstance(new Object[] {fault.getMessage(), e});
          } catch (NoSuchMethodException e1) {
            // Use reflection to convert fault bean to exception
            e = convertFaultBean(exClass, e, fault);
          }
        }
        msg.setContent(Exception.class, e);
      } catch (Exception e1) {
        LogUtils.log(LOG, Level.INFO, "EXCEPTION_WHILE_CREATING_EXCEPTION", e1, e1.getMessage());
      }
    } else {
      if (fault.getMessage() != null) {
        Field f;
        try {
          f = Throwable.class.getDeclaredField("detailMessage");
          ReflectionUtil.setAccessible(f);
          f.set(e, fault.getMessage());
        } catch (Exception e1) {
          // ignore
        }
      }
      msg.setContent(Exception.class, e);
    }
  }