Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
GetStep |
|
| 4.666666666666667;4.667 |
1 | /* | |
2 | * Copyright 1999-2001,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.workflow.io; | |
18 | ||
19 | ||
20 | import java.io.BufferedInputStream; | |
21 | import java.io.InputStream; | |
22 | import java.io.InputStreamReader; | |
23 | import java.io.IOException; | |
24 | import java.net.MalformedURLException; | |
25 | import java.net.URL; | |
26 | import java.net.URLConnection; | |
27 | import java.util.EmptyStackException; | |
28 | import org.apache.commons.workflow.Context; | |
29 | import org.apache.commons.workflow.StepException; | |
30 | import org.apache.commons.workflow.base.BaseStep; | |
31 | import org.apache.commons.workflow.util.WorkflowUtils; | |
32 | ||
33 | ||
34 | /** | |
35 | * <p>Retrieve the contents of a specified URL resource, and push the | |
36 | * contents as a String object onto the evaluation stack.</p> | |
37 | * | |
38 | * <p>Supported Attributes:</p> | |
39 | * <ul> | |
40 | * <li><strong>url</strong> - URL of the resource to be retrieved, or | |
41 | * omitted to pop a computed String value from the top of the | |
42 | * evaluation stack.</li> | |
43 | * </ul> | |
44 | * | |
45 | * <strong>DESIGN QUESTION - What about binary content?</strong> | |
46 | * | |
47 | * @version $Revision: 155475 $ $Date: 2005-02-26 13:31:11 +0000 (Sat, 26 Feb 2005) $ | |
48 | * @author Craig R. McClanahan | |
49 | */ | |
50 | ||
51 | public class GetStep extends BaseStep { | |
52 | ||
53 | ||
54 | // ----------------------------------------------------------= Constructors | |
55 | ||
56 | ||
57 | /** | |
58 | * Construct a default instance of this Step. | |
59 | */ | |
60 | public GetStep() { | |
61 | ||
62 | 0 | super(); |
63 | ||
64 | 0 | } |
65 | ||
66 | ||
67 | /** | |
68 | * Construct an instance of this Step with the specified identifier. | |
69 | * | |
70 | * @param id Step identifier | |
71 | */ | |
72 | public GetStep(String id) { | |
73 | ||
74 | 0 | super(); |
75 | 0 | setId(id); |
76 | ||
77 | 0 | } |
78 | ||
79 | ||
80 | /** | |
81 | * Construct a fully configured instance of this Step. | |
82 | * | |
83 | * @param id Step identifier | |
84 | * @param url Resource url | |
85 | */ | |
86 | public GetStep(String id, String url) { | |
87 | ||
88 | 0 | super(); |
89 | 0 | setId(id); |
90 | 0 | setUrl(url); |
91 | ||
92 | 0 | } |
93 | ||
94 | ||
95 | // ------------------------------------------------------------- Properties | |
96 | ||
97 | ||
98 | /** | |
99 | * The URL of the resource to be retrieved. | |
100 | */ | |
101 | 0 | protected String url = null; |
102 | ||
103 | public String getUrl() { | |
104 | 0 | return (this.url); |
105 | } | |
106 | ||
107 | public void setUrl(String url) { | |
108 | 0 | this.url = url; |
109 | 0 | } |
110 | ||
111 | ||
112 | // --------------------------------------------------------- Public Methods | |
113 | ||
114 | ||
115 | /** | |
116 | * Perform the executable actions related to this Step, in the context of | |
117 | * the specified Context. | |
118 | * | |
119 | * @param context The Context that is tracking our execution state | |
120 | * | |
121 | * @exception StepException if a processing error has occurred | |
122 | */ | |
123 | public void execute(Context context) throws StepException { | |
124 | ||
125 | // Get the remote URL we will be contacting | |
126 | 0 | Object remote = this.url; |
127 | 0 | URL remoteURL = null; |
128 | 0 | if (remote == null) { |
129 | try { | |
130 | 0 | remote = (String) context.pop(); |
131 | 0 | } catch (EmptyStackException e) { |
132 | 0 | throw new StepException("Evaluation stack is empty", this); |
133 | 0 | } |
134 | } | |
135 | 0 | if (remote instanceof URL) { |
136 | 0 | remoteURL = (URL) remote; |
137 | 0 | } else if (remote instanceof String) { |
138 | try { | |
139 | 0 | remoteURL = new URL((String) remote); |
140 | 0 | } catch (MalformedURLException e) { |
141 | 0 | throw new StepException("Invalid URL '" + remote + "'", |
142 | e, this); | |
143 | 0 | } |
144 | } else { | |
145 | try { | |
146 | 0 | remoteURL = new URL(remote.toString()); |
147 | 0 | } catch (MalformedURLException e) { |
148 | 0 | throw new StepException("Invalid URL '" + remote + "'", |
149 | e, this); | |
150 | 0 | } |
151 | } | |
152 | ||
153 | // Define variables we will need later | |
154 | 0 | URLConnection conn = null; |
155 | 0 | InputStream is = null; |
156 | 0 | BufferedInputStream bis = null; |
157 | 0 | InputStreamReader isr = null; |
158 | 0 | StringBuffer sb = null; |
159 | 0 | StepException se = null; |
160 | ||
161 | try { | |
162 | ||
163 | // Open a connection to the specified URL | |
164 | 0 | conn = remoteURL.openConnection(); |
165 | 0 | conn.setDoInput(true); |
166 | 0 | conn.setDoOutput(false); |
167 | 0 | conn.connect(); |
168 | 0 | int contentLength = conn.getContentLength(); |
169 | 0 | if (contentLength < 2048) |
170 | 0 | contentLength = 2048; |
171 | 0 | sb = new StringBuffer(contentLength); |
172 | ||
173 | // Parse the character encoding to be used | |
174 | 0 | String contentType = conn.getContentType(); |
175 | 0 | String encoding = |
176 | WorkflowUtils.parseCharacterEncoding(contentType); | |
177 | ||
178 | // Construct a suitable InputStreamReader | |
179 | 0 | is = conn.getInputStream(); |
180 | 0 | bis = new BufferedInputStream(is, 2048); |
181 | 0 | if (encoding == null) |
182 | 0 | isr = new InputStreamReader(bis); |
183 | else | |
184 | 0 | isr = new InputStreamReader(bis, encoding); |
185 | ||
186 | // Copy all characters from this resource | |
187 | while (true) { | |
188 | 0 | int ch = isr.read(); |
189 | 0 | if (ch < 0) |
190 | 0 | break; |
191 | 0 | sb.append((char) ch); |
192 | 0 | } |
193 | ||
194 | // Close the input file | |
195 | 0 | isr.close(); |
196 | 0 | isr = null; |
197 | 0 | bis = null; |
198 | 0 | is = null; |
199 | ||
200 | 0 | } catch (IOException e) { |
201 | ||
202 | 0 | se = new StepException("IOException processing '" + remoteURL + |
203 | "'", e, this); | |
204 | ||
205 | } finally { | |
206 | ||
207 | 0 | if (isr != null) { |
208 | try { | |
209 | 0 | isr.close(); |
210 | 0 | } catch (Throwable t) { |
211 | ; | |
212 | 0 | } |
213 | 0 | } else if (bis != null) { |
214 | try { | |
215 | 0 | bis.close(); |
216 | 0 | } catch (Throwable t) { |
217 | ; | |
218 | 0 | } |
219 | 0 | } else if (is != null) { |
220 | try { | |
221 | 0 | is.close(); |
222 | 0 | } catch (Throwable t) { |
223 | ; | |
224 | 0 | } |
225 | } | |
226 | ||
227 | } | |
228 | ||
229 | // Push results or throw exception as appropriate | |
230 | 0 | if (se != null) |
231 | 0 | throw se; |
232 | 0 | context.push(sb.toString()); |
233 | ||
234 | 0 | } |
235 | ||
236 | ||
237 | } |