1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 public class GetStep extends BaseStep {
52
53
54
55
56
57
58
59
60 public GetStep() {
61
62 super();
63
64 }
65
66
67
68
69
70
71
72 public GetStep(String id) {
73
74 super();
75 setId(id);
76
77 }
78
79
80
81
82
83
84
85
86 public GetStep(String id, String url) {
87
88 super();
89 setId(id);
90 setUrl(url);
91
92 }
93
94
95
96
97
98
99
100
101 protected String url = null;
102
103 public String getUrl() {
104 return (this.url);
105 }
106
107 public void setUrl(String url) {
108 this.url = url;
109 }
110
111
112
113
114
115
116
117
118
119
120
121
122
123 public void execute(Context context) throws StepException {
124
125
126 Object remote = this.url;
127 URL remoteURL = null;
128 if (remote == null) {
129 try {
130 remote = (String) context.pop();
131 } catch (EmptyStackException e) {
132 throw new StepException("Evaluation stack is empty", this);
133 }
134 }
135 if (remote instanceof URL) {
136 remoteURL = (URL) remote;
137 } else if (remote instanceof String) {
138 try {
139 remoteURL = new URL((String) remote);
140 } catch (MalformedURLException e) {
141 throw new StepException("Invalid URL '" + remote + "'",
142 e, this);
143 }
144 } else {
145 try {
146 remoteURL = new URL(remote.toString());
147 } catch (MalformedURLException e) {
148 throw new StepException("Invalid URL '" + remote + "'",
149 e, this);
150 }
151 }
152
153
154 URLConnection conn = null;
155 InputStream is = null;
156 BufferedInputStream bis = null;
157 InputStreamReader isr = null;
158 StringBuffer sb = null;
159 StepException se = null;
160
161 try {
162
163
164 conn = remoteURL.openConnection();
165 conn.setDoInput(true);
166 conn.setDoOutput(false);
167 conn.connect();
168 int contentLength = conn.getContentLength();
169 if (contentLength < 2048)
170 contentLength = 2048;
171 sb = new StringBuffer(contentLength);
172
173
174 String contentType = conn.getContentType();
175 String encoding =
176 WorkflowUtils.parseCharacterEncoding(contentType);
177
178
179 is = conn.getInputStream();
180 bis = new BufferedInputStream(is, 2048);
181 if (encoding == null)
182 isr = new InputStreamReader(bis);
183 else
184 isr = new InputStreamReader(bis, encoding);
185
186
187 while (true) {
188 int ch = isr.read();
189 if (ch < 0)
190 break;
191 sb.append((char) ch);
192 }
193
194
195 isr.close();
196 isr = null;
197 bis = null;
198 is = null;
199
200 } catch (IOException e) {
201
202 se = new StepException("IOException processing '" + remoteURL +
203 "'", e, this);
204
205 } finally {
206
207 if (isr != null) {
208 try {
209 isr.close();
210 } catch (Throwable t) {
211 ;
212 }
213 } else if (bis != null) {
214 try {
215 bis.close();
216 } catch (Throwable t) {
217 ;
218 }
219 } else if (is != null) {
220 try {
221 is.close();
222 } catch (Throwable t) {
223 ;
224 }
225 }
226
227 }
228
229
230 if (se != null)
231 throw se;
232 context.push(sb.toString());
233
234 }
235
236
237 }