/** * Called when a message is received through the push connection * * @since 7.5.0 * @param resource * The related atmosphere resources */ void onMessage(AtmosphereResource resource) { callWithUi(resource, receiveCallback, resource.transport() == TRANSPORT.WEBSOCKET); }
/** * Suspends the given resource. * * @since 7.6 * @param resource * the resource to suspend */ protected void suspend(AtmosphereResource resource) { if (resource.transport() == TRANSPORT.LONG_POLLING) { resource.suspend(getLongPollingSuspendTimeout()); } else { resource.suspend(-1); } }
/** * Reads and buffers a (possibly partial) message. If a complete message was * received, or if the call resulted in the completion of a partially * received message, returns a {@link Reader} yielding the complete message. * Otherwise, returns null. * * @param reader * A Reader from which to read the (partial) message * @return A Reader yielding a complete message or null if the message is * not yet complete. * @throws IOException */ protected Reader receiveMessage(Reader reader) throws IOException { if (resource == null || resource.transport() != TRANSPORT.WEBSOCKET) { return reader; } if (incomingMessage == null) { // No existing partially received message incomingMessage = new FragmentedMessage(reader); } if (incomingMessage.append(reader)) { // Message is complete Reader completeReader = incomingMessage.getReader(); incomingMessage = null; return completeReader; } else { // Only received a partial message return null; } }
/** * Called when a message is received through the push connection * * @param resource * The related atmosphere resources */ void onMessage(AtmosphereResource resource) { callWithUi(resource, receiveCallback, resource.transport() == TRANSPORT.WEBSOCKET); }
/** * Called when a message is received through the push connection * * @since 7.5.0 * @param resource * The related atmosphere resources */ void onMessage(AtmosphereResource resource) { callWithUi(resource, receiveCallback, resource.transport() == TRANSPORT.WEBSOCKET); }
public AtmosphereResource.TRANSPORT transport() { return request.resource().transport(); }
@Override public BroadcastAction filter(AtmosphereResource r, Object originalMessage, Object message) { String m = message.toString(); if (r.transport().equals(AtmosphereResource.TRANSPORT.LONG_POLLING)) { m += END_MESSAGE; } return new BroadcastAction(BroadcastAction.ACTION.CONTINUE, m); }
/** * Suspends the given resource * * @param resource * the resource to suspend */ protected void suspend(AtmosphereResource resource) { if (resource.transport() == TRANSPORT.LONG_POLLING) { resource.suspend(getLongPollingSuspendTimeout()); } else { resource.suspend(-1); } }
private final void addContextValue(AtmosphereRequest request, String name, Object value) { if (request.resource().transport().equals(AtmosphereResource.TRANSPORT.WEBSOCKET)) { request.setAttribute(name, value); } else { request.getSession().setAttribute(name, value); } }
private final Object getContextValue(AtmosphereRequest request, String name) { if (request.resource().transport().equals(AtmosphereResource.TRANSPORT.WEBSOCKET)) { return request.getAttribute(name); } else { return request.getSession().getAttribute(name); } }
/** * Suspends the given resource * * @since 7.6 * @param resource * the resource to suspend */ protected void suspend(AtmosphereResource resource) { if (resource.transport() == TRANSPORT.LONG_POLLING) { resource.suspend(getLongPollingSuspendTimeout()); } else { resource.suspend(-1); } }
private void incrementUserSessionCount(AtmosphereResource resource) { String userId = getCurrentUserId(resource); boolean autoDelete = !(resource.transport() == AtmosphereResource.TRANSPORT.WEBSOCKET); userSessionCounterRepository.updateSession(userId, resource.uuid(), autoDelete); }
protected void close(AtmosphereResource r) throws IOException { if(r.transport() == AtmosphereResource.TRANSPORT.WEBSOCKET) { r.close(); } else { r.getResponse().sendError(HttpServletResponse.SC_NOT_FOUND); } }
@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)); } } });
/** * Reads and buffers a (possibly partial) message. If a complete message was * received, or if the call resulted in the completion of a partially * received message, returns a {@link Reader} yielding the complete message. * Otherwise, returns null. * * @param reader * A Reader from which to read the (partial) message * @return A Reader yielding a complete message or null if the message is * not yet complete. * @throws IOException */ protected Reader receiveMessage(Reader reader) throws IOException { if (resource == null || resource.transport() != TRANSPORT.WEBSOCKET) { return reader; } if (incomingMessage == null) { // No existing partially received message incomingMessage = new FragmentedMessage(reader); } if (incomingMessage.append(reader)) { // Message is complete Reader completeReader = incomingMessage.getReader(); incomingMessage = null; return completeReader; } else { // Only received a partial message return null; } }
@Override public AsyncIOWriter write(final AtmosphereResponse r, byte[] data, int offset, int length) throws IOException { logger.trace("Writing {} with transport {}", r.resource().uuid(), r.resource().transport()); boolean transform = filters.size() > 0 && r.getStatus() < 400; if (transform) { data = transform(r, data, offset, length); offset = 0; length = data.length; } pendingWrite.incrementAndGet(); out.write(new String(data, offset, length, r.getCharacterEncoding())); byteWritten = true; lastWrite = System.currentTimeMillis(); if (resumeOnBroadcast) { out.close(); _close(r.request()); } return this; }
@Override public AsyncIOWriter write(final AtmosphereResponse r, byte[] data, int offset, int length) throws IOException { logger.trace("Writing {} with transport {}", r.resource().uuid(), r.resource().transport()); boolean transform = filters.size() > 0 && r.getStatus() < 400; if (transform) { data = transform(r, data, offset, length); offset = 0; length = data.length; } pendingWrite.incrementAndGet(); if (!headerWritten) { out.setChunked(true); constructStatusAndHeaders(r, out); headerWritten = true; } String sdata = new String(data, offset, length, r.getCharacterEncoding()); out.write(sdata); lastWrite = System.currentTimeMillis(); AtmosphereResourceImpl impl = AtmosphereResourceImpl.class.cast(r.resource()); if (sdata.trim().length() > 0 && impl.transport().equals(AtmosphereResource.TRANSPORT.LONG_POLLING)) { close(r); } return this; }
@Override public Action inspect(final AtmosphereResource r) { AtmosphereRequest request = r.getRequest(); if (r.transport().equals(AtmosphereResource.TRANSPORT.WEBSOCKET)) { if (r.getRequest().getAttribute(FrameworkConfig.INJECTED_ATMOSPHERE_RESOURCE) != null) { return Action.CANCELLED; } } request.setAttribute(Continuation.ATTRIBUTE, new AtmosphereContinuation(r)); return Action.CONTINUE; }
protected final Object wrapMessage(AtmosphereResponse res, String message) { if (message != null && message.startsWith("heartbeat-")) { String identity = (String) getContextValue(res.request(), IDENTITY); return new Heartbeat(String.valueOf(System.nanoTime()), identity); } else { Response.Builder builder = createResponseBuilder(res, message); String identity = (String) getContextValue(res.request(), IDENTITY); AtomicInteger expectedResponseCount = (AtomicInteger) getContextValue(res.request(), transactionIdentity.get() + RESPONSE_COUNTER); ResponseMessage m = null; if (expectedResponseCount != null && res.resource().transport() != AtmosphereResource.TRANSPORT.WEBSOCKET) { m = (ResponseMessage) getContextValue(res.request(), transactionIdentity.get() + ResponseMessage.class.getName()); if (m != null) { m.response(builder.build()); } else { m = new ResponseMessage(identity, builder.build()); } if (expectedResponseCount.decrementAndGet() <= 0) { return m; } else { addContextValue(res.request(), transactionIdentity.get() + ResponseMessage.class.getName(), m); return null; } } if (m == null) { m = new ResponseMessage(identity, builder.build()); } return m; } }