是否可以重新發送RequestFactory傳輸?在使用RequestFactory時,我希望這樣做:How to resend a GWT RPC request。從以前的請求重新發送相同的有效負載相當簡單,但我也需要調用相同的方法。這是我的RequestTransport課,我希望服用後護理只是「重燒」原來的要求,在這種情況下,用戶的請求登錄憑據:如何重新發送GWT RequestFactory請求
package org.greatlogic.rfexample2.client;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.web.bindery.requestfactory.gwt.client.DefaultRequestTransport;
/**
* Every request factory transmission will pass through the single instance of this class. This can
* be used to ensure that when a response is received any global conditions (e.g., the user is no
* longer logged in) can be handled in a consistent manner.
*/
public class RFERequestTransport extends DefaultRequestTransport {
//--------------------------------------------------------------------------------------------------
private IClientFactory _clientFactory;
//==================================================================================================
private final class RFERequestCallback implements RequestCallback {
private RequestCallback _requestCallback;
private RFERequestCallback(final RequestCallback requestCallback) {
_requestCallback = requestCallback;
} // RFERequestCallback()
@Override
public void onError(final Request request, final Throwable exception) {
_requestCallback.onError(request, exception);
} // onError()
@Override
public void onResponseReceived(final Request request, final Response response) {
if (response.getStatusCode() == Response.SC_UNAUTHORIZED) {
_clientFactory.login();
}
else {
_clientFactory.setLastPayload(null);
_clientFactory.setLastReceiver(null);
_requestCallback.onResponseReceived(request, response);
}
} // onResponseReceived()
} // class RFERequestCallback
//==================================================================================================
@Override
protected void configureRequestBuilder(final RequestBuilder builder) {
super.configureRequestBuilder(builder);
} // configureRequestBuilder()
//--------------------------------------------------------------------------------------------------
@Override
protected RequestCallback createRequestCallback(final TransportReceiver receiver) {
return new RFERequestCallback(super.createRequestCallback(receiver));
} // createRequestCallback()
//--------------------------------------------------------------------------------------------------
void initialize(final IClientFactory clientFactory) {
_clientFactory = clientFactory;
} // initialize()
//--------------------------------------------------------------------------------------------------
@Override
public void send(final String payload, final TransportReceiver receiver) {
String actualPayload = _clientFactory.getLastPayload();
TransportReceiver actualReceiver;
if (actualPayload == null) {
actualPayload = payload;
actualReceiver = receiver;
_clientFactory.setLastPayload(payload);
_clientFactory.setLastReceiver(receiver);
}
else {
actualReceiver = _clientFactory.getLastReceiver();
}
super.send(actualPayload, actualReceiver);
} // send()
//--------------------------------------------------------------------------------------------------
}
你的代碼很難閱讀,但你在正確的軌道上,所以你試過的是什麼問題? –
我的問題是知道如何發起觸發SC_UNAUTHORIZED響應的請求。例如,如果我有請求'requestContext.getFooById(fooId).fire(...)'並且這個請求返回未經授權的響應,那麼我想再次觸發它。我可以發送相同的有效負載,並回調到同一個接收器,但我不知道如何自動將「fire()」應用於「getFooId()」。 (順便說一下,是什麼讓我的代碼「難以閱讀」......我很好奇?「)。謝謝! –
如果你用相同的接收器發送相同的負載,會發生什麼?我的猜測是它應該Just Work™。我發現你的代碼很難閱讀,因爲缺乏縮進 - 主要 - 缺少空行和太多無用的評論;這是我猜想的味道的問題) –