@Override public boolean handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response) throws IOException { if (!ServletPortletHelper.isPushRequest(request)) { return false; } if (request instanceof VaadinServletRequest) { if (atmosphere == null) { response.sendError(500, "Atmosphere initialization failed. No push available."); return true; } try { atmosphere.doCometSupport( AtmosphereRequestImpl .wrap((VaadinServletRequest) request), AtmosphereResponseImpl .wrap((VaadinServletResponse) response)); } catch (ServletException e) { // TODO PUSH decide how to handle throw new RuntimeException(e); } } else { throw new IllegalArgumentException( "Portlets not currently supported"); } return true; }
private AtmosphereFramework create(Bus bus) { final AtmosphereFramework instance = new AtmosphereFramework(false, true); instance.setUseNativeImplementation(false); instance.addInitParameter(ApplicationConfig.PROPERTY_NATIVE_COMETSUPPORT, "true"); instance.addInitParameter(ApplicationConfig.PROPERTY_SESSION_SUPPORT, "true"); instance.addInitParameter(ApplicationConfig.WEBSOCKET_SUPPORT, "true"); instance.addInitParameter(ApplicationConfig.WEBSOCKET_PROTOCOL_EXECUTION, "true"); // workaround for atmosphere's jsr356 initialization requiring servletConfig instance.addInitParameter(ApplicationConfig.WEBSOCKET_SUPPRESS_JSR356, "true"); AtmosphereUtils.addInterceptors(instance, bus); instance.addAtmosphereHandler("/", new DestinationHandler()); return instance; }
atmosphere.addAtmosphereHandler("/*", new PushAtmosphereHandler()); atmosphere.addInitParameter(ApplicationConfig.BROADCASTER_CACHE, UUIDBroadcasterCache.class.getName()); atmosphere.addInitParameter(ApplicationConfig.ANNOTATION_PROCESSOR, VoidAnnotationProcessor.class.getName()); atmosphere.addInitParameter(ApplicationConfig.PROPERTY_SESSION_SUPPORT, "true"); atmosphere.addInitParameter(ApplicationConfig.MESSAGE_DELIMITER, String.valueOf(PushConstants.MESSAGE_DELIMITER)); atmosphere.addInitParameter( ApplicationConfig.DROP_ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "false"); atmosphere.addInitParameter(ApplicationConfig.WEBSOCKET_BUFFER_SIZE, bufferSize); atmosphere.addInitParameter(ApplicationConfig.WEBSOCKET_MAXTEXTSIZE, bufferSize); atmosphere.addInitParameter(ApplicationConfig.WEBSOCKET_MAXBINARYSIZE, bufferSize); atmosphere.addInitParameter( ApplicationConfig.PROPERTY_ALLOW_SESSION_TIMEOUT_REMOVAL, "false"); atmosphere.addInitParameter(ApplicationConfig.RECOVER_DEAD_BROADCASTER, "false"); atmosphere.addInitParameter("org.atmosphere.cpr.showSupportMessage", "false");
atmosphere.getAtmosphereConfig().getInitParameter( com.vaadin.server.Constants.SERVLET_PARAMETER_PUSH_SUSPEND_TIMEOUT_LONGPOLLING, -1)); for (AtmosphereHandlerWrapper handlerWrapper : atmosphere .getAtmosphereHandlers().values()) { AtmosphereHandler handler = handlerWrapper.atmosphereHandler; if (handler instanceof PushAtmosphereHandler) {
protected void activate() { super.activate(); if (handler.getServletContext().getAttribute("org.eclipse.jetty.util.DecoratedObjectFactory") == null) { try { Class<?> dcc = ClassUtils.forName("org.eclipse.jetty.util.DecoratedObjectFactory", getClass().getClassLoader()); handler.getServletContext().setAttribute("org.eclipse.jetty.util.DecoratedObjectFactory", dcc.newInstance()); } catch (ClassNotFoundException | LinkageError | InstantiationException | IllegalAccessException e) { //ignore, old version of Jetty } } ServletConfig config = new VoidServletConfig(initParams) { @Override public ServletContext getServletContext() { return handler.getServletContext(); } }; try { framework.init(config); } catch (ServletException e) { throw new Fault(new Message("Could not initialize WebSocket Framework", LOG, e.getMessage()), e); } }
@Override public void contextDestroyed(ServletContextEvent sce) { // Destroy any AtmosphereFramework instance we have initialized. // This must be done here to ensure that we cleanup Atmosphere instances // related to servlets which are never initialized ServletContext servletContext = sce.getServletContext(); Enumeration<String> attributeNames = servletContext.getAttributeNames(); while (attributeNames.hasMoreElements()) { String attributeName = attributeNames.nextElement(); if (isAtmosphereFrameworkAttribute(attributeName)) { Object value = servletContext.getAttribute(attributeName); if (value instanceof AtmosphereFramework) { // This might result in calling destroy() twice, once from // here and once from PushRequestHandler but // AtmosphereFramework.destroy() deals with that ((AtmosphereFramework) value).destroy(); } } } }
@Override public void onServletConfigAvailable(ServletConfig config) throws ServletException { // Very likely there is JSR-356 implementation available, let us reconfigure the Atmosphere framework // to use it since ServletConfig instance is already available. final Object container = config.getServletContext() .getAttribute("javax.websocket.server.ServerContainer"); if (container != null) { if (framework.initialized()) { framework.destroy(); } framework = create(getBus()); framework.addInitParameter(ApplicationConfig.PROPERTY_NATIVE_COMETSUPPORT, "false"); framework.addInitParameter(ApplicationConfig.WEBSOCKET_SUPPRESS_JSR356, "false"); framework.init(config); } }
Injector injector = (Injector) framework().getAtmosphereConfig().getServletContext().getAttribute(Injector.class.getName()); GuiceContainer guiceServlet = injector.getInstance(GuiceContainer.class); Thread.currentThread().getContextClassLoader().loadClass(JERSEY_CONTAINER); setDefaultBroadcasterClassName(broadcasterClassName) .setUseStreamForFlushingComments(true) .getAtmosphereConfig().setSupportSession(false); isJersey = true; } catch (Throwable t) { framework().addInitParameter(p, props.get(p));
@Override public void init(final ServletConfig sc) throws ServletException { if (!initialized) { super.init(new ServletConfigDefaultsFacade(sc, DEFAULT_INIT_PARAMETERS)); this.initialized = true; String mapping = (String) sc.getServletContext() .getAttribute(PushContextFactoryImpl.PUSH_HANDLER_MAPPING_ATTRIBUTE); if (mapping == null) { mapping = "*"; } ReflectorServletProcessor r = new ReflectorServletProcessor(this); r.setFilterClassName(PushHandlerFilter.class.getName()); framework().addAtmosphereHandler(mapping, r).initAtmosphereHandler(sc); } }
.getAttribute(ApplicationConfig.SUSPENDED_ATMOSPHERE_RESOURCE_UUID); final AtmosphereResponse suspendedResponse = suspendedResponses.get(saruuid); LOG.fine("Attaching a proxy writer to suspended response"); r.getResponse().asyncIOWriter(new AtmosphereInterceptorWriter() { @Override public AsyncIOWriter write(AtmosphereResponse r, String data) throws IOException { r.getResponse().destroyable(false); return Action.CONTINUE; if (request.getAttribute(REQUEST_DISPATCHED) == null) { AtmosphereResponse response = null; AtmosphereFramework framework = r.getAtmosphereConfig().framework(); try { byte[] data = WebSocketUtils.readBody(request.getInputStream()); response.request(ar); attachWriter(r); Action action = framework.doCometSupport(ar, response);
@Override public Action inspect(final AtmosphereResource r) { final AtmosphereRequest request = r.getRequest(); r.addEventListener(new AtmosphereResourceEventListenerAdapter() { if (request.getHeader("SwaggerSocket") != null) { ok = true; if (ok && request.attributes().get(SWAGGER_SOCKET_DISPATCHED) == null) { AtmosphereResponse response = new WrappedAtmosphereResponse(r.getResponse(), request); logger.debug("Method {} Transport {}", request.getMethod(), r.transport()); if (request.getMethod() == "GET" && r.transport().equals(AtmosphereResource.TRANSPORT.LONG_POLLING)) { response.setContentType("application/json"); response.getOutputStream().write(mapper.writeValueAsBytes(statusMessage)); if (r.transport() == AtmosphereResource.TRANSPORT.WEBSOCKET) { request.setAttribute("swaggerSocketRequest", req); Action action = framework.doCometSupport(ar, response); if (action.type() == Action.TYPE.SUSPEND) { ar.destroyable(false);
@Override public Action inspect(AtmosphereResource r) { final AtmosphereRequest request = r.getRequest(); if (request.getHeader("SwaggerSocket") != null && request.getAttribute(SWAGGER_SOCKET_DISPATCHED) == null && r.transport() != AtmosphereResource.TRANSPORT.WEBSOCKET) { AtmosphereResponse response = r.getResponse(); response.setContentType("application/json"); if (request.getMethod() == "GET") { BlockingQueue<AtmosphereResource> queue = (BlockingQueue<AtmosphereResource>) request.getSession().getAttribute("PendingResource"); if (queue == null) { AtmosphereFramework framework = r.getAtmosphereConfig().framework(); response.asyncIOWriter(new AsyncIOWriterAdapter() { response.getOutputStream().write(mapper.writeValueAsBytes(statusMessage)); ar.setAttribute(SWAGGER_SOCKET_DISPATCHED, "true"); framework.doCometSupport(ar, response.request(ar).destroyable(false)); } catch (ServletException e) { logger.warn("", e);
public AtmosphereBridge(ServletContext context, String path) { AtmosphereServlet servlet = null; try { servlet = context.createServlet(AtmosphereServlet.class); } catch (ServletException e) { throw new RuntimeException(e); } AtmosphereFramework framework = servlet.framework(); framework.addAtmosphereHandler("/", new AtmosphereHandlerAdapter() { @Override public void onRequest(AtmosphereResource resource) throws IOException { if (resource.transport() == TRANSPORT.WEBSOCKET) { if (resource.getRequest().getMethod().equals("GET")) { wsActions.fire(new AtmosphereServerWebSocket(resource)); } } else { httpActions.fire(new AtmosphereServerHttpExchange(resource)); } } }); ServletRegistration.Dynamic reg = context.addServlet("wes#" + UUID.randomUUID(), servlet); reg.setAsyncSupported(true); reg.addMapping(path); }
@Override public void resume() { try { r.getAtmosphereConfig().framework().doCometSupport(r.getRequest(), r.getResponse()); } catch (IOException e) { logger.warn("",e); } catch (ServletException e) { logger.warn("", e); } r.resume(); }
public BridgeRuntime(final Config config) { super(config.path()); this.config = config; framework = new AtmosphereFramework(); framework.setDefaultBroadcasterClassName(config.broadcaster().getName()); framework.setAtmosphereDotXmlPath(config.configFile()); framework.setBroadcasterFactory(config.broadcasterFactory()); framework.setBroadcasterCacheClassName(config.broadcasterCache().getName()); } catch (Throwable t) { logger.trace("", t); framework.addAtmosphereHandler(e.getKey(), e.getValue()); framework.addAtmosphereHandler(Broadcaster.ROOT_MASTER, REFLECTOR_ATMOSPHEREHANDLER); framework.getAtmosphereConfig().startupHook(new AtmosphereConfig.StartupHook() { @Override public void started(AtmosphereFramework framework) { framework.setWebSocketProtocolClassName(config.webSocketProtocol().getName()); framework.interceptor(i); framework.addAnnotationPackage(s); framework.externalizeDestroy(true).init(new NettyServletConfig(config.initParams(), ctx));
request.setAttribute(KEEP_ALIVE, ka); AtmosphereRequestImpl.Body b = request.body(); if (!aggregateBodyInMemory && !hrequest.getMethod().equals(GET) request = State.class.cast(ctx.attr(ATTACHMENT).get()).request; boolean isLast = HttpChunkedInput.class.cast(messageEvent).isEndOfInput(); Boolean ka = (Boolean) request.getAttribute(KEEP_ALIVE); response.setHeader("Transfer-Encoding", "chunked"); a = framework.doCometSupport(request, response); if (forceSuspend) { a.type(Action.TYPE.SUSPEND); if (request != null && a != null && a.type() != Action.TYPE.SUSPEND) { request.destroy(); response.destroy(); framework.notify(Action.TYPE.DESTROYED, request, response);
private Action iframe(AtmosphereResource r) { final AtmosphereResponse response = r.getResponse(); response.setContentType("text/html"); String origin = framework.getAtmosphereConfig().getInitParameter(SOCKS_JS_ORIGIN); if (origin == null) { origin = "http://localhost:8080/lib/sockjs.js"; } try { response.write(IFrameUtils.generateIFrame(origin)).flushBuffer(); } catch (IOException e) { logger.error("", e); } return Action.CANCELLED; }
public AtmosphereCoordinator route(AtmosphereRequest request, AtmosphereResponse response) throws IOException { final VertxAsyncIOWriter w = VertxAsyncIOWriter.class.cast(response.getAsyncIOWriter()); try { Action a = framework.doCometSupport(request, response); final AtmosphereResourceImpl impl = (AtmosphereResourceImpl) request.getAttribute(FrameworkConfig.ATMOSPHERE_RESOURCE); String transport = (String) request.getAttribute(FrameworkConfig.TRANSPORT_IN_USE); if (transport == null) { transport = request.getHeader(X_ATMOSPHERE_TRANSPORT); } logger.debug("Transport {} action {}", transport, a); final Action action = (Action) request.getAttribute(NettyCometSupport.SUSPEND); if (action != null && action.type() == Action.TYPE.SUSPEND && action.timeout() != -1) { final AtomicReference<Future<?>> f = new AtomicReference<>(); f.set(suspendTimer.scheduleAtFixedRate(new Runnable() { @Override public void run() { if (!w.isClosed() && (System.currentTimeMillis() - w.lastTick()) > action.timeout()) { asynchronousProcessor.endRequest(impl, false); f.get().cancel(true); } } }, action.timeout(), action.timeout(), TimeUnit.MILLISECONDS)); } } catch (Throwable e) { logger.error("Unable to process request", e); } return this; }
protected PushServlet configureFramework(ServletConfig sc) throws ServletException { super.configureFramework(sc, false); framework().interceptor(new AtmosphereResourceLifecycleInterceptor()) .interceptor(new TrackMessageSizeInterceptor()) .addAnnotationPackage(PushEndpointProcessor.class); framework().getAtmosphereConfig().startupHook(new AtmosphereConfig.StartupHook() { public void started(AtmosphereFramework framework) { configureMetaBroadcasterCache(framework); if (notConfigured) { framework().addInitParameter(ApplicationConfig.READ_GET_BODY, "true"); framework().init(sc); PushContextFactory pcf = new PushContextFactory(framework().getAtmosphereConfig().metaBroadcaster()); PushContext c = pcf.getPushContext(); if (PushContextImpl.class.isAssignableFrom(c.getClass())) { framework().asyncSupportListener(PushContextImpl.class.cast(c)); framework().newClassInstance(EventBusFactory.class, EventBusFactory.class); if (framework().getAtmosphereHandlers().size() == 0) { logger.error("No Annotated class using @PushEndpoint found. Push will not work.");
@Override public Action suspended(AtmosphereRequest request, AtmosphereResponse response) throws IOException, ServletException { Action a = super.suspended(request, response); if (framework.getAtmosphereConfig().isSupportSession()) { AtmosphereResource r = request.resource(); HttpSession s = request.getSession(true); if (s != null) { sessions.put(r.uuid(), request.getSession(true)); } } return a; }