%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jelly.tags.soap.InvokeRawTag |
|
|
1 | /* |
|
2 | * Copyright 2002,2004 The Apache Software Foundation. |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
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: 1.1 $ |
|
37 | */ |
|
38 | public class InvokeRawTag extends TagSupport |
|
39 | { |
|
40 | ||
41 | private String var; |
|
42 | 0 | private String endpoint = null; |
43 | private String soapAction; |
|
44 | ||
45 | public InvokeRawTag() |
|
46 | 0 | { |
47 | 0 | } |
48 | ||
49 | // Tag interface |
|
50 | //------------------------------------------------------------------------- |
|
51 | public void doTag(XMLOutput output) |
|
52 | throws MissingAttributeException, JellyTagException |
|
53 | { |
|
54 | 0 | if (endpoint == null) |
55 | { |
|
56 | 0 | throw new MissingAttributeException("endpoint"); |
57 | } |
|
58 | ||
59 | 0 | String request = getBodyText(); |
60 | ||
61 | 0 | String answer = null; |
62 | try |
|
63 | { |
|
64 | // Prepare HTTP post |
|
65 | 0 | PostMethod post = new PostMethod(endpoint); |
66 | ||
67 | // Request content will be retrieved directly |
|
68 | // from the input stream |
|
69 | 0 | post.setRequestBody(new StringInputStream(request)); |
70 | ||
71 | // Per default, the request content needs to be buffered |
|
72 | // in order to determine its length. |
|
73 | // Request body buffering can be avoided when |
|
74 | // = content length is explicitly specified |
|
75 | // = chunk-encoding is used |
|
76 | 0 | if (request.length() < Integer.MAX_VALUE) |
77 | { |
|
78 | 0 | post.setRequestContentLength((int) request.length()); |
79 | } |
|
80 | else |
|
81 | { |
|
82 | 0 | post.setRequestContentLength( |
83 | EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED); |
|
84 | } |
|
85 | ||
86 | // Specify content type and encoding |
|
87 | // If content encoding is not explicitly specified |
|
88 | // ISO-8859-1 is assumed |
|
89 | 0 | post.setRequestHeader( |
90 | "Content-type", |
|
91 | "text/xml; charset=ISO-8859-1"); |
|
92 | ||
93 | // Set the SOAPAction header |
|
94 | 0 | if ( soapAction == null ) |
95 | { |
|
96 | 0 | post.setRequestHeader( "SOAPAction", ""); |
97 | } |
|
98 | else |
|
99 | { |
|
100 | 0 | post.setRequestHeader( "SOAPAction", soapAction); |
101 | } |
|
102 | ||
103 | // Get HTTP client |
|
104 | 0 | HttpClient httpclient = new HttpClient(); |
105 | // Execute request |
|
106 | 0 | int result = httpclient.executeMethod(post); |
107 | ||
108 | 0 | answer = post.getResponseBodyAsString(); |
109 | ||
110 | // Release current connection to the connection pool once you are done |
|
111 | 0 | post.releaseConnection(); |
112 | 0 | } |
113 | catch (MalformedURLException e) |
|
114 | { |
|
115 | 0 | throw new JellyTagException(e); |
116 | } |
|
117 | catch (RemoteException e) |
|
118 | { |
|
119 | 0 | throw new JellyTagException(e); |
120 | } |
|
121 | catch (HttpException e) |
|
122 | { |
|
123 | 0 | throw new JellyTagException(e); |
124 | } |
|
125 | catch (IOException e) |
|
126 | { |
|
127 | 0 | throw new JellyTagException(e); |
128 | } |
|
129 | ||
130 | 0 | if (var != null) |
131 | { |
|
132 | 0 | context.setVariable(var, answer); |
133 | } |
|
134 | else |
|
135 | { |
|
136 | // should turn the answer into XML events... |
|
137 | 0 | throw new JellyTagException( |
138 | "Not implemented yet; should stream results as XML events. Results: " |
|
139 | + answer); |
|
140 | } |
|
141 | 0 | } |
142 | ||
143 | // Properties |
|
144 | //------------------------------------------------------------------------- |
|
145 | /** |
|
146 | * Sets the end point to which the invocation will occur |
|
147 | */ |
|
148 | public void setEndpoint(String endpoint) |
|
149 | { |
|
150 | 0 | this.endpoint = endpoint; |
151 | 0 | } |
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 | 0 | this.var = class="keyword">var; |
159 | 0 | } |
160 | ||
161 | /** |
|
162 | * The SOAPAction HTTP header. |
|
163 | */ |
|
164 | public void setSoapAction(String action) |
|
165 | { |
|
166 | 0 | soapAction = action; |
167 | 0 | } |
168 | ||
169 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |