com.linkage.utils.wsdl.support.UrlWsdlLoader.java Source code

Java tutorial

Introduction

Here is the source code for com.linkage.utils.wsdl.support.UrlWsdlLoader.java

Source

/*
 *  soapUI, copyright (C) 2004-2010 eviware.com 
 *
 *  soapUI is free software; you can redistribute it and/or modify it under the 
 *  terms of version 2.1 of the GNU Lesser General Public License as published by 
 *  the Free Software Foundation.
 *
 *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
 *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *  See the GNU Lesser General Public License for more details at gnu.org.
 */

package com.linkage.utils.wsdl.support;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.NTCredentials;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScheme;
import org.apache.commons.httpclient.auth.CredentialsNotAvailableException;
import org.apache.commons.httpclient.auth.CredentialsProvider;
import org.apache.commons.httpclient.auth.NTLMScheme;
import org.apache.commons.httpclient.auth.RFC2617Scheme;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.log4j.Logger;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;

import com.linkage.utils.wsdl.impl.wsdl.support.xsd.SchemaUtils;

/**
 * WsdlLoader for URLs
 * 
 * @author ole.matzura,zhanght5@asiainfo-linkage.com
 */

public class UrlWsdlLoader implements Loader {

    private final static Logger log = Logger.getLogger(UrlWsdlLoader.class);

    private Map<String, XmlObject> wsdlcache = new HashMap<String, XmlObject>();//url:definition

    protected GetMethod getMethod;

    /**
     * function   building XmlObject
     */
    public XmlObject load(String url, XmlOptions option) throws XmlException, IOException, Exception {
        XmlObject object = null;
        if (!PathUtils.isHttpPath(url)) {
            try {
                File file = new File(url.replace('/', File.separatorChar));
                if (file.exists())
                    url = file.toURI().toURL().toString();
            } catch (Exception e) {
            }
        }

        if (wsdlcache.containsKey(url)) {
            return wsdlcache.get(url);
        }

        if (url.startsWith("file:")) {
            object = load(handleFile(url), option);
            wsdlcache.put(url, object);
            return object;
        } else if (url.startsWith("http:")) {
            log.debug("Getting wsdl component from [" + url + "]");
            //
            HttpClient client = new HttpClient();
            client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
            getMethod = new GetMethod(url);
            client.executeMethod(getMethod);
            byte[] content = getMethod.getResponseBody();
            getMethod.releaseConnection();
            object = load(new ByteArrayInputStream(content), option);
            wsdlcache.put(url, object);
            return object;
        } else {
            return object;
        }
    }

    public XmlObject load(InputStream in, XmlOptions option) throws XmlException, IOException, Exception {
        if (option == null) {
            option = new XmlOptions().setLoadLineNumbers();
        }
        return XmlObject.Factory.parse(in, option);
    }

    protected InputStream handleFile(String url) throws IOException {
        return new URL(url).openStream();
    }

}