Extending ApplicationConnection and overriding doAjaxRequest should be enough to achieve what you're trying to do. Something like this:
public class MyApplicationConnection extends ApplicationConnection {
private final Logger logger = Logger
.getLogger(MyApplicationConnection.class.getName());
@Override
protected void doAjaxRequest(final String uri, final JSONObject payload,
final RequestCallback requestCallback) throws RequestException {
// wrap the original request callback handle the retries
RequestCallback myRequestCallback = new RequestCallback() {
private int retries = 3;
@Override
public void onResponseReceived(Request request, Response response) {
int status = response.getStatusCode();
if (status / 100 == 2) { // 2xx Successful
requestCallback.onResponseReceived(request, response);
} else {
handleError(request, response, null);
}
}
@Override
public void onError(Request request, Throwable exception) {
handleError(request, null, exception);
}
private void handleError(Request request, Response response,
Throwable exception) {
if (retries == 0) {
logger.info("Ajax request failed.");
if (response == null) {
requestCallback.onError(request, exception);
} else {
requestCallback.onResponseReceived(request, response);
}
} else {
try {
logger.info("Ajax request failed, retrying " + retries
+ " more times.");
retries--;
MyApplicationConnection.super.doAjaxRequest(uri,
payload, this);
} catch (RequestException e) {
// something went wrong in the ajax send() call, so no
// point in retrying
logger.warning("Sending Ajax request failed. Cause: "
+ e.getMessage());
requestCallback.onError(request, exception);
}
}
}
};
super.doAjaxRequest(uri, payload, myRequestCallback);
}
}
And in your *.gwt.xml file:
<replace-with class="com.example.client.MyApplicationConnection">
<when-type-is class="com.vaadin.client.ApplicationConnection"/>
</replace-with>
You might also want to add a Timer or something to the handleError method, so that when the network is down, the request will wait for a while for it to come back up. It should be fairly trivial though.