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

org.kurento.jsonrpc.internal.server.PerSessionJsonRpcHandler Maven / Gradle / Ivy

/*
 * (C) Copyright 2013 Kurento (http://kurento.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.kurento.jsonrpc.internal.server;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.kurento.jsonrpc.JsonRpcHandler;
import org.kurento.jsonrpc.Session;
import org.kurento.jsonrpc.Transaction;
import org.kurento.jsonrpc.message.Request;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.util.Assert;
import org.springframework.web.socket.handler.PerConnectionWebSocketHandler;

import com.google.common.collect.ImmutableList;

public class PerSessionJsonRpcHandler implements JsonRpcHandler, BeanFactoryAware {

  private static final Log logger = LogFactory.getLog(PerConnectionWebSocketHandler.class);

  private final BeanCreatingHelper> provider;

  private final Map> handlers = new ConcurrentHashMap<>();

  private boolean useSockJS;

  private String label;

  private List allowedOrigins = ImmutableList.of();
  
  private List interceptors = ImmutableList.of();

  private boolean pingWachdog;

  public PerSessionJsonRpcHandler(String handlerName) {
    this(handlerName, null);
  }

  public PerSessionJsonRpcHandler(Class> handlerType) {
    this(null, handlerType);
  }

  public PerSessionJsonRpcHandler(String handlerName,
      Class> handlerType) {
    this.provider = new BeanCreatingHelper<>(handlerType, handlerName);
  }

  @Override
  @SuppressWarnings("unchecked")
  public Class> getHandlerType() {
    Class> clazz =
        (Class>) provider.getCreatedBeanType();

    // FIXME this has to be done in order to obtain the type of T when the
    // bean is created from a name
    if (clazz == null) {
      this.provider.createBean();
      clazz = (Class>) provider.getCreatedBeanType();
    }

    return clazz;
  }

  @Override
  public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
    this.provider.setBeanFactory(beanFactory);
  }

  @Override
  public void handleRequest(Transaction transaction, Request request) throws Exception {

    JsonRpcHandler handler = getHandler(transaction.getSession());

    Assert.isTrue(handler != null, "Handler of class " + provider.getClass()
        + " can't be created. Be sure that there" + " is a bean registered of this type");

    try {
      handler.handleRequest(transaction, request);
    } catch (Exception e) {
      handler.handleUncaughtException(transaction.getSession(), e);
    }
  }

  private JsonRpcHandler getHandler(Session session) {
    JsonRpcHandler handler = this.handlers.get(session);
    Assert.isTrue(handler != null, "JsonRpcHandler not found for " + session);
    return handler;
  }

  @Override
  public void afterConnectionEstablished(Session session) throws Exception {
    JsonRpcHandler handler = this.provider.createBean();
    this.handlers.put(session, handler);

    try {
      handler.afterConnectionEstablished(session);
    } catch (Exception e) {
      handler.handleUncaughtException(session, e);
    }
  }

  @Override
  public void afterConnectionClosed(Session session, String status) throws Exception {
    try {
      JsonRpcHandler handler = getHandler(session);
      try {
        handler.afterConnectionClosed(session, status);
      } catch (Exception e) {
        handler.handleUncaughtException(session, e);
      }
    } finally {
      destroy(session);
    }
  }

  	@Override
	public void afterReconnection(Session session) throws Exception {
		JsonRpcHandler handler = null;
		try {
			handler = getHandler(session);
			handler.afterReconnection(session);
		} catch (Exception e) {
			handler.handleUncaughtException(session, e);
		}
	}

  private void destroy(Session session) {
    JsonRpcHandler handler = this.handlers.remove(session);
    try {
      if (handler != null) {
        this.provider.destroy(handler);
      }
    } catch (Throwable t) {
      logger.warn("Error while destroying handler", t);
    }
  }

  @Override
  public void handleTransportError(Session session, Throwable exception) throws Exception {
    JsonRpcHandler handler = getHandler(session);
    try {
      handler.handleTransportError(session, exception);
    } catch (Exception e) {
      handler.handleUncaughtException(session, e);
    }
  }

  @Override
  public void handleUncaughtException(Session session, Exception exception) {
    logger.error("Uncaught exception while execution PerSessionJsonRpcHandler", exception);
  }

  @Override
  public PerSessionJsonRpcHandler withSockJS() {
    this.useSockJS = true;
    return this;
  }

  @Override
  public boolean isSockJSEnabled() {
    return this.useSockJS;
  }

  @Override
  public PerSessionJsonRpcHandler withLabel(String label) {
    this.label = label;
    return this;
  }

  @Override
  public String getLabel() {
    return label;
  }

  public PerSessionJsonRpcHandler withPingWachdog(boolean pingAsWachdog) {
    this.pingWachdog = pingAsWachdog;
    return this;
  }

  @Override
  public boolean isPingWatchdog() {
    return pingWachdog;
  }

  @Override
  public final PerSessionJsonRpcHandler withAllowedOrigins(String... origins) {
    this.allowedOrigins = ImmutableList.copyOf(origins);
    return this;
  }

  @Override
  public List allowedOrigins() {
    return this.allowedOrigins;
  }

  @Override
  public JsonRpcHandler withInterceptors(Object... interceptors) {
    this.interceptors = ImmutableList.copyOf(interceptors);
    return this;
  }

  @Override
  public List interceptors() {
    return this.interceptors;
  }

}