Blame view

src/com/sitech/ismp/coll/tivoli/db2/SoapClient.java 6.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
package com.sitech.ismp.coll.tivoli.db2;


import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * 该类为一个工具类
 * 负责将soap内容发给soap server
 * 得到返回结果
 * @author winnerbao
 *
 */

public class SoapClient {


	private String SOAPUrl;

	public SoapClient(String SOAPUrl){
		this.SOAPUrl=SOAPUrl;

	}
	
	/**
	 * 功能:按指定编码格式返回字符串
	 * SoapReq is soap request
	 * strCharSet 是编码格式 例如 GBK、GB2312、UTF-8等.
	 * @param strSoapReq
	 * @param strCharSet
	 * @return
	 * @throws java.io.IOException
	 */
	public String getSoapStringResponse(String strSoapReq,String strCharSet)throws IOException{
		byte[] b = strSoapReq.getBytes();
		InputStream ins = this.getSoapResponse(b);
		
		String result = this.stream2String(ins,strCharSet);
        ins.close();
        return result;
	}//:~ getSoapStringResponse
	
	public String getSoapStringResponse(String strSoapReq)throws IOException{
		byte[] b = strSoapReq.getBytes();
		InputStream ins = this.getSoapResponse(b);
		
		String result = this.stream2String(ins);
        
        ins.close();
        
        return result;
	}//:~ getSoapStringResponse
	
	
	private InputStream getSoapSteamResponse(String strSoapReq)throws IOException	{
		byte[] b = strSoapReq.getBytes();
		return this.getSoapResponse(b);
	}//:~ end getSoapSteamResponse
		
	/**
	 *inStreamSoapReq
	 */
	private String getSoapStringResponse(InputStream inStreamSoapReq) throws IOException	{
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        // Copy the SOAP req stream to the open connection.
        copy(inStreamSoapReq,bout);
        byte[] b = bout.toByteArray();
        
		InputStream ins = this.getSoapResponse(b);
		
		String reult = this.stream2String(ins);
        
        ins.close();
        
        return reult;
	}//:~ end getSoapStringResponse
	
	/**
	 *inStreamSoapReq
	 */
	private InputStream getSoapStreamResponse(InputStream inStreamSoapReq) throws IOException	{
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        // Copy the SOAP req stream to the open connection.
        copy(inStreamSoapReq,bout);
        byte[] b = bout.toByteArray();
        
		return this.getSoapResponse(b);
	}//:~ 	getSoapStreamResponse
	
	/**
	 * 功能:按照指定的编码格式转换成字符串
	 * @param inStream
	 * @param strCharSet
	 * @return
	 * @throws java.io.IOException
	 */
	private String stream2String(InputStream inStream,String strCharSet)throws IOException{
        InputStreamReader isr =   new InputStreamReader(inStream,strCharSet);
        BufferedReader in = new BufferedReader(isr);

        String inputLine;

		StringBuffer buffer = new StringBuffer();
        while ((inputLine = in.readLine()) != null)
        {
            //System.out.println(inputLine);
            buffer.append(inputLine);
            buffer.append('\n');
		}
		
        return buffer.toString();	
	}//:~ end stream2String
	
	private String stream2String(InputStream inStream)throws IOException{
        InputStreamReader isr =   new InputStreamReader(inStream);
        BufferedReader in = new BufferedReader(isr);

        String inputLine;

		StringBuffer buffer = new StringBuffer();
        while ((inputLine = in.readLine()) != null)
        {
            //System.out.println(inputLine);
            buffer.append(inputLine);
            buffer.append('\n');
		}
		
        return buffer.toString();	
	}//:~ end stream2String

	private InputStream getSoapResponse(byte[] bytesSoapReq)throws IOException
	{
		String SOAPAction="";
        // Create the connection where we're going to send the file.
        URL url = new URL(SOAPUrl);
        URLConnection connection = url.openConnection();
        HttpURLConnection httpConn = (HttpURLConnection) connection;	

        // Set the appropriate HTTP parameters.
        httpConn.setRequestProperty( "Content-Length",
                                     String.valueOf( bytesSoapReq.length ) );
        httpConn.setRequestProperty("Content-Type","text/xml; charset=utf-8");
		httpConn.setRequestProperty("SOAPAction",SOAPAction);
        httpConn.setRequestMethod( "POST" );
        httpConn.setDoOutput(true);
        httpConn.setDoInput(true);

        // Everything's set up; send the XML that was read in to b.
        OutputStream out = httpConn.getOutputStream();
        out.write( bytesSoapReq );    
        out.close();

        // Read the response and write it to standard out.

        InputStream is = httpConn.getInputStream();
		
        //httpConn.disconnect();
        
		return is;
	}//:~ end getSoapResponse

  // copy method from From E.R. Harold's book "Java I/O"
  public void copy(InputStream in, OutputStream out) 
   throws IOException {

    // do not allow other threads to read from the
    // input or write to the output while copying is
    // taking place

    synchronized (in) {
      synchronized (out) {

        byte[] buffer = new byte[256];
        while (true) {
          int bytesRead = in.read(buffer);
          if (bytesRead == -1) break;
          out.write(buffer, 0, bytesRead);
        }
      }
    }
  }//:~ end copy
  
  public static void main(String[] args)
  {
  	/*String URL="http://172.16.9.98:1920///cms/soap";
  	String req="<CT_Get><userid>sysadmin</userid><password></password><object>ManagedSystem</object><target>ManagedSystemName</target></CT_Get>";
  	SoapClient client = new SoapClient(URL);
  	String out = client.getSoapStringResponse(req);
  	System.out.println(out);*/
	/*FileReader ufr = new FileReader("d:\\u.TXT");
	FileReader afr = new FileReader("d:\\a.TXT");
	BufferedReader ubr = new BufferedReader(ufr);
	BufferedReader abr = new BufferedReader(afr);
	String uvalue = "";
	String avalue = "";
	String uline = "";
	String aline = "";
	
	StringBuffer usb = new StringBuffer();
	StringBuffer asb = new StringBuffer();
	
	while((uline=ubr.readLine())!=null)
	{
		usb.append(uline);
	}
	uvalue = usb.toString();
	
	while((aline=abr.readLine())!=null)
	{
		asb.append(aline);
	}
	avalue = asb.toString();
	
	ubr.close();
	abr.close();
	ufr.close();
	afr.close();
	
	byte [] u = uvalue.getBytes();
	System.out.println("u byte line : " + u.length);
	
	byte [] a = avalue.getBytes();
	System.out.println("a byte line : " + a.length);*/
	String soapurl = "SOAP_URL=http://10.110.16.201:1920///cms/soap";
	SoapClient soapClient = new SoapClient(soapurl);
   } 	
}