下面列出了怎么用org.glassfish.jersey.server.ApplicationHandler的API类实例代码及写法,或者点击链接到github查看源代码。
static RouteStrategiesConfig validateRouteStrategies(
final ApplicationHandler applicationHandler,
final RouteExecutionStrategyFactory<HttpExecutionStrategy> strategyFactory) {
final ExtendedResourceContext resourceContext =
applicationHandler.getInjectionManager().getInstance(ExtendedResourceContext.class);
if (resourceContext == null) {
return new RouteStrategiesConfig(emptyMap(), emptyMap());
}
final RouteExecutionStrategyValidator validator = new RouteExecutionStrategyValidator(strategyFactory);
resourceContext.getResourceModel().accept(validator);
if (!validator.errors.isEmpty()) {
throw new IllegalArgumentException("Invalid execution strategy configuration found:\n" + validator.errors);
}
return new RouteStrategiesConfig(validator.routeStrategies, validator.methodDefaultStrategies);
}
/**
* An extending class must implement the {@link #configure()} method to
* provide an application descriptor.
*
* @throws TestContainerException if the default test container factory
* cannot be obtained, or the application descriptor is not
* supported by the test container factory.
*/
public SpringContextJerseyTest() throws TestContainerException {
ResourceConfig config = getResourceConfig(configure());
config.register(new ServiceFinderBinder<TestContainerFactory>(TestContainerFactory.class, null, RuntimeType.SERVER));
if (isLogRecordingEnabled()) {
registerLogHandler();
}
this.application = new ApplicationHandler(config);
this.tc = getContainer(application, getTestContainerFactory());
if (isLogRecordingEnabled()) {
loggedStartupRecords.addAll(loggedRuntimeRecords);
loggedRuntimeRecords.clear();
unregisterLogHandler();
}
}
/**
* Construct a new instance with a test container factory.
* <p/>
* An extending class must implement the {@link #configure()} method to
* provide an application descriptor.
*
* @param testContainerFactory the test container factory to use for testing.
* @throws TestContainerException if the application descriptor is not
* supported by the test container factory.
*/
public SpringContextJerseyTest(TestContainerFactory testContainerFactory) {
setTestContainerFactory(testContainerFactory);
ResourceConfig config = getResourceConfig(configure());
config.register(new ServiceFinderBinder<TestContainerFactory>(TestContainerFactory.class, null, RuntimeType.SERVER));
if (isLogRecordingEnabled()) {
registerLogHandler();
}
this.application = new ApplicationHandler(config);
this.tc = getContainer(application, testContainerFactory);
if (isLogRecordingEnabled()) {
loggedStartupRecords.addAll(loggedRuntimeRecords);
loggedRuntimeRecords.clear();
unregisterLogHandler();
}
}
/**
* An extending class must implement the {@link #configure()} method to
* provide an application descriptor.
*
* @throws TestContainerException if the default test container factory
* cannot be obtained, or the application descriptor is not
* supported by the test container factory.
*/
public SpringContextJerseyTest() throws TestContainerException {
ResourceConfig config = getResourceConfig(configure());
config.register(new ServiceFinderBinder<TestContainerFactory>(TestContainerFactory.class, null, RuntimeType.SERVER));
if (isLogRecordingEnabled()) {
registerLogHandler();
}
this.application = new ApplicationHandler(config);
this.tc = getContainer(application, getTestContainerFactory());
if (isLogRecordingEnabled()) {
loggedStartupRecords.addAll(loggedRuntimeRecords);
loggedRuntimeRecords.clear();
unregisterLogHandler();
}
}
/**
* Construct a new instance with a test container factory.
* <p/>
* An extending class must implement the {@link #configure()} method to
* provide an application descriptor.
*
* @param testContainerFactory the test container factory to use for testing.
* @throws TestContainerException if the application descriptor is not
* supported by the test container factory.
*/
public SpringContextJerseyTest(TestContainerFactory testContainerFactory) {
setTestContainerFactory(testContainerFactory);
ResourceConfig config = getResourceConfig(configure());
config.register(new ServiceFinderBinder<TestContainerFactory>(TestContainerFactory.class, null, RuntimeType.SERVER));
if (isLogRecordingEnabled()) {
registerLogHandler();
}
this.application = new ApplicationHandler(config);
this.tc = getContainer(application, testContainerFactory);
if (isLogRecordingEnabled()) {
loggedStartupRecords.addAll(loggedRuntimeRecords);
loggedRuntimeRecords.clear();
unregisterLogHandler();
}
}
@BeforeMethod
public void setup() {
applicationMapping = mock(ApplicationMapping.class);
request = mock(FullHttpRequest.class);
response = mock(FullHttpResponse.class);
when(request.getMethod()).thenReturn(HttpMethod.GET);
when(request.getUri()).thenReturn("/test");
when(request.headers()).thenReturn(new DefaultHttpHeaders());
when(request.content()).thenReturn(mock(ByteBuf.class));
when(request.getProtocolVersion()).thenReturn(HttpVersion.HTTP_1_1);
when(response.getStatus()).thenReturn(HttpResponseStatus.PROCESSING);
application = mock(Application.class);
context = mock(MessageContext.class);
router = spy(new Router(applicationMapping));
doReturn(new ApplicationHandler()).when(router).getApplicationHandler(application);
when(application.getPath()).thenReturn(URI.create("/app"));
when(context.getRequest()).thenReturn(request);
when(context.getResponse()).thenReturn(response);
when(context.getApplication()).thenReturn(application);
when(context.getBaseUri()).thenReturn(URI.create("http://localhost:8080"));
when(applicationMapping.resolve(request)).thenReturn(application);
}
/**
* An extending class must implement the {@link #configure()} method to
* provide an application descriptor.
*
* @throws TestContainerException if the default test container factory
* cannot be obtained, or the application descriptor is not
* supported by the test container factory.
*/
public SpringContextJerseyTest() throws TestContainerException {
ResourceConfig config = getResourceConfig(configure());
config.register(new ServiceFinderBinder<TestContainerFactory>(TestContainerFactory.class, null, RuntimeType.SERVER));
if (isLogRecordingEnabled()) {
registerLogHandler();
}
this.application = new ApplicationHandler(config);
this.tc = getContainer(application, getTestContainerFactory());
if (isLogRecordingEnabled()) {
loggedStartupRecords.addAll(loggedRuntimeRecords);
loggedRuntimeRecords.clear();
unregisterLogHandler();
}
}
/**
* Construct a new instance with a test container factory.
* <p/>
* An extending class must implement the {@link #configure()} method to
* provide an application descriptor.
*
* @param testContainerFactory the test container factory to use for testing.
* @throws TestContainerException if the application descriptor is not
* supported by the test container factory.
*/
public SpringContextJerseyTest(TestContainerFactory testContainerFactory) {
setTestContainerFactory(testContainerFactory);
ResourceConfig config = getResourceConfig(configure());
config.register(new ServiceFinderBinder<TestContainerFactory>(TestContainerFactory.class, null, RuntimeType.SERVER));
if (isLogRecordingEnabled()) {
registerLogHandler();
}
this.application = new ApplicationHandler(config);
this.tc = getContainer(application, testContainerFactory);
if (isLogRecordingEnabled()) {
loggedStartupRecords.addAll(loggedRuntimeRecords);
loggedRuntimeRecords.clear();
unregisterLogHandler();
}
}
DefaultJerseyStreamingHttpRouter(final Application application,
final int publisherInputStreamQueueCapacity,
final BiFunction<ConnectionContext, HttpRequestMetaData, String> baseUriFunction,
final RouteExecutionStrategyFactory<HttpExecutionStrategy> strategyFactory) {
this(new ApplicationHandler(application), publisherInputStreamQueueCapacity, baseUriFunction,
strategyFactory);
}
DefaultJerseyStreamingHttpRouter(final Class<? extends Application> applicationClass,
final int publisherInputStreamQueueCapacity,
final BiFunction<ConnectionContext, HttpRequestMetaData, String> baseUriFunction,
final RouteExecutionStrategyFactory<HttpExecutionStrategy> strategyFactory) {
this(new ApplicationHandler(applicationClass), publisherInputStreamQueueCapacity, baseUriFunction,
strategyFactory);
}
private DefaultJerseyStreamingHttpRouter(final ApplicationHandler applicationHandler,
final int publisherInputStreamQueueCapacity,
final BiFunction<ConnectionContext, HttpRequestMetaData,
String> baseUriFunction,
final RouteExecutionStrategyFactory<HttpExecutionStrategy>
strategyFactory) {
if (!applicationHandler.getConfiguration().isEnabled(ServiceTalkFeature.class)) {
throw new IllegalStateException("The " + ServiceTalkFeature.class.getSimpleName()
+ " needs to be enabled for this application.");
}
final RouteStrategiesConfig routeStrategiesConfig =
validateRouteStrategies(applicationHandler, strategyFactory);
this.applicationHandler = applicationHandler;
this.publisherInputStreamQueueCapacity = publisherInputStreamQueueCapacity;
this.baseUriFunction = requireNonNull(baseUriFunction);
applicationHandler.getInjectionManager().register(new AbstractBinder() {
@Override
protected void configure() {
bind(routeStrategiesConfig).to(RouteStrategiesConfig.class).proxy(false);
}
});
container = new DefaultContainer(applicationHandler);
applicationHandler.onStartup(container);
}
/**
* Constructs a new handler filter with a Jax RS application object.
* @param jaxApplication The JAX RS application to load
*/
JerseyHandlerFilter(Application jaxApplication) {
Timer.start("JERSEY_FILTER_CONSTRUCTOR");
app = jaxApplication;
jersey = new ApplicationHandler(app);
jersey.onStartup(this);
Timer.stop("JERSEY_FILTER_CONSTRUCTOR");
}
@Test
public void reload_ConfigGiven_ShouldReloadNewAppHandler() {
ResourceConfig config = new ApplicationHandler().getConfiguration();
ApplicationHandler newAppHandler = mock(ApplicationHandler.class);
doReturn(newAppHandler).when(container).createNewApplicationHandler(any());
container.reload(config);
verify(newAppHandler, times(1)).onReload(container);
}
@Test
public void reload_ConfigGiven_ShouldStartNewAppHandler() {
ResourceConfig config = new ApplicationHandler().getConfiguration();
ApplicationHandler newAppHandler = mock(ApplicationHandler.class);
doReturn(newAppHandler).when(container).createNewApplicationHandler(any());
container.reload(config);
verify(newAppHandler, times(1)).onStartup(container);
}
@Test
public void reload_ConfigGiven_ShouldResetAppHandler() {
ResourceConfig config = new ApplicationHandler().getConfiguration();
ApplicationHandler newAppHandler = mock(ApplicationHandler.class);
doReturn(newAppHandler).when(container).createNewApplicationHandler(any());
container.reload(config);
assertSame(newAppHandler, container.getApplicationHandler());
}
@Override
public void onAfterShutdown(Object obj) {
if (container instanceof NettyRestHandlerContainer) {
NettyRestHandlerContainer restHandlerContainer = ((NettyRestHandlerContainer) container);
ApplicationHandler applicationHandler = restHandlerContainer.getApplicationHandler();
ContainerLifecycleListener lifecycleListener = ConfigHelper.getContainerLifecycleListener(applicationHandler);
lifecycleListener.onShutdown(container);
}
}
@Override
public void onAfterStart(Object obj) {
if (container instanceof NettyRestHandlerContainer) {
NettyRestHandlerContainer restHandlerContainer = ((NettyRestHandlerContainer) container);
ApplicationHandler applicationHandler = restHandlerContainer.getApplicationHandler();
ContainerLifecycleListener lifecycleListener = ConfigHelper.getContainerLifecycleListener(applicationHandler);
lifecycleListener.onStartup(container);
}
}
@Override
public <T> T createContainer(Class<T> type, ApplicationHandler application) throws ProcessingException {
if (type != NettyRestHandlerContainer.class &&
(type == null || !ChannelHandler.class.isAssignableFrom(type))) {
return null;
}
return type.cast(new NettyRestHandlerContainer(application));
}
@Override
public void onShutdown(final Container container) {
final ApplicationHandler handler = container.getApplicationHandler();
final InjectionManager injectionManager = handler.getInjectionManager();
// Call @PreDestroy method on Application.
injectionManager.preDestroy(getWrappedApplication(handler.getConfiguration()));
// Shutdown ServiceLocator.
injectionManager.shutdown();
}
/**
* Construct a new instance with an application descriptor that defines
* how the test container is configured.
*
* @param jaxrsApplication an application describing how to configure the
* test container.
* @throws TestContainerException if the default test container factory
* cannot be obtained, or the application descriptor is not
* supported by the test container factory.
*/
public SpringContextJerseyTest(Application jaxrsApplication) throws TestContainerException {
ResourceConfig config = getResourceConfig(jaxrsApplication);
config.register(new ServiceFinderBinder<TestContainerFactory>(TestContainerFactory.class, null, RuntimeType.SERVER));
if (isLogRecordingEnabled()) {
registerLogHandler();
}
this.application = new ApplicationHandler(config);
this.tc = getContainer(application, getTestContainerFactory());
if (isLogRecordingEnabled()) {
loggedStartupRecords.addAll(loggedRuntimeRecords);
loggedRuntimeRecords.clear();
unregisterLogHandler();
}
}
/**
* Construct a new instance with an {@link Application} class.
*
* @param jaxrsApplicationClass an application describing how to configure the
* test container.
* @throws TestContainerException if the default test container factory
* cannot be obtained, or the application descriptor is not
* supported by the test container factory.
*/
public SpringContextJerseyTest(Class<? extends Application> jaxrsApplicationClass) throws TestContainerException {
ResourceConfig config = ResourceConfig.forApplicationClass(jaxrsApplicationClass);
config.register(new ServiceFinderBinder<TestContainerFactory>(TestContainerFactory.class, null, RuntimeType.SERVER));
if (isLogRecordingEnabled()) {
registerLogHandler();
}
this.application = new ApplicationHandler(config);
this.tc = getContainer(application, getTestContainerFactory());
if (isLogRecordingEnabled()) {
loggedStartupRecords.addAll(loggedRuntimeRecords);
loggedRuntimeRecords.clear();
unregisterLogHandler();
}
}
private TestContainer getContainer(ApplicationHandler application, TestContainerFactory tcf) {
if (application == null) {
throw new IllegalArgumentException("The application cannot be null");
}
return tcf.create(getBaseUri(), application);
}
@Override
public TestContainer create(URI baseUri, ApplicationHandler application) throws IllegalArgumentException {
URI uri = UriBuilder.fromUri(baseUri).port(baseUri.getPort() + getCount()).build();
System.out.println("Uri: " + uri);
System.out.println("App: " + application.getConfiguration().getApplication().getClass().getName());
return new MyTestContainer(uri, application);
}
public void init(String url) throws Exception {
URI uri = URI.create(url);
ApplicationHandler h = new ApplicationHandler(TestApplication.class);
WebContainerFactory factory = new WebContainerFactory();
factory.enableEncrementPort(false);
container = factory.create(uri,h);
container.start();
}
/**
* Construct a new instance with an application descriptor that defines
* how the test container is configured.
*
* @param jaxrsApplication an application describing how to configure the
* test container.
* @throws TestContainerException if the default test container factory
* cannot be obtained, or the application descriptor is not
* supported by the test container factory.
*/
public SpringContextJerseyTest(Application jaxrsApplication) throws TestContainerException {
ResourceConfig config = getResourceConfig(jaxrsApplication);
config.register(new ServiceFinderBinder<TestContainerFactory>(TestContainerFactory.class, null, RuntimeType.SERVER));
if (isLogRecordingEnabled()) {
registerLogHandler();
}
this.application = new ApplicationHandler(config);
this.tc = getContainer(application, getTestContainerFactory());
if (isLogRecordingEnabled()) {
loggedStartupRecords.addAll(loggedRuntimeRecords);
loggedRuntimeRecords.clear();
unregisterLogHandler();
}
}
/**
* Construct a new instance with an {@link Application} class.
*
* @param jaxrsApplicationClass an application describing how to configure the
* test container.
* @throws TestContainerException if the default test container factory
* cannot be obtained, or the application descriptor is not
* supported by the test container factory.
*/
public SpringContextJerseyTest(Class<? extends Application> jaxrsApplicationClass) throws TestContainerException {
ResourceConfig config = ResourceConfig.forApplicationClass(jaxrsApplicationClass);
config.register(new ServiceFinderBinder<TestContainerFactory>(TestContainerFactory.class, null, RuntimeType.SERVER));
if (isLogRecordingEnabled()) {
registerLogHandler();
}
this.application = new ApplicationHandler(config);
this.tc = getContainer(application, getTestContainerFactory());
if (isLogRecordingEnabled()) {
loggedStartupRecords.addAll(loggedRuntimeRecords);
loggedRuntimeRecords.clear();
unregisterLogHandler();
}
}
private TestContainer getContainer(ApplicationHandler application, TestContainerFactory tcf) {
if (application == null) {
throw new IllegalArgumentException("The application cannot be null");
}
return tcf.create(getBaseUri(), application);
}
/**
* Returns the handler for the given application
*
* @param application
* @return
*/
protected ApplicationHandler getApplicationHandler(Application<ApplicationConfiguration> application) {
ApplicationHandler handler = handlers.get(application);
if (handler == null) {
handler = createApplicationHandler(application.getResourceConfig());
handlers.put(application, handler);
}
return handler;
}
@Test
public void shouldInvokeApplicationHandlerOnRoute() {
ApplicationHandler handler = new ApplicationHandler();
doReturn(handler).when(router).getApplicationHandler(application);
router.route(context);
// FIXME: ApplicationHandler cant be mocked. How to test this ???
}
@Test
public void shouldCreateApplicationHandlerFirstTime() {
router = spy(new Router(applicationMapping));
ResourceConfig config = mock(ResourceConfig.class);
when(application.getResourceConfig()).thenReturn(config);
ApplicationHandler handler = new ApplicationHandler();
doReturn(handler).when(router).createApplicationHandler(config);
assertNotNull(router.getApplicationHandler(application));
}