实现 GWTP placeManager 的其他方法而不是 Constructor @Inject 注释

Other way to achive GWTP placeManager than Constructor's @Inject annotation

我使用 GWTP 和 restyGWT。我想在 restyGWT DispatcherCallback 中使用 placeManager,当我的休息服务器将回答 401 unauthorised 我想将应用程序重定向到登录页面,用户可以应用凭据并重试他的请求。

为此,我必须以某种方式获取 PlaceManager 的实例(来自 gwtp 框架)。我不能使用 @Inject 注释,因为我手动调用构造函数如下:

public class ForbiddenDispatcherFilter implements DispatcherFilter {
    @Override
    public boolean filter(Method method, RequestBuilder builder) {
        builder.setCallback(new ForbiddenDispatcherCallback(method));
        return true;
    }
}


public class ForbiddenDispatcherCallback implements RequestCallback {
    protected RequestCallback requestCallback;

    public ForbiddenDispatcherCallback(Method method) {
        this.requestCallback = method.builder.getCallback();
    }

    @Override
    public void onResponseReceived(Request request, Response response) {
    if (response.getStatusCode() == Response.SC_FORBIDDEN || response.getStatusCode() == Response.SC_UNAUTHORIZED) {
        // make a hard redirect to login page
        // TODO change redirect to GWTP native
        Window.Location.assign("#login");
        // PlaceRequest placeRequest = new
        // PlaceRequest.Builder(placeManager.getCurrentPlaceRequest()).nameToken(Routing.Url.login).build();
        // placeManager.revealPlace(placeRequest);
    } else {
        requestCallback.onResponseReceived(request, response);
    }

}


public class RestyDispatcher extends DefaultFilterawareDispatcher {

    public RestyDispatcher() {
    addFilter(new ForbiddenDispatcherFilter());
    addFilter(new BasicAuthHeaderDispatcherFilter());
    }

    @Override
    public Request send(Method method, RequestBuilder builder) throws RequestException {
    return super.send(method, builder);
    }
}

请帮忙


编辑

public class ClientModule extends AbstractPresenterModule {
    @Override
    protected void configure() {
    bind(RestyGwtConfig.class).asEagerSingleton();

    install(new DefaultModule.Builder()//
        .defaultPlace(Routing.HOME.url)//
        .errorPlace(Routing.ERROR.url)//
        .unauthorizedPlace(Routing.LOGIN.url)//
        .tokenFormatter(RouteTokenFormatter.class).build());
    install(new AppModule());
    // install(new
    // GinFactoryModuleBuilder().build(AssistedInjectionFactory.class));

    bind(CurrentUser.class).in(Singleton.class);
    bind(IsAdminGatekeeper.class).in(Singleton.class);
    bind(UserLoginGatekeeper.class).in(Singleton.class);

    // Google Analytics
    // bindConstant().annotatedWith(GaAccount.class).to("UA-8319339-6");

    // Load and inject CSS resources
    bind(ResourceLoader.class).asEagerSingleton();

    }

}

和:

public class RestyGwtConfig {

    static {

    // GWT.log("--> RestyGwtConfig -> setDispatcher");
    Defaults.setDispatcher(new RestyDispatcher());

    // GWT.log("--> RestyGwtConfig -> setServiceRoot");
    Defaults.setServiceRoot(new Resource(GWT.getModuleBaseURL()).resolve(ServiceRouting.SERVICE_ROOT).getUri());
    UserCredentials.INSTANCE.setUserName("ronan");
    UserCredentials.INSTANCE.setPassword("password");
    }

}

您如何以及在哪里创建您的 ForbiddenDispatcherFilter

您可以使用 guice 的 AssistedInjectionPlaceManager 注入您的 ForbiddenDispatcherCallback

public class ForbiddenDispatcherCallback implements RequestCallback {
    protected RequestCallback requestCallback;
    protected PlaceManager placeManager;

    @Inject
    public ForbiddenDispatcherCallback(PlaceManager placeManager, @Assisted Method method) {
        this.placeManager = placeManager;
        this.requestCallback = method.builder.getCallback();
    }
}

您需要定义一个工厂接口:

public interface AssistedInjectionFactory {
    ForbiddenDispatcherCallback createForbiddenCallback(Method method);
}

ClientModuleconfigure 方法中,您需要调用:

install(new GinFactoryModuleBuilder().build(AssistedInjectionFactory.class));

然后你可以这样实例化你的class:

public class ForbiddenDispatcherFilter implements DispatcherFilter {
    AssistedInjectionFactory factory;

    @Inject
    public ForbiddenDispatcherFilter(AssistedInjectionFactory factory) 
    {
        this.factory = factory;
    }

    @Override
    public boolean filter(Method method, RequestBuilder builder) {
        builder.setCallback(factory.AssistedInjectionFactory(method)) 
        return true;
    }
}

当然这需要你也注入ForbiddenDispatcherFilter

编辑:

您可以尝试将 RestyDispatcher 传递给 RestyGWTConfig 的构造函数:

public class RestyGwtConfig {

    @Inject
    public RestyGwtConfig(RestyDispatcher dispatcher) {
        Defaults.setDispatcher(dispatcher);
    }

    static {
    // GWT.log("--> RestyGwtConfig -> setServiceRoot");
    Defaults.setServiceRoot(new Resource(GWT.getModuleBaseURL()).resolve(ServiceRouting.SERVICE_ROOT).getUri());
    UserCredentials.INSTANCE.setUserName("ronan");
    UserCredentials.INSTANCE.setPassword("password");
    }
}

RestyDispatcher 看起来像这样:

public class RestyDispatcher extends DefaultFilterawareDispatcher {

    @Inject
    public RestyDispatcher(ForbiddenDispatcherFilter filter) {
      addFilter(filter);
      addFilter(new BasicAuthHeaderDispatcherFilter());
    }

    @Override
    public Request send(Method method, RequestBuilder builder) throws RequestException {
      return super.send(method, builder);
    }
}