1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jelly.tags.soap;
18
19 import java.io.IOException;
20 import java.net.MalformedURLException;
21 import java.rmi.RemoteException;
22
23 import org.apache.commons.httpclient.HttpClient;
24 import org.apache.commons.httpclient.HttpException;
25 import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
26 import org.apache.commons.httpclient.methods.PostMethod;
27 import org.apache.commons.jelly.JellyTagException;
28 import org.apache.commons.jelly.MissingAttributeException;
29 import org.apache.commons.jelly.TagSupport;
30 import org.apache.commons.jelly.XMLOutput;
31
32 /***
33 * Invokes a web service
34 *
35 * @author <a href="mailto:jim@bnainc.net">James Birchfield</a>
36 * @version $Revision: 155420 $
37 */
38 public class InvokeRawTag extends TagSupport
39 {
40
41 private String var;
42 private String endpoint = null;
43 private String soapAction;
44
45 public InvokeRawTag()
46 {
47 }
48
49
50
51 public void doTag(XMLOutput output)
52 throws MissingAttributeException, JellyTagException
53 {
54 if (endpoint == null)
55 {
56 throw new MissingAttributeException("endpoint");
57 }
58
59 String request = getBodyText();
60
61 String answer = null;
62 try
63 {
64
65 PostMethod post = new PostMethod(endpoint);
66
67
68
69 post.setRequestBody(new StringInputStream(request));
70
71
72
73
74
75
76 if (request.length() < Integer.MAX_VALUE)
77 {
78 post.setRequestContentLength((int) request.length());
79 }
80 else
81 {
82 post.setRequestContentLength(
83 EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);
84 }
85
86
87
88
89 post.setRequestHeader(
90 "Content-type",
91 "text/xml; charset=ISO-8859-1");
92
93
94 if ( soapAction == null )
95 {
96 post.setRequestHeader( "SOAPAction", "");
97 }
98 else
99 {
100 post.setRequestHeader( "SOAPAction", soapAction);
101 }
102
103
104 HttpClient httpclient = new HttpClient();
105
106 int result = httpclient.executeMethod(post);
107
108 answer = post.getResponseBodyAsString();
109
110
111 post.releaseConnection();
112 }
113 catch (MalformedURLException e)
114 {
115 throw new JellyTagException(e);
116 }
117 catch (RemoteException e)
118 {
119 throw new JellyTagException(e);
120 }
121 catch (HttpException e)
122 {
123 throw new JellyTagException(e);
124 }
125 catch (IOException e)
126 {
127 throw new JellyTagException(e);
128 }
129
130 if (var != null)
131 {
132 context.setVariable(var, answer);
133 }
134 else
135 {
136
137 throw new JellyTagException(
138 "Not implemented yet; should stream results as XML events. Results: "
139 + answer);
140 }
141 }
142
143
144
145 /***
146 * Sets the end point to which the invocation will occur
147 */
148 public void setEndpoint(String endpoint)
149 {
150 this.endpoint = endpoint;
151 }
152
153 /***
154 * Sets the name of the variable to output the results of the SOAP call to.
155 */
156 public void setVar(String var)
157 {
158 this.var = var;
159 }
160
161 /***
162 * The SOAPAction HTTP header.
163 */
164 public void setSoapAction(String action)
165 {
166 soapAction = action;
167 }
168
169 }