1 package org
.argeo
.slc
.core
.execution
.http
;
3 import java
.io
.InputStream
;
4 import java
.nio
.ByteBuffer
;
5 import java
.nio
.channels
.AsynchronousByteChannel
;
6 import java
.nio
.channels
.Channels
;
7 import java
.nio
.channels
.ReadableByteChannel
;
8 import java
.util
.concurrent
.Callable
;
9 import java
.util
.concurrent
.Future
;
11 import org
.apache
.commons
.httpclient
.HttpClient
;
12 import org
.apache
.commons
.httpclient
.methods
.InputStreamRequestEntity
;
13 import org
.apache
.commons
.httpclient
.methods
.PostMethod
;
14 import org
.apache
.commons
.httpclient
.methods
.RequestEntity
;
16 public class WebServiceTask
implements Callable
<Integer
> {
18 private String requContentType
;
19 private String respContentType
;
21 private AsynchronousByteChannel channel
;
23 public WebServiceTask(AsynchronousByteChannel channel
, String url
) {
24 this(url
, "application/json", "application/json", channel
);
27 public WebServiceTask(String url
, String requContentType
, String respContentType
, AsynchronousByteChannel channel
) {
29 this.requContentType
= requContentType
;
30 this.respContentType
= respContentType
;
31 this.channel
= channel
;
35 public Integer
call() throws Exception
{
37 HttpClient httpClient
= new HttpClient();
38 PostMethod postMethod
= new PostMethod(url
);
39 InputStream in
= Channels
.newInputStream(channel
);
40 RequestEntity requestEntity
= new InputStreamRequestEntity(in
, requContentType
);
41 // StringRequestEntity requestEntity = new
42 // StringRequestEntity(payloadStr, "application/json", "UTF-8");
43 postMethod
.setRequestEntity(requestEntity
);
44 httpClient
.executeMethod(postMethod
);
45 InputStream answerIn
= postMethod
.getResponseBodyAsStream();
46 ReadableByteChannel answer
= Channels
.newChannel(answerIn
);
47 ByteBuffer buffer
= ByteBuffer
.allocate(8 * 1024);
51 read
= answer
.read(buffer
);
55 Future
<Integer
> f
= channel
.write(buffer
);
56 writeRes
= writeRes
+ f
.get();
62 public String
getUrl() {
66 public String
getRequContentType() {
67 return requContentType
;
70 public String
getRespContentType() {
71 return respContentType
;