PageRenderTime 31ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/servers/diameter/core/jdiameter/impl/src/main/java/org/jdiameter/common/impl/app/gq/GqSessionFactoryImpl.java

http://mobicents.googlecode.com/
Java | 434 lines | 243 code | 56 blank | 135 comment | 33 complexity | 7e2c3dc0a9c6e655f6f2089b6bfcefd5 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
  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc. and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jdiameter.common.impl.app.gq;
  23. import org.jdiameter.api.Answer;
  24. import org.jdiameter.api.ApplicationId;
  25. import org.jdiameter.api.IllegalDiameterStateException;
  26. import org.jdiameter.api.InternalException;
  27. import org.jdiameter.api.Message;
  28. import org.jdiameter.api.OverloadException;
  29. import org.jdiameter.api.Request;
  30. import org.jdiameter.api.RouteException;
  31. import org.jdiameter.api.SessionFactory;
  32. import org.jdiameter.api.app.AppAnswerEvent;
  33. import org.jdiameter.api.app.AppRequestEvent;
  34. import org.jdiameter.api.app.AppSession;
  35. import org.jdiameter.api.app.StateChangeListener;
  36. import org.jdiameter.api.gq.GqClientSession;
  37. import org.jdiameter.api.gq.GqServerSession;
  38. import org.jdiameter.api.auth.ClientAuthSession;
  39. import org.jdiameter.api.auth.ServerAuthSession;
  40. import org.jdiameter.api.auth.ClientAuthSessionListener;
  41. import org.jdiameter.api.auth.ServerAuthSessionListener;
  42. import org.jdiameter.api.auth.events.AbortSessionAnswer;
  43. import org.jdiameter.api.auth.events.AbortSessionRequest;
  44. import org.jdiameter.api.auth.events.ReAuthAnswer;
  45. import org.jdiameter.api.auth.events.ReAuthRequest;
  46. import org.jdiameter.api.auth.events.SessionTermAnswer;
  47. import org.jdiameter.api.auth.events.SessionTermRequest;
  48. import org.jdiameter.client.api.ISessionFactory;
  49. import org.jdiameter.client.impl.app.gq.GqClientSessionImpl;
  50. import org.jdiameter.client.impl.app.auth.IClientAuthSessionData;
  51. import org.jdiameter.common.api.app.IAppSessionDataFactory;
  52. import org.jdiameter.common.api.app.auth.IAuthMessageFactory;
  53. import org.jdiameter.common.api.app.auth.IAuthSessionData;
  54. import org.jdiameter.common.api.app.auth.IAuthSessionFactory;
  55. import org.jdiameter.common.api.app.auth.IClientAuthActionContext;
  56. import org.jdiameter.common.api.app.auth.IServerAuthActionContext;
  57. import org.jdiameter.common.api.data.ISessionDatasource;
  58. import org.jdiameter.common.impl.app.AppAnswerEventImpl;
  59. import org.jdiameter.common.impl.app.AppRequestEventImpl;
  60. import org.jdiameter.server.impl.app.auth.IServerAuthSessionData;
  61. import org.jdiameter.server.impl.app.gq.GqServerSessionImpl;
  62. import org.slf4j.Logger;
  63. import org.slf4j.LoggerFactory;
  64. /**
  65. * Default Diameter Gq Application Session Factory implementation
  66. *
  67. * @author <a href="mailto:webdev@web-ukraine.info"> Yulian Oifa </a>
  68. * @author <a href="mailto:brainslog@gmail.com"> Alexandre Mendonca </a>
  69. * @author <a href="mailto:baranowb@gmail.com"> Bartosz Baranowski </a>
  70. */
  71. public class GqSessionFactoryImpl implements IAuthSessionFactory, IAuthMessageFactory, ServerAuthSessionListener,
  72. ClientAuthSessionListener, IClientAuthActionContext, IServerAuthActionContext, StateChangeListener<AppSession> {
  73. private final static long authAppId = 16777222L;
  74. protected IAuthMessageFactory messageFactory;
  75. protected ServerAuthSessionListener serverSessionListener;
  76. protected StateChangeListener<AppSession> stateListener;
  77. protected ClientAuthSessionListener clientSessionListener;
  78. protected boolean stateles;
  79. protected long messageTimeout = 5000;
  80. protected Logger logger = LoggerFactory.getLogger(GqSessionFactoryImpl.class);
  81. protected ISessionDatasource iss;
  82. protected ISessionFactory sessionFactory = null;
  83. protected IServerAuthActionContext serverSessionContext;
  84. protected IClientAuthActionContext clientSessionContext;
  85. protected IAppSessionDataFactory<IAuthSessionData> sessionDataFactory;
  86. public GqSessionFactoryImpl(SessionFactory sessionFactory) {
  87. super();
  88. this.sessionFactory = (ISessionFactory) sessionFactory;
  89. this.iss = this.sessionFactory.getContainer().getAssemblerFacility().getComponentInstance(ISessionDatasource.class);
  90. this.sessionDataFactory = (IAppSessionDataFactory<IAuthSessionData>) this.iss.getDataFactory(IAuthSessionData.class);
  91. }
  92. /**
  93. * @return the clientSessionContext
  94. */
  95. public IClientAuthActionContext getClientSessionContext() {
  96. return clientSessionContext != null ? clientSessionContext : this;
  97. }
  98. /**
  99. * @param clientSessionContext
  100. * the clientSessionContext to set
  101. */
  102. public void setClientSessionContext(IClientAuthActionContext clientSessionContext) {
  103. this.clientSessionContext = clientSessionContext;
  104. }
  105. /**
  106. * @return the serverSessionContext
  107. */
  108. public IServerAuthActionContext getServerSessionContext() {
  109. return serverSessionContext != null ? serverSessionContext : this;
  110. }
  111. /**
  112. * @param serverSessionContext
  113. * the serverSessionContext to set
  114. */
  115. public void setServerSessionContext(IServerAuthActionContext serverSessionContext) {
  116. this.serverSessionContext = serverSessionContext;
  117. }
  118. /**
  119. * @return the messageTimeout
  120. */
  121. public long getMessageTimeout() {
  122. return messageTimeout;
  123. }
  124. /**
  125. * @param messageTimeout
  126. * the messageTimeout to set
  127. */
  128. public void setMessageTimeout(long messageTimeout) {
  129. this.messageTimeout = messageTimeout;
  130. }
  131. /**
  132. * @return the messageFactory
  133. */
  134. public IAuthMessageFactory getMessageFactory() {
  135. return messageFactory != null ? messageFactory : this;
  136. }
  137. /**
  138. * @param messageFactory
  139. * the messageFactory to set
  140. */
  141. public void setMessageFactory(IAuthMessageFactory messageFactory) {
  142. this.messageFactory = messageFactory;
  143. }
  144. /**
  145. * @return the serverSessionListener
  146. */
  147. public ServerAuthSessionListener getServerSessionListener() {
  148. return serverSessionListener != null ? serverSessionListener : this;
  149. }
  150. /**
  151. * @param serverSessionListener
  152. * the serverSessionListener to set
  153. */
  154. public void setServerSessionListener(ServerAuthSessionListener serverSessionListener) {
  155. this.serverSessionListener = serverSessionListener;
  156. }
  157. /**
  158. * @return the stateListener
  159. */
  160. public StateChangeListener<AppSession> getStateListener() {
  161. return stateListener != null ? stateListener : this;
  162. }
  163. /**
  164. * @param stateListener
  165. * the stateListener to set
  166. */
  167. public void setStateListener(StateChangeListener<AppSession> stateListener) {
  168. this.stateListener = stateListener;
  169. }
  170. /**
  171. * @return the clientSessionListener
  172. */
  173. public ClientAuthSessionListener getClientSessionListener() {
  174. return clientSessionListener != null ? clientSessionListener : this;
  175. }
  176. /**
  177. * @param clientSessionListener
  178. * the clientSessionListener to set
  179. */
  180. public void setClientSessionListener(ClientAuthSessionListener clientSessionListener) {
  181. this.clientSessionListener = clientSessionListener;
  182. }
  183. public boolean isStateles() {
  184. return stateles;
  185. }
  186. public void setStateles(boolean stateles) {
  187. this.stateles = stateles;
  188. }
  189. @Override
  190. public AppSession getSession(String sessionId, Class<? extends AppSession> aClass) {
  191. if (sessionId == null) {
  192. throw new IllegalArgumentException("SessionId must not be null");
  193. }
  194. if(!this.iss.exists(sessionId)) {
  195. return null;
  196. }
  197. try {
  198. if (aClass == GqServerSession.class) {
  199. IServerAuthSessionData sessionData = (IServerAuthSessionData) this.sessionDataFactory.getAppSessionData(GqServerSession.class, sessionId);
  200. GqServerSessionImpl session = new GqServerSessionImpl(sessionData, sessionFactory, getServerSessionListener(), getMessageFactory(),
  201. getStateListener(), getServerSessionContext(), messageTimeout, isStateles());
  202. session.getSessions().get(0).setRequestListener(session);
  203. return session;
  204. }
  205. else {
  206. if (aClass == GqClientSession.class) {
  207. IClientAuthSessionData sessionData = (IClientAuthSessionData) this.sessionDataFactory.getAppSessionData(GqClientSession.class, sessionId);
  208. GqClientSessionImpl session = new GqClientSessionImpl(sessionData, sessionFactory, getClientSessionListener(), getMessageFactory(),
  209. getStateListener(), getClientSessionContext(), isStateles());
  210. session.getSessions().get(0).setRequestListener(session);
  211. return session;
  212. }
  213. }
  214. }
  215. catch (Exception e) {
  216. logger.error("Failure trying to obtain new authorization session", e);
  217. }
  218. return null;
  219. }
  220. public AppSession getNewSession(String sessionId, Class<? extends AppSession> aClass, ApplicationId applicationId, Object[] args) {
  221. try {
  222. if (aClass == GqServerSession.class) {
  223. if (sessionId == null) {
  224. if (args != null && args.length > 0 && args[0] instanceof Request) {
  225. Request request = (Request) args[0];
  226. sessionId = request.getSessionId();
  227. }
  228. else {
  229. sessionId = this.sessionFactory.getSessionId();
  230. }
  231. }
  232. IServerAuthSessionData sessionData = (IServerAuthSessionData) this.sessionDataFactory.getAppSessionData(ServerAuthSession.class, sessionId);
  233. sessionData.setApplicationId(applicationId);
  234. GqServerSessionImpl session = new GqServerSessionImpl(sessionData, sessionFactory, getServerSessionListener(), getMessageFactory(),
  235. getStateListener(), getServerSessionContext(), messageTimeout, isStateles());
  236. iss.addSession(session);
  237. session.getSessions().get(0).setRequestListener(session);
  238. return session;
  239. }
  240. else {
  241. if (aClass == GqClientSession.class) {
  242. if (sessionId == null) {
  243. if (args != null && args.length > 0 && args[0] instanceof Request) {
  244. Request request = (Request) args[0];
  245. sessionId = request.getSessionId();
  246. }
  247. else {
  248. sessionId = this.sessionFactory.getSessionId();
  249. }
  250. }
  251. IClientAuthSessionData sessionData = (IClientAuthSessionData) this.sessionDataFactory.getAppSessionData(ClientAuthSession.class, sessionId);
  252. sessionData.setApplicationId(applicationId);
  253. GqClientSessionImpl session = new GqClientSessionImpl(sessionData, sessionFactory, getClientSessionListener(), getMessageFactory(),
  254. getStateListener(), getClientSessionContext(), isStateles());
  255. iss.addSession(session);
  256. // iss.setSessionListener(clientSession.getSessionId(), (NetworkReqListener) appSession);
  257. session.getSessions().get(0).setRequestListener(session);
  258. return session;
  259. }
  260. }
  261. }
  262. catch (Exception e) {
  263. logger.error("Failure trying to obtain new authorization session", e);
  264. }
  265. return null;
  266. }
  267. // Message Factory Methods ------------------------------------------------
  268. public AppAnswerEvent createAuthAnswer(Answer answer) {
  269. return new AppAnswerEventImpl(answer);
  270. }
  271. public AppRequestEvent createAuthRequest(Request request) {
  272. return new AppRequestEventImpl(request);
  273. }
  274. /*
  275. * (non-Javadoc)
  276. *
  277. * @see org.jdiameter.common.api.app.auth.IAuthMessageFactory#getAuthMessageCommandCode()
  278. */
  279. public int getAuthMessageCommandCode() {
  280. return 265;
  281. }
  282. public ApplicationId getApplicationId() {
  283. return ApplicationId.createByAuthAppId(authAppId);
  284. }
  285. // Message Handlers -------------------------------------------------------
  286. /* (non-Javadoc)
  287. * @see org.jdiameter.api.auth.ClientAuthSessionListener#doAbortSessionRequestEvent(org.jdiameter.api.auth.ClientAuthSession, org.jdiameter.api.auth.events.AbortSessionRequest)
  288. */
  289. public void doAbortSessionRequestEvent(ClientAuthSession appSession, AbortSessionRequest asr) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  290. logger.info("Diameter Gq AuthorizationSessionFactory :: doAbortSessionRequestEvent :: appSession[{}], ASR[{}]", appSession, asr);
  291. }
  292. /* (non-Javadoc)
  293. * @see org.jdiameter.api.auth.ServerAuthSessionListener#doAbortSessionAnswerEvent(org.jdiameter.api.auth.ServerAuthSession, org.jdiameter.api.auth.events.AbortSessionAnswer)
  294. */
  295. public void doAbortSessionAnswerEvent(ServerAuthSession appSession, AbortSessionAnswer asa) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  296. logger.info("Diameter Gq AuthorizationSessionFactory :: doAbortSessionAnswerEvent :: appSession[{}], ASA[{}]", appSession, asa);
  297. }
  298. /* (non-Javadoc)
  299. * @see org.jdiameter.api.auth.ServerAuthSessionListener#doSessionTerminationRequestEvent(org.jdiameter.api.auth.ServerAuthSession, org.jdiameter.api.auth.events.SessionTermRequest)
  300. */
  301. public void doSessionTerminationRequestEvent(ServerAuthSession appSession, SessionTermRequest str) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  302. logger.info("Diameter Gq AuthorizationSessionFactory :: doSessionTerminationRequestEvent :: appSession[{}], STR[{}]", appSession, str);
  303. }
  304. /* (non-Javadoc)
  305. * @see org.jdiameter.api.auth.ClientAuthSessionListener#doSessionTerminationAnswerEvent(org.jdiameter.api.auth.ClientAuthSession, org.jdiameter.api.auth.events.SessionTermAnswer)
  306. */
  307. public void doSessionTerminationAnswerEvent(ClientAuthSession appSession, SessionTermAnswer sta) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  308. logger.info("Diameter Gq AuthorizationSessionFactory :: doSessionTerminationAnswerEvent :: appSession[{}], STA[{}]", appSession, sta);
  309. }
  310. /* (non-Javadoc)
  311. * @see org.jdiameter.api.auth.ServerAuthSessionListener#doAuthRequestEvent(org.jdiameter.api.auth.ServerAuthSession, org.jdiameter.api.app.AppRequestEvent)
  312. */
  313. public void doAuthRequestEvent(ServerAuthSession appSession, AppRequestEvent request) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  314. logger.info("Diameter Gq AuthorizationSessionFactory :: doAuthRequestEvent :: appSession[{}], Request[{}]", appSession, request);
  315. }
  316. /* (non-Javadoc)
  317. * @see org.jdiameter.api.auth.ClientAuthSessionListener#doAuthAnswerEvent(org.jdiameter.api.auth.ClientAuthSession, org.jdiameter.api.app.AppRequestEvent, org.jdiameter.api.app.AppAnswerEvent)
  318. */
  319. public void doAuthAnswerEvent(ClientAuthSession appSession, AppRequestEvent request, AppAnswerEvent answer) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  320. logger.info("Diameter Gq AuthorizationSessionFactory :: doAuthAnswerEvent :: appSession[{}], Request[{}], Answer[{}]", new Object[]{appSession, request, answer});
  321. }
  322. /* (non-Javadoc)
  323. * @see org.jdiameter.api.auth.ClientAuthSessionListener#doReAuthRequestEvent(org.jdiameter.api.auth.ClientAuthSession, org.jdiameter.api.auth.events.ReAuthRequest)
  324. */
  325. public void doReAuthRequestEvent(ClientAuthSession appSession, ReAuthRequest rar) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  326. logger.info("Diameter Gq AuthorizationSessionFactory :: doReAuthRequestEvent :: appSession[{}], RAR[{}]", appSession, rar);
  327. }
  328. /* (non-Javadoc)
  329. * @see org.jdiameter.api.auth.ServerAuthSessionListener#doReAuthAnswerEvent(org.jdiameter.api.auth.ServerAuthSession, org.jdiameter.api.auth.events.ReAuthRequest, org.jdiameter.api.auth.events.ReAuthAnswer)
  330. */
  331. public void doReAuthAnswerEvent(ServerAuthSession appSession, ReAuthRequest rar, ReAuthAnswer raa) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  332. logger.info("Diameter Gq AuthorizationSessionFactory :: doReAuthAnswerEvent :: appSession[{}], RAR[{}], RAA[{}]", new Object[]{appSession, rar, raa});
  333. }
  334. /* (non-Javadoc)
  335. * @see org.jdiameter.api.auth.ServerAuthSessionListener#doOtherEvent(org.jdiameter.api.app.AppSession, org.jdiameter.api.app.AppRequestEvent, org.jdiameter.api.app.AppAnswerEvent)
  336. */
  337. public void doOtherEvent(AppSession appSession, AppRequestEvent request, AppAnswerEvent answer) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  338. logger.info("Diameter Gq AuthorizationSessionFactory :: doOtherEvent :: appSession[{}], Request[{}], Answer[{}]", new Object[]{appSession, request, answer});
  339. }
  340. // State Change Listener --------------------------------------------------
  341. /* (non-Javadoc)
  342. * @see org.jdiameter.api.app.StateChangeListener#stateChanged(java.lang.Enum, java.lang.Enum)
  343. */
  344. public void stateChanged(Enum oldState, Enum newState) {
  345. logger.info("Diameter Gq AuthorizationSessionFactory :: stateChanged :: oldState[{}], newState[{}]", oldState, newState);
  346. }
  347. /* (non-Javadoc)
  348. * @see org.jdiameter.api.app.StateChangeListener#stateChanged(java.lang.Object, java.lang.Enum, java.lang.Enum)
  349. */
  350. public void stateChanged(AppSession source, Enum oldState, Enum newState) {
  351. logger.info("Diameter Gq AuthorizationSessionFactory :: stateChanged :: source[{}], oldState[{}], newState[{}]", new Object[] {source, oldState, newState});
  352. }
  353. // Context Methods --------------------------------------------------------
  354. /* (non-Javadoc)
  355. * @see org.jdiameter.common.api.app.auth.IClientAuthActionContext#accessTimeoutElapses(org.jdiameter.api.auth.ClientAuthSession)
  356. */
  357. public void accessTimeoutElapses(ClientAuthSession session) throws InternalException {
  358. // TODO Auto-generated method stub
  359. }
  360. /* (non-Javadoc)
  361. * @see org.jdiameter.common.api.app.auth.IClientAuthActionContext#getAccessTimeout()
  362. */
  363. public long getAccessTimeout() throws InternalException {
  364. return 20000;
  365. }
  366. /* (non-Javadoc)
  367. * @see org.jdiameter.common.api.app.auth.IClientAuthActionContext#disconnectUserOrDev(org.jdiameter.api.auth.ClientAuthSession, org.jdiameter.api.Message)
  368. */
  369. public void disconnectUserOrDev(ClientAuthSession session, Message request) throws InternalException {
  370. // TODO Auto-generated method stub
  371. }
  372. /* (non-Javadoc)
  373. * @see org.jdiameter.common.api.app.auth.IServerAuthActionContext#accessTimeoutElapses(org.jdiameter.api.auth.ServerAuthSession)
  374. */
  375. public void accessTimeoutElapses(ServerAuthSession session) throws InternalException {
  376. // TODO Auto-generated method stub
  377. }
  378. }