KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > jndiproviders > dsml > SoapClient


1 package com.ca.jndiproviders.dsml;
2
3
4 /**
5  * <p>SoapClient allows for a 'raw' soap message to be constructed and
6  * retrieved.</p>
7  *
8  * <p>It takes the soap message as text, constructs the appropriate
9  * http envelope, and hoofs it off to the server. It then takes
10  * the response, and returns it as raw text.</p>
11  *
12  * <p>The original functionality here is inspired by the ibm dev works
13  * article: http://www-128.ibm.com/developerworks/xml/library/x-soapcl/
14  * on a 'zero overhead' soap client by Bod duCharme and Michael Brennan.
15  *
16  */

17
18 import java.io.*;
19 import java.net.*;
20 import java.util.logging.Logger JavaDoc;
21
22 public class SoapClient
23 {
24     private static Logger JavaDoc log = Logger.getLogger(SoapClient.class.getName());
25
26     /**
27      * Original main method for stand alone operation...
28      *
29      * @param args
30      * @throws Exception
31      */

32     /*
33     public static void main(String[] args) throws Exception
34     {
35         if (args.length < 2)
36         {
37             System.err.println("Usage: java com.ca.jndiproviders.dsml.SoapClient " +
38                     "http://soapURL soapEnvelopefile.xml" +
39                     " [SOAPAction]");
40             System.err.println("SOAPAction is optional.");
41             System.exit(1);
42         }
43
44         String SOAPUrl = args[0];
45         String xmlFile2Send = args[1];
46
47         String SOAPAction = "";
48         if (args.length > 2)
49             SOAPAction = args[2];
50
51         // Open the input file. After we copy it to a byte array, we can see
52         // how big it is so that we can set the HTTP Content-Length
53         // property.
54
55         FileInputStream fin = new FileInputStream(xmlFile2Send);
56         ByteArrayOutputStream bout = new ByteArrayOutputStream();
57
58         // Copy the SOAP file to the open connection.
59         copy(fin, bout);
60         fin.close();
61
62         byte[] b = bout.toByteArray();
63
64         sendSoapMsg(SOAPUrl, b, SOAPAction);
65
66     }
67     */

68     /**
69      * This takes a byte array and hoofs off the contents to the target URL, adding
70      * a bunch of http headers, including an optional 'SOAPaction:' header. It returns
71      * the raw contents of the reply, sans any http headers.
72      *
73      * @param SOAPUrl
74      * @param b
75      * @param SOAPAction
76      * @return the response data
77      * @throws IOException
78      */

79     public static String JavaDoc sendSoapMsg(String JavaDoc SOAPUrl, byte[] b, String JavaDoc SOAPAction)
80             throws IOException
81     {
82         log.finest("HTTP REQUEST SIZE " + b.length );
83
84         // bracket Soap Action with quotes. *shrug*.
85
if (SOAPAction.startsWith("\"") == false)
86             SOAPAction = "\"" + SOAPAction + "\"";
87
88         //TODO: reuse connection...
89

90         //TODO: time out connection using ldap time out...
91
// Create the connection where we're going to send the file.
92
URL url = new URL(SOAPUrl);
93         HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
94
95         // Set the appropriate HTTP parameters.
96
httpConn.setRequestProperty("SOAPAction", SOAPAction);
97         httpConn.setRequestProperty("Content-Type", "text/xml; charset=\"utf-8\"");
98         httpConn.setRequestProperty("Content-Length",
99                 String.valueOf(b.length));
100         httpConn.setRequestProperty("Cache-Control", "no-cache");
101         httpConn.setRequestProperty("Pragma", "no-cache");
102         httpConn.setRequestMethod("POST");
103         httpConn.setDoOutput(true);
104         httpConn.setDoInput(true);
105
106         // Everything's set up; send the XML that was read in to b.
107
OutputStream out = httpConn.getOutputStream();
108         out.write(b);
109         out.close();
110
111         // Read the response
112

113         InputStreamReader isr = new InputStreamReader(httpConn.getInputStream());
114         BufferedReader in = new BufferedReader(isr);
115
116         StringBuffer JavaDoc response = new StringBuffer JavaDoc(1024);
117
118         String JavaDoc inputLine;
119         while ((inputLine = in.readLine()) != null)
120             response.append(inputLine);
121
122         in.close();
123
124         log.finest("HTTP RESPONSE SIZE: " + response.length());
125
126         return response.toString();
127     }
128
129     /*
130     // copy method from From E.R. Harold's book "Java I/O"
131     public static void copy(InputStream in, OutputStream out)
132             throws IOException
133     {
134         // do not allow other threads to read from the
135         // input or write to the output while copying is
136         // taking place
137
138         synchronized (in)
139         {
140             synchronized (out)
141             {
142
143                 byte[] buffer = new byte[256];
144                 while (true)
145                 {
146                     int bytesRead = in.read(buffer);
147                     if (bytesRead == -1) break;
148                     out.write(buffer, 0, bytesRead);
149                 }
150             }
151         }
152     }
153     */

154 }
Popular Tags