All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.whaleal.icefrog.http.webservice.SoapUtil Maven / Gradle / Ivy

The newest version!
package com.whaleal.icefrog.http.webservice;

import com.whaleal.icefrog.core.exceptions.UtilException;
import com.whaleal.icefrog.core.util.CharsetUtil;
import com.whaleal.icefrog.core.util.XmlUtil;

import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

/**
 * SOAP相关工具类
 *
 * @author Looly
 * @author wh
 * @since 1.0.0
 */
public class SoapUtil {

    /**
     * 创建SOAP客户端,默认使用soap1.1版本协议
     *
     * @param url WS的URL地址
     * @return {@link SoapClient}
     */
    public static SoapClient createClient( String url ) {
        return SoapClient.create(url);
    }

    /**
     * 创建SOAP客户端
     *
     * @param url      WS的URL地址
     * @param protocol 协议,见{@link SoapProtocol}
     * @return {@link SoapClient}
     */
    public static SoapClient createClient( String url, SoapProtocol protocol ) {
        return SoapClient.create(url, protocol);
    }

    /**
     * 创建SOAP客户端
     *
     * @param url          WS的URL地址
     * @param protocol     协议,见{@link SoapProtocol}
     * @param namespaceURI 方法上的命名空间URI
     * @return {@link SoapClient}
     * @since 1.0.0
     */
    public static SoapClient createClient( String url, SoapProtocol protocol, String namespaceURI ) {
        return SoapClient.create(url, protocol, namespaceURI);
    }

    /**
     * {@link SOAPMessage} 转为字符串
     *
     * @param message SOAP消息对象
     * @param pretty  是否格式化
     * @return SOAP XML字符串
     */
    public static String toString( SOAPMessage message, boolean pretty ) {
        return toString(message, pretty, CharsetUtil.CHARSET_UTF_8);
    }

    /**
     * {@link SOAPMessage} 转为字符串
     *
     * @param message SOAP消息对象
     * @param pretty  是否格式化
     * @param charset 编码
     * @return SOAP XML字符串
     * @since 1.0.0
     */
    public static String toString( SOAPMessage message, boolean pretty, Charset charset ) {
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            message.writeTo(out);
        } catch (SOAPException | IOException e) {
            throw new SoapRuntimeException(e);
        }
        String messageToString;
        try {
            messageToString = out.toString(charset.toString());
        } catch (UnsupportedEncodingException e) {
            throw new UtilException(e);
        }
        return pretty ? XmlUtil.format(messageToString) : messageToString;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy