@Override public AsyncIOWriter writeError(AtmosphereResponse r, int errorCode, String message) throws IOException { Request swaggerSocketRequest = Request.class.cast(r.request().getAttribute(String.valueOf(r.request().hashCode()))); if (swaggerSocketRequest == null) { logger.debug("Handshake mapping (could be expected) {} : {}", errorCode, message); return this; } return this; }
@Override public AsyncIOWriter write(AtmosphereResponse r, byte[] data) throws IOException { SocketIOSessionManagerImpl.SocketIOProtocol p = (SocketIOSessionManagerImpl.SocketIOProtocol) r.request().getAttribute(SOCKETIO_PACKET); if (p == null) { r.getResponse().getOutputStream().write(data); } else { write(r, new String(data, r.request().getCharacterEncoding())); } return this; }
@Override public String filter(AtmosphereResponse r, String message) { // This is the handshake, nothing to do. if (r.request() == null || r.request().getAttribute("swaggersocket.handshake") != null) { return message; } if (invalidState((Request) r.request().getAttribute(String.valueOf(r.request().hashCode())))) { logger.error("Response's body not allowed on handshake {}", message); return null; } try { ResponseMessage m = wrapMessage(r, message); if (m != null) { return mapper.writeValueAsString(m); } else { return null; } } catch (IOException e) { throw new RuntimeException(e); } }
@Override public AsyncIOWriter write(AtmosphereResponse r, byte[] data, int offset, int length) throws IOException { SocketIOSessionManagerImpl.SocketIOProtocol p = (SocketIOSessionManagerImpl.SocketIOProtocol) r.request().getAttribute(SOCKETIO_PACKET); if (p == null) { r.getResponse().getOutputStream().write(data, offset, length); } else { write(new String(data, offset, length, r.request().getCharacterEncoding())); } return this; }
@Override public AsyncIOWriter write(AtmosphereResponse r, byte[] data, int offset, int length) throws IOException { SocketIOSessionManagerImpl.SocketIOProtocol p = (SocketIOSessionManagerImpl.SocketIOProtocol) r.request().getAttribute(SOCKETIO_PACKET); if (p == null) { r.getResponse().getOutputStream().write(data, offset, length); } else { write(r, new String(data, offset, length, r.request().getCharacterEncoding())); } return this; }
@Override public AsyncIOWriter write(AtmosphereResponse r, byte[] data) throws IOException { SocketIOSessionManagerImpl.SocketIOProtocol p = (SocketIOSessionManagerImpl.SocketIOProtocol) r.request().getAttribute(SOCKETIO_PACKET); if (p == null) { r.getResponse().getOutputStream().write(data); } else { write(new String(data, r.request().getCharacterEncoding())); } return this; }
WrappedAtmosphereResponse(AtmosphereResponse resp, AtmosphereRequest req) throws IOException { super((HttpServletResponse)resp.getResponse(), null, req, resp.isDestroyable()); response = resp; response.request(req); }
WrappedAtmosphereResponse(AtmosphereResponse resp, AtmosphereRequest req) throws IOException { super((HttpServletResponse)resp.getResponse(), null, req, resp.isDestroyable()); response = resp; response.request(req); }
@Override @SuppressWarnings("deprecation") public byte[] transformPayload(AtmosphereResponse response, byte[] responseDraft, byte[] data) throws IOException { if (LOG.isLoggable(Level.FINE)) { LOG.log(Level.FINE, "transformPayload with draft={0}", new String(responseDraft)); } AtmosphereRequest request = response.request(); if (request.attributes().get(RESPONSE_PARENT) == null) { request.attributes().put(RESPONSE_PARENT, "true"); return createResponse(response, responseDraft, true); } return createResponse(response, responseDraft, false); }
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; } }
AtmosphereRequest request = response.request(); String refid = (String)request.getAttribute(WebSocketConstants.DEFAULT_REQUEST_ID_KEY);
@Override @SuppressWarnings("deprecation") public byte[] transformPayload(AtmosphereResponse response, byte[] responseDraft, byte[] data) throws IOException { if (LOG.isLoggable(Level.FINE)) { LOG.log(Level.FINE, "transformPayload with draft={0}", new String(responseDraft)); } AtmosphereRequest request = response.request(); if (request.attributes().get(RESPONSE_PARENT) == null) { request.attributes().put(RESPONSE_PARENT, "true"); return createResponse(response, responseDraft, true); } return createResponse(response, responseDraft, false); }
AtmosphereRequest request = response.request(); String refid = (String)request.getAttribute(WebSocketConstants.DEFAULT_REQUEST_ID_KEY);
@Override public AsyncIOWriter write(AtmosphereResponse r, String data) throws IOException { SocketIOSessionOutbound outbound = (SocketIOSessionOutbound) request.getAttribute(SocketIOAtmosphereHandler.SOCKETIO_SESSION_OUTBOUND); SocketIOSessionManagerImpl.SocketIOProtocol p = (SocketIOSessionManagerImpl.SocketIOProtocol) r.request().getAttribute(SOCKETIO_PACKET); String msg = p == null ? data : mapper.writeValueAsString(p.clearArgs().addArgs(data)); if (outbound != null) { outbound.sendMessage(new SocketIOPacketImpl(SocketIOPacketImpl.PacketType.EVENT, msg)); } else { r.getResponse().getOutputStream().write(msg.getBytes(r.getCharacterEncoding())); } return this; }
@Override public AsyncIOWriter write(AtmosphereResponse r, String data) throws IOException { SocketIOSessionOutbound outbound = (SocketIOSessionOutbound) request.getAttribute(SocketIOAtmosphereHandler.SOCKETIO_SESSION_OUTBOUND); SocketIOSessionManagerImpl.SocketIOProtocol p = (SocketIOSessionManagerImpl.SocketIOProtocol) r.request().getAttribute(SOCKETIO_PACKET); String msg = p == null ? data : mapper.writeValueAsString(p.clearArgs().addArgs(data)); if (outbound != null) { outbound.sendMessage(new SocketIOPacketImpl(SocketIOPacketImpl.PacketType.EVENT, msg)); } else { r.getResponse().getOutputStream().write(msg.getBytes(r.getCharacterEncoding())); } return this; }
private Builder createResponseBuilder(AtmosphereResponse res, String message) { Request swaggerSocketRequest = lookupRequest(res.request()); Response.Builder builder = new Response.Builder(); builder.status(res.getStatus(), res.getStatusMessage()); // only include some headers and not all headers from the response if (message != null && message.length() > 0) { builder.body(message).header(new Header("Content-Type", res.getContentType())); } // transfer those headers that match included and does not match excluded for (Map.Entry<String, String> hv : res.headers().entrySet()) { if (!"Content-Type".equalsIgnoreCase(hv.getKey()) && includedheaders != null && includedheaders.matcher(hv.getKey()).matches() && !(excludedheaders != null && excludedheaders.matcher(hv.getKey()).matches())) { builder.header(new Header(hv.getKey(), hv.getValue())); } } builder.uuid(swaggerSocketRequest.getUuid()).path(swaggerSocketRequest.getPath()); if (res instanceof WrappedAtmosphereResponse && ((WrappedAtmosphereResponse)res).isLast()) { builder.last(true); } return builder; }
@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; }
Request.class.cast(res.request().getAttribute(String.valueOf(res.request().hashCode()))); String identity = (String) res.request().getAttribute("swaggersocket.identity"); AtomicInteger expectedResponseCount = (AtomicInteger) res.request().getAttribute("ResponseCountNumber"); ResponseMessage m = null; if (expectedResponseCount != null) { m = (ResponseMessage) res.request().getAttribute(ResponseMessage.class.getName()); if (m != null) { m.response(builder.build()); return m; } else { res.request().setAttribute(ResponseMessage.class.getName(), m); return null;
@Override public byte[] error(AtmosphereResponse response, int statusCode, String reasonPhrase) { Request swaggerSocketRequest = lookupRequest(response.request()); if (swaggerSocketRequest == null) { logger.debug("Handshake mapping (could be expected) {} : {}", response.getStatus(), response.getStatusMessage()); return new byte[0]; } logger.debug("Unexpected status code {} : {}", response.getStatus(), response.getStatusMessage()); StatusMessage statusMessage = new StatusMessage.Builder() .status(new StatusMessage.Status(response.getStatus(), response.getStatusMessage())) .identity(swaggerSocketRequest.getUuid()).build(); try { return mapper.writeValueAsBytes(statusMessage); } catch (IOException e) { throw new RuntimeException(e); } } }
/** * {@inheritDoc} */ @Override public void onError(WebSocket webSocket, WebSocketProcessor.WebSocketException t) { if (t.response() != null) { Request swaggerSocketRequest = Request.class.cast(t.response().request().getAttribute(String.valueOf(t.response().request().hashCode()))); if (swaggerSocketRequest == null) { logger.debug("Handshake mapping (could be expected) {} : {}", t.response().getStatus(), t.response().getStatusMessage()); return; } logger.debug("Unexpected status code {} : {}", t.response().getStatus(), t.response().getStatusMessage()); StatusMessage statusMessage = new StatusMessage.Builder() .status(new StatusMessage.Status(t.response().getStatus(), t.response().getStatusMessage())) .identity(swaggerSocketRequest.getUuid()).build(); try { byte[] b = mapper.writeValueAsBytes(statusMessage); webSocket.write(t.response(), b, 0, b.length); } catch (IOException e) { throw new RuntimeException(e); } } }