1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.imixs.workflow.services.rest;
28
29 import java.io.BufferedWriter;
30 import java.io.OutputStreamWriter;
31 import java.io.PrintWriter;
32 import java.io.StringWriter;
33 import java.net.HttpURLConnection;
34 import java.net.URL;
35
36 import javax.xml.bind.JAXBContext;
37 import javax.xml.bind.Marshaller;
38
39 import org.imixs.workflow.util.Base64;
40 import org.imixs.workflow.xml.EntityCollection;
41 import org.imixs.workflow.xml.XMLItemCollection;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public class RestClient {
59
60
61
62 private String serviceEndpoint;
63
64 private String sUser = null;
65
66 private String sPassword = null;
67
68 private String encoding = "UTF-8";
69
70 private int iLastHTTPResult = 0;
71
72
73 public void setCredentials(String auser, String apw) {
74 sUser = auser;
75 sPassword = apw;
76 }
77
78 public void setEncoding(String aEncoding) {
79 encoding = aEncoding;
80 }
81
82
83
84
85
86
87
88
89
90
91
92
93 public int postEntity(String uri, XMLItemCollection aItemCol)
94 throws Exception {
95 PrintWriter printWriter = null;
96
97 HttpURLConnection urlConnection = null;
98 try {
99 serviceEndpoint = uri;
100 iLastHTTPResult = 500;
101
102 urlConnection = (HttpURLConnection) new URL(serviceEndpoint)
103 .openConnection();
104 urlConnection.setRequestMethod("POST");
105 urlConnection.setDoOutput(true);
106 urlConnection.setDoInput(true);
107 urlConnection.setAllowUserInteraction(false);
108
109
110 if (sUser != null) {
111 urlConnection.setRequestProperty("Authorization", "Basic "
112 + this.getAccessByUser());
113 }
114
115 urlConnection.setRequestProperty("Content-Type",
116 "application/xml; charset=" + encoding);
117
118 StringWriter writer = new StringWriter();
119
120 JAXBContext context = JAXBContext
121 .newInstance(XMLItemCollection.class);
122 Marshaller m = context.createMarshaller();
123 m.marshal(aItemCol, writer);
124
125
126
127
128 urlConnection.setRequestProperty("Content-Length", ""
129 + Integer.valueOf(writer.toString().getBytes().length));
130
131 printWriter = new PrintWriter(new BufferedWriter(
132 new OutputStreamWriter(urlConnection.getOutputStream(),
133 encoding)));
134
135 printWriter.write(writer.toString());
136 printWriter.close();
137 String sHTTPResponse = urlConnection.getHeaderField(0);
138 try {
139 iLastHTTPResult = Integer.parseInt(sHTTPResponse.substring(9,
140 12));
141 } catch (Exception eNumber) {
142
143 iLastHTTPResult = 500;
144 }
145 } catch (Exception ioe) {
146
147 throw ioe;
148 } finally {
149
150 if (printWriter != null)
151 printWriter.close();
152 }
153
154 return iLastHTTPResult;
155 }
156
157
158
159
160
161
162
163
164
165
166
167
168 public int postCollection(String uri, EntityCollection aEntityCol)
169 throws Exception {
170 PrintWriter printWriter = null;
171
172 HttpURLConnection urlConnection = null;
173 try {
174 serviceEndpoint = uri;
175 iLastHTTPResult = 500;
176
177 urlConnection = (HttpURLConnection) new URL(serviceEndpoint)
178 .openConnection();
179 urlConnection.setRequestMethod("POST");
180 urlConnection.setDoOutput(true);
181 urlConnection.setDoInput(true);
182 urlConnection.setAllowUserInteraction(false);
183
184
185 if (sUser != null) {
186 urlConnection.setRequestProperty("Authorization", "Basic "
187 + this.getAccessByUser());
188 }
189
190
191 urlConnection.setRequestProperty("Content-Type",
192 "application/xml; charset=" + encoding);
193
194 StringWriter writer = new StringWriter();
195
196 JAXBContext context = JAXBContext
197 .newInstance(EntityCollection.class);
198 Marshaller m = context.createMarshaller();
199 m.marshal(aEntityCol, writer);
200
201
202
203
204 urlConnection.setRequestProperty("Content-Length", ""
205 + Integer.valueOf(writer.toString().getBytes().length));
206
207 printWriter = new PrintWriter(new BufferedWriter(
208 new OutputStreamWriter(urlConnection.getOutputStream(),
209 encoding)));
210
211 printWriter.write(writer.toString());
212 printWriter.close();
213 String sHTTPResponse = urlConnection.getHeaderField(0);
214 try {
215 iLastHTTPResult = Integer.parseInt(sHTTPResponse.substring(9,
216 12));
217 } catch (Exception eNumber) {
218
219 iLastHTTPResult = 500;
220 }
221
222
223
224
225
226
227
228
229
230
231 } catch (Exception ioe) {
232
233 throw ioe;
234 } finally {
235
236 if (printWriter != null)
237 printWriter.close();
238 }
239
240 return iLastHTTPResult;
241 }
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262 private String getAccessByUser() {
263 String sURLAccess = "";
264
265 String sUserCode = sUser + ":" + sPassword;
266
267
268 char[] authcode = Base64.encode(sUserCode.getBytes());
269
270 sURLAccess = String.valueOf(authcode);
271 return sURLAccess;
272 }
273
274 }