/frameworks/sip-servlets/ssf/sf-core/src/main/java/org/mobicents/ssf/servlet/handler/support/DefaultEventStrategy.java
Java | 596 lines | 419 code | 81 blank | 96 comment | 80 complexity | 55a5ec46f083aaacca7f77117ea70127 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, CC0-1.0, Apache-2.0, BSD-3-Clause
- /**
- * This is free software; you can redistribute it and/or modify it under the
- * terms of the GNU Lesser General Public License as published by the Free
- * Software Foundation; either version 2.1 of the License, or (at your option)
- * any later version.
- *
- * This software is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
- * details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this software; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
- * site: http://www.fsf.org.
- */
- package org.mobicents.ssf.servlet.handler.support;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import java.util.StringTokenizer;
- import javax.servlet.ServletException;
- import javax.servlet.sip.SipApplicationSession;
- import javax.servlet.sip.SipServletResponse;
- import javax.servlet.sip.SipSession;
- import org.mobicents.ssf.SsfException;
- import org.mobicents.ssf.bind.SignalingError;
- import org.mobicents.ssf.bind.SignalingErrors;
- import org.mobicents.ssf.bind.SignalingResult;
- import org.mobicents.ssf.bind.SipRequestError;
- import org.mobicents.ssf.context.SignalingAttributes;
- import org.mobicents.ssf.context.SipContextHolder;
- import org.mobicents.ssf.event.DefaultEventFactory;
- import org.mobicents.ssf.event.Event;
- import org.mobicents.ssf.event.EventFactory;
- import org.mobicents.ssf.event.EventType;
- import org.mobicents.ssf.internal.ResourceMessage;
- import org.mobicents.ssf.servlet.handler.ErrorHandler;
- import org.mobicents.ssf.servlet.handler.EventStrategy;
- import org.mobicents.ssf.servlet.handler.SipHandlerAdapter;
- import org.mobicents.ssf.servlet.handler.SipHandlerFactory;
- import org.mobicents.ssf.util.MessageQueue;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.BeanFactoryUtils;
- import org.springframework.beans.factory.NoSuchBeanDefinitionException;
- import org.springframework.context.ApplicationContext;
- import org.springframework.core.OrderComparator;
- /**
- * ????????????????????????????????????????
- *
- * @author nisihara
- *
- */
- public class DefaultEventStrategy implements EventStrategy {
-
- public Logger logger = LoggerFactory.getLogger(DefaultEventStrategy.class);
-
- private ApplicationContext ac;
-
- private List<EventFactory> eventFactories;
-
- private List<SipHandlerFactory> sipHandlerFactories;
-
- private List<SipHandlerAdapter> sipHandlerAdapters;
-
- private List<ErrorHandler> errorHandlers;
-
- private boolean detectAllEventFactories = true;
-
- private boolean detectAllSipHandlerFactories = true;
-
- private boolean detectAllSipHandlerAdapters = true;
-
- private boolean detectAllErrorHandlers = true;
-
- private String servletName;
-
- /**
- * ?????????
- */
- private String stateNameSeparator = ";";
-
- /* (? Javadoc)
- * @see org.mobicents.ssf.servlet.handler.EventStrategy#getEventFactories()
- */
- public List<EventFactory> getEventFactories() {
- return this.eventFactories;
- }
-
- /* (? Javadoc)
- * @see org.mobicents.ssf.servlet.handler.EventStrategy#getSipHandlerAdapters()
- */
- public List<SipHandlerAdapter> getSipHandlerAdapters() {
- return this.sipHandlerAdapters;
- }
- /* (? Javadoc)
- * @see org.mobicents.ssf.servlet.handler.EventStrategy#getSipHandlerFactories()
- */
- public List<SipHandlerFactory> getSipHandlerFactories() {
- return sipHandlerFactories;
- }
- public String getStateNameSeparator() {
- return this.stateNameSeparator;
- }
- /* (? Javadoc)
- * @see org.mobicents.ssf.servlet.handler.EventStrategy#handle(java.lang.Object, java.lang.Object, com.oki.sip.sf.event.EventType)
- */
- @SuppressWarnings("unchecked")
- public Object handle(Object source, Object obj, EventType type) throws ServletException {
- // ??????????
- // NOTE ????????????
-
- List<Event> eventList = new LinkedList<Event>();
- for(EventFactory efactory:this.eventFactories) {
- try {
- if(logger.isTraceEnabled()) {
- logger.trace("[EventFactory:" + efactory + "],[obj:" + obj + "],[type:" + type + "]");
- }
- Event evt = efactory.createEvent(source, obj, type);
- if(evt != null) {
- if(logger.isTraceEnabled()) {
- logger.trace(ResourceMessage.getMessage(9304, efactory, evt));
- }
- eventList.add(evt);
- }
- } catch (Throwable t) {
- // ????????
- logger.error(ResourceMessage.getMessage(303, efactory, obj, type), t);
- throw new ServletException("Exception Occured.", t);
- }
- }
-
- if(logger.isDebugEnabled()) {
- logger.debug(ResourceMessage.getMessage(9305, eventList));
- }
-
- if(eventList.isEmpty()) {
- logger.error(ResourceMessage.getMessage(302, source, obj, type));
- for(ErrorHandler ehandler: this.errorHandlers) {
- // ?????????????
- SipServletResponse res = ehandler.cannotCreateEvent(obj, type);
- if(res != null) {
- try {
- // ?????????????????????????
- MessageQueue.send(res);
- return null;
- } finally {
- try {
- MessageQueue.flush(this.ac);
- } catch (Exception e) {
- logger.error("Cannot send the messages.", e);
- }
- }
- }
- }
- // ???????????????????????????????????????
- // throw new ServletException("Cannot create event.[" + source + "," + obj + "," + type);
- return null;
- }
-
- // ?????????????
- // Event??????????????Map
- Map<Event, List<Object>> handlers = new HashMap<Event, List<Object>>();
- for(SipHandlerFactory hfactory:this.sipHandlerFactories) {
- for(Event evt: eventList) {
- Object handler = hfactory.getHandler(evt);
- // Event???????????????
- if(handler != null) {
- if(logger.isTraceEnabled()) {
- logger.trace(ResourceMessage.getMessage(9306, hfactory, evt, handler));
- }
- List<Object> list = handlers.get(evt);
- if(list == null) {
- list = new LinkedList<Object>();
- }
- list.add(handler);
- handlers.put(evt, list);
- }
- }
- }
-
- for(Event evt: eventList) {
- // ?????????????????????
- List<Object> handlerList = handlers.get(evt);
- if(handlerList != null) {
- // ????????
- for(SipHandlerAdapter adapter: this.sipHandlerAdapters) {
- // ???????????????????
- for(Object handler: handlerList) {
- if(adapter.supports(handler)) {
- try {
- // ?????????????????
- // NOTE ?????????????????
- // TODO ?????????????
- if(logger.isTraceEnabled()) {
- logger.trace(ResourceMessage.getMessage(9307, adapter, evt, handler));
- }
-
- Object rv = adapter.handle(evt, handler);
-
- // ????
- if(logger.isTraceEnabled()) {
- logger.trace(ResourceMessage.getMessage(9308, adapter, evt, handler));
- }
-
- if(rv != null) {
- if(rv instanceof String) {
- // ?????????String??????
- // ???????????????
- String stateName = (String)rv;
-
- SignalingAttributes attr = SipContextHolder.currentSignalingAttributes();
-
- setupState(attr, stateName);
- } else if(rv instanceof Map) {
- // ?????????Map?????????SipSession????
- // SipApplicationSession????????????
- SignalingAttributes attr = SipContextHolder.currentSignalingAttributes();
- Map<Object, String> stateMap = (Map<Object, String>)rv;
- setupState(attr, stateMap);
- }
- }
-
- SignalingAttributes attr = SipContextHolder.currentSignalingAttributes();
- SignalingResult result = attr.getSignalingResult();
- if(result.hasErrors()) {
- // ??????????????????
- boolean isErrorSended = false;
- SignalingErrors errors = result.getSignalingErrors();
- for(SignalingError error: errors.getErrors()) {
- if(error instanceof SipRequestError) {
- SipRequestError requestError = (SipRequestError)error;
- SipServletResponse res = requestError.createResponse();
- result.push(res);
- isErrorSended = true;
- }
- }
-
- if(!isErrorSended) {
- // ?????????????????????
- throw new SsfException("SingalingError occured:" + errors);
- }
- }
-
- return rv;
- } catch (Throwable t) {
- // ?????
- // ????????ServletException????
- // ?????????????????????????
-
- MessageQueue.cancel();
-
- for(ErrorHandler ehandler: this.errorHandlers) {
- ehandler.handleError(evt, adapter, handler, t);
- }
- return t;
- } finally {
- SignalingAttributes attr = SipContextHolder.currentSignalingAttributes();
- SignalingResult result = attr.getSignalingResult();
- try {
- result.flush();
- } catch (Exception e) {
- // ????????????????????????????????
- // TODO SignalingAttributes????????rollback????
- logger.error("Cannot send the messages.", e);
- }
-
- // ?????SipSession?Handler???
- SipApplicationSession appSession = attr.getSipApplicationSession();
-
- appSession.setAttribute(HANDLER_NAME, this.servletName);
- Iterator<?> iter = appSession.getSessions("SIP");
- while(iter.hasNext()) {
- Object s = iter.next();
- if(s instanceof SipSession) {
- ((SipSession)s).setHandler(this.servletName);
- }
- }
- }
- }
- }
- }
- }
- }
-
- for(ErrorHandler ehandler: this.errorHandlers) {
- SipServletResponse res = ehandler.handlerNotFound(obj, type);
- try {
- if(res != null) {
- // ?????????????????????????
- MessageQueue.send(res);
- return null;
- }
- } finally {
- try {
- MessageQueue.flush(this.ac);
- } catch (Exception e) {
- logger.error("Cannot send the messages.", e);
- }
- }
- }
-
- // ErrorHandler????????????????????????????
- //throw new ServletException("SipHandlerAdapter NOT FOUND.[event=" + obj + "],[type=" + type + "]");
- logger.error("SipHandlerAdapter NOT FOUND.[event=" + obj + "],[type=" + type + "]");
- return null;
- }
- public List<ErrorHandler> getErrorHandlers() {
- return errorHandlers;
- }
- public void setErrorHandlers(List<ErrorHandler> errorHandlers) {
- this.errorHandlers = errorHandlers;
- }
- public boolean isDetectAllErrorHandlers() {
- return detectAllErrorHandlers;
- }
- public void setDetectAllErrorHandlers(boolean detectAllErrorHandlers) {
- this.detectAllErrorHandlers = detectAllErrorHandlers;
- }
- /* (? Javadoc)
- * @see org.mobicents.ssf.servlet.handler.EventStrategy#init(String)
- */
- public void init(String servletName) {
- this.servletName = servletName;
-
- initEventFactories();
- initSipHandlerFactories();
- initSipHandlerAdaptors();
- initErrorHandlers();
- }
- public boolean isDetectAllEventFactories() {
- return this.detectAllEventFactories;
- }
- public boolean isDetectAllSipHandlerAdapters() {
- return detectAllSipHandlerAdapters;
- }
- public boolean isDetectAllSipHandlerFactories() {
- return this.detectAllSipHandlerFactories;
- }
- /* (? Javadoc)
- * @see org.mobicents.ssf.servlet.handler.EventStrategy#setServletContext(org.springframework.context.ApplicationContext)
- */
- public void setApplicationContext(ApplicationContext ac) {
- this.ac = ac;
- }
- public void setDetectAllEventFactories(boolean detectAllEventFactories) {
- this.detectAllEventFactories = detectAllEventFactories;
- }
- public void setDetectAllSipHandlerAdapters(boolean detectAllSipHandlerAdapters) {
- this.detectAllSipHandlerAdapters = detectAllSipHandlerAdapters;
- }
- public void setDetectAllSipHandlerFactories(boolean detectAllSipHandlerFactories) {
- this.detectAllSipHandlerFactories = detectAllSipHandlerFactories;
- }
- /* (? Javadoc)
- * @see org.mobicents.ssf.servlet.handler.EventStrategy#setEventFactories(java.util.List)
- */
- public void setEventFactories(List<EventFactory> eventFactories) {
- this.eventFactories = eventFactories;
- }
- /* (? Javadoc)
- * @see org.mobicents.ssf.servlet.handler.EventStrategy#setSipHandlerAdapters(java.util.List)
- */
- public void setSipHandlerAdapters(List<SipHandlerAdapter> sipHandlerAdapters) {
- this.sipHandlerAdapters = sipHandlerAdapters;
- }
- /* (? Javadoc)
- * @see org.mobicents.ssf.servlet.handler.EventStrategy#setSipHandlerFactories(java.util.List)
- */
- public void setSipHandlerFactories(List<SipHandlerFactory> sipHandlerFactories) {
- this.sipHandlerFactories = sipHandlerFactories;
- }
- public void setStateNameSeparator(String separator) {
- this.stateNameSeparator = separator;
- }
-
- @SuppressWarnings("unchecked")
- private void initEventFactories() {
- this.eventFactories = null;
-
- if(this.detectAllEventFactories) {
- if(logger.isTraceEnabled()) {
- logger.trace(ResourceMessage.getMessage(9301, EventFactory.class.getName(), ac));
- }
-
- Map matchedBeans =
- BeanFactoryUtils.beansOfTypeIncludingAncestors(ac, EventFactory.class, true, false);
-
- if(!matchedBeans.isEmpty()) {
- this.eventFactories = new ArrayList(matchedBeans.values());
- Collections.sort(this.eventFactories, new OrderComparator());
- }
- } else {
- try {
- if(logger.isTraceEnabled()) {
- logger.trace(ResourceMessage.getMessage(9302, EVENT_FACTORY_BEAN_NAME, EventFactory.class.getName()));
- }
- EventFactory ef =
- (EventFactory)this.ac.getBean(EVENT_FACTORY_BEAN_NAME, EventFactory.class);
- this.eventFactories = Collections.singletonList(ef);
- } catch (NoSuchBeanDefinitionException e) {
- //
- }
- }
-
- if(this.eventFactories == null) {
- logger.info(ResourceMessage.getMessage(301, EventFactory.class.getName(), DefaultEventFactory.class.getName()));
- this.eventFactories = Collections.singletonList((EventFactory)new DefaultEventFactory());
- }
- if(logger.isDebugEnabled()) {
- logger.debug(ResourceMessage.getMessage(9303, EventFactory.class.getName(), this.eventFactories));
- }
- }
-
- @SuppressWarnings("unchecked")
- private void initSipHandlerAdaptors() {
- this.sipHandlerAdapters = null;
-
- if(this.detectAllSipHandlerAdapters) {
- if(logger.isTraceEnabled()) {
- logger.trace(ResourceMessage.getMessage(9301, SipHandlerAdapter.class.getName(), ac));
- }
- Map matchedBeans =
- BeanFactoryUtils.beansOfTypeIncludingAncestors(ac, SipHandlerAdapter.class, true, false);
-
- if(!matchedBeans.isEmpty()) {
- this.sipHandlerAdapters = new ArrayList(matchedBeans.values());
- Collections.sort(this.sipHandlerAdapters, new OrderComparator());
- }
- } else {
- try {
- if(logger.isTraceEnabled()) {
- logger.trace(ResourceMessage.getMessage(9302, SIP_HANDLER_ADAPTER_BEAN_NAME, SipHandlerAdapter.class.getName()));
- }
- SipHandlerAdapter sha =
- (SipHandlerAdapter)this.ac.getBean(SIP_HANDLER_ADAPTER_BEAN_NAME, SipHandlerAdapter.class);
- this.sipHandlerAdapters = Collections.singletonList(sha);
- } catch (NoSuchBeanDefinitionException e) {
- // ignore
- }
- }
-
- if(this.sipHandlerAdapters == null) {
- logger.info(ResourceMessage.getMessage(301, SipHandlerAdapter.class.getName(), DefaultSipHandlerAdapter.class.getName()));
- this.sipHandlerAdapters = Collections.singletonList((SipHandlerAdapter)new DefaultSipHandlerAdapter());
- }
- if(logger.isDebugEnabled()) {
- logger.debug(ResourceMessage.getMessage(9303, SipHandlerAdapter.class.getName(), this.sipHandlerAdapters));
- }
- }
-
- @SuppressWarnings("unchecked")
- private void initSipHandlerFactories() {
- this.sipHandlerFactories = null;
-
- if(this.detectAllSipHandlerFactories) {
- if(logger.isTraceEnabled()) {
- logger.trace(ResourceMessage.getMessage(9301, SipHandlerFactory.class.getName(), ac));
- }
- Map matchedBeans =
- BeanFactoryUtils.beansOfTypeIncludingAncestors(ac, SipHandlerFactory.class, true, false);
- if(logger.isDebugEnabled()) {
- logger.debug("initSipHandlerFactories:[matchedBeans=" + matchedBeans + "]");
- }
- if(!matchedBeans.isEmpty()) {
- this.sipHandlerFactories = new ArrayList(matchedBeans.values());
- Collections.sort(this.sipHandlerFactories, new OrderComparator());
- }
- } else {
- try {
- if(logger.isTraceEnabled()) {
- logger.trace(ResourceMessage.getMessage(9302, SIP_HANDLER_FACTORY_BEAN_NAME, SipHandlerFactory.class.getName()));
- }
- SipHandlerFactory shf =
- (SipHandlerFactory)this.ac.getBean(SIP_HANDLER_FACTORY_BEAN_NAME, SipHandlerFactory.class);
- this.sipHandlerFactories = Collections.singletonList(shf);
- } catch (NoSuchBeanDefinitionException e) {
- // ignore
- }
- }
-
- if(this.sipHandlerFactories == null) {
- logger.info(ResourceMessage.getMessage(301, SipHandlerAdapter.class.getName(), DefaultSipHandlerFactory.class.getName()));
- this.sipHandlerFactories = Collections.singletonList((SipHandlerFactory)new DefaultSipHandlerFactory());
- }
-
- if(logger.isDebugEnabled()) {
- logger.debug(ResourceMessage.getMessage(9303, SipHandlerFactory.class.getName(), this.sipHandlerFactories));
- }
- }
-
- @SuppressWarnings("unchecked")
- private void initErrorHandlers() {
- this.errorHandlers = null;
-
- if(this.detectAllErrorHandlers) {
- if(logger.isTraceEnabled()) {
- logger.trace(ResourceMessage.getMessage(9301, ErrorHandler.class.getName(), ac));
- }
- Map matchedBeans =
- BeanFactoryUtils.beansOfTypeIncludingAncestors(ac, ErrorHandler.class, true, false);
- if(logger.isDebugEnabled()) {
- logger.debug("initErrorHandlers:[matchedBeans=" + matchedBeans + "]");
- }
- if(!matchedBeans.isEmpty()) {
- this.errorHandlers = new ArrayList(matchedBeans.values());
- Collections.sort(this.errorHandlers, new OrderComparator());
- }
- } else {
- try {
- if(logger.isTraceEnabled()) {
- logger.trace(ResourceMessage.getMessage(9302, ERROR_HANDLER_BEAN_NAME, ErrorHandler.class.getName()));
- }
- ErrorHandler eh =
- (ErrorHandler)this.ac.getBean(ERROR_HANDLER_BEAN_NAME, ErrorHandler.class);
- this.errorHandlers = Collections.singletonList(eh);
- } catch (NoSuchBeanDefinitionException e) {
- // ignore
- }
- }
-
- if(this.errorHandlers == null) {
- logger.info(ResourceMessage.getMessage(301, ErrorHandler.class.getName(), DefaultErrorHandler.class.getName()));
- this.errorHandlers = Collections.singletonList((ErrorHandler)new DefaultErrorHandler());
- }
-
- if(logger.isDebugEnabled()) {
- logger.debug(ResourceMessage.getMessage(9303, ErrorHandler.class.getName(), this.errorHandlers));
- }
- }
-
- private void setupState(SignalingAttributes attr, Map<Object, String> stateMap) {
- Set<Map.Entry<Object, String>> set = stateMap.entrySet();
- for(Map.Entry<Object, String> entry: set) {
- // ?Entry???????????
- attr.setStateName(entry.getKey(), entry.getValue());
- }
- }
-
- private void setupState(SignalingAttributes attr, String stateName) {
- // ??????
- StringTokenizer token = new StringTokenizer(stateName, stateNameSeparator);
- int countTokens = token.countTokens();
- if(countTokens == 1) {
- // ????????SipApplicationSession????
- attr.setStateName(attr.getSipApplicationSession(), stateName);
- return;
- } else if(countTokens == 2){
- // ?????key??????????
- // ???SipSesion?ID???????????
- // ?session.getId() + ";" + calling???????????????ID?SipSession????
- // ?????calling?????????????
- String key = token.nextToken();
- String name = token.nextToken();
- attr.setStateName(key, name);
- } else {
- // TODO ????????????????????
- throw new IllegalArgumentException("Cannot parse state-name:[state-name:" + stateName +
- "],[separator:" + stateNameSeparator + "]");
- }
- }
- }