PageRenderTime 154ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/servers/diameter/core/jdiameter/impl/src/main/java/org/jdiameter/client/impl/parser/AvpImpl.java

http://mobicents.googlecode.com/
Java | 261 lines | 195 code | 36 blank | 30 comment | 14 complexity | 2e48a6d2a793f6faaff120b9a947225f 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.client.impl.parser;
  23. import java.net.InetAddress;
  24. import java.net.URISyntaxException;
  25. import java.net.UnknownServiceException;
  26. import java.util.Date;
  27. import org.jdiameter.api.Avp;
  28. import org.jdiameter.api.AvpDataException;
  29. import org.jdiameter.api.AvpSet;
  30. import org.jdiameter.api.InternalException;
  31. import org.jdiameter.api.URI;
  32. import org.slf4j.Logger;
  33. import org.slf4j.LoggerFactory;
  34. /**
  35. *
  36. * @author erick.svenson@yahoo.com
  37. * @author <a href="mailto:brainslog@gmail.com"> Alexandre Mendonca </a>
  38. * @author <a href="mailto:baranowb@gmail.com"> Bartosz Baranowski </a>
  39. */
  40. class AvpImpl implements Avp {
  41. private static final long serialVersionUID = 1L;
  42. private static final ElementParser parser = new ElementParser();
  43. int avpCode;
  44. long vendorID;
  45. boolean isMandatory = false;
  46. boolean isEncrypted = false;
  47. boolean isVendorSpecific = false;
  48. byte[] rawData = new byte[0];
  49. AvpSet groupedData;
  50. private static final Logger logger = LoggerFactory.getLogger(AvpImpl.class);
  51. AvpImpl(int code, int flags, long vnd, byte[] data) {
  52. avpCode = code;
  53. //
  54. isMandatory = (flags & 0x40) != 0;
  55. isEncrypted = (flags & 0x20) != 0;
  56. isVendorSpecific = (flags & 0x80) != 0;
  57. //
  58. vendorID = vnd;
  59. rawData = data;
  60. }
  61. AvpImpl(Avp avp) {
  62. avpCode = avp.getCode();
  63. vendorID = avp.getVendorId();
  64. isMandatory = avp.isMandatory();
  65. isEncrypted = avp.isEncrypted();
  66. isVendorSpecific = avp.isVendorId();
  67. try {
  68. rawData = avp.getRaw();
  69. if (rawData == null || rawData.length == 0) {
  70. groupedData = avp.getGrouped();
  71. }
  72. }
  73. catch (AvpDataException e) {
  74. logger.debug("Can not create Avp", e);
  75. }
  76. }
  77. public AvpImpl(int newCode, Avp avp) {
  78. this(avp);
  79. avpCode = newCode;
  80. }
  81. public int getCode() {
  82. return avpCode;
  83. }
  84. public boolean isVendorId() {
  85. return isVendorSpecific;
  86. }
  87. public boolean isMandatory() {
  88. return isMandatory;
  89. }
  90. public boolean isEncrypted() {
  91. return isEncrypted;
  92. }
  93. public long getVendorId() {
  94. return vendorID;
  95. }
  96. public byte[] getRaw() throws AvpDataException {
  97. return rawData;
  98. }
  99. public byte[] getOctetString() throws AvpDataException {
  100. return rawData;
  101. }
  102. public String getUTF8String() throws AvpDataException {
  103. try {
  104. return parser.bytesToUtf8String(rawData);
  105. }
  106. catch (Exception e) {
  107. throw new AvpDataException(e, this);
  108. }
  109. }
  110. public int getInteger32() throws AvpDataException {
  111. try {
  112. return parser.bytesToInt(rawData);
  113. }
  114. catch (Exception e) {
  115. throw new AvpDataException(e, this);
  116. }
  117. }
  118. public long getInteger64() throws AvpDataException {
  119. try {
  120. return parser.bytesToLong(rawData);
  121. }
  122. catch (Exception e) {
  123. throw new AvpDataException(e, this);
  124. }
  125. }
  126. public long getUnsigned32() throws AvpDataException {
  127. try {
  128. byte[] u32ext = new byte[8];
  129. System.arraycopy(rawData, 0, u32ext, 4, 4);
  130. return parser.bytesToLong(u32ext);
  131. }
  132. catch (Exception e) {
  133. throw new AvpDataException(e, this);
  134. }
  135. }
  136. public long getUnsigned64() throws AvpDataException {
  137. try {
  138. return parser.bytesToLong(rawData);
  139. }
  140. catch (Exception e) {
  141. throw new AvpDataException(e, this);
  142. }
  143. }
  144. public float getFloat32() throws AvpDataException {
  145. try {
  146. return parser.bytesToFloat(rawData);
  147. }
  148. catch (Exception e) {
  149. throw new AvpDataException(e, this);
  150. }
  151. }
  152. public double getFloat64() throws AvpDataException {
  153. try {
  154. return parser.bytesToDouble(rawData);
  155. }
  156. catch (Exception e) {
  157. throw new AvpDataException(e, this);
  158. }
  159. }
  160. public InetAddress getAddress() throws AvpDataException {
  161. try {
  162. return parser.bytesToAddress(rawData);
  163. }
  164. catch (Exception e) {
  165. throw new AvpDataException(e, this);
  166. }
  167. }
  168. public Date getTime() throws AvpDataException {
  169. try {
  170. return parser.bytesToDate(rawData);
  171. }
  172. catch (Exception e) {
  173. throw new AvpDataException(e, this);
  174. }
  175. }
  176. public String getDiameterIdentity() throws AvpDataException {
  177. try {
  178. return parser.bytesToOctetString(rawData);
  179. }
  180. catch (Exception e) {
  181. throw new AvpDataException(e, this);
  182. }
  183. }
  184. public URI getDiameterURI() throws AvpDataException {
  185. try {
  186. return new URI(parser.bytesToOctetString(rawData));
  187. }
  188. catch (URISyntaxException e) {
  189. throw new AvpDataException(e, this);
  190. }
  191. catch (UnknownServiceException e) {
  192. throw new AvpDataException(e, this);
  193. }
  194. }
  195. public AvpSet getGrouped() throws AvpDataException {
  196. try {
  197. if (groupedData == null) {
  198. groupedData = parser.decodeAvpSet(rawData);
  199. rawData = new byte[0];
  200. }
  201. return groupedData;
  202. }
  203. catch (Exception e) {
  204. throw new AvpDataException(e, this);
  205. }
  206. }
  207. public boolean isWrapperFor(Class<?> aClass) throws InternalException {
  208. return false;
  209. }
  210. public <T> T unwrap(Class<T> aClass) throws InternalException {
  211. return null;
  212. }
  213. public byte[] getRawData() {
  214. return (rawData == null || rawData.length == 0) ? parser.encodeAvpSet(groupedData) : rawData;
  215. }
  216. // Caching toString.. Avp shouldn't be modified once created.
  217. private String toString;
  218. @Override
  219. public String toString() {
  220. if(toString == null) {
  221. this.toString = new StringBuffer("AvpImpl [avpCode=").append(avpCode).append(", vendorID=").append(vendorID).append("]@").append(super.hashCode()).toString();
  222. }
  223. return this.toString;
  224. }
  225. }