org.atmosphere.handler.AbstractReflectorAtmosphereHandler Maven / Gradle / Ivy
/*
* Copyright 2017 Async-IO.org
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.atmosphere.handler;
import org.atmosphere.cpr.ApplicationConfig;
import org.atmosphere.cpr.AtmosphereConfig;
import org.atmosphere.cpr.AtmosphereHandler;
import org.atmosphere.cpr.AtmosphereRequest;
import org.atmosphere.cpr.AtmosphereResource;
import org.atmosphere.cpr.AtmosphereResourceEvent;
import org.atmosphere.cpr.AtmosphereResourceImpl;
import org.atmosphere.cpr.AtmosphereResponse;
import org.atmosphere.cpr.AtmosphereResponseImpl;
import org.atmosphere.cpr.AtmosphereServletProcessor;
import org.atmosphere.cpr.Broadcaster;
import org.atmosphere.util.IOUtils;
import com.vaadin.external.org.slf4j.Logger;
import com.vaadin.external.org.slf4j.LoggerFactory;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import static org.atmosphere.cpr.ApplicationConfig.PROPERTY_USE_STREAM;
/**
* Simple {@link AtmosphereHandler} that reflect every call to
* {@link Broadcaster#broadcast}, eg sent the broadcasted event back to the remote client. All broadcasts will be by default returned
* as it is to the suspended {@link AtmosphereResponseImpl#getOutputStream}
* or {@link AtmosphereResponseImpl#getWriter()}.
*
* @author Jean-francois Arcand
*/
public abstract class AbstractReflectorAtmosphereHandler implements AtmosphereServletProcessor {
private static final Logger logger = LoggerFactory.getLogger(AbstractReflectorAtmosphereHandler.class);
private boolean twoStepsWrite;
/**
* 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.
*
* 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