All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.atmosphere.handler.OnMessage Maven / Gradle / Ivy

There is a newer version: 3.0.13
Show newest version
/*
 * Copyright 2013 Jean-Francois Arcand
 *
 * 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.AtmosphereHandler;
import org.atmosphere.cpr.AtmosphereResource;
import org.atmosphere.cpr.AtmosphereResourceEvent;
import org.atmosphere.cpr.AtmosphereResponse;

import java.io.IOException;
import java.util.List;

/**
 * Simple {@link AtmosphereHandler} that can be used with the {@link org.atmosphere.interceptor.AtmosphereResourceLifecycleInterceptor}
 * and {@link org.atmosphere.interceptor.BroadcastOnPostAtmosphereInterceptor} to reduce the handling of the suspend/resume/disconnect and
 * broadcast operation.
 *
 * @author Jeanfrancois Arcand
 */
public abstract class OnMessage extends AbstractReflectorAtmosphereHandler {
    @Override
    public final void onRequest(AtmosphereResource resource) throws IOException {
    }

    @Override
    public final void onStateChange(AtmosphereResourceEvent event) throws IOException {
        if (event.isSuspended() && event.getMessage() != null) {

            if (List.class.isAssignableFrom(event.getMessage().getClass())){
                List l = (List) event.getMessage();
                for (String t : l) {
                    onMessage(event.getResource().getResponse(),(T) t);
                }
            }  else {
                onMessage(event.getResource().getResponse(), (T) event.getMessage());
            }
        }
        postStateChange(event);
    }

    @Override
    public final void destroy() {
    }

    /**
     * Implement this method to get invoked every time a new {@link org.atmosphere.cpr.Broadcaster#broadcast(Object)}
     * occurs.
     *
     * @param message a message of type T
     */
    abstract public void onMessage(AtmosphereResponse response, T message) throws IOException;

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy