Package org.apache.yoko.bindings.corba.interceptors

Source Code of org.apache.yoko.bindings.corba.interceptors.CorbaFaultInInterceptor

/**
* 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.yoko.bindings.corba.interceptors;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import java.lang.reflect.Constructor;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.namespace.QName;

import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;

import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.interceptor.ClientFaultConverter;
import org.apache.cxf.jaxb.io.EventDataReader;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.apache.cxf.service.model.BindingOperationInfo;
import org.apache.cxf.service.model.FaultInfo;
import org.apache.cxf.service.model.OperationInfo;
import org.apache.cxf.service.model.ServiceInfo;
import org.apache.schemas.yoko.bindings.corba.OperationType;
import org.apache.schemas.yoko.bindings.corba.TypeMappingType;
import org.apache.yoko.bindings.corba.ContextUtils;
import org.apache.yoko.bindings.corba.CorbaBindingException;
import org.apache.yoko.bindings.corba.CorbaMessage;
import org.apache.yoko.bindings.corba.CorbaStaxObject;
import org.apache.yoko.bindings.corba.CorbaStreamable;
import org.apache.yoko.bindings.corba.CorbaTypeMap;

import org.apache.yoko.wsdl.CorbaConstants;

public class CorbaFaultInInterceptor extends AbstractPhaseInterceptor<Message> {
       
    private static final Logger LOG = LogUtils.getL7dLogger(CorbaFaultInInterceptor.class)
   
    private XMLOutputFactory xof;
    private XMLInputFactory xif;
    private CorbaStaxObject corbaStaxObject;

    public CorbaFaultInInterceptor() {
        super();
        setPhase(Phase.UNMARSHAL);
        addAfter(ClientFaultConverter.class.getName());
        corbaStaxObject = new CorbaStaxObject();     
    }   

    public void handleMessage(Message msg) {
        CorbaMessage message = (CorbaMessage)msg;
        try {
            CorbaStreamable exStreamable = message.getStreamableException();
            if (exStreamable != null) {
                EventDataReader reader = (EventDataReader) ContextUtils.getDataReader(message);

                BindingOperationInfo bopInfo = message.getExchange().get(BindingOperationInfo.class);
                OperationType opType = bopInfo.getExtensor(OperationType.class);
                OperationInfo opInfo = bopInfo.getOperationInfo();
               
                List<CorbaTypeMap> typeMaps = new ArrayList<CorbaTypeMap>();

                ServiceInfo service = message.getExchange().get(ServiceInfo.class);
                List<TypeMappingType> corbaTypes =
                    service.getDescription().getExtensors(TypeMappingType.class);
                if (corbaTypes != null) {
                    corbaStaxObject.setTypeMaps(typeMaps);
                    corbaStaxObject.setServiceInfo(service);
                }

                org.omg.CORBA.ORB orb = (org.omg.CORBA.ORB) message.get(CorbaConstants.ORB);
                if (orb == null) {
                    orb = (org.omg.CORBA.ORB) message.getExchange().get(org.omg.CORBA.ORB.class);
                }
                corbaStaxObject.setOrb(orb);
                QName elName = new QName("", exStreamable.getName());

                XMLInputFactory inputFactory = getXMLInputFactory();
                XMLOutputFactory outputFactory = getXMLOutputFactory();
               
                ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                XMLEventWriter evtWriter = outputFactory.createXMLEventWriter(outStream);
                corbaStaxObject.writeObjectToStax(exStreamable.getObject(),
                                                  evtWriter,
                                                  XMLEventFactory.newInstance());
                evtWriter.flush();
                ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
                XMLEventReader evtReader = inputFactory.createXMLEventReader(inStream);
                FaultInfo fault = getFaultInfo(opInfo, elName);
                Object e = reader.read(fault.getMessageParts().get(0), evtReader);
                if (!(e instanceof Exception)) {
                    Class exClass = fault.getProperty(Class.class.getName(), Class.class);
                    Class beanClass = e.getClass();
                    Constructor constructor =
                        exClass.getConstructor(new Class[]{String.class, beanClass});
                    e = constructor.newInstance(new Object[]{"", e});
                }
                message.setContent(Exception.class, (Exception) e);
            }
        } catch (java.lang.Exception ex) {
            LOG.log(Level.SEVERE, "CORBA unmarshalFault exception", ex);
            throw new CorbaBindingException("CORBA unmarshalFault exception", ex);
        }

    }

    protected FaultInfo getFaultInfo(OperationInfo opInfo, QName faultName) {
        Iterator<FaultInfo> faults = opInfo.getFaults().iterator();
        while (faults.hasNext()) {
            FaultInfo fault = faults.next();
            if (fault.getFaultName().getLocalPart().equals(faultName.getLocalPart())) {
                return fault;
            }
        }
        return null;
    }

    protected XMLOutputFactory getXMLOutputFactory() {
        if (xof == null) {
            xof = XMLOutputFactory.newInstance();
            xof.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
        }
        return xof;
    }

    protected XMLInputFactory getXMLInputFactory() {
        if (xif == null) {
            xif = XMLInputFactory.newInstance();           
        }
        return xif;
    }       
}
TOP

Related Classes of org.apache.yoko.bindings.corba.interceptors.CorbaFaultInInterceptor

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.