/*
 *  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.synapse;

import java.io.File;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.xml.stream.XMLStreamReader;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMDocument;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMXMLBuilderFactory;
import org.apache.axiom.om.OMXMLParserWrapper;
import org.apache.axiom.om.util.StAXUtils;
import org.apache.axiom.soap.SOAPBody;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.engine.AxisConfiguration;
import org.apache.synapse.config.Entry;
import org.apache.synapse.config.SynapseConfiguration;
import org.apache.synapse.core.SynapseEnvironment;
import org.apache.synapse.core.axis2.Axis2MessageContext;
import org.apache.synapse.core.axis2.Axis2SynapseEnvironment;

/**
 * Utility class to build test message contexts in a flexible way.
 */
public class TestMessageContextBuilder {
    private final Map<String,Entry> entries = new HashMap<String,Entry>();
    private boolean requireAxis2MessageContext;
    private String contentString;
    private URL contentURL;
    private boolean contentIsEnvelope;
    private boolean addTextAroundBody;
    
    public TestMessageContextBuilder setRequireAxis2MessageContext(boolean requireAxis2MessageContext) {
        this.requireAxis2MessageContext = requireAxis2MessageContext;
        return this;
    }

    public TestMessageContextBuilder setBodyFromString(String string) {
        this.contentString = string;
        contentIsEnvelope = false;
        return this;
    }

    private TestMessageContextBuilder setContentFromFile(String path, boolean contentIsEnvelope) {
        try {
            contentURL = new File(path).toURL();
        } catch (MalformedURLException ex) {
            throw new Error(ex);
        }
        this.contentIsEnvelope = contentIsEnvelope;
        return this;
    }
    
    public TestMessageContextBuilder setBodyFromFile(String path) {
        return setContentFromFile(path, false);
    }
    
    public TestMessageContextBuilder setEnvelopeFromFile(String path) {
        return setContentFromFile(path, true);
    }
    
    public TestMessageContextBuilder setEnvelope(URL url) {
        contentURL = url;
        contentIsEnvelope = true;
        return this;
    }
    
    public TestMessageContextBuilder addTextAroundBody() {
        addTextAroundBody = true;
        return this;
    }
    
    public TestMessageContextBuilder addEntry(String key, Entry entry) {
        entries.put(key, entry);
        return this;
    }
    
    public TestMessageContextBuilder addEntry(String key, URL url) {
        Entry entry = new Entry();
        entry.setType(Entry.URL_SRC);
        entry.setSrc(url);
        entries.put(key, entry);
        return this;
    }
    
    public TestMessageContextBuilder addFileEntry(String key, String path) throws MalformedURLException {
        return addEntry(key, new File(path).toURL());
    }
    
    /**
     * Build the test message context.
     * This method returns a new (and independent) instance on every invocation.
     * 
     * @return
     * @throws Exception
     */
    public MessageContext build() throws Exception {
        SynapseConfiguration testConfig = new SynapseConfiguration();
        // TODO: check whether we need a SynapseEnvironment in all cases
        SynapseEnvironment synEnv
            = new Axis2SynapseEnvironment(new ConfigurationContext(new AxisConfiguration()),
                                          testConfig);
        MessageContext synCtx;
        if (requireAxis2MessageContext) {
            synCtx = new Axis2MessageContext(new org.apache.axis2.context.MessageContext(),
                                             testConfig, synEnv);
        } else {
            synCtx = new TestMessageContext();
            synCtx.setEnvironment(synEnv);
            synCtx.setConfiguration(testConfig);
        }

        for (Map.Entry<String,Entry> mapEntry : entries.entrySet()) {
            testConfig.addEntry(mapEntry.getKey(), mapEntry.getValue());
        }

        XMLStreamReader parser = null;
        if (contentString != null) {
            parser = StAXUtils.createXMLStreamReader(new StringReader(contentString));
        } else if (contentURL != null) {
            parser = StAXUtils.createXMLStreamReader(contentURL.openStream());
        }
        
        SOAPEnvelope envelope;
        if (parser != null) {
            if (contentIsEnvelope) {
                envelope = OMXMLBuilderFactory.createStAXSOAPModelBuilder(parser).getSOAPEnvelope();
            } else {
                envelope = OMAbstractFactory.getSOAP11Factory().getDefaultEnvelope();
                
                // TODO: don't know why this is here, but without it some unit tests fail...
                OMDocument omDoc = OMAbstractFactory.getSOAP11Factory().createOMDocument();
                omDoc.addChild(envelope);
                
                SOAPBody body = envelope.getBody();
                OMXMLParserWrapper builder = OMXMLBuilderFactory.createStAXOMBuilder(parser);
                OMElement bodyElement = builder.getDocumentElement();
                if (addTextAroundBody) {
                    OMFactory fac = OMAbstractFactory.getOMFactory();
                    body.addChild(fac.createOMText("\n"));
                    body.addChild(bodyElement);
                    body.addChild(fac.createOMText("\n"));
                } else {
                    body.addChild(bodyElement);
                }
            }
        } else {
            envelope = OMAbstractFactory.getSOAP11Factory().getDefaultEnvelope();
        }

        synCtx.setEnvelope(envelope);
        return synCtx;
    }
}
