PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/servers/diameter/core/jdiameter/impl/src/main/java/org/jdiameter/common/impl/app/auth/AuthSessionFactoryImpl.java

http://mobicents.googlecode.com/
Java | 415 lines | 243 code | 57 blank | 115 comment | 33 complexity | fcb08524450e46bce81fdfc7be22754f 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 2006, 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.auth;
  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.auth.ClientAuthSession;
  37. import org.jdiameter.api.auth.ClientAuthSessionListener;
  38. import org.jdiameter.api.auth.ServerAuthSession;
  39. import org.jdiameter.api.auth.ServerAuthSessionListener;
  40. import org.jdiameter.api.auth.events.AbortSessionAnswer;
  41. import org.jdiameter.api.auth.events.AbortSessionRequest;
  42. import org.jdiameter.api.auth.events.ReAuthAnswer;
  43. import org.jdiameter.api.auth.events.ReAuthRequest;
  44. import org.jdiameter.api.auth.events.SessionTermAnswer;
  45. import org.jdiameter.api.auth.events.SessionTermRequest;
  46. import org.jdiameter.client.api.ISessionFactory;
  47. import org.jdiameter.client.impl.app.auth.ClientAuthSessionImpl;
  48. import org.jdiameter.client.impl.app.auth.IClientAuthSessionData;
  49. import org.jdiameter.common.api.app.IAppSessionDataFactory;
  50. import org.jdiameter.common.api.app.auth.IAuthMessageFactory;
  51. import org.jdiameter.common.api.app.auth.IAuthSessionData;
  52. import org.jdiameter.common.api.app.auth.IAuthSessionFactory;
  53. import org.jdiameter.common.api.app.auth.IClientAuthActionContext;
  54. import org.jdiameter.common.api.app.auth.IServerAuthActionContext;
  55. import org.jdiameter.common.api.data.ISessionDatasource;
  56. import org.jdiameter.common.impl.app.AppAnswerEventImpl;
  57. import org.jdiameter.common.impl.app.AppRequestEventImpl;
  58. import org.jdiameter.server.impl.app.auth.IServerAuthSessionData;
  59. import org.jdiameter.server.impl.app.auth.ServerAuthSessionImpl;
  60. import org.slf4j.Logger;
  61. import org.slf4j.LoggerFactory;
  62. /**
  63. * Default Diameter Authorization Session Factory implementation
  64. *
  65. * @author <a href="mailto:baranowb@gmail.com"> Bartosz Baranowski </a>
  66. * @author <a href="mailto:brainslog@gmail.com"> Alexandre Mendonca </a>
  67. */
  68. public class AuthSessionFactoryImpl implements IAuthSessionFactory, IAuthMessageFactory, ServerAuthSessionListener,
  69. ClientAuthSessionListener, IClientAuthActionContext, IServerAuthActionContext, StateChangeListener<AppSession> {
  70. private final static long authAppId = 19301L;
  71. protected IAuthMessageFactory messageFactory;
  72. protected ServerAuthSessionListener serverSessionListener;
  73. protected StateChangeListener<AppSession> stateListener;
  74. protected ClientAuthSessionListener clientSessionListener;
  75. protected boolean stateles;
  76. protected long messageTimeout = 5000;
  77. protected Logger logger = LoggerFactory.getLogger(AuthSessionFactoryImpl.class);
  78. protected ISessionDatasource iss;
  79. protected ISessionFactory sessionFactory = null;
  80. protected IServerAuthActionContext serverSessionContext;
  81. protected IClientAuthActionContext clientSessionContext;
  82. protected IAppSessionDataFactory<IAuthSessionData> sessionDataFactory;
  83. public AuthSessionFactoryImpl(SessionFactory sessionFactory) {
  84. super();
  85. this.sessionFactory = (ISessionFactory) sessionFactory;
  86. this.iss = this.sessionFactory.getContainer().getAssemblerFacility().getComponentInstance(ISessionDatasource.class);
  87. this.sessionDataFactory = (IAppSessionDataFactory<IAuthSessionData>) this.iss.getDataFactory(IAuthSessionData.class);
  88. }
  89. /**
  90. * @return the clientSessionContext
  91. */
  92. public IClientAuthActionContext getClientSessionContext() {
  93. return clientSessionContext != null ? clientSessionContext : this;
  94. }
  95. /**
  96. * @param clientSessionContext
  97. * the clientSessionContext to set
  98. */
  99. public void setClientSessionContext(IClientAuthActionContext clientSessionContext) {
  100. this.clientSessionContext = clientSessionContext;
  101. }
  102. /**
  103. * @return the serverSessionContext
  104. */
  105. public IServerAuthActionContext getServerSessionContext() {
  106. return serverSessionContext != null ? serverSessionContext : this;
  107. }
  108. /**
  109. * @param serverSessionContext
  110. * the serverSessionContext to set
  111. */
  112. public void setServerSessionContext(IServerAuthActionContext serverSessionContext) {
  113. this.serverSessionContext = serverSessionContext;
  114. }
  115. /**
  116. * @return the messageTimeout
  117. */
  118. public long getMessageTimeout() {
  119. return messageTimeout;
  120. }
  121. /**
  122. * @param messageTimeout
  123. * the messageTimeout to set
  124. */
  125. public void setMessageTimeout(long messageTimeout) {
  126. this.messageTimeout = messageTimeout;
  127. }
  128. /**
  129. * @return the messageFactory
  130. */
  131. public IAuthMessageFactory getMessageFactory() {
  132. return messageFactory != null ? messageFactory : this;
  133. }
  134. /**
  135. * @param messageFactory
  136. * the messageFactory to set
  137. */
  138. public void setMessageFactory(IAuthMessageFactory messageFactory) {
  139. this.messageFactory = messageFactory;
  140. }
  141. /**
  142. * @return the serverSessionListener
  143. */
  144. public ServerAuthSessionListener getServerSessionListener() {
  145. return serverSessionListener != null ? serverSessionListener : this;
  146. }
  147. /**
  148. * @param serverSessionListener
  149. * the serverSessionListener to set
  150. */
  151. public void setServerSessionListener(ServerAuthSessionListener serverSessionListener) {
  152. this.serverSessionListener = serverSessionListener;
  153. }
  154. /**
  155. * @return the stateListener
  156. */
  157. public StateChangeListener<AppSession> getStateListener() {
  158. return stateListener != null ? stateListener : this;
  159. }
  160. /**
  161. * @param stateListener
  162. * the stateListener to set
  163. */
  164. public void setStateListener(StateChangeListener<AppSession> stateListener) {
  165. this.stateListener = stateListener;
  166. }
  167. /**
  168. * @return the clientSessionListener
  169. */
  170. public ClientAuthSessionListener getClientSessionListener() {
  171. return clientSessionListener != null ? clientSessionListener : this;
  172. }
  173. /**
  174. * @param clientSessionListener
  175. * the clientSessionListener to set
  176. */
  177. public void setClientSessionListener(ClientAuthSessionListener clientSessionListener) {
  178. this.clientSessionListener = clientSessionListener;
  179. }
  180. public boolean isStateles() {
  181. return stateles;
  182. }
  183. public void setStateles(boolean stateles) {
  184. this.stateles = stateles;
  185. }
  186. @Override
  187. public AppSession getSession(String sessionId, Class<? extends AppSession> aClass) {
  188. if (sessionId == null) {
  189. throw new IllegalArgumentException("SessionId must not be null");
  190. }
  191. if(!this.iss.exists(sessionId)) {
  192. return null;
  193. }
  194. try {
  195. if (aClass == ServerAuthSession.class) {
  196. IServerAuthSessionData sessionData = (IServerAuthSessionData) this.sessionDataFactory.getAppSessionData(ServerAuthSession.class, sessionId);
  197. ServerAuthSessionImpl session = new ServerAuthSessionImpl(sessionData, sessionFactory, getServerSessionListener(), getMessageFactory(),
  198. getStateListener(), getServerSessionContext(), messageTimeout, isStateles());
  199. session.getSessions().get(0).setRequestListener(session);
  200. return session;
  201. }
  202. else {
  203. if (aClass == ClientAuthSession.class) {
  204. IClientAuthSessionData sessionData = (IClientAuthSessionData) this.sessionDataFactory.getAppSessionData(ClientAuthSession.class, sessionId);
  205. ClientAuthSessionImpl session = new ClientAuthSessionImpl(sessionData, sessionFactory, getClientSessionListener(), getMessageFactory(),
  206. getStateListener(), getClientSessionContext(), isStateles());
  207. session.getSessions().get(0).setRequestListener(session);
  208. return session;
  209. }
  210. }
  211. }
  212. catch (Exception e) {
  213. logger.error("Failure trying to obtain new authorization session", e);
  214. }
  215. return null;
  216. }
  217. public AppSession getNewSession(String sessionId, Class<? extends AppSession> aClass, ApplicationId applicationId, Object[] args) {
  218. try {
  219. if (aClass == ServerAuthSession.class) {
  220. if (sessionId == null) {
  221. if (args != null && args.length > 0 && args[0] instanceof Request) {
  222. Request request = (Request) args[0];
  223. sessionId = request.getSessionId();
  224. }
  225. else {
  226. sessionId = this.sessionFactory.getSessionId();
  227. }
  228. }
  229. IServerAuthSessionData sessionData = (IServerAuthSessionData) this.sessionDataFactory.getAppSessionData(ServerAuthSession.class, sessionId);
  230. sessionData.setApplicationId(applicationId);
  231. ServerAuthSessionImpl session = new ServerAuthSessionImpl(sessionData, sessionFactory, getServerSessionListener(), getMessageFactory(),
  232. getStateListener(), getServerSessionContext(), messageTimeout, isStateles());
  233. iss.addSession(session);
  234. session.getSessions().get(0).setRequestListener(session);
  235. return session;
  236. }
  237. else {
  238. if (aClass == ClientAuthSession.class) {
  239. if (sessionId == null) {
  240. if (args != null && args.length > 0 && args[0] instanceof Request) {
  241. Request request = (Request) args[0];
  242. sessionId = request.getSessionId();
  243. }
  244. else {
  245. sessionId = this.sessionFactory.getSessionId();
  246. }
  247. }
  248. IClientAuthSessionData sessionData = (IClientAuthSessionData) this.sessionDataFactory.getAppSessionData(ClientAuthSession.class, sessionId);
  249. sessionData.setApplicationId(applicationId);
  250. ClientAuthSessionImpl session = new ClientAuthSessionImpl(sessionData, sessionFactory, getClientSessionListener(), getMessageFactory(),
  251. getStateListener(), getClientSessionContext(), isStateles());
  252. iss.addSession(session);
  253. // iss.setSessionListener(clientSession.getSessionId(), (NetworkReqListener) appSession);
  254. session.getSessions().get(0).setRequestListener(session);
  255. return session;
  256. }
  257. }
  258. }
  259. catch (Exception e) {
  260. logger.error("Failure trying to obtain new authorization session", e);
  261. }
  262. return null;
  263. }
  264. // Message Factory Methods ------------------------------------------------
  265. public AppAnswerEvent createAuthAnswer(Answer answer) {
  266. return new AppAnswerEventImpl(answer);
  267. }
  268. public AppRequestEvent createAuthRequest(Request request) {
  269. return new AppRequestEventImpl(request);
  270. }
  271. /*
  272. * (non-Javadoc)
  273. *
  274. * @see org.jdiameter.common.api.app.auth.IAuthMessageFactory#getAuthMessageCommandCode()
  275. */
  276. public int getAuthMessageCommandCode() {
  277. return 258;
  278. }
  279. public ApplicationId getApplicationId() {
  280. return ApplicationId.createByAuthAppId(authAppId);
  281. }
  282. // Message Handlers -------------------------------------------------------
  283. public void doAbortSessionRequestEvent(ClientAuthSession appSession, AbortSessionRequest asr) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  284. logger.info("Diameter Base AuthorizationSessionFactory :: doAbortSessionRequestEvent :: appSession[{}], ASR[{}]", appSession, asr);
  285. }
  286. public void doAbortSessionAnswerEvent(ServerAuthSession appSession, AbortSessionAnswer asa) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  287. logger.info("Diameter Base AuthorizationSessionFactory :: doAbortSessionAnswerEvent :: appSession[{}], ASA[{}]", appSession, asa);
  288. }
  289. public void doSessionTerminationRequestEvent(ServerAuthSession appSession, SessionTermRequest str) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  290. logger.info("Diameter Base AuthorizationSessionFactory :: doSessionTerminationRequestEvent :: appSession[{}], STR[{}]", appSession, str);
  291. }
  292. public void doSessionTerminationAnswerEvent(ClientAuthSession appSession, SessionTermAnswer sta) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  293. logger.info("Diameter Base AuthorizationSessionFactory :: doSessionTerminationAnswerEvent :: appSession[{}], STA[{}]", appSession, sta);
  294. }
  295. public void doAuthRequestEvent(ServerAuthSession appSession, AppRequestEvent request) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  296. logger.info("Diameter Base AuthorizationSessionFactory :: doAuthRequestEvent :: appSession[{}], Request[{}]", appSession, request);
  297. }
  298. public void doAuthAnswerEvent(ClientAuthSession appSession, AppRequestEvent request, AppAnswerEvent answer) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  299. logger.info("Diameter Base AuthorizationSessionFactory :: doAuthAnswerEvent :: appSession[{}], Request[{}], Answer[{}]", new Object[]{appSession, request, answer});
  300. }
  301. public void doReAuthRequestEvent(ClientAuthSession appSession, ReAuthRequest rar) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  302. logger.info("Diameter Base AuthorizationSessionFactory :: doReAuthRequestEvent :: appSession[{}], RAR[{}]", appSession, rar);
  303. }
  304. public void doReAuthAnswerEvent(ServerAuthSession appSession, ReAuthRequest rar, ReAuthAnswer raa) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  305. logger.info("Diameter Base AuthorizationSessionFactory :: doReAuthAnswerEvent :: appSession[{}], RAR[{}], RAA[{}]", new Object[]{appSession, rar, raa});
  306. }
  307. public void doOtherEvent(AppSession appSession, AppRequestEvent request, AppAnswerEvent answer) throws InternalException, IllegalDiameterStateException, RouteException, OverloadException {
  308. logger.info("Diameter Base AuthorizationSessionFactory :: doOtherEvent :: appSession[{}], Request[{}], Answer[{}]", new Object[]{appSession, request, answer});
  309. }
  310. // State Change Listener --------------------------------------------------
  311. @SuppressWarnings("unchecked")
  312. public void stateChanged(Enum oldState, Enum newState) {
  313. logger.info("Diameter Base AuthorizationSessionFactory :: stateChanged :: oldState[{}], newState[{}]", oldState, newState);
  314. }
  315. /*
  316. * (non-Javadoc)
  317. *
  318. * @see org.jdiameter.api.app.StateChangeListener#stateChanged(java.lang.Object, java.lang.Enum, java.lang.Enum)
  319. */
  320. @SuppressWarnings("unchecked")
  321. public void stateChanged(AppSession source, Enum oldState, Enum newState) {
  322. logger.info("Diameter Base AuthorizationSessionFactory :: stateChanged :: source[{}], oldState[{}], newState[{}]", new Object[] {source, oldState, newState});
  323. }
  324. // Context Methods --------------------------------------------------------
  325. /*
  326. * (non-Javadoc)
  327. *
  328. * @seeorg.jdiameter.common.api.app.auth.IClientAuthActionContext#accessTimeoutElapses(org.jdiameter.api.auth.ClientAuthSession)
  329. */
  330. public void accessTimeoutElapses(ClientAuthSession session) throws InternalException {
  331. // TODO Auto-generated method stub
  332. }
  333. /*
  334. * (non-Javadoc)
  335. *
  336. * @see org.jdiameter.common.api.app.auth.IClientAuthActionContext#createAccessTimer()
  337. */
  338. public long getAccessTimeout() throws InternalException {
  339. return 20000;
  340. }
  341. /*
  342. * (non-Javadoc)
  343. *
  344. * @see org.jdiameter.common.api.app.auth.IClientAuthActionContext#disconnectUserOrDev(org.jdiameter.api.auth.ClientAuthSession, org.jdiameter.api.Message)
  345. */
  346. public void disconnectUserOrDev(ClientAuthSession session, Message request) throws InternalException {
  347. // TODO Auto-generated method stub
  348. }
  349. /*
  350. * (non-Javadoc)
  351. *
  352. * @seeorg.jdiameter.common.api.app.auth.IServerAuthActionContext#
  353. * accessTimeoutElapses(org.jdiameter.api.auth.ServerAuthSession)
  354. */
  355. public void accessTimeoutElapses(ServerAuthSession session) throws InternalException {
  356. // TODO Auto-generated method stub
  357. }
  358. }