Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 
 package org.apache.cxf.binding.corba.interceptors;
 
 import java.util.List;
 
 
 
 
 
     private static final Logger LOG = LogUtils.getL7dLogger(CorbaStreamFaultOutInterceptor.class);
     private ORB orb;
 
     public CorbaStreamFaultOutInterceptor() {
         super(.);
     }
 
     public void handleMessage(Message msg) {
         CorbaMessage message = (CorbaMessagemsg;
         Exchange exchange = message.getExchange();
         CorbaDestination destination;
         Fault faultEx = null;
         if (message.getDestination() != null) {
             destination = (CorbaDestination)message.getDestination();
         } else {
             destination = (CorbaDestination)exchange.getDestination();
         }
 
          = (ORBmessage.get(.);
         if ( == null) {
              = exchange.get(ORB.class); 
         }
         
         DataWriter<XMLStreamWriterwriter = getDataWriter(message);
 
         Throwable ex = message.getContent(Exception.class);
         // JCGS. If the cause is not available I can only continue if the exception 
         //       is a Fault instance and contains a detail object.
         if (ex.getCause() == null) {
            if ((ex instanceof Fault) && (((Fault)ex).getDetail() != null)) {
                faultEx = (Faultex;
            } else {
                throw new CorbaBindingException(ex);
            }
        } else {
            ex = ex.getCause();
        }
       
        if (ex instanceof InvocationTargetException) {
            ex = ex.getCause();
        }
        if (ex instanceof SystemException) {
            setSystemException(messageexdestination);
            return;
        }
        
        String exClassName = null;
        if (faultEx == null) {
            //REVISIT, we should not have to depend on WebFault annotation
            //Try changing the fault name to the proper mangled java exception classname.
            WebFault fault = ex.getClass().getAnnotation(WebFault.class);
            if (fault == null) {
                throw new CorbaBindingException(ex);
            }
            exClassName = fault.name();
        }  else {
            //JCGS: exClassName to be set to the exception name
            Element faultElement = (ElementfaultEx.getDetail().getFirstChild();
            exClassName = faultElement.getLocalName();
        }
        // Get information about the operation being invoked from the WSDL
        // definition.
        // We need this to marshal data correctly
        BindingInfo bInfo = destination.getBindingInfo();     
        
        String opName = message.getExchange().get(String.class);
                
        Iterator<BindingOperationInfoiter = bInfo.getOperations().iterator();
        BindingOperationInfo bopInfo = null;
        OperationType opType = null;           
        while (iter.hasNext()) {
            bopInfo = iter.next();
            if (bopInfo.getName().getLocalPart().equals(opName)) {
                opType = bopInfo.getExtensor(OperationType.class);
                break;
            }
        }
        if (opType == null) {
            throw new CorbaBindingException("Unable to find binding operation for " + opName);
        }
        OperationInfo opInfo = bopInfo.getOperationInfo();
        
        if (faultEx != null) {
            MessagePartInfo partInfo = getFaultMessagePartInfo(opInfonew QName(""exClassName));
            if (partInfo != null) {
                exClassName = partInfo.getTypeQName().getLocalPart();
            }
            
        }
        RaisesType exType = getRaisesType(opTypeexClassNameex);
        try {
            if (exType != null) {
                if (faultEx != null) {
                    setUserExceptionFromFaultDetail(message
                            faultEx.getDetail(), 
                            exTypeopInfowriter,
                            exchange.get(ServiceInfo.class));
                } else {
                    setUserException(messageexexTypeopInfowriter,
                                    exchange.get(ServiceInfo.class));
                }
            } else {
                throw new CorbaBindingException(ex);
            }
        } catch (Exception exp) {
            throw new CorbaBindingException(exp);
        }
    }
    protected RaisesType getRaisesType(OperationType opTypeString exClassNameThrowable ex) {
        RaisesType result = null;
        List<RaisesTypeexList = opType.getRaises();
        result = findRaisesType(exListexClassName);
        if (result == null) {
            //REVISIT, need to find a better way to match the corba binding exception name with the wsdl one
            //if doc-literal, the part element name should be used, but for RPC, the message part name
            try {
                Method faultMethod = ex.getClass().getMethod("getFaultInfo");
                if (faultMethod != null) {
                    Class<?> faultClass = faultMethod.getReturnType();
                    XmlType exType = faultClass.getAnnotation(XmlType.class);
                    exClassName = exType.name();
                    result = findRaisesType(exListexClassName);
                }
            } catch (Exception exp) {
                //Ignore it
            }
        }
        return result;
    }
    protected RaisesType findRaisesType(List<RaisesTypeexListString exClassName) {
        RaisesType result = null;
        for (Iterator<RaisesTypei = exList.iterator(); i.hasNext();) {
            RaisesType raises = i.next();
            if (raises.getException().getLocalPart().equals(exClassName)) {
                result = raises;
                break;
            }
        }
        return result;
    }
    protected void setSystemException(CorbaMessage message,
                                      Throwable ex,
                                      CorbaDestination dest) {
        SystemException sysEx = (SystemException)ex;
        message.setSystemException(sysEx);
        ServerRequest request  = message.getExchange().get(ServerRequest.class);
        Any exAny = dest.getOrbConfig().createSystemExceptionAny(sysEx);
        request.set_exception(exAny);
    }
    protected void setUserException(CorbaMessage message,
                                    Throwable ex,
                                    RaisesType exType,
                                    OperationInfo opInfo,
                                    DataWriter<XMLStreamWriterwriter,
                                    ServiceInfo service)
        throws Exception {
        QName exIdlType = exType.getException();
        QName elName = new QName(""exIdlType.getLocalPart());
        MessagePartInfo faultPart = getFaultMessagePartInfo(opInfoelName);
        if (faultPart == null) {
            throw new CorbaBindingException("Coulnd't find the message fault part : " + elName);
        }
        Method faultMethod = ex.getClass().getMethod("getFaultInfo");
        if (faultMethod == null) {
            return;
        }
        Object fault = faultMethod.invoke(ex);
        // This creates a default instance of the class representing the exception schema type if
        // one has not been created on the servant side which throws the UserException.
        if (fault == null) {
            Class<?> faultClass = faultMethod.getReturnType();
            fault = faultClass.newInstance();
        }
        
        CorbaFaultStreamWriter faultWriter = new CorbaFaultStreamWriter(exType
                message.getCorbaTypeMap(), service);
        writer.write(faultfaultPartfaultWriter);
        CorbaObjectHandler[] objs = faultWriter.getCorbaObjects();      
        CorbaStreamable streamable = message.createStreamableObject(objs[0], elName);
        message.setStreamableException(streamable);
    }
    protected void setUserExceptionFromFaultDetail(CorbaMessage message,
                                                   org.w3c.dom.Element faultDetail,
                                                   RaisesType exType,
                                                   OperationInfo opInfo,
                                                   DataWriter<XMLStreamWriterwriter,
                                                   ServiceInfo service)
        throws Exception {
        QName exIdlType = exType.getException();
        QName elName = new QName(""exIdlType.getLocalPart());
        MessagePartInfo faultPart = getFaultMessagePartInfo(opInfoelName);
        
        // faultDetailt.getFirstChild() skips the "detail" element
        Object fault = extractPartsInfoFromDetail((ElementfaultDetail.getFirstChild(), exType);
        
        CorbaFaultStreamWriter faultWriter = new CorbaFaultStreamWriter(exType
                                                message.getCorbaTypeMap(), service);
        writer.write(faultfaultPartfaultWriter);
        CorbaObjectHandler[] objs = faultWriter.getCorbaObjects();      
        CorbaStreamable streamable = message.createStreamableObject(objs[0], elName);
        message.setStreamableException(streamable);
    }
    
    private Object extractPartsInfoFromDetail(Element faultDetailRaisesType exType) {
        Document faultDoc = DOMUtils.createDocument();
        Element faultElement = faultDoc.createElement(exType.getException().getLocalPart());
        faultDoc.appendChild(faultElement);
        
        Node node = faultDetail.getFirstChild();
        while (node != null) {
            Node importedFaultData = faultDoc.importNode(nodetrue);
            faultElement.appendChild(importedFaultData);
            node = node.getNextSibling();     
        }
        
        if (.isLoggable(.)) {
            .fine("Exception DOM: " + XMLUtils.toString(faultElement));
        }
        return faultDoc;
    }
    
    protected DataWriter<XMLStreamWritergetDataWriter(CorbaMessage message) {
        Service serviceModel = ServiceModelUtil.getService(message.getExchange());
        DataWriter<XMLStreamWriterdataWriter = 
            serviceModel.getDataBinding().createWriter(XMLStreamWriter.class);
        if (dataWriter == null) {
            throw new CorbaBindingException("Couldn't create data writer for outgoing fault message");
        }
        return dataWriter;
    }
    protected MessagePartInfo getFaultMessagePartInfo(OperationInfo opInfoQName faultName) {
        Iterator<FaultInfofaults = opInfo.getFaults().iterator();
        while (faults.hasNext()) {
            FaultInfo fault = faults.next();
            MessagePartInfo partInfo = fault.getMessageParts().get(0);
            if (partInfo.isElement()
                && partInfo.getElementQName().getLocalPart().equals(faultName.getLocalPart())) {
                return partInfo;
            } else if (partInfo.getTypeQName().getLocalPart().equals(faultName.getLocalPart())) {
                return partInfo;
            }
        }
        return null;
    }
    
New to GrepCode? Check out our FAQ X