Cao, William (NSB - CN/Qingdao)
2018-05-14 08:50:47 UTC
Hello,
I am trying to use Jetty client (9.4.7 and 9.2.24) to develop a https client (HTTP/1.1). When it tried to send out the request, the server can receive the request and respond it, and in the client side the jetty log also shows that it got 200 OK response. However, the client application got timeout. I think the major point is the onContent(Response response, ByteBuffer buffer, Callback callback), and we can call callback.succeeded() to terminate the onContent(...). How should I check that all the content is received so that I can call callback.succeeded() in onContent(...) so as to avoid the timeout?
Below is the jetty debug log and the code:
Padded plaintext after DECRYPTION: len = 456
0000: 48 54 54 50 2F 31 2E 31 20 32 30 30 20 4F 4B 0D HTTP/1.1 200 OK.
0010: 0A 53 65 6E 64 2D 54 69 6D 65 3A 20 31 35 32 36 .Send-Time: 1526
0020: 32 38 35 31 36 34 38 31 34 0D 0A 45 63 68 6F 2D 285164814..Echo-
...
The received content is <?xml version="1.0" encoding="UTF-8"?><cn:action xmlns:cn="urn:oma:xml:rest:netapi:callnotification:1"><actionToPerform>Continue</actionToPerform><decisionId>continue89271a52-08b6-4698-afd7-3e91a5b94050</decisionId></cn:action>
The request failed with Exception java.util.concurrent.TimeoutException: Total timeout 3000 ms elapsed
Exception with stacktrace: java.lang.Exception: java.util.concurrent.TimeoutException: Total timeout 3000 ms elapsed
com.mycom.securityas.http.HttpsRequestHandler$3.onFailure(HttpsRequestHandler.java:475)
org.eclipse.jetty.client.ResponseNotifier.notifyFailure(ResponseNotifier.java:170)
org.eclipse.jetty.client.ResponseNotifier.notifyFailure(ResponseNotifier.java:162)
org.eclipse.jetty.client.HttpReceiver.abort(HttpReceiver.java:533)
org.eclipse.jetty.client.HttpChannel.abortResponse(HttpChannel.java:129)
org.eclipse.jetty.client.HttpChannel.abort(HttpChannel.java:122)
org.eclipse.jetty.client.HttpExchange.abort(HttpExchange.java:257)
org.eclipse.jetty.client.HttpConversation.abort(HttpConversation.java:141)
org.eclipse.jetty.client.HttpRequest.abort(HttpRequest.java:748)
org.eclipse.jetty.client.TimeoutCompleteListener.run(TimeoutCompleteListener.java:71)
java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
java.util.concurrent.FutureTask.run(FutureTask.java:266)
java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
java.lang.Thread.run(Thread.java:748)
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.client.api.Response;
import org.eclipse.jetty.client.api.Result;
import org.eclipse.jetty.client.util.BytesContentProvider;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.util.Callback;
//......
public class HttpsRequestHandler {
......
private HttpClient httpsclient = null;
private SslContextFactory clientTLSFactory = null;
private void sendHttpsRequest(String destinationUrl, String msgBody) {
byte[] bytesBody = msgBody.getBytes(Charset.forName("UTF-8"));
Request httpsreq = client
.newRequest(destinationUrl)
.method(HttpMethod.POST)
.timeout(3000, TimeUnit.MILLISECONDS)
.content(new BytesContentProvider(bytesBody), "application/xml");
//...set headers ... ignored
httpsreq.send(new Response.Listener.Adapter() {
@Override
public void onContent(Response response, ByteBuffer buffer, Callback callback) {
System.out.println("Https request onContent 1");
StringBuffer sbf = new StringBuffer();
byte[] thebuffer = new byte[buffer.limit()];
buffer.get(thebuffer);
sbf.append(new String(thebuffer));
System.out.println("The received content is " + sbf.toString());
//callback.succeeded();
}
@Override
public void onFailure(Response response, Throwable failure) {
Exception e = new Exception(failure);
System.out.println("The request failed with Exception " + e.getMessage());
e.printStackTrace();
}
@Override
public void onSuccess(Response response) {
System.out.println("The request succeeded");
}
//...... //other logics
});
}
public boolean init() {}
clientTLSFactory = createSslContextFactory();
if (!startClient(clientTLSFactory)) {
return false;
}
return true;
}
private SslContextFactory createSslContextFactory() {
SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStorePath("/etc/pki/CA/cert/trust.jks");
sslContextFactory.setKeyStorePassword("xxxxxxxxx");
sslContextFactory.setIncludeProtocols("TLSv1.2");
return sslContextFactory;
}
private boolean startClient(String v4IpLocal, SslContextFactory sslContextFactory)
{
//local ip settings, set IPv4 as the default binding address, and will be changed when sending request
InetSocketAddress socketOfLocal;
if ((v4IpLocal != null) && (!v4IpLocal.equals("none"))) {
socketOfLocal = new InetSocketAddress(v4IpLocal, 0);
} else {
return false;
}
QueuedThreadPool clientThreads = new QueuedThreadPool();
clientThreads.setName("httpsClient");
httpsclient = new HttpClient(sslContextFactory);
httpsclient.setExecutor(clientThreads);
httpsclient.setBindAddress(socketOfLocal);
httpsclient.setConnectTimeout(3000);
httpsclient.setIdleTimeout(0); //ms, persistent connection?
httpsclient.setRemoveIdleDestinations(false);
httpsclient.setStrictEventOrdering(false);
try {
httpsclient.start();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
//......
}
Best Regards,
William Cao
I am trying to use Jetty client (9.4.7 and 9.2.24) to develop a https client (HTTP/1.1). When it tried to send out the request, the server can receive the request and respond it, and in the client side the jetty log also shows that it got 200 OK response. However, the client application got timeout. I think the major point is the onContent(Response response, ByteBuffer buffer, Callback callback), and we can call callback.succeeded() to terminate the onContent(...). How should I check that all the content is received so that I can call callback.succeeded() in onContent(...) so as to avoid the timeout?
Below is the jetty debug log and the code:
Padded plaintext after DECRYPTION: len = 456
0000: 48 54 54 50 2F 31 2E 31 20 32 30 30 20 4F 4B 0D HTTP/1.1 200 OK.
0010: 0A 53 65 6E 64 2D 54 69 6D 65 3A 20 31 35 32 36 .Send-Time: 1526
0020: 32 38 35 31 36 34 38 31 34 0D 0A 45 63 68 6F 2D 285164814..Echo-
...
The received content is <?xml version="1.0" encoding="UTF-8"?><cn:action xmlns:cn="urn:oma:xml:rest:netapi:callnotification:1"><actionToPerform>Continue</actionToPerform><decisionId>continue89271a52-08b6-4698-afd7-3e91a5b94050</decisionId></cn:action>
The request failed with Exception java.util.concurrent.TimeoutException: Total timeout 3000 ms elapsed
Exception with stacktrace: java.lang.Exception: java.util.concurrent.TimeoutException: Total timeout 3000 ms elapsed
com.mycom.securityas.http.HttpsRequestHandler$3.onFailure(HttpsRequestHandler.java:475)
org.eclipse.jetty.client.ResponseNotifier.notifyFailure(ResponseNotifier.java:170)
org.eclipse.jetty.client.ResponseNotifier.notifyFailure(ResponseNotifier.java:162)
org.eclipse.jetty.client.HttpReceiver.abort(HttpReceiver.java:533)
org.eclipse.jetty.client.HttpChannel.abortResponse(HttpChannel.java:129)
org.eclipse.jetty.client.HttpChannel.abort(HttpChannel.java:122)
org.eclipse.jetty.client.HttpExchange.abort(HttpExchange.java:257)
org.eclipse.jetty.client.HttpConversation.abort(HttpConversation.java:141)
org.eclipse.jetty.client.HttpRequest.abort(HttpRequest.java:748)
org.eclipse.jetty.client.TimeoutCompleteListener.run(TimeoutCompleteListener.java:71)
java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
java.util.concurrent.FutureTask.run(FutureTask.java:266)
java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
java.lang.Thread.run(Thread.java:748)
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.client.api.Response;
import org.eclipse.jetty.client.api.Result;
import org.eclipse.jetty.client.util.BytesContentProvider;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.util.Callback;
//......
public class HttpsRequestHandler {
......
private HttpClient httpsclient = null;
private SslContextFactory clientTLSFactory = null;
private void sendHttpsRequest(String destinationUrl, String msgBody) {
byte[] bytesBody = msgBody.getBytes(Charset.forName("UTF-8"));
Request httpsreq = client
.newRequest(destinationUrl)
.method(HttpMethod.POST)
.timeout(3000, TimeUnit.MILLISECONDS)
.content(new BytesContentProvider(bytesBody), "application/xml");
//...set headers ... ignored
httpsreq.send(new Response.Listener.Adapter() {
@Override
public void onContent(Response response, ByteBuffer buffer, Callback callback) {
System.out.println("Https request onContent 1");
StringBuffer sbf = new StringBuffer();
byte[] thebuffer = new byte[buffer.limit()];
buffer.get(thebuffer);
sbf.append(new String(thebuffer));
System.out.println("The received content is " + sbf.toString());
//callback.succeeded();
}
@Override
public void onFailure(Response response, Throwable failure) {
Exception e = new Exception(failure);
System.out.println("The request failed with Exception " + e.getMessage());
e.printStackTrace();
}
@Override
public void onSuccess(Response response) {
System.out.println("The request succeeded");
}
//...... //other logics
});
}
public boolean init() {}
clientTLSFactory = createSslContextFactory();
if (!startClient(clientTLSFactory)) {
return false;
}
return true;
}
private SslContextFactory createSslContextFactory() {
SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStorePath("/etc/pki/CA/cert/trust.jks");
sslContextFactory.setKeyStorePassword("xxxxxxxxx");
sslContextFactory.setIncludeProtocols("TLSv1.2");
return sslContextFactory;
}
private boolean startClient(String v4IpLocal, SslContextFactory sslContextFactory)
{
//local ip settings, set IPv4 as the default binding address, and will be changed when sending request
InetSocketAddress socketOfLocal;
if ((v4IpLocal != null) && (!v4IpLocal.equals("none"))) {
socketOfLocal = new InetSocketAddress(v4IpLocal, 0);
} else {
return false;
}
QueuedThreadPool clientThreads = new QueuedThreadPool();
clientThreads.setName("httpsClient");
httpsclient = new HttpClient(sslContextFactory);
httpsclient.setExecutor(clientThreads);
httpsclient.setBindAddress(socketOfLocal);
httpsclient.setConnectTimeout(3000);
httpsclient.setIdleTimeout(0); //ms, persistent connection?
httpsclient.setRemoveIdleDestinations(false);
httpsclient.setStrictEventOrdering(false);
try {
httpsclient.start();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
//......
}
Best Regards,
William Cao