comparison src/main/java/de/intevation/artifacts/httpclient/http/HttpClientImpl.java @ 6:09a84c6e263a

Enhanced the HttpClient with a method sto trigger a collection specific action. http-client/trunk@1552 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 23 Mar 2011 16:27:56 +0000
parents 7917c21fad01
children 06e9e25632cd
comparison
equal deleted inserted replaced
5:7917c21fad01 6:09a84c6e263a
49 public static final String PATH_SERVICE = "/service"; 49 public static final String PATH_SERVICE = "/service";
50 50
51 /** The URL path of the resource to create new artifact collections.*/ 51 /** The URL path of the resource to create new artifact collections.*/
52 public static final String PATH_CREATE_COLLECTION = "/create-collection"; 52 public static final String PATH_CREATE_COLLECTION = "/create-collection";
53 53
54 /** The URL path of the resource to work with an artifact collections.*/
55 public static final String PATH_ACTION_COLLECTION = "/collection";
56
54 private String serverUrl; 57 private String serverUrl;
55 58
56 59
57 public HttpClientImpl(String serverUrl) { 60 public HttpClientImpl(String serverUrl) {
58 this.serverUrl = serverUrl; 61 this.serverUrl = serverUrl;
107 throw new ConnectionException( 110 throw new ConnectionException(
108 "Connection to server failed. No Artifact created."); 111 "Connection to server failed. No Artifact created.");
109 } 112 }
110 } 113 }
111 114
115
116 @Override
117 public Object describe(
118 Artifact artifact,
119 Document doc,
120 ResponseHandler handler)
121 throws ConnectionException
122 {
123 try {
124 String url = serverUrl + "/artifact/" + artifact.getUuid();
125 return handler.handle(doPost(url, doc));
126 }
127 catch (IOException ioe) {
128 throw new ConnectionException(
129 "Connection to server failed: " + ioe.getMessage());
130 }
131 }
132
133
134 @Override
135 public Object feed(Artifact artifact, Document doc, ResponseHandler handler)
136 throws ConnectionException
137 {
138 try {
139 String url = serverUrl + "/artifact/" + artifact.getUuid();
140 Document result = (Document) handler.handle(doPost(url, doc));
141
142 return result;
143 }
144 catch (IOException ioe) {
145 throw new ConnectionException(
146 "Connection to server failed: " + ioe.getMessage());
147 }
148 }
149
150
151 @Override
152 public Object advance(Artifact artifact, Document doc, ResponseHandler handler)
153 throws ConnectionException
154 {
155 try {
156 String url = serverUrl + "/artifact/" + artifact.getUuid();
157 Document result = (Document) handler.handle(doPost(url, doc));
158
159 return result;
160 }
161 catch (IOException ioe) {
162 throw new ConnectionException(
163 "Connection to server failed: " + ioe.getMessage());
164 }
165 }
166
167
168 @Override
169 public void out(
170 Artifact artifact,
171 Document doc,
172 String target,
173 OutputStream out)
174 throws ConnectionException
175 {
176 try {
177 String url =
178 serverUrl
179 + "/artifact/"
180 + artifact.getUuid()
181 + "/" + target;
182
183 ResponseHandler handler = new StreamResponseHandler();
184
185 InputStream stream = (InputStream) handler.handle(doPost(url, doc));
186
187 byte[] b = new byte[4096];
188 int i = -1;
189 while ((i = stream.read(b)) > 0) {
190 out.write(b, 0, i);
191 }
192 }
193 catch (IOException ioe) {
194 throw new ConnectionException(
195 "Connection to server failed: " + ioe.getMessage());
196 }
197 }
198
199
200 private Response doPost(String url, Document body) throws IOException {
201 logger.info("Start HTTP-POST request to: "+ url);
202
203 Client client = new Client(Protocol.HTTP);
204 Request request = new Request(Method.POST, url);
205
206 Representation representation = new DomRepresentation(
207 MediaType.APPLICATION_XML,
208 body);
209
210 request.setEntity(representation);
211 Response response = client.handle(request);
212
213 Status status = response.getStatus();
214 if (status.getCode() != 200) {
215 logger.error("Response status: " + status.getCode());
216 throw new IOException(status.getDescription());
217 }
218
219 return response;
220 }
221
222
223 private Response doGet(String url) throws IOException {
224 logger.info("Start HTTP-POST request to: "+ url);
225
226 Client client = new Client(Protocol.HTTP);
227 Request request = new Request(Method.GET, url);
228
229 Response response = client.handle(request);
230
231 Status status = response.getStatus();
232 if (status.getCode() != 200) {
233 logger.error("Response status: " + status.getCode());
234 throw new IOException(status.getDescription());
235 }
236
237 return response;
238 }
239
240
241 //==============================
242 // Collection API
243 //==============================
112 244
113 /** 245 /**
114 * This method triggers the artifact servers resource to create a new 246 * This method triggers the artifact servers resource to create a new
115 * artifact collection. 247 * artifact collection.
116 * 248 *
135 throw new ConnectionException(ioe.getMessage(), ioe); 267 throw new ConnectionException(ioe.getMessage(), ioe);
136 } 268 }
137 } 269 }
138 270
139 271
140 @Override 272 /**
141 public Object describe( 273 * This method might be used to trigger a collection specific action. The
142 Artifact artifact, 274 * action that is executed depends on the document <i>actionDoc</i>.
143 Document doc, 275 *
276 * @param actionDoc The document that describes the action to be executed.
277 * @param uuid The uuid of the collection.
278 * @param handler The handler that is used to create the result object.
279 *
280 * @return a result object created by <i>handler</i>.
281 */
282 public Object doCollectionAction(
283 Document actionDoc,
284 String uuid,
144 ResponseHandler handler) 285 ResponseHandler handler)
145 throws ConnectionException 286 throws ConnectionException
146 { 287 {
147 try { 288 String url = serverUrl + PATH_ACTION_COLLECTION + "/" + uuid;
148 String url = serverUrl + "/artifact/" + artifact.getUuid(); 289
149 return handler.handle(doPost(url, doc)); 290 try {
150 } 291 return handler.handle(doPost(url, actionDoc));
151 catch (IOException ioe) { 292 }
152 throw new ConnectionException( 293 catch (IOException ioe) {
153 "Connection to server failed: " + ioe.getMessage()); 294 throw new ConnectionException(ioe.getMessage(), ioe);
154 } 295 }
155 }
156
157
158 @Override
159 public Object feed(Artifact artifact, Document doc, ResponseHandler handler)
160 throws ConnectionException
161 {
162 try {
163 String url = serverUrl + "/artifact/" + artifact.getUuid();
164 Document result = (Document) handler.handle(doPost(url, doc));
165
166 return result;
167 }
168 catch (IOException ioe) {
169 throw new ConnectionException(
170 "Connection to server failed: " + ioe.getMessage());
171 }
172 }
173
174
175 @Override
176 public Object advance(Artifact artifact, Document doc, ResponseHandler handler)
177 throws ConnectionException
178 {
179 try {
180 String url = serverUrl + "/artifact/" + artifact.getUuid();
181 Document result = (Document) handler.handle(doPost(url, doc));
182
183 return result;
184 }
185 catch (IOException ioe) {
186 throw new ConnectionException(
187 "Connection to server failed: " + ioe.getMessage());
188 }
189 }
190
191
192 @Override
193 public void out(
194 Artifact artifact,
195 Document doc,
196 String target,
197 OutputStream out)
198 throws ConnectionException
199 {
200 try {
201 String url =
202 serverUrl
203 + "/artifact/"
204 + artifact.getUuid()
205 + "/" + target;
206
207 ResponseHandler handler = new StreamResponseHandler();
208
209 InputStream stream = (InputStream) handler.handle(doPost(url, doc));
210
211 byte[] b = new byte[4096];
212 int i = -1;
213 while ((i = stream.read(b)) > 0) {
214 out.write(b, 0, i);
215 }
216 }
217 catch (IOException ioe) {
218 throw new ConnectionException(
219 "Connection to server failed: " + ioe.getMessage());
220 }
221 }
222
223
224 private Response doPost(String url, Document body) throws IOException {
225 logger.info("Start HTTP-POST request to: "+ url);
226
227 Client client = new Client(Protocol.HTTP);
228 Request request = new Request(Method.POST, url);
229
230 Representation representation = new DomRepresentation(
231 MediaType.APPLICATION_XML,
232 body);
233
234 request.setEntity(representation);
235 Response response = client.handle(request);
236
237 Status status = response.getStatus();
238 if (status.getCode() != 200) {
239 logger.error("Response status: " + status.getCode());
240 throw new IOException(status.getDescription());
241 }
242
243 return response;
244 }
245
246
247 private Response doGet(String url) throws IOException {
248 logger.info("Start HTTP-POST request to: "+ url);
249
250 Client client = new Client(Protocol.HTTP);
251 Request request = new Request(Method.GET, url);
252
253 Response response = client.handle(request);
254
255 Status status = response.getStatus();
256 if (status.getCode() != 200) {
257 logger.error("Response status: " + status.getCode());
258 throw new IOException(status.getDescription());
259 }
260
261 return response;
262 } 296 }
263 297
264 298
265 /******************************* 299 /*******************************
266 * Service API 300 * Service API

http://dive4elements.wald.intevation.org