实现GWTP placeManager的其他方式不是构造函数的@Inject注释 [英] Other way to achive GWTP placeManager than Constructor's @Inject annotation

查看:258
本文介绍了实现GWTP placeManager的其他方式不是构造函数的@Inject注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

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


public class ForbiddenDispatcherCallback实现RequestCallback {
protected RequestCallback requestCallback;

public ForbiddenDispatcherCallback(方法方法){
this.requestCallback = method.builder.getCallback();

$ b @Override
public void onResponseReceived(Request request,Response response){
if(response.getStatusCode()== Response.SC_FORBIDDEN || response。 getStatusCode()== Response.SC_UNAUTHORIZED){
//将硬重定向到登录页面
// TODO更改重定向到GWTP本地
Window.Location.assign(#login) ;
// PlaceRequest placeRequest = new
// PlaceRequest.Builder(placeManager.getCurrentPlaceRequest())。nameToken(Routing.Url.login).build();
// placeManager.revealPlace(placeRequest);
} else {
requestCallback.onResponseReceived(request,response);




$ b公共类RestyDispatcher扩展了DefaultFilterawareDispatcher {

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

$ b @Override
public请求发送(Method method,RequestBuilder builder)throws RequestException {
return super.send(method,builder);
}
}

请帮助。



编辑

p $ p $ 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(新的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);

//加载并注入CSS资源
bind(ResourceLoader.class).asEagerSingleton();





$ b

  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的 AssistedInjection PlaceManager 注入到 ForbiddenDispatcherCallback 中。

  public class ForbiddenDispatcherCallback implements RequestCallback {
protected RequestCallback requestCallback;
受保护的PlaceManager placeManager;

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


您需要定义一个工厂界面:

  public interface AssistedInjectionFactory {
ForbiddenDispatcherCallback createForbiddenCallback(Method method);

$ / code>

配置你需要调用 $ b

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

然后你可以用这种方式实例化你的类:

  public class ForbiddenDispatcherFilter实现DispatcherFilter {
AssistedInjectionFactory factory;

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

$ b @Override
public boolean filter(Method method,RequestBuilder builder){
builder.setCallback(factory.AssistedInjectionFactory(method))
return真正;


当然这需要你注入 ForbiddenDispatcherFilter

编辑:



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

  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过滤器){
addFilter(filter);
addFilter(new BasicAuthHeaderDispatcherFilter());

$ b @Override
public请求发送(Method method,RequestBuilder builder)throws RequestException {
return super.send(method,builder);
}
}


I use GWTP and restyGWT. I would like to use placeManager in restyGWT DispatcherCallback, when my rest server will answer with 401 unauthorised I would like to redirect application to login page, that User could apply credentials and retried his request.

To do this I have to somehow get instance of PlaceManager (from gwtp framework). I cannot use @Inject annotation, cause I have manuall call to constructor as follow:

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

Please help.


Edit

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();

    }

}

and:

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

}

解决方案

How and where do you create your ForbiddenDispatcherFilter ?

You could use guice's AssistedInjection to inject the PlaceManager into your 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();
    }
}

You need to define an factory interface:

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

In the configure method of your ClientModule you need to call:

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

Then you can instantiate your class this way:

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

Of course this requires that you also inject the ForbiddenDispatcherFilter.

Edit:

You could try to pass the RestyDispatcher to the constructor of your 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");
    }
}

The RestyDispatcher looks like this:

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

这篇关于实现GWTP placeManager的其他方式不是构造函数的@Inject注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆