2

I am migrating from Jersey 1.19 to Jersey 2.25. I am not finding enough documentation to replace InjectableProvider

Can some one help me.

import java.lang.reflect.Type;
import java.util.List;
import java.util.Locale;

import javax.ws.rs.core.Context;
import javax.ws.rs.ext.Provider;

import org.glassfish.hk2.api.InjectionResolver;
import org.springframework.stereotype.Component;

import com.sun.jersey.api.core.HttpContext;
import com.sun.jersey.core.spi.component.ComponentContext;
import com.sun.jersey.core.spi.component.ComponentScope;
import com.sun.jersey.server.impl.inject.AbstractHttpContextInjectable;
import com.sun.jersey.spi.inject.Injectable;
import com.sun.jersey.spi.inject.InjectableProvider;

@Provider
@Component
public class LocaleProvider extends AbstractHttpContextInjectable<Locale>
    implements InjectionResolver<Context, Type> {

  private final Locale swedish = new Locale("sv", "", "");

  @Override
  public ComponentScope getScope() {
    return ComponentScope.PerRequest;
  }

  @Override
  public Injectable<Locale> getInjectable(ComponentContext ic, Context a, Type c) {
    if (c.equals(Locale.class)) {
      return this;
    }

    return null;
  }

  @Override
  public Locale getValue(HttpContext c) {
    final List<Locale> locales = c.getRequest().getAcceptableLanguages();
    if (locales.isEmpty()) {
      return Locale.US;
    }

    for (Locale locale : locales) {
      if (locale.getLanguage().equals(swedish.getLanguage())) {
        return swedish;
      } else if (locale.getLanguage().equals(Locale.US.getLanguage())) {
        return Locale.US;
      }
    }

    // return english if no other supported language is found
    return Locale.US;
  }

}
Patan
  • 17,073
  • 36
  • 124
  • 198

2 Answers2

1

Use an HK2 Factory. A helper abstract class is AbstractContainerRequestValueFactory, which give you easy access to the ContainerRequest. You can get the acceptable languages from that

public class LocaleFactory
        extends AbstractContainerRequestValueFactory<Local> {

    @Override
    public Locale provide() {
        ContainerRequest cr = getContainerRequest();
    }
}

Then you need to register it. If you are using a ResourceConfig you can do

public AppConfig extends ResourceConfig {
    public AppConfig() {
        register(new AbstractBinder() {
            @Override
            public void configure() {
                bindFactory(LocalFactory.class)
                    .to(Locale.class).in(RequestScoped.class);
            }
        });
    }
}

See Also:

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
0

AbstractContainerRequestValueFactory class has been removed, since Jersey 2.26 version. Alternatively, we can use org.glassfish.hk2.api.Factory to inject custom instance values which can be retrieved using @Context

Dependency

add jersey-hk2 dependency dependency in the classpath org.glassfish.jersey.inject:jersey-hk2

Factory class

Define the factory to construct the value to inject.

import org.glassfish.hk2.api.Factory;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.core.Context;
import javax.ws.rs.ext.Provider;
import java.util.List;
import java.util.Locale;

@Provider
public class LocaleFactory implements Factory<Locale> {
    private final Locale swedish = new Locale("sv", "", "");
    @Context
    private ContainerRequestContext ctx;

    @Override
    public Locale provide() {
        final List<Locale> locales = ctx.getAcceptableLanguages();
        if (locales.isEmpty()) {
            return Locale.US;
        }
        for (Locale locale : locales) {
            if (locale.getLanguage().equals(swedish.getLanguage())) {
                return swedish;
            } else if (locale.getLanguage().equals(Locale.US.getLanguage())) {
                return Locale.US;
            }
        }
        // return english if no other supported language is found
        return Locale.US;
    }

    @Override
    public void dispose(Locale instance) {/**Noop**/}
}

Registering the factory class

Registering LocaleFactory for the type Locale with the RequestScoped(for every request the LocaleFactory#provide method will be called)

import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.process.internal.RequestScoped;
import org.glassfish.jersey.server.ResourceConfig;
import javax.ws.rs.ext.Provider;
import java.util.Locale;
@Provider
class ApplicationConfig extends ResourceConfig {
    public ApplicationConfig() {
        register(new AbstractBinder() {
            @Override
            protected void configure() {
                bindFactory(LocaleFactory.class)
                        .to(Locale.class)
                        .in(RequestScoped.class);
            }
        });
    }
}
Prasanth Rajendran
  • 4,570
  • 2
  • 42
  • 59