@Override public WebSocket write(AtmosphereResponse r, byte[] data) throws IOException { if (data == null) { logger.error("Cannot write null value for {}", resource()); return this; } return write(r, data, 0, data.length); }
@Override public void run() { if (WebSocket.class.isAssignableFrom(webSocket.getClass()) && System.currentTimeMillis() - WebSocket.class.cast(webSocket).lastWriteTimeStampInMilliseconds() > action.timeout()) { asynchronousProcessor.endRequest(((AtmosphereResourceImpl) webSocket.resource()), false); f.get().cancel(true); } } }, action.timeout(), action.timeout(), TimeUnit.MILLISECONDS));
@Override public List<AtmosphereRequest> onMessage(WebSocket webSocket, byte[] data, int offset, int length) { logger.trace("broadcast byte"); byte[] b = new byte[length]; System.arraycopy(data, offset, b, 0, length); webSocket.resource().getBroadcaster().broadcast(b); return null; }
@Override public List<AtmosphereRequest> onMessage(WebSocket webSocket, String data) { logger.trace("broadcast String"); webSocket.resource().getBroadcaster().broadcast(data); return null; }
private WebSocketHandlerProxy webSocketHandlerForMessage(WebSocket webSocket) { AtmosphereResourceImpl impl = AtmosphereResourceImpl.class.cast(webSocket.resource()); if (impl != null) { impl.getRequest(false).setAttribute(FrameworkConfig.WEBSOCKET_MESSAGE, "true"); try { Utils.inject(impl); } catch (IllegalAccessException e) { logger.warn("", e); } } return WebSocketHandlerProxy.class.cast(webSocket.webSocketHandler()); }
@Override public List<AtmosphereRequest> onTextStream(WebSocket webSocket, Reader r) { //Converting to a string and delegating to onMessage(WebSocket webSocket, String d) causes issues because the binary data may not be a valid string. AtmosphereResourceImpl resource = (AtmosphereResourceImpl) webSocket.resource(); if (resource == null) { logger.trace("The WebSocket has been closed before the message was processed."); return null; } AtmosphereRequest request = resource.getRequest(); request.setAttribute(FrameworkConfig.WEBSOCKET_SUBPROTOCOL, FrameworkConfig.STREAMING_HTTP_OVER_WEBSOCKET); List<AtmosphereRequest> list = new ArrayList<AtmosphereRequest>(); list.add(constructRequest(webSocket, request.getPathInfo(), request.getRequestURI(), methodType, contentType.equalsIgnoreCase(TEXT) ? null : contentType, destroyable).reader(r).build()); return list; }
@Override public WebSocket write(AtmosphereResponse r, String data) throws IOException { firstWrite.set(true); if (data == null) { logger.error("Cannot write null value for {}", resource()); return this; } if (!isOpen()) throw new IOException("Connection remotely closed for " + uuid); logger.trace("WebSocket.write() {}", data); boolean transform = !filters.isEmpty() && r.getStatus() < 400; if (binaryWrite) { byte[] b = data.getBytes(resource().getResponse().getCharacterEncoding()); if (transform) { b = transform(r, b, 0, b.length); } if (b != null) { write(b, 0, b.length); } } else { if (transform) { byte[] b = data.getBytes(resource().getResponse().getCharacterEncoding()); data = new String(transform(r, b, 0, b.length), r.getCharacterEncoding()); } if (data != null) { write(data); } } lastWrite = System.currentTimeMillis(); return this; }
@Override public List<AtmosphereRequest> onBinaryStream(WebSocket webSocket, InputStream stream) { //Converting to a string and delegating to onMessage(WebSocket webSocket, String d) causes issues because the binary data may not be a valid string. AtmosphereResourceImpl resource = (AtmosphereResourceImpl) webSocket.resource(); if (resource == null) { logger.trace("The WebSocket has been closed before the message was processed."); return null; } AtmosphereRequest request = resource.getRequest(); request.setAttribute(FrameworkConfig.WEBSOCKET_SUBPROTOCOL, FrameworkConfig.STREAMING_HTTP_OVER_WEBSOCKET); List<AtmosphereRequest> list = new ArrayList<AtmosphereRequest>(); list.add(constructRequest(webSocket, request.getPathInfo(), request.getRequestURI(), methodType, contentType.equalsIgnoreCase(TEXT) ? null : contentType, destroyable).inputStream(stream).build()); return list; }
@Override public List<AtmosphereRequest> onMessage(WebSocket webSocket, String message) { AtmosphereResourceImpl resource = (AtmosphereResourceImpl) webSocket.resource(); if (resource == null) { logger.trace("The WebSocket has been closed before the message was processed.");
@Override public List<AtmosphereRequest> onMessage(WebSocket webSocket, byte[] d, final int offset, final int length) { //Converting to a string and delegating to onMessage(WebSocket webSocket, String d) causes issues because the binary data may not be a valid string. AtmosphereResourceImpl resource = (AtmosphereResourceImpl) webSocket.resource(); if (resource == null) { logger.trace("The WebSocket has been closed before the message was processed."); return null; } AtmosphereRequest request = resource.getRequest(false); request.setAttribute(FrameworkConfig.WEBSOCKET_SUBPROTOCOL, FrameworkConfig.SIMPLE_HTTP_OVER_WEBSOCKET); if (!resource.isInScope()) return Collections.emptyList(); List<AtmosphereRequest> list = new ArrayList<AtmosphereRequest>(); list.add(constructRequest(webSocket, request.getPathInfo(), request.getRequestURI(), methodType, contentType.equalsIgnoreCase(TEXT) ? null : contentType, destroyable).body(d, offset, length).build()); return list; }
public void executeClose(WebSocket webSocket, int closeCode) { AtmosphereResource r = webSocket.resource(); boolean isClosedByClient = r == null ? true : r.getAtmosphereResourceEvent().isClosedByClient(); try { if (r != null) { asynchronousProcessor.endRequest(AtmosphereResourceImpl.class.cast(r), true); } } finally { if (!isClosedByClient) { notifyListener(webSocket, new WebSocketEventListener.WebSocketEvent(closeCode, CLOSE, webSocket)); } } }
private void handleException(Exception ex, WebSocket webSocket, WebSocketHandler webSocketHandler) { logger.error("", ex); AtmosphereResource r = webSocket.resource(); if (r != null) { webSocketHandler.onError(webSocket, new WebSocketException(ex, new AtmosphereResponseImpl.Builder() .request(r != null ? AtmosphereResourceImpl.class.cast(r).getRequest(false) : null) .status(500) .statusMessage("Server Error").build())); } }
webSocket.resource().setBroadcaster(AnnotationUtil.broadcaster(framework, p != null ? p.broadcasterClazz : w.broadcasterClazz, path)); } catch (Exception e) { logger.error("", e);
private void invokeInterceptors(WebSocketHandlerProxy webSocketHandler, WebSocket webSocket, Object webSocketMessageAsBody, int offset, int length) throws IOException { AtmosphereResourceImpl resource = AtmosphereResourceImpl.class.cast(webSocket.resource()); if (resource == null) { return;
private void finish(WebSocket webSocket, AtmosphereResource resource, AtmosphereRequest r, AtmosphereResponse s, boolean closeWebSocket) { // Don't take any risk in case something goes wrong and remove the associated resource. framework.atmosphereFactory().remove(resource.uuid()); if (webSocket != null) { try { r.setAttribute(WebSocket.CLEAN_CLOSE, Boolean.TRUE); webSocket.resource(null); if (closeWebSocket) webSocket.close(s); } catch (IOException e) { logger.trace("", e); } } if (r != null) { r.destroy(true); } if (s != null) { s.destroy(true); } }
@Override public void notifyListener(WebSocket webSocket, WebSocketEventListener.WebSocketEvent event) { AtmosphereResource resource = webSocket.resource(); if (resource == null) return;
protected static AtmosphereRequestImpl.Builder constructRequest(WebSocket webSocket, String pathInfo, String requestURI, String methodType, String contentType, boolean destroyable) { AtmosphereResource resource = webSocket.resource(); AtmosphereRequest request = AtmosphereResourceImpl.class.cast(resource).getRequest(false); Map<String, Object> m = attributes(webSocket, request); // We need to create a new AtmosphereRequest as WebSocket message may arrive concurrently on the same connection. AtmosphereRequestImpl.Builder b = (new AtmosphereRequestImpl.Builder() .request(request) .method(methodType) .contentType(contentType == null ? request.getContentType() : contentType) .attributes(m) .pathInfo(pathInfo) .contextPath(request.getContextPath()) .servletPath(request.getServletPath()) .requestURI(requestURI) .requestURL(request.requestURL()) .destroyable(destroyable) .headers(request.headersMap()) .session(resource.session())); return b; }