com.google.inject.servlet.RequestScoped Java Examples

The following examples show how to use com.google.inject.servlet.RequestScoped. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GuiceScopingVisitor.java    From dropwizard-guicey with MIT License 6 votes vote down vote up
@Override
public Class<? extends Annotation> visitScope(final Scope scope) {
    Class<? extends Annotation> res = null;
    if (scope == Scopes.SINGLETON) {
        res = javax.inject.Singleton.class;
    }
    if (scope == Scopes.NO_SCOPE) {
        res = Prototype.class;
    }
    if (scope == ServletScopes.REQUEST) {
        res = RequestScoped.class;
    }
    if (scope == ServletScopes.SESSION) {
        res = SessionScoped.class;
    }
    // not supporting custom scopes
    return res;
}
 
Example #2
Source File: WebAppTests.java    From hadoop with Apache License 2.0 4 votes vote down vote up
/**
 * Create a mock injector for tests
 * @param <T> type of class/interface
 * @param api the interface class of the object to inject
 * @param impl the implementation object to inject
 * @param modules additional guice modules
 * @return an injector
 */
public static <T> Injector createMockInjector(final Class<T> api,
                                              final T impl,
                                              final Module... modules) {
  return Guice.createInjector(new AbstractModule() {
    final PrintWriter writer = spy(new PrintWriter(System.out));
    final HttpServletRequest request = createRequest();
    final HttpServletResponse response = createResponse();

    @Override
    protected void configure() {
      if (api != null) {
        bind(api).toInstance(impl);
      }
      bindScope(RequestScoped.class, Scopes.SINGLETON);
      if (modules != null) {
        for (Module module : modules) {
          install(module);
        }
      }
    }

    @Provides HttpServletRequest request() {
      return request;
    }

    @Provides HttpServletResponse response() {
      return response;
    }

    @Provides PrintWriter writer() {
      return writer;
    }

    HttpServletRequest createRequest() {
      // the default suffices for now
      return mock(HttpServletRequest.class);
    }

    HttpServletResponse createResponse() {
      try {
        HttpServletResponse res = mock(HttpServletResponse.class);
        when(res.getWriter()).thenReturn(writer);
        return res;
      } catch (Exception e) {
        throw new WebAppException(e);
      }
    }
  });
}
 
Example #3
Source File: WebAppTests.java    From big-c with Apache License 2.0 4 votes vote down vote up
/**
 * Create a mock injector for tests
 * @param <T> type of class/interface
 * @param api the interface class of the object to inject
 * @param impl the implementation object to inject
 * @param modules additional guice modules
 * @return an injector
 */
public static <T> Injector createMockInjector(final Class<T> api,
                                              final T impl,
                                              final Module... modules) {
  return Guice.createInjector(new AbstractModule() {
    final PrintWriter writer = spy(new PrintWriter(System.out));
    final HttpServletRequest request = createRequest();
    final HttpServletResponse response = createResponse();

    @Override
    protected void configure() {
      if (api != null) {
        bind(api).toInstance(impl);
      }
      bindScope(RequestScoped.class, Scopes.SINGLETON);
      if (modules != null) {
        for (Module module : modules) {
          install(module);
        }
      }
    }

    @Provides HttpServletRequest request() {
      return request;
    }

    @Provides HttpServletResponse response() {
      return response;
    }

    @Provides PrintWriter writer() {
      return writer;
    }

    HttpServletRequest createRequest() {
      // the default suffices for now
      return mock(HttpServletRequest.class);
    }

    HttpServletResponse createResponse() {
      try {
        HttpServletResponse res = mock(HttpServletResponse.class);
        when(res.getWriter()).thenReturn(writer);
        return res;
      } catch (Exception e) {
        throw new WebAppException(e);
      }
    }
  });
}
 
Example #4
Source File: JerseyGuicierModule.java    From dropwizard-guicier with Apache License 2.0 4 votes vote down vote up
@Provides
@RequestScoped
public ContainerRequestContext providesContainerRequestContext(ServiceLocator serviceLocator) {
  return serviceLocator.getService(ContainerRequestContext.class);
}
 
Example #5
Source File: JerseyGuicierModule.java    From dropwizard-guicier with Apache License 2.0 4 votes vote down vote up
@Provides
@RequestScoped
public ExtendedUriInfo providesExtendedUriInfo(ServiceLocator serviceLocator) {
  return serviceLocator.getService(ExtendedUriInfo.class);
}
 
Example #6
Source File: GraphGuiceModule.java    From Orienteer with Apache License 2.0 4 votes vote down vote up
@Provides
@RequestScoped
public OrientGraph provideOrientGraph(ODatabaseDocumentTx dbTx)
{
	return new OrientGraph(dbTx, false);
}
 
Example #7
Source File: GuiceBindingsModule.java    From dropwizard-guicey with MIT License 3 votes vote down vote up
/**
 * Important moment: request scoped jersey objects must be bound to guice request scope (if guice web used)
 * because otherwise scope delegation to other thread will not work
 * (see {@link com.google.inject.servlet.ServletScopes#transferRequest(java.util.concurrent.Callable)}).
 * <p>
 * WARNING: bean instance must be obtained in current (request) thread in order to be us used later
 * inside transferred thread (simply call {@code provider.get()} (for jersey-managed bean like {@link UriInfo})
 * before {@code ServletScopes.transferRequest()}.
 *
 * @param type   jersey type to bind
 * @param global true for global type binding
 */
private void jerseyToGuiceBinding(final Class<?> type, final boolean global) {
    final ScopedBindingBuilder binding = bindJerseyComponent(binder(), provider, type);
    if (!global && guiceServletSupport) {
        binding.in(RequestScoped.class);
    }
}