use of org.atmosphere.cpr.AtmosphereResponse in project atmosphere by Atmosphere.
the class AbstractReflectorAtmosphereHandler method onStateChange.
/**
* Write the {@link AtmosphereResourceEvent#getMessage()} back to the client using
* the {@link AtmosphereResponseImpl#getOutputStream()} or {@link AtmosphereResponseImpl#getWriter()}.
* If a {@link org.atmosphere.cpr.Serializer} is defined, it will be invoked and the write operation
* will be delegated to it.
* <p/>
* By default, this method will try to use {@link AtmosphereResponseImpl#getWriter()}.
*
* @param event the {@link AtmosphereResourceEvent#getMessage()}
* @throws java.io.IOException
*/
@Override
public void onStateChange(AtmosphereResourceEvent event) throws IOException {
Object message = event.getMessage();
AtmosphereResource resource = event.getResource();
AtmosphereResponse r = resource.getResponse();
AtmosphereRequest request = resource.getRequest();
boolean writeAsBytes = IOUtils.isBodyBinary(request);
if (message == null) {
logger.trace("Message was null for AtmosphereEvent {}", event);
return;
}
if (resource.getSerializer() != null) {
try {
if (message instanceof List) {
for (Object s : (List<Object>) message) {
resource.getSerializer().write(resource.getResponse().getOutputStream(), s);
}
} else {
resource.getSerializer().write(resource.getResponse().getOutputStream(), message);
}
} catch (Throwable ex) {
logger.warn("Serializer exception: message: {}", message, ex);
throw new IOException(ex);
}
} else {
boolean isUsingStream = true;
Object o = resource.getRequest().getAttribute(PROPERTY_USE_STREAM);
if (o != null) {
isUsingStream = (Boolean) o;
}
if (!isUsingStream) {
try {
r.getWriter();
} catch (IllegalStateException e) {
isUsingStream = true;
}
if (writeAsBytes) {
throw new IllegalStateException("Cannot write bytes using PrintWriter");
}
}
if (message instanceof List) {
Iterator<Object> i = ((List) message).iterator();
try {
Object s;
while (i.hasNext()) {
s = i.next();
if (String.class.isAssignableFrom(s.getClass())) {
if (isUsingStream) {
r.getOutputStream().write(s.toString().getBytes(r.getCharacterEncoding()));
} else {
r.getWriter().write(s.toString());
}
} else if (byte[].class.isAssignableFrom(s.getClass())) {
if (isUsingStream) {
r.getOutputStream().write((byte[]) s);
} else {
r.getWriter().write(s.toString());
}
} else {
if (isUsingStream) {
r.getOutputStream().write(s.toString().getBytes(r.getCharacterEncoding()));
} else {
r.getWriter().write(s.toString());
}
}
i.remove();
}
} catch (IOException ex) {
event.setMessage(new ArrayList<String>().addAll((List) message));
throw ex;
}
if (isUsingStream) {
r.getOutputStream().flush();
} else {
r.getWriter().flush();
}
} else {
if (isUsingStream) {
r.getOutputStream().write(writeAsBytes ? (byte[]) message : message.toString().getBytes(r.getCharacterEncoding()));
r.getOutputStream().flush();
} else {
r.getWriter().write(message.toString());
r.getWriter().flush();
}
}
}
postStateChange(event);
}
use of org.atmosphere.cpr.AtmosphereResponse in project atmosphere by Atmosphere.
the class SimpleRestInterceptor method inspect.
@Override
public Action inspect(final AtmosphereResource r) {
if (AtmosphereResource.TRANSPORT.WEBSOCKET != r.transport() && AtmosphereResource.TRANSPORT.SSE != r.transport() && AtmosphereResource.TRANSPORT.POLLING != r.transport()) {
LOG.debug("Skipping for non websocket request");
return Action.CONTINUE;
}
if (AtmosphereResource.TRANSPORT.POLLING == r.transport()) {
final String saruuid = (String) r.getRequest().getAttribute(ApplicationConfig.SUSPENDED_ATMOSPHERE_RESOURCE_UUID);
final AtmosphereResponse suspendedResponse = suspendedResponses.get(saruuid);
LOG.debug("Attaching a proxy writer to suspended response");
r.getResponse().asyncIOWriter(new AtmosphereInterceptorWriter() {
@Override
public AsyncIOWriter write(AtmosphereResponse r, String data) throws IOException {
suspendedResponse.write(data);
suspendedResponse.flushBuffer();
return this;
}
@Override
public AsyncIOWriter write(AtmosphereResponse r, byte[] data) throws IOException {
suspendedResponse.write(data);
suspendedResponse.flushBuffer();
return this;
}
@Override
public AsyncIOWriter write(AtmosphereResponse r, byte[] data, int offset, int length) throws IOException {
suspendedResponse.write(data, offset, length);
suspendedResponse.flushBuffer();
return this;
}
@Override
public void close(AtmosphereResponse response) throws IOException {
}
});
// REVISIT we need to keep this response's asyncwriter alive so that data can be written to the
// suspended response, but investigate if there is a better alternative.
r.getResponse().destroyable(false);
return Action.CONTINUE;
}
r.addEventListener(new AtmosphereResourceEventListenerAdapter() {
@Override
public void onSuspend(AtmosphereResourceEvent event) {
final String srid = (String) event.getResource().getRequest().getAttribute(ApplicationConfig.SUSPENDED_ATMOSPHERE_RESOURCE_UUID);
LOG.debug("Registrering suspended resource: {}", srid);
suspendedResponses.put(srid, event.getResource().getResponse());
AsyncIOWriter writer = event.getResource().getResponse().getAsyncIOWriter();
if (writer == null) {
writer = new AtmosphereInterceptorWriter();
r.getResponse().asyncIOWriter(writer);
}
if (writer instanceof AtmosphereInterceptorWriter) {
((AtmosphereInterceptorWriter) writer).interceptor(interceptor);
}
}
@Override
public void onDisconnect(AtmosphereResourceEvent event) {
super.onDisconnect(event);
final String srid = (String) event.getResource().getRequest().getAttribute(ApplicationConfig.SUSPENDED_ATMOSPHERE_RESOURCE_UUID);
LOG.debug("Unregistrering suspended resource: {}", srid);
suspendedResponses.remove(srid);
}
});
AtmosphereRequest request = r.getRequest();
if (request.getAttribute(REQUEST_DISPATCHED) == null) {
try {
// REVISIT use a more efficient approach for the detached mode (i.e.,avoid reading the message into a string)
// read the message entity and dispatch a service call
String body = IOUtils.readEntirelyAsString(r).toString();
LOG.debug("Request message: '{}'", body);
if (body.length() == 0) {
// TODO we might want to move this heartbeat scheduling after the handshake phase (if that is added)
if ((AtmosphereResource.TRANSPORT.WEBSOCKET == r.transport() || AtmosphereResource.TRANSPORT.SSE == r.transport()) && request.getAttribute(HEARTBEAT_SCHEDULED) == null) {
r.suspend();
scheduleHeartbeat(r);
request.setAttribute(HEARTBEAT_SCHEDULED, "true");
return Action.SUSPEND;
}
return Action.CANCELLED;
}
AtmosphereRequest ar = createAtmosphereRequest(request, body);
if (ar == null) {
return Action.CANCELLED;
}
AtmosphereResponse response = r.getResponse();
ar.localAttributes().put(REQUEST_DISPATCHED, "true");
request.removeAttribute(FrameworkConfig.INJECTED_ATMOSPHERE_RESOURCE);
response.request(ar);
attachWriter(r);
Action action = r.getAtmosphereConfig().framework().doCometSupport(ar, response);
if (action.type() == Action.TYPE.SUSPEND) {
ar.destroyable(false);
response.destroyable(false);
}
return Action.CANCELLED;
} catch (IOException | ServletException e) {
LOG.error("Failed to process", e);
}
}
return Action.CONTINUE;
}
use of org.atmosphere.cpr.AtmosphereResponse in project atmosphere by Atmosphere.
the class TrackMessageSizeInterceptor method inspect.
@Override
public Action inspect(final AtmosphereResource r) {
if (AtmosphereResource.TRANSPORT.UNDEFINED == r.transport() || Utils.webSocketMessage(r))
return Action.CONTINUE;
final AtmosphereResponse response = r.getResponse();
super.inspect(r);
AsyncIOWriter writer = response.getAsyncIOWriter();
if (AtmosphereInterceptorWriter.class.isAssignableFrom(writer.getClass())) {
((AtmosphereInterceptorWriter) writer).interceptor(interceptor);
} else {
logger.warn("Unable to apply {}. Your AsyncIOWriter must implement {}", getClass().getName(), AtmosphereInterceptorWriter.class.getName());
}
return Action.CONTINUE;
}
use of org.atmosphere.cpr.AtmosphereResponse in project cxf by apache.
the class DefaultProtocolInterceptorTest method testCreateResponseWithHeadersFiltering.
@Test
public void testCreateResponseWithHeadersFiltering() throws Exception {
DefaultProtocolInterceptor dpi = new DefaultProtocolInterceptor();
AtmosphereRequest request = AtmosphereRequestImpl.newInstance();
AtmosphereResponse response = AtmosphereResponseImpl.newInstance();
AtmosphereResourceImpl resource = new AtmosphereResourceImpl();
resource.transport(AtmosphereResource.TRANSPORT.WEBSOCKET);
request.localAttributes().put(FrameworkConfig.ATMOSPHERE_RESOURCE, resource);
response.request(request);
String payload = "hello cxf";
String contentType = "text/plain";
response.headers().put("Content-Type", contentType);
byte[] transformed = dpi.createResponse(response, payload.getBytes(), true);
verifyTransformed("200", new String[] { "Content-Type", contentType }, payload, transformed);
response.headers().put("X-fruit", "peach");
response.headers().put("X-vegetable", "tomato");
transformed = dpi.createResponse(response, payload.getBytes(), true);
verifyTransformed("200", new String[] { "Content-Type", contentType }, payload, transformed);
dpi.includedheaders("X-f.*");
transformed = dpi.createResponse(response, payload.getBytes(), true);
verifyTransformed("200", new String[] { "Content-Type", contentType, "X-Fruit", "peach" }, payload, transformed);
dpi.includedheaders("X-.*");
transformed = dpi.createResponse(response, payload.getBytes(), true);
verifyTransformed("200", new String[] { "Content-Type", contentType, "X-Fruit", "peach", "X-vegetable", "tomato" }, payload, transformed);
dpi.excludedheaders(".*able");
transformed = dpi.createResponse(response, payload.getBytes(), true);
verifyTransformed("200", new String[] { "Content-Type", contentType, "X-Fruit", "peach" }, payload, transformed);
}
Aggregations