javagwtresty-gwt

RestyGWT- custom dispatcher doesn't call registered filters


I am trying to write my custom dispatcher for resty gwt.

My dispatcher RestyDispatcher will contain two filters:

I have problem, that my filters are registered by gwt/restygwt, unfortunatelly they don't work.

Here is code:

gin client module

public class ClientModule extends AbstractPresenterModule {
    @Override
    protected void configure() {
    install(new DefaultModule.Builder().defaultPlace(Routing.PAGE.url).errorPlace(Routing.PAGE.url).unauthorizedPlace(Routing.LOGIN.url).tokenFormatter(RouteTokenFormatter.class).build());
    install(new AppModule());

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

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

    bind(RestyGwtConfig.class).asEagerSingleton();
    }

}

RestyGwtConfig

public class RestyGwtConfig {
    public RestyGwtConfig() {
    final Dispatcher dispatcher = new RestyDispatcher();
    GWT.log("--> RestyGwtConfig -> setDispatcher");
    Defaults.setDispatcher(dispatcher);

    UserCredentials.INSTANCE.setUserName("ronan");
    UserCredentials.INSTANCE.setPassword("password");
    }

    static {
    GWT.log("--> RestyGwtConfig -> setServiceRoot");
    Defaults.setServiceRoot(new Resource(GWT.getModuleBaseURL()).resolve("../../cms/services").getUri());
    }

}

RestyDispatcher

public class RestyDispatcher extends DefaultFilterawareDispatcher {

    public RestyDispatcher() {
    addFilter(new ForbiddenDispatcherFilter());
    GWT.log("--> RestyDispatcher -> RestyDispatcher -> addFilter -> ForbiddenDispatcherFilter");
    addFilter(new BasicAuthHeaderDispatcherFilter());
    GWT.log("--> RestyDispatcher -> RestyDispatcher -> addFilter -> BasicAuthHeaderDispatcherFilter");
    }

    @Override
    public Request send(Method method, RequestBuilder builder) throws RequestException {
    GWT.log("SimpleDispatcher.Request.send.start");
    Request send = builder.send();
    GWT.log("SimpleDispatcher.Request.send.done");

    return send;
    }

}

ForbiddenDispatcherFilter

public class ForbiddenDispatcherFilter implements DispatcherFilter {

    @Override
    public boolean filter(Method method, RequestBuilder builder) {
    GWT.log("--> ForbiddenDispatcherFilter -> filter -> setCallback");
    builder.setCallback(new ForbiddenDispatcherCallback(method));
    return true;
    }

}

ForbiddenDispatcherCallback

public class ForbiddenDispatcherCallback implements RequestCallback {
    protected RequestCallback requestCallback;

    public ForbiddenDispatcherCallback(Method method) {
    GWT.log("_________> ForbiddenDispatcherCallback " + method.toString());

    this.requestCallback = method.builder.getCallback();
    }

    @Override
    public void onResponseReceived(Request request, Response response) {
    GWT.log("_________> ForbiddenDispatcherCallback -> onResponseReceived");

    GWT.log(response.getStatusText() + response.getStatusCode());
    if (response.getStatusCode() == Response.SC_FORBIDDEN || response.getStatusCode() == Response.SC_UNAUTHORIZED) {
        // make a hard redirect to login page
        Window.Location.assign("#/login");
    } else {
        requestCallback.onResponseReceived(request, response);
    }

    }

    @Override
    public void onError(Request request, Throwable exception) {
    GWT.log("_________> ForbiddenDispatcherCallback -> onError");
    requestCallback.onError(request, exception);
    }

}

BasicAuthHeaderDispatcherFilter

final class BasicAuthHeaderDispatcherFilter implements DispatcherFilter {

    public static final String AUTHORIZATION_HEADER = "Authorization";

    @Override
    public boolean filter(Method method, RequestBuilder builder) {
    String basicAuthHeaderValue = createBasicAuthHeader(UserCredentials.INSTANCE.getUserName(), UserCredentials.INSTANCE.getPassword());
    builder.setHeader(AUTHORIZATION_HEADER, basicAuthHeaderValue);

    return true;
    }

    private String createBasicAuthHeader(String userName, String password) {
    String credentials = userName + ":" + password;
    String encodedCredentials = credentials;
    return AUTHORIZATION_HEADER + ": Basic " + encodedCredentials;

    }
}

When I run app I get this logs in web browser console:

--> RestyGwtConfig -> setServiceRoot
SuperDevModeLogger.java:71--> RestyDispatcher -> RestyDispatcher -> addFilter -> ForbiddenDispatcherFilter
SuperDevModeLogger.java:71--> RestyDispatcher -> RestyDispatcher -> addFilter -> BasicAuthHeaderDispatcherFilter
SuperDevModeLogger.java:71--> RestyGwtConfig -> setDispatcher

So I assume that my Dispatcher, whith does 2 filters is registered correctly. Unfortunatelly I do not see log from ForbiddenDispatcherFilter saying that it set my callback ForbiddenDispatcherCallback. I also don't see any log from mentioned callback.

When I execute any resty service, I am getting logs entries:

SimpleDispatcher.Request.send.start SuperDevModeLogger.java:71

SimpleDispatcher.Request.send.done XMLHttpRequest.java:305

POST http://localhost:8080/cms/services/authenticated/testService/setInfo 401 (Unauthorized)

Cause I didn't found much about it in documentation, To achieve this I've base on those examples:

but none of this where enought specified. Please help.


Solution

  • I manually override send method in RestyDispatcher class, So my implementation have to manually execute super.send(method, builder). Or I should not override send method at all.

    So RestyDispatcher it should look like this:

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

    or

    public class RestyDispatcher extends DefaultFilterawareDispatcher {
    
        public RestyDispatcher() {
        addFilter(new ForbiddenDispatcherFilter());
        addFilter(new BasicAuthHeaderDispatcherFilter());
        }
    }