如何重新发送GWT RequestFactory请求

时间:2012-12-12 05:59:04

标签: gwt gwt-rpc requestfactory

是否可以重新发送RequestFactory传输?在使用RequestFactory时,我想做相同的事情:How to resend a GWT RPC request。从先前的请求重新发送相同的有效负载相当简单,但我还需要调用相同的方法。这是我的RequestTransport类,我希望在处理用户登录凭据的请求之后,只需“重新启动”原始请求:

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()
//--------------------------------------------------------------------------------------------------
}

2 个答案:

答案 0 :(得分:1)

根据托马斯的建议,我尝试发送另一个请求,然后用RequestTransport.send()方法替换了有效载荷和接收器,这有效;我想请求工厂没有保留更多的上下文,并且来自服务器的响应足以让RF确定需要做什么才能将响应解压缩到返回的requestresponse之外到RequestCallback.onResponseReceived()方法。如果有人有兴趣看到我的代码,那就告诉我,我会在这里发布。

答案 1 :(得分:0)

这是可能的,但你还有很多工作要做。

我有同样的想法。我正在寻找一个好的解决方案大约2天。我试图拦截RequestContext.java和其他类上的服务器调用。但是,如果你这样做,你必须为几乎每一类gwt requestfactories自己实现。所以我决定采用一种更简单的方法。

在我发出请求的任何地方,我处理了响应并再次解雇了它。 当然,你必须小心,不要陷入困境。