org.springframework.web.server.WebHandler Java Examples
The following examples show how to use
org.springframework.web.server.WebHandler.
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: ExploreSpring5URLPatternUsingRouterFunctions.java From tutorials with MIT License | 6 votes |
WebServer start() throws Exception { WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction()); HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler) .filter(new IndexRewriteFilter()) .build(); Tomcat tomcat = new Tomcat(); tomcat.setHostname("localhost"); tomcat.setPort(9090); Context rootContext = tomcat.addContext("", System.getProperty("java.io.tmpdir")); ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler); Wrapper servletWrapper = Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet); servletWrapper.setAsyncSupported(true); rootContext.addServletMappingDecoded("/", "httpHandlerServlet"); TomcatWebServer server = new TomcatWebServer(tomcat); server.start(); return server; }
Example #2
Source File: WebHttpHandlerBuilder.java From spring-analysis-note with MIT License | 6 votes |
/** * Build the {@link HttpHandler}. */ public HttpHandler build() { WebHandler decorated = new FilteringWebHandler(this.webHandler, this.filters); decorated = new ExceptionHandlingWebHandler(decorated, this.exceptionHandlers); HttpWebHandlerAdapter adapted = new HttpWebHandlerAdapter(decorated); if (this.sessionManager != null) { adapted.setSessionManager(this.sessionManager); } if (this.codecConfigurer != null) { adapted.setCodecConfigurer(this.codecConfigurer); } if (this.localeContextResolver != null) { adapted.setLocaleContextResolver(this.localeContextResolver); } if (this.forwardedHeaderTransformer != null) { adapted.setForwardedHeaderTransformer(this.forwardedHeaderTransformer); } if (this.applicationContext != null) { adapted.setApplicationContext(this.applicationContext); } adapted.afterPropertiesSet(); return adapted; }
Example #3
Source File: WebHttpHandlerBuilder.java From java-technology-stack with MIT License | 6 votes |
/** * Build the {@link HttpHandler}. */ public HttpHandler build() { WebHandler decorated = new FilteringWebHandler(this.webHandler, this.filters); decorated = new ExceptionHandlingWebHandler(decorated, this.exceptionHandlers); HttpWebHandlerAdapter adapted = new HttpWebHandlerAdapter(decorated); if (this.sessionManager != null) { adapted.setSessionManager(this.sessionManager); } if (this.codecConfigurer != null) { adapted.setCodecConfigurer(this.codecConfigurer); } if (this.localeContextResolver != null) { adapted.setLocaleContextResolver(this.localeContextResolver); } if (this.forwardedHeaderTransformer != null) { adapted.setForwardedHeaderTransformer(this.forwardedHeaderTransformer); } if (this.applicationContext != null) { adapted.setApplicationContext(this.applicationContext); } adapted.afterPropertiesSet(); return adapted; }
Example #4
Source File: SimpleUrlHandlerMappingIntegrationTests.java From spring-analysis-note with MIT License | 6 votes |
@Bean public SimpleUrlHandlerMapping handlerMapping() { return new SimpleUrlHandlerMapping() { { Map<String, Object> map = new HashMap<>(); map.put("/foo", (WebHandler) exchange -> exchange.getResponse().writeWith(Flux.just(asDataBuffer("foo")))); map.put("/bar", (WebHandler) exchange -> exchange.getResponse().writeWith(Flux.just(asDataBuffer("bar")))); map.put("/header", (WebHandler) exchange -> { exchange.getResponse().getHeaders().add("foo", "bar"); return Mono.empty(); }); setUrlMap(map); } }; }
Example #5
Source File: SimpleUrlHandlerMappingIntegrationTests.java From java-technology-stack with MIT License | 6 votes |
@Bean public SimpleUrlHandlerMapping handlerMapping() { return new SimpleUrlHandlerMapping() { { Map<String, Object> map = new HashMap<>(); map.put("/foo", (WebHandler) exchange -> exchange.getResponse().writeWith(Flux.just(asDataBuffer("foo")))); map.put("/bar", (WebHandler) exchange -> exchange.getResponse().writeWith(Flux.just(asDataBuffer("bar")))); map.put("/header", (WebHandler) exchange -> { exchange.getResponse().getHeaders().add("foo", "bar"); return Mono.empty(); }); setUrlMap(map); } }; }
Example #6
Source File: FunctionalWebApplication.java From tutorials with MIT License | 6 votes |
WebServer start() throws Exception { WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction()); HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler) .filter(new IndexRewriteFilter()) .build(); Tomcat tomcat = new Tomcat(); tomcat.setHostname("localhost"); tomcat.setPort(9090); Context rootContext = tomcat.addContext("", System.getProperty("java.io.tmpdir")); ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler); Wrapper servletWrapper = Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet); servletWrapper.setAsyncSupported(true); rootContext.addServletMappingDecoded("/", "httpHandlerServlet"); TomcatWebServer server = new TomcatWebServer(tomcat); server.start(); return server; }
Example #7
Source File: FunctionalWebApplication.java From tutorials with MIT License | 6 votes |
WebServer start() throws Exception { WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction()); HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler) .filter(new IndexRewriteFilter()) .build(); Tomcat tomcat = new Tomcat(); tomcat.setHostname("localhost"); tomcat.setPort(9090); Context rootContext = tomcat.addContext("", System.getProperty("java.io.tmpdir")); ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler); Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet); rootContext.addServletMappingDecoded("/", "httpHandlerServlet"); TomcatWebServer server = new TomcatWebServer(tomcat); server.start(); return server; }
Example #8
Source File: DefaultWebFilterChain.java From spring-analysis-note with MIT License | 5 votes |
private static DefaultWebFilterChain initChain(List<WebFilter> filters, WebHandler handler) { DefaultWebFilterChain chain = new DefaultWebFilterChain(filters, handler, null, null); ListIterator<? extends WebFilter> iterator = filters.listIterator(filters.size()); while (iterator.hasPrevious()) { chain = new DefaultWebFilterChain(filters, handler, iterator.previous(), chain); } return chain; }
Example #9
Source File: DefaultWebFilterChain.java From spring-analysis-note with MIT License | 5 votes |
/** * Private constructor to represent one link in the chain. */ private DefaultWebFilterChain(List<WebFilter> allFilters, WebHandler handler, @Nullable WebFilter currentFilter, @Nullable DefaultWebFilterChain chain) { this.allFilters = allFilters; this.currentFilter = currentFilter; this.handler = handler; this.chain = chain; }
Example #10
Source File: DefaultWebFilterChain.java From spring-analysis-note with MIT License | 5 votes |
/** * Public constructor with the list of filters and the target handler to use. * @param handler the target handler * @param filters the filters ahead of the handler * @since 5.1 */ public DefaultWebFilterChain(WebHandler handler, List<WebFilter> filters) { Assert.notNull(handler, "WebHandler is required"); this.allFilters = Collections.unmodifiableList(filters); this.handler = handler; DefaultWebFilterChain chain = initChain(filters, handler); this.currentFilter = chain.currentFilter; this.chain = chain.chain; }
Example #11
Source File: Application.java From spring-reactive-sample with GNU General Public License v3.0 | 5 votes |
public static void main(String[] args) throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); PostRepository posts = new PostRepository(); PostHandler postHandler = new PostHandler(posts); Routes routesBean = new Routes(postHandler); context.registerBean(PostRepository.class, () -> posts); context.registerBean(PostHandler.class, () -> postHandler); context.registerBean(Routes.class, () -> routesBean); context.registerBean(WebHandler.class, () -> RouterFunctions.toWebHandler(routesBean.routes(), HandlerStrategies.builder().build())); context.refresh(); nettyServer(context).onDispose().block(); }
Example #12
Source File: FunctionalSpringBootApplication.java From tutorials with MIT License | 5 votes |
@Bean public ServletRegistrationBean servletRegistrationBean() throws Exception { HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler((WebHandler) toHttpHandler(routingFunction())) .filter(new IndexRewriteFilter()) .build(); ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(new RootServlet(httpHandler), "/"); registrationBean.setLoadOnStartup(1); registrationBean.setAsyncSupported(true); return registrationBean; }
Example #13
Source File: WebFluxConfigurationSupportTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void resourceHandler() throws Exception { ApplicationContext context = loadConfig(CustomResourceHandlingConfig.class); String name = "resourceHandlerMapping"; AbstractUrlHandlerMapping handlerMapping = context.getBean(name, AbstractUrlHandlerMapping.class); assertNotNull(handlerMapping); assertEquals(Ordered.LOWEST_PRECEDENCE - 1, handlerMapping.getOrder()); SimpleUrlHandlerMapping urlHandlerMapping = (SimpleUrlHandlerMapping) handlerMapping; WebHandler webHandler = (WebHandler) urlHandlerMapping.getUrlMap().get("/images/**"); assertNotNull(webHandler); }
Example #14
Source File: DispatcherHandlerErrorTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void webExceptionHandler() { ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/unknown-argument-type")); List<WebExceptionHandler> handlers = Collections.singletonList(new ServerError500ExceptionHandler()); WebHandler webHandler = new ExceptionHandlingWebHandler(this.dispatcherHandler, handlers); webHandler.handle(exchange).block(Duration.ofSeconds(5)); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, exchange.getResponse().getStatusCode()); }
Example #15
Source File: ResourceHandlerRegistry.java From spring-analysis-note with MIT License | 5 votes |
/** * Return a handler mapping with the mapped resource handlers; or {@code null} in case * of no registrations. */ @Nullable protected AbstractUrlHandlerMapping getHandlerMapping() { if (this.registrations.isEmpty()) { return null; } Map<String, WebHandler> urlMap = new LinkedHashMap<>(); for (ResourceHandlerRegistration registration : this.registrations) { for (String pathPattern : registration.getPathPatterns()) { ResourceWebHandler handler = registration.getRequestHandler(); handler.getResourceTransformers().forEach(transformer -> { if (transformer instanceof ResourceTransformerSupport) { ((ResourceTransformerSupport) transformer).setResourceUrlProvider(this.resourceUrlProvider); } }); try { handler.afterPropertiesSet(); } catch (Throwable ex) { throw new BeanInitializationException("Failed to init ResourceHttpRequestHandler", ex); } urlMap.put(pathPattern, handler); } } SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping(); handlerMapping.setOrder(this.order); handlerMapping.setUrlMap(urlMap); return handlerMapping; }
Example #16
Source File: RouterFunctions.java From spring-analysis-note with MIT License | 5 votes |
/** * Convert the given {@linkplain RouterFunction router function} into a {@link WebHandler}, * using the given strategies. * @param routerFunction the router function to convert * @param strategies the strategies to use * @return a web handler that handles web request using the given router function */ public static WebHandler toWebHandler(RouterFunction<?> routerFunction, HandlerStrategies strategies) { Assert.notNull(routerFunction, "RouterFunction must not be null"); Assert.notNull(strategies, "HandlerStrategies must not be null"); return exchange -> { ServerRequest request = new DefaultServerRequest(exchange, strategies.messageReaders()); addAttributes(exchange, request); return routerFunction.route(request) .defaultIfEmpty(notFound()) .flatMap(handlerFunction -> wrapException(() -> handlerFunction.handle(request))) .flatMap(response -> wrapException(() -> response.writeTo(exchange, new HandlerStrategiesResponseContext(strategies)))); }; }
Example #17
Source File: ExceptionHandlingWebHandler.java From spring-analysis-note with MIT License | 5 votes |
public ExceptionHandlingWebHandler(WebHandler delegate, List<WebExceptionHandler> handlers) { super(delegate); List<WebExceptionHandler> handlersToUse = new ArrayList<>(); handlersToUse.add(new CheckpointInsertingHandler()); handlersToUse.addAll(handlers); this.exceptionHandlers = Collections.unmodifiableList(handlersToUse); }
Example #18
Source File: WebHttpHandlerBuilderTests.java From spring-analysis-note with MIT License | 5 votes |
@Bean public WebHandler webHandler() { return exchange -> { String value = exchange.getAttributeOrDefault(ATTRIBUTE, "none"); return writeToResponse(exchange, value); }; }
Example #19
Source File: SpringWebFluxTest.java From java-specialagent with Apache License 2.0 | 5 votes |
@BeforeClass public static void beforeClass() { APPLICATION_CONTEXT.registerBean("jettyReactiveWebServerFactory", JettyReactiveWebServerFactory.class, () -> new JettyReactiveWebServerFactory(0)); APPLICATION_CONTEXT.registerBean("httpHandler", HttpHandler.class, () -> WebHttpHandlerBuilder.applicationContext(APPLICATION_CONTEXT).build()); APPLICATION_CONTEXT.registerBean("webHandler", WebHandler.class, () -> SpringWebFluxTest::handler); APPLICATION_CONTEXT.refresh(); final int serverPort = APPLICATION_CONTEXT.getWebServer().getPort(); testRestTemplate = new TestRestTemplate(new RestTemplateBuilder().rootUri("http://127.0.0.1:" + serverPort)); }
Example #20
Source File: DefaultRouterFunctionSpec.java From java-technology-stack with MIT License | 5 votes |
@Override protected WebHttpHandlerBuilder initHttpHandlerBuilder() { WebHandler webHandler = RouterFunctions.toWebHandler(this.routerFunction, this.handlerStrategies); return WebHttpHandlerBuilder.webHandler(webHandler) .filters(filters -> filters.addAll(this.handlerStrategies.webFilters())) .exceptionHandlers(handlers -> handlers.addAll(this.handlerStrategies.exceptionHandlers())) .localeContextResolver(this.handlerStrategies.localeContextResolver()); }
Example #21
Source File: DefaultWebFilterChain.java From java-technology-stack with MIT License | 5 votes |
private static DefaultWebFilterChain initChain(List<WebFilter> filters, WebHandler handler) { DefaultWebFilterChain chain = new DefaultWebFilterChain(filters, handler, null, null); ListIterator<? extends WebFilter> iterator = filters.listIterator(filters.size()); while (iterator.hasPrevious()) { chain = new DefaultWebFilterChain(filters, handler, iterator.previous(), chain); } return chain; }
Example #22
Source File: DefaultRouterFunctionSpec.java From spring-analysis-note with MIT License | 5 votes |
@Override protected WebHttpHandlerBuilder initHttpHandlerBuilder() { WebHandler webHandler = RouterFunctions.toWebHandler(this.routerFunction, this.handlerStrategies); return WebHttpHandlerBuilder.webHandler(webHandler) .filters(filters -> filters.addAll(this.handlerStrategies.webFilters())) .exceptionHandlers(handlers -> handlers.addAll(this.handlerStrategies.exceptionHandlers())) .localeContextResolver(this.handlerStrategies.localeContextResolver()); }
Example #23
Source File: WebHttpHandlerBuilderTests.java From java-technology-stack with MIT License | 5 votes |
@Bean public WebHandler webHandler() { return exchange -> { String value = exchange.getAttributeOrDefault(ATTRIBUTE, "none"); return writeToResponse(exchange, value); }; }
Example #24
Source File: RouterFunctions.java From java-technology-stack with MIT License | 5 votes |
/** * Convert the given {@linkplain RouterFunction router function} into a {@link WebHandler}, * using the given strategies. * @param routerFunction the router function to convert * @param strategies the strategies to use * @return a web handler that handles web request using the given router function */ public static WebHandler toWebHandler(RouterFunction<?> routerFunction, HandlerStrategies strategies) { Assert.notNull(routerFunction, "RouterFunction must not be null"); Assert.notNull(strategies, "HandlerStrategies must not be null"); return exchange -> { ServerRequest request = new DefaultServerRequest(exchange, strategies.messageReaders()); addAttributes(exchange, request); return routerFunction.route(request) .defaultIfEmpty(notFound()) .flatMap(handlerFunction -> wrapException(() -> handlerFunction.handle(request))) .flatMap(response -> wrapException(() -> response.writeTo(exchange, new HandlerStrategiesResponseContext(strategies)))); }; }
Example #25
Source File: ResourceHandlerRegistry.java From java-technology-stack with MIT License | 5 votes |
/** * Return a handler mapping with the mapped resource handlers; or {@code null} in case * of no registrations. */ @Nullable protected AbstractUrlHandlerMapping getHandlerMapping() { if (this.registrations.isEmpty()) { return null; } Map<String, WebHandler> urlMap = new LinkedHashMap<>(); for (ResourceHandlerRegistration registration : this.registrations) { for (String pathPattern : registration.getPathPatterns()) { ResourceWebHandler handler = registration.getRequestHandler(); handler.getResourceTransformers().forEach(transformer -> { if (transformer instanceof ResourceTransformerSupport) { ((ResourceTransformerSupport) transformer).setResourceUrlProvider(this.resourceUrlProvider); } }); try { handler.afterPropertiesSet(); } catch (Throwable ex) { throw new BeanInitializationException("Failed to init ResourceHttpRequestHandler", ex); } urlMap.put(pathPattern, handler); } } SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping(); handlerMapping.setOrder(this.order); handlerMapping.setUrlMap(urlMap); return handlerMapping; }
Example #26
Source File: DispatcherHandlerErrorTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void webExceptionHandler() { ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/unknown-argument-type")); List<WebExceptionHandler> handlers = Collections.singletonList(new ServerError500ExceptionHandler()); WebHandler webHandler = new ExceptionHandlingWebHandler(this.dispatcherHandler, handlers); webHandler.handle(exchange).block(Duration.ofSeconds(5)); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, exchange.getResponse().getStatusCode()); }
Example #27
Source File: WebFluxConfigurationSupportTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void resourceHandler() throws Exception { ApplicationContext context = loadConfig(CustomResourceHandlingConfig.class); String name = "resourceHandlerMapping"; AbstractUrlHandlerMapping handlerMapping = context.getBean(name, AbstractUrlHandlerMapping.class); assertNotNull(handlerMapping); assertEquals(Ordered.LOWEST_PRECEDENCE - 1, handlerMapping.getOrder()); SimpleUrlHandlerMapping urlHandlerMapping = (SimpleUrlHandlerMapping) handlerMapping; WebHandler webHandler = (WebHandler) urlHandlerMapping.getUrlMap().get("/images/**"); assertNotNull(webHandler); }
Example #28
Source File: DefaultWebFilterChain.java From java-technology-stack with MIT License | 5 votes |
/** * Private constructor to represent one link in the chain. */ private DefaultWebFilterChain(List<WebFilter> allFilters, WebHandler handler, @Nullable WebFilter currentFilter, @Nullable DefaultWebFilterChain next) { this.allFilters = allFilters; this.currentFilter = currentFilter; this.handler = handler; this.next = next; }
Example #29
Source File: DefaultWebFilterChain.java From java-technology-stack with MIT License | 5 votes |
/** * Public constructor with the list of filters and the target handler to use. * @param handler the target handler * @param filters the filters ahead of the handler * @since 5.1 */ public DefaultWebFilterChain(WebHandler handler, List<WebFilter> filters) { Assert.notNull(handler, "WebHandler is required"); this.allFilters = Collections.unmodifiableList(filters); this.handler = handler; DefaultWebFilterChain chain = initChain(filters, handler); this.currentFilter = chain.currentFilter; this.next = chain.next; }
Example #30
Source File: DefaultWebFilterChain.java From java-technology-stack with MIT License | 4 votes |
public WebHandler getHandler() { return this.handler; }