< prev index next >

src/java.xml.ws/share/classes/com/sun/xml/internal/ws/fault/SOAPFaultBuilder.java

Print this page




  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.ws.fault;
  27 
  28 import com.sun.istack.internal.NotNull;
  29 import com.sun.istack.internal.Nullable;
  30 import com.sun.xml.internal.ws.api.SOAPVersion;
  31 import com.sun.xml.internal.ws.api.message.Message;
  32 import com.sun.xml.internal.ws.api.model.ExceptionType;
  33 import com.sun.xml.internal.ws.encoding.soap.SOAP12Constants;
  34 import com.sun.xml.internal.ws.encoding.soap.SOAPConstants;
  35 import com.sun.xml.internal.ws.encoding.soap.SerializationException;
  36 import com.sun.xml.internal.ws.message.jaxb.JAXBMessage;
  37 import com.sun.xml.internal.ws.message.FaultMessage;
  38 import com.sun.xml.internal.ws.model.CheckedExceptionImpl;
  39 import com.sun.xml.internal.ws.model.JavaMethodImpl;

  40 import com.sun.xml.internal.ws.spi.db.XMLBridge;
  41 import com.sun.xml.internal.ws.util.DOMUtil;
  42 import com.sun.xml.internal.ws.util.StringUtils;
  43 import org.w3c.dom.Document;
  44 import org.w3c.dom.Element;
  45 import org.w3c.dom.Node;
  46 
  47 import javax.xml.bind.JAXBContext;
  48 import javax.xml.bind.JAXBException;
  49 import javax.xml.bind.annotation.XmlTransient;
  50 import javax.xml.namespace.QName;
  51 import javax.xml.soap.SOAPFault;
  52 import javax.xml.soap.Detail;
  53 import javax.xml.soap.DetailEntry;
  54 import javax.xml.transform.dom.DOMResult;
  55 import javax.xml.ws.ProtocolException;
  56 import javax.xml.ws.WebServiceException;
  57 import javax.xml.ws.soap.SOAPFaultException;
  58 import java.lang.reflect.Constructor;
  59 import java.lang.reflect.Field;


 289                 return t;
 290             }
 291         }
 292 
 293         return t;
 294     }
 295 
 296     abstract protected Throwable getProtocolException();
 297 
 298     private Object getJAXBObject(Node jaxbBean, CheckedExceptionImpl ce) throws JAXBException {
 299         XMLBridge bridge = ce.getBond();
 300         return bridge.unmarshal(jaxbBean,null);
 301     }
 302 
 303     private Exception createUserDefinedException(CheckedExceptionImpl ce) {
 304         Class exceptionClass = ce.getExceptionClass();
 305         Class detailBean = ce.getDetailBean();
 306         try{
 307             Node detailNode = getDetail().getDetails().get(0);
 308             Object jaxbDetail = getJAXBObject(detailNode, ce);

 309             Constructor exConstructor;
 310             try{
 311                 exConstructor = exceptionClass.getConstructor(String.class, detailBean);
 312                 return (Exception) exConstructor.newInstance(getFaultString(), jaxbDetail);
 313             }catch(NoSuchMethodException e){
 314                 exConstructor = exceptionClass.getConstructor(String.class);
 315                 return (Exception) exConstructor.newInstance(getFaultString());
 316             }
 317         } catch (Exception e) {
 318             throw new WebServiceException(e);
 319         }
 320     }
 321 
 322     private static String getWriteMethod(Field f) {
 323         return "set" + StringUtils.capitalize(f.getName());
 324     }
 325 
 326     private static Object getFaultDetail(CheckedExceptionImpl ce, Throwable exception) {
 327         if (ce == null)
 328             return null;
 329         if (ce.getExceptionType().equals(ExceptionType.UserDefined)) {
 330             return createDetailFromUserDefinedException(ce, exception);
 331         }
 332         try {
 333             Method m = exception.getClass().getMethod("getFaultInfo");
 334             return m.invoke(exception);
 335         } catch (Exception e) {
 336             throw new SerializationException(e);
 337         }
 338     }
 339 
 340     private static Object createDetailFromUserDefinedException(CheckedExceptionImpl ce, Object exception) {
 341         Class detailBean = ce.getDetailBean();

 342         Field[] fields = detailBean.getDeclaredFields();
 343         try {
 344             Object detail = detailBean.newInstance();
 345             for (Field f : fields) {
 346                 Method em = exception.getClass().getMethod(getReadMethod(f));
 347                 try {
 348                     Method sm = detailBean.getMethod(getWriteMethod(f), em.getReturnType());
 349                     sm.invoke(detail, em.invoke(exception));
 350                 } catch(NoSuchMethodException ne) {
 351                     // Try to use exception bean's public field to populate the value.
 352                     Field sf = detailBean.getField(f.getName());
 353                     sf.set(detail, em.invoke(exception));
 354                 }
 355             }
 356             return detail;
 357         } catch (Exception e) {
 358             throw new SerializationException(e);
 359         }
 360     }
 361 




  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.ws.fault;
  27 
  28 import com.sun.istack.internal.NotNull;
  29 import com.sun.istack.internal.Nullable;
  30 import com.sun.xml.internal.ws.api.SOAPVersion;
  31 import com.sun.xml.internal.ws.api.message.Message;
  32 import com.sun.xml.internal.ws.api.model.ExceptionType;
  33 import com.sun.xml.internal.ws.encoding.soap.SOAP12Constants;
  34 import com.sun.xml.internal.ws.encoding.soap.SOAPConstants;
  35 import com.sun.xml.internal.ws.encoding.soap.SerializationException;
  36 import com.sun.xml.internal.ws.message.jaxb.JAXBMessage;
  37 import com.sun.xml.internal.ws.message.FaultMessage;
  38 import com.sun.xml.internal.ws.model.CheckedExceptionImpl;
  39 import com.sun.xml.internal.ws.model.JavaMethodImpl;
  40 import com.sun.xml.internal.ws.spi.db.WrapperComposite;
  41 import com.sun.xml.internal.ws.spi.db.XMLBridge;
  42 import com.sun.xml.internal.ws.util.DOMUtil;
  43 import com.sun.xml.internal.ws.util.StringUtils;
  44 import org.w3c.dom.Document;
  45 import org.w3c.dom.Element;
  46 import org.w3c.dom.Node;
  47 
  48 import javax.xml.bind.JAXBContext;
  49 import javax.xml.bind.JAXBException;
  50 import javax.xml.bind.annotation.XmlTransient;
  51 import javax.xml.namespace.QName;
  52 import javax.xml.soap.SOAPFault;
  53 import javax.xml.soap.Detail;
  54 import javax.xml.soap.DetailEntry;
  55 import javax.xml.transform.dom.DOMResult;
  56 import javax.xml.ws.ProtocolException;
  57 import javax.xml.ws.WebServiceException;
  58 import javax.xml.ws.soap.SOAPFaultException;
  59 import java.lang.reflect.Constructor;
  60 import java.lang.reflect.Field;


 290                 return t;
 291             }
 292         }
 293 
 294         return t;
 295     }
 296 
 297     abstract protected Throwable getProtocolException();
 298 
 299     private Object getJAXBObject(Node jaxbBean, CheckedExceptionImpl ce) throws JAXBException {
 300         XMLBridge bridge = ce.getBond();
 301         return bridge.unmarshal(jaxbBean,null);
 302     }
 303 
 304     private Exception createUserDefinedException(CheckedExceptionImpl ce) {
 305         Class exceptionClass = ce.getExceptionClass();
 306         Class detailBean = ce.getDetailBean();
 307         try{
 308             Node detailNode = getDetail().getDetails().get(0);
 309             Object jaxbDetail = getJAXBObject(detailNode, ce);
 310             if (jaxbDetail instanceof Exception) return (Exception)jaxbDetail;
 311             Constructor exConstructor;
 312             try{
 313                 exConstructor = exceptionClass.getConstructor(String.class, detailBean);
 314                 return (Exception) exConstructor.newInstance(getFaultString(), jaxbDetail);
 315             }catch(NoSuchMethodException e){
 316                 exConstructor = exceptionClass.getConstructor(String.class);
 317                 return (Exception) exConstructor.newInstance(getFaultString());
 318             }
 319         } catch (Exception e) {
 320             throw new WebServiceException(e);
 321         }
 322     }
 323 
 324     private static String getWriteMethod(Field f) {
 325         return "set" + StringUtils.capitalize(f.getName());
 326     }
 327 
 328     private static Object getFaultDetail(CheckedExceptionImpl ce, Throwable exception) {
 329         if (ce == null)
 330             return null;
 331         if (ce.getExceptionType().equals(ExceptionType.UserDefined)) {
 332             return createDetailFromUserDefinedException(ce, exception);
 333         }
 334         try {
 335             return ce.getFaultInfoGetter().invoke(exception);

 336         } catch (Exception e) {
 337             throw new SerializationException(e);
 338         }
 339     }
 340 
 341     private static Object createDetailFromUserDefinedException(CheckedExceptionImpl ce, Object exception) {
 342         Class detailBean = ce.getDetailBean();
 343         if (ce.getExceptionClass().equals(detailBean)) return exception;
 344         Field[] fields = detailBean.getDeclaredFields();
 345         try {
 346             Object detail = detailBean.newInstance();
 347             for (Field f : fields) {
 348                 Method em = exception.getClass().getMethod(getReadMethod(f));
 349                 try {
 350                     Method sm = detailBean.getMethod(getWriteMethod(f), em.getReturnType());
 351                     sm.invoke(detail, em.invoke(exception));
 352                 } catch(NoSuchMethodException ne) {
 353                     // Try to use exception bean's public field to populate the value.
 354                     Field sf = detailBean.getField(f.getName());
 355                     sf.set(detail, em.invoke(exception));
 356                 }
 357             }
 358             return detail;
 359         } catch (Exception e) {
 360             throw new SerializationException(e);
 361         }
 362     }
 363 


< prev index next >