PageRenderTime 147ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/servers/jain-slee/resources/diameter-sh-client/ra/src/main/java/org/mobicents/slee/resource/diameter/sh/client/ShClientActivityImpl.java

http://mobicents.googlecode.com/
Java | 169 lines | 110 code | 23 blank | 36 comment | 0 complexity | a1296821be5366ec6abb066c32085da7 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.mobicents.slee.resource.diameter.sh.client;
  23. import java.io.IOException;
  24. import net.java.slee.resource.diameter.base.events.DiameterMessage;
  25. import net.java.slee.resource.diameter.base.events.avp.AvpNotAllowedException;
  26. import net.java.slee.resource.diameter.base.events.avp.DiameterIdentity;
  27. import net.java.slee.resource.diameter.sh.DiameterShAvpFactory;
  28. import net.java.slee.resource.diameter.sh.client.ShClientActivity;
  29. import net.java.slee.resource.diameter.sh.client.ShClientMessageFactory;
  30. import net.java.slee.resource.diameter.sh.events.ProfileUpdateRequest;
  31. import net.java.slee.resource.diameter.sh.events.SubscribeNotificationsRequest;
  32. import net.java.slee.resource.diameter.sh.events.UserDataRequest;
  33. import org.jdiameter.api.Answer;
  34. import org.jdiameter.api.EventListener;
  35. import org.jdiameter.api.Request;
  36. import org.jdiameter.api.app.AppSession;
  37. import org.jdiameter.api.app.StateChangeListener;
  38. import org.jdiameter.api.sh.ClientShSession;
  39. import org.jdiameter.common.impl.app.sh.ProfileUpdateRequestImpl;
  40. import org.jdiameter.common.impl.app.sh.SubscribeNotificationsRequestImpl;
  41. import org.jdiameter.common.impl.app.sh.UserDataRequestImpl;
  42. import org.mobicents.slee.resource.diameter.base.DiameterActivityImpl;
  43. import org.mobicents.slee.resource.diameter.base.events.DiameterMessageImpl;
  44. /**
  45. *
  46. * Sh Client activity created for request/response use casses
  47. *
  48. * @author <a href="mailto:baranowb@gmail.com"> Bartosz Baranowski </a>
  49. * @author <a href="mailto:brainslog@gmail.com"> Alexandre Mendonca </a>
  50. * @see ShClientActivity
  51. */
  52. public class ShClientActivityImpl extends DiameterActivityImpl implements ShClientActivity , StateChangeListener<AppSession> {
  53. private static final long serialVersionUID = -1182214629020823688L;
  54. protected transient ClientShSession clientSession = null;
  55. protected transient DiameterShAvpFactory shAvpFactory = null;
  56. protected transient ShClientMessageFactory messageFactory = null;
  57. //Is there any way to add
  58. public ShClientActivityImpl(ShClientMessageFactory shClientMessageFactory, DiameterShAvpFactory shAvpFactory, ClientShSession session, DiameterIdentity destinationHost, DiameterIdentity destinationRealm) {
  59. super(shClientMessageFactory.getBaseMessageFactory(), shAvpFactory.getBaseFactory(), null, (EventListener<Request, Answer>) session, destinationHost, destinationRealm);
  60. setSession(session);
  61. super.setCurrentWorkingSession(this.clientSession.getSessions().get(0));
  62. this.shAvpFactory = shAvpFactory;
  63. this.messageFactory = shClientMessageFactory;
  64. }
  65. public void setSession(ClientShSession session) {
  66. this.clientSession = session;
  67. this.clientSession.addStateChangeNotification(this);
  68. }
  69. public void sendProfileUpdateRequest(ProfileUpdateRequest message) throws IOException {
  70. try {
  71. DiameterMessageImpl msg = (DiameterMessageImpl) message;
  72. clientSession.sendProfileUpdateRequest(new ProfileUpdateRequestImpl((Request) msg.getGenericData()));
  73. }
  74. catch (org.jdiameter.api.validation.AvpNotAllowedException e) {
  75. throw new AvpNotAllowedException("Message validation failed.", e, e.getAvpCode(), e.getVendorId());
  76. }
  77. catch (Exception e) {
  78. throw new IOException("Failed to send message, due to: " + e.getLocalizedMessage());
  79. }
  80. }
  81. public void sendSubscribeNotificationsRequest(
  82. SubscribeNotificationsRequest message) throws IOException {
  83. // FIXME: IMHO this should not be here.
  84. try {
  85. DiameterMessageImpl msg = (DiameterMessageImpl) message;
  86. this.clientSession
  87. .sendSubscribeNotificationsRequest(new SubscribeNotificationsRequestImpl(
  88. (Request) msg.getGenericData()));
  89. }
  90. catch (org.jdiameter.api.validation.AvpNotAllowedException e) {
  91. throw new AvpNotAllowedException("Message validation failed.", e, e.getAvpCode(), e.getVendorId());
  92. }
  93. catch (Exception e) {
  94. throw new IOException("Failed to send message, due to: " + e);
  95. }
  96. }
  97. public void sendUserDataRequest(UserDataRequest message) throws IOException {
  98. try {
  99. DiameterMessageImpl msg = (DiameterMessageImpl) message;
  100. this.clientSession.sendUserDataRequest(new UserDataRequestImpl((Request) msg.getGenericData()));
  101. }
  102. catch (org.jdiameter.api.validation.AvpNotAllowedException e) {
  103. throw new AvpNotAllowedException("Message validation failed.", e, e.getAvpCode(), e.getVendorId());
  104. }
  105. catch (Exception e) {
  106. throw new IOException("Failed to send message, due to: " + e);
  107. }
  108. }
  109. public ShClientMessageFactory getClientMessageFactory() {
  110. return this.messageFactory;
  111. }
  112. public DiameterShAvpFactory getClientAvpFactory() {
  113. return this.shAvpFactory;
  114. }
  115. public void setClientMessageFactory(ShClientMessageFactory v) {
  116. this.messageFactory = v;
  117. }
  118. public void setClientAvpFactory(DiameterShAvpFactory v) {
  119. this.shAvpFactory = v;
  120. }
  121. public String getSessionId() {
  122. return super.getSessionId();
  123. }
  124. public void sendMessage(DiameterMessage message) throws IOException {
  125. super.sendMessage(message);
  126. }
  127. /*
  128. * (non-Javadoc)
  129. * @see org.jdiameter.api.app.StateChangeListener#stateChanged(java.lang.Object, java.lang.Enum, java.lang.Enum)
  130. */
  131. public void stateChanged(AppSession source, Enum oldState, Enum newState) {
  132. this.stateChanged(oldState, newState);
  133. }
  134. public void stateChanged(Enum oldState, Enum newState) {
  135. // no state changes, its stateless!
  136. }
  137. ClientShSession getClientSession() {
  138. return this.clientSession;
  139. }
  140. @Override
  141. public void endActivity() {
  142. this.clientSession.release();
  143. this.clientSession.removeStateChangeNotification(this);
  144. super.endActivity();
  145. }
  146. }