jetty.project
jetty.project copied to clipboard
How to duplicate the request without copying the failures from content?
I have to implement a case of failover, where in I have 2 servers - primary and secondary server. The client connects to primary server. In case of failure, it needs to send request to secondary server.
In case of failure, we are trying to duplicate request using the org.eclipse.jetty.client.HttpClient.copyRequest() with the conversation from old request and a new URI of secondary server.
In the copyRequest() method, body from old request is getting copied into the new request. The body also contains failure messages and exceptions of previous tries as primary server was down. Also the conversation contains the failure messages. We cleared the conversation in the new request and then tried to send this request to secondary server, it fails giving the same error as given by the primary server. In our case, the error was Connection Refused: No further information.
Our request body was of type RequestContentAdapter, so after getting failures, we created a new RequestContentAdapter with the ContentProvider from old request. It worked without giving failure this time.
But when we looked into content of each type of Request.Content, there is no generic method in Content class to copy the contents of the old request. There are multiple implementations of this class, each having failure in their objects and different way to get the content.
1) Is there any way to duplicate the request without copying the failures from previous request? 2) As per expectation, the copyRequest() method should copy the contents of the request and not the failures of previous request.
The code snippet was working in Jetty 9.x, but it started failing in Jetty 10.x
Request newRequest = super.copyRequest(oldRequest, newURI); ((HttpRequest) newRequest).getConversation().getExchanges().clear();
In Jetty 10.x, new code added for RequestContentAdapter so that failures are not getting copied:- if (oldRequest.getBody() != null && oldRequest.getBody() instanceof RequestContentAdapter) { RequestContentAdapter adapter = new RequestContentAdapter(((RequestContentAdapter) oldRequest.getBody()).getContentProvider()); newRequest.body(adapter); } But this code is specific to the content of type RequestContentAdapter. It will fail for other types of Content in Jetty 10.x.
HttpClient.copyRequest() is not meant to be used by applications.
Your best option is to create a new request for the new URI, and manually create the new request with the parts of the old request you want to copy.
The request content is, in general, not copyable (for example when it comes from an InputStream, that can be read only once).
Request contents can be rewound (by calling rewind()) for those that support it, but in general not all the request content can be rewound.
You should not be using ContentProvider anymore, as it is deprecated.
Please upgrade to Jetty 12, as Jetty 9, 10 and 11 are at end of community support.
This issue has been automatically marked as stale because it has been a full year without activity. It will be closed if no further activity occurs. Thank you for your contributions.
Closing as answered.