/** 
 * 
 * Copyright 2004 Protique Ltd
 * 
 * Licensed 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.activemq.ws.addressing;

import javanet.staxutils.XMLStreamEventReader;
import javanet.staxutils.XMLStreamUtils;
import javanet.staxutils.helpers.ListEventReader;
import org.activemq.ws.xmlbeans.addressing.v2003_03.EndpointReferenceType;
import org.codehaus.activesoap.MessageExchange;
import org.codehaus.activesoap.handler.HandlerLifecycle;
import org.codehaus.activesoap.util.XMLEventStreamReader;
import org.codehaus.activesoap.util.XMLStreamBuffer;
import org.codehaus.activesoap.util.XMLStreamHelper;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlSaxHandler;
import org.xml.sax.ContentHandler;

import javax.xml.namespace.QName;
import java.util.List;

/**
 * @version $Revision: 1.1 $
 */
public class AddressingHandler implements HandlerLifecycle {
    protected static final String KEY = AddressingHandler.class.getName();

    private static final QName WS_ADDRESSING_QNAME = EndpointReferenceType.type.getName();

    public void invoke(MessageExchange messageExchange) throws Exception {
        XmlSaxHandler buffer = (XmlSaxHandler) messageExchange.getProperty(KEY);
        if (buffer == null) {
            buffer = XmlObject.Factory.newXmlSaxHandler();
            messageExchange.setProperty(KEY, buffer);

            // lets write a start element for the WS-Addressing element
            ContentHandler contentHandler = buffer.getContentHandler();
            contentHandler.startDocument();
            XMLStreamHelper.writeStartElement(WS_ADDRESSING_QNAME, contentHandler);
        }

        XMLStreamHelper.copy(messageExchange.getIn(), buffer.getContentHandler());
    }

    public void onComplete(MessageExchange messageExchange) throws Exception {
        XmlSaxHandler buffer = (XmlSaxHandler) messageExchange.getProperty(KEY);
        if (buffer == null) {
            throw new IllegalStateException("Cannot issue complete lifecycle when the handler was never actually invoked - no buffer avialable!");
        }
        ContentHandler contentHandler = buffer.getContentHandler();
        XMLStreamHelper.writeEndElement(WS_ADDRESSING_QNAME, contentHandler);
        contentHandler.endDocument();

        XmlObject object = buffer.getObject();
        if (object instanceof EndpointReferenceType) {
            EndpointReferenceType endpointReference = (EndpointReferenceType) object;
            System.out.println("Parsed endpoint reference: " + endpointReference);
            AddressingContext.setCurrentContext(null);
        }
        else {
            AddressingContext.setCurrentContext(null);
            System.out.println("Parsed object: " + object);
            System.out.println("Type: " + object.getClass());
            throw new IllegalStateException("Parsed XMLBean is not an EndpointReferenceType: " + object);
        }
    }
}
