获取GWTP placeManager的其他方法比使用Constructor的@Inject注释

时间:2016-03-03 11:17:12

标签: java gwt dependency-injection gwtp gwt-platform

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

要做到这一点,我必须以某种方式获取PlaceManager的实例(来自gwtp框架)。我不能使用@Inject注释,因为我有manuall调用构造函数如下:

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");
    }

}

1 个答案:

答案 0 :(得分:2)

您如何以及在何处创建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);
}

在您configure的{​​{1}}方法中,您需要致电:

ClientModule

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

install(new GinFactoryModuleBuilder().build(AssistedInjectionFactory.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