7

是否可以重新发送 RequestFactory 传输?我想做与此等效的操作:使用 RequestFactory 时如何重新发送 GWT RPC 请求。从先前的请求重新发送相同的有效负载相当简单,但我还需要调用相同的方法。这是我的 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()
//--------------------------------------------------------------------------------------------------
}
4

2 回答 2

1

Based upon Thomas' suggestion I tried sending another request, and just replaced the payload and receiver in the RequestTransport.send() method, and this worked; I guess there is no further context retained by request factory, and that the response from the server is sufficient for RF to determine what needs to be done to unpack the response beyond the request and response that are returned to the RequestCallback.onResponseReceived() method. If anyone is interested in seeing my code then just let me know and I'll post it here.

于 2012-12-13T03:29:36.003 回答
0

这是可能的,但你有很多事情要做。

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

在我发出请求的任何地方,我都会处理响应并再次触发它。当然,你必须小心,不要陷入循环。

于 2012-12-12T08:56:45.840 回答