@Override protected void send(Object element) throws IOException { if (element instanceof ServerSentEvent) { ServerSentEvent<?> event = (ServerSentEvent<?>) element; ((SseEmitter) getEmitter()).send(adapt(event)); } else { getEmitter().send(element, MediaType.APPLICATION_JSON); } }
/** * Template method to create a new InitBinderDataBinderFactory instance. * <p>The default implementation creates a ServletRequestDataBinderFactory. * This can be overridden for custom ServletRequestDataBinder subclasses. * @param binderMethods {@code @InitBinder} methods * @return the InitBinderDataBinderFactory instance to use * @throws Exception in case of invalid state or arguments */ protected InitBinderDataBinderFactory createDataBinderFactory(List<InvocableHandlerMethod> binderMethods) throws Exception { return new ServletRequestDataBinderFactory(binderMethods, getWebBindingInitializer()); }
@Override public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) { for (RequestBodyAdvice advice : getMatchingAdvice(parameter, RequestBodyAdvice.class)) { if (advice.supports(parameter, targetType, converterType)) { body = advice.afterBodyRead(body, inputMessage, parameter, targetType, converterType); } } return body; }
@Test public void sendEventFullWithTwoDataLinesInTheMiddle() throws Exception { this.emitter.send(event().comment("blah").data("foo").data("bar").name("test").reconnectTime(5000L).id("1")); this.handler.assertSentObjectCount(5); this.handler.assertObject(0, ":blah\ndata:", SseEmitter.TEXT_PLAIN); this.handler.assertObject(1, "foo"); this.handler.assertObject(2, "\ndata:", SseEmitter.TEXT_PLAIN); this.handler.assertObject(3, "bar"); this.handler.assertObject(4, "\nevent:test\nretry:5000\nid:1\n\n", SseEmitter.TEXT_PLAIN); }
@Test public void sendEventWithTwoDataLines() throws Exception { this.emitter.send(event().data("foo").data("bar")); this.handler.assertSentObjectCount(5); this.handler.assertObject(0, "data:", SseEmitter.TEXT_PLAIN); this.handler.assertObject(1, "foo"); this.handler.assertObject(2, "\ndata:", SseEmitter.TEXT_PLAIN); this.handler.assertObject(3, "bar"); this.handler.assertObject(4, "\n\n", SseEmitter.TEXT_PLAIN); }
@Override public HttpInputMessage beforeBodyRead(HttpInputMessage request, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException { for (RequestBodyAdvice advice : getMatchingAdvice(parameter, RequestBodyAdvice.class)) { if (advice.supports(parameter, targetType, converterType)) { request = advice.beforeBodyRead(request, parameter, targetType, converterType); } } return request; }
@Override protected void send(Object element) throws IOException { getEmitter().send(element, MediaType.APPLICATION_JSON); getEmitter().send("\n", MediaType.TEXT_PLAIN); } }
@Override protected void send(Object element) throws IOException { getEmitter().send(element, MediaType.TEXT_PLAIN); } }
/** * An alternative to {@link #fromMethodName(Class, String, Object...)}} for * use with an instance of this class created via {@link #relativeTo}. * <p><strong>Note:</strong> This method extracts values from "Forwarded" * and "X-Forwarded-*" headers if found. See class-level docs. * @since 4.2 */ public UriComponentsBuilder withMethodName(Class<?> controllerType, String methodName, Object... args) { return fromMethodName(this.baseUrl, controllerType, methodName, args); }
/** * Create a nested ServletInvocableHandlerMethod subclass that returns the * the given value (or raises an Exception if the value is one) rather than * actually invoking the controller method. This is useful when processing * async return values (e.g. Callable, DeferredResult, ListenableFuture). */ ServletInvocableHandlerMethod wrapConcurrentResult(Object result) { return new ConcurrentResultHandlerMethod(result, new ConcurrentResultMethodParameter(result)); }
/** * An alternative to {@link #fromController(Class)} for use with an instance * of this class created via a call to {@link #relativeTo}. * <p><strong>Note:</strong> This method extracts values from "Forwarded" * and "X-Forwarded-*" headers if found. See class-level docs. * @since 4.2 */ public UriComponentsBuilder withController(Class<?> controllerType) { return fromController(this.baseUrl, controllerType); }
/** * An alternative to {@link #fromMethodCall(Object)} for use with an instance * of this class created via {@link #relativeTo}. * <p><strong>Note:</strong> This method extracts values from "Forwarded" * and "X-Forwarded-*" headers if found. See class-level docs. * @since 4.2 */ public UriComponentsBuilder withMethodCall(Object invocationInfo) { return fromMethodCall(this.baseUrl, invocationInfo); }
@Override public final void onComplete() { this.terminated = true; trySchedule(); }
/** * Bridge to actual controller type-level annotations. */ @Override public Class<?> getBeanType() { return ServletInvocableHandlerMethod.this.getBeanType(); }
/** * Bridge to controller method-level annotations. */ @Override public <A extends Annotation> boolean hasMethodAnnotation(Class<A> annotationType) { return ServletInvocableHandlerMethod.this.hasMethodAnnotation(annotationType); } }
/** * Create an instance of this class with a base URL. After that calls to one * of the instance based {@code withXxx(...}} methods will create URLs relative * to the given base URL. */ public static MvcUriComponentsBuilder relativeTo(UriComponentsBuilder baseUrl) { return new MvcUriComponentsBuilder(baseUrl); }
/** * Register code to invoke when the async request times out. This method is * called from a container thread when an async request times out. */ public synchronized void onTimeout(Runnable callback) { this.timeoutCallback.setDelegate(callback); }
/** * Register code to invoke for an error during async request processing. * This method is called from a container thread when an error occurred * while processing an async request. * @since 5.0 */ public synchronized void onError(Consumer<Throwable> callback) { this.errorCallback.setDelegate(callback); }
@Test public void sendEventFull() throws Exception { this.emitter.send(event().comment("blah").name("test").reconnectTime(5000L).id("1").data("foo")); this.handler.assertSentObjectCount(3); this.handler.assertObject(0, ":blah\nevent:test\nretry:5000\nid:1\ndata:", SseEmitter.TEXT_PLAIN); this.handler.assertObject(1, "foo"); this.handler.assertObject(2, "\n\n", SseEmitter.TEXT_PLAIN); }
@Test public void sendEventWithDataLine() throws Exception { this.emitter.send(event().data("foo")); this.handler.assertSentObjectCount(3); this.handler.assertObject(0, "data:", SseEmitter.TEXT_PLAIN); this.handler.assertObject(1, "foo"); this.handler.assertObject(2, "\n\n", SseEmitter.TEXT_PLAIN); }