Revision: 8349674e7126
Author:   [email protected] <[email protected]>
Date:     Fri Jan 25 02:57:39 2013
Log: Issue 228:Adding XML serialization support for a list of MAP/CAP messages (that are used in CAP V2) : CancelRequest

http://code.google.com/p/jss7/source/detail?r=8349674e7126

Modified:
/cap/cap-impl/src/main/java/org/mobicents/protocols/ss7/cap/service/circuitSwitchedCall/CancelRequestImpl.java /cap/cap-impl/src/main/java/org/mobicents/protocols/ss7/cap/service/circuitSwitchedCall/primitive/CallSegmentToCancelImpl.java /cap/cap-impl/src/test/java/org/mobicents/protocols/ss7/cap/service/circuitSwitchedCall/CancelRequestTest.java /cap/cap-impl/src/test/java/org/mobicents/protocols/ss7/cap/service/circuitSwitchedCall/primitive/CallSegmentToCancelTest.java

=======================================
--- /cap/cap-impl/src/main/java/org/mobicents/protocols/ss7/cap/service/circuitSwitchedCall/CancelRequestImpl.java Wed May 16 05:00:01 2012 +++ /cap/cap-impl/src/main/java/org/mobicents/protocols/ss7/cap/service/circuitSwitchedCall/CancelRequestImpl.java Fri Jan 25 02:57:39 2013
@@ -1,6 +1,6 @@
 /*
- * JBoss, Home of Professional Open Source
- * Copyright 2011, Red Hat, Inc. and individual contributors
+ * TeleStax, Open Source Cloud Communications
+ * Copyright 2012, Telestax Inc and individual contributors
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
@@ -24,6 +24,9 @@

 import java.io.IOException;

+import javolution.xml.XMLFormat;
+import javolution.xml.stream.XMLStreamException;
+
 import org.mobicents.protocols.asn.AsnException;
 import org.mobicents.protocols.asn.AsnInputStream;
 import org.mobicents.protocols.asn.AsnOutputStream;
@@ -38,13 +41,18 @@
import org.mobicents.protocols.ss7.cap.service.circuitSwitchedCall.primitive.CallSegmentToCancelImpl;

 /**
-*
-*
-* @author sergey vetyutnev
-*
-*/
+ *
+ *
+ * @author sergey vetyutnev
+ * @author Amit Bhayani
+ *
+ */
public class CancelRequestImpl extends CircuitSwitchedCallMessageImpl implements CancelRequest {

+       private static final String INVOKE_ID = "invokeID";
+       private static final String ALL_REQUESTS = "allRequests";
+ private static final String CALL_SEGMENT_TO_CANCEL = "callSegmentToCancel";
+
        public static final int _ID_invokeID = 0;
        public static final int _ID_allRequests = 1;
        public static final int _ID_callSegmentToCancel = 2;
@@ -54,7 +62,7 @@
        private Integer invokeID;
        private boolean allRequests;
        private CallSegmentToCancel callSegmentToCancel;
-
+
        public CancelRequestImpl() {
        }

@@ -129,11 +137,11 @@
                        int length = ansIS.readLength();
                        this._decode(ansIS, length);
                } catch (IOException e) {
- throw new CAPParsingComponentException("IOException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
-                                       
CAPParsingComponentExceptionReason.MistypedParameter);
+ throw new CAPParsingComponentException("IOException when decoding " + _PrimitiveName + ": " + + e.getMessage(), e, CAPParsingComponentExceptionReason.MistypedParameter);
                } catch (AsnException e) {
- throw new CAPParsingComponentException("AsnException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
-                                       
CAPParsingComponentExceptionReason.MistypedParameter);
+ throw new CAPParsingComponentException("AsnException when decoding " + _PrimitiveName + ": " + + e.getMessage(), e, CAPParsingComponentExceptionReason.MistypedParameter);
                }
        }

@@ -143,11 +151,11 @@
                try {
                        this._decode(ansIS, length);
                } catch (IOException e) {
- throw new CAPParsingComponentException("IOException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
-                                       
CAPParsingComponentExceptionReason.MistypedParameter);
+ throw new CAPParsingComponentException("IOException when decoding " + _PrimitiveName + ": " + + e.getMessage(), e, CAPParsingComponentExceptionReason.MistypedParameter);
                } catch (AsnException e) {
- throw new CAPParsingComponentException("AsnException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
-                                       
CAPParsingComponentExceptionReason.MistypedParameter);
+ throw new CAPParsingComponentException("AsnException when decoding " + _PrimitiveName + ": " + + e.getMessage(), e, CAPParsingComponentExceptionReason.MistypedParameter);
                }
        }

@@ -174,8 +182,8 @@
((CallSegmentToCancelImpl) this.callSegmentToCancel).decodeData(ais, length);
                        break;
                default:
- throw new CAPParsingComponentException("Error while decoding " + _PrimitiveName + ": bad tag: " + ais.getTag(),
-                                       
CAPParsingComponentExceptionReason.MistypedParameter);
+ throw new CAPParsingComponentException("Error while decoding " + _PrimitiveName + ": bad tag: "
+                                       + ais.getTag(), 
CAPParsingComponentExceptionReason.MistypedParameter);
                }
        }

@@ -207,9 +215,10 @@
                        choiceCnt++;
                if (this.callSegmentToCancel != null)
                        choiceCnt++;
-
+
                if (choiceCnt != 1)
- throw new CAPException("Error while encoding " + _PrimitiveName + ": only one choice must be definite, found: " + choiceCnt);
+                       throw new CAPException("Error while encoding " + 
_PrimitiveName
+                                       + ": only one choice must be definite, 
found: " + choiceCnt);

                try {
                        if (this.invokeID != null)
@@ -246,5 +255,28 @@

                return sb.toString();
        }
+
+       /**
+        * XML Serialization/Deserialization
+        */
+ protected static final XMLFormat<CancelRequestImpl> CANCEL_REQUEST_XML = new XMLFormat<CancelRequestImpl>(
+                       CancelRequestImpl.class) {
+
+               @Override
+ public void read(javolution.xml.XMLFormat.InputElement xml, CancelRequestImpl cancelRequest)
+                               throws XMLStreamException {
+                       cancelRequest.invokeID = xml.get(INVOKE_ID, 
Integer.class);
+                       cancelRequest.allRequests = xml.get(ALL_REQUESTS, 
Boolean.class);
+ cancelRequest.callSegmentToCancel = xml.get(CALL_SEGMENT_TO_CANCEL, CallSegmentToCancelImpl.class);
+               }
+
+               @Override
+ public void write(CancelRequestImpl cancelRequest, javolution.xml.XMLFormat.OutputElement xml)
+                               throws XMLStreamException {
+                       xml.add(cancelRequest.invokeID, INVOKE_ID, 
Integer.class);
+                       xml.add(cancelRequest.allRequests, ALL_REQUESTS, 
Boolean.class);
+ xml.add((CallSegmentToCancelImpl) cancelRequest.callSegmentToCancel, CALL_SEGMENT_TO_CANCEL,
+                                       CallSegmentToCancelImpl.class);
+               }
+       };
 }
-
=======================================
--- /cap/cap-impl/src/main/java/org/mobicents/protocols/ss7/cap/service/circuitSwitchedCall/primitive/CallSegmentToCancelImpl.java Mon Apr 23 10:53:14 2012 +++ /cap/cap-impl/src/main/java/org/mobicents/protocols/ss7/cap/service/circuitSwitchedCall/primitive/CallSegmentToCancelImpl.java Fri Jan 25 02:57:39 2013
@@ -1,6 +1,6 @@
 /*
- * JBoss, Home of Professional Open Source
- * Copyright 2011, Red Hat, Inc. and individual contributors
+ * TeleStax, Open Source Cloud Communications
+ * Copyright 2012, Telestax Inc and individual contributors
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
@@ -24,6 +24,9 @@

 import java.io.IOException;

+import javolution.xml.XMLFormat;
+import javolution.xml.stream.XMLStreamException;
+
 import org.mobicents.protocols.asn.AsnException;
 import org.mobicents.protocols.asn.AsnInputStream;
 import org.mobicents.protocols.asn.AsnOutputStream;
@@ -37,19 +40,22 @@
 /**
  *
  * @author sergey vetyutnev
+ * @author Amit Bhayani
  *
  */
public class CallSegmentToCancelImpl implements CallSegmentToCancel, CAPAsnPrimitive {

+       private static final String INVOKE_ID = "invokeID";
+       private static final String CALL_SEGMENT_ID = "callSegmentID";
+
        public static final int _ID_invokeID = 0;
        public static final int _ID_callSegmentID = 1;

        public static final String _PrimitiveName = "CallSegmentToCancel";

        private Integer invokeID;
-       private Integer callSegmentID;
-
-
+       private Integer callSegmentID;
+
        public CallSegmentToCancelImpl() {
        }

@@ -57,7 +63,7 @@
                this.invokeID = invokeID;
                this.callSegmentID = callSegmentID;
        }
-
+
        @Override
        public Integer getInvokeID() {
                return invokeID;
@@ -90,11 +96,11 @@
                        int length = ansIS.readLength();
                        this._decode(ansIS, length);
                } catch (IOException e) {
- throw new CAPParsingComponentException("IOException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
-                                       
CAPParsingComponentExceptionReason.MistypedParameter);
+ throw new CAPParsingComponentException("IOException when decoding " + _PrimitiveName + ": " + + e.getMessage(), e, CAPParsingComponentExceptionReason.MistypedParameter);
                } catch (AsnException e) {
- throw new CAPParsingComponentException("AsnException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
-                                       
CAPParsingComponentExceptionReason.MistypedParameter);
+ throw new CAPParsingComponentException("AsnException when decoding " + _PrimitiveName + ": " + + e.getMessage(), e, CAPParsingComponentExceptionReason.MistypedParameter);
                }
        }

@@ -104,15 +110,16 @@
                try {
                        this._decode(ansIS, length);
                } catch (IOException e) {
- throw new CAPParsingComponentException("IOException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
-                                       
CAPParsingComponentExceptionReason.MistypedParameter);
+ throw new CAPParsingComponentException("IOException when decoding " + _PrimitiveName + ": " + + e.getMessage(), e, CAPParsingComponentExceptionReason.MistypedParameter);
                } catch (AsnException e) {
- throw new CAPParsingComponentException("AsnException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
-                                       
CAPParsingComponentExceptionReason.MistypedParameter);
+ throw new CAPParsingComponentException("AsnException when decoding " + _PrimitiveName + ": " + + e.getMessage(), e, CAPParsingComponentExceptionReason.MistypedParameter);
                }
        }

- private void _decode(AsnInputStream ansIS, int length) throws CAPParsingComponentException, IOException, AsnException { + private void _decode(AsnInputStream ansIS, int length) throws CAPParsingComponentException, IOException,
+                       AsnException {

                this.invokeID = null;
                this.callSegmentID = null;
@@ -146,7 +153,7 @@
        @Override
        public void encodeAll(AsnOutputStream asnOs) throws CAPException {
                this.encodeAll(asnOs, this.getTagClass(), this.getTag());
-
+
        }

        @Override
@@ -170,7 +177,7 @@
aos.writeInteger(Tag.CLASS_CONTEXT_SPECIFIC, _ID_invokeID, this.invokeID);
                        if (this.callSegmentID != null)
aos.writeInteger(Tag.CLASS_CONTEXT_SPECIFIC, _ID_callSegmentID, this.callSegmentID);
-
+
                } catch (IOException e) {
throw new CAPException("IOException when encoding " + _PrimitiveName + ": " + e.getMessage(), e);
                } catch (AsnException e) {
@@ -198,5 +205,33 @@

                return sb.toString();
        }
+
+       /**
+        * XML Serialization/Deserialization
+        */
+ protected static final XMLFormat<CallSegmentToCancelImpl> CALL_SEGMENT_TO_CANCEL_XML = new XMLFormat<CallSegmentToCancelImpl>(
+                       CallSegmentToCancelImpl.class) {
+
+               @Override
+ public void read(javolution.xml.XMLFormat.InputElement xml, CallSegmentToCancelImpl callSegmentToCancel)
+                               throws XMLStreamException {
+
+                       callSegmentToCancel.invokeID = xml.get(INVOKE_ID, 
Integer.class);
+ callSegmentToCancel.callSegmentID = xml.get(CALL_SEGMENT_ID, Integer.class);
+               }
+
+               @Override
+ public void write(CallSegmentToCancelImpl callSegmentToCancel, javolution.xml.XMLFormat.OutputElement xml)
+                               throws XMLStreamException {
+
+                       if (callSegmentToCancel.invokeID != null) {
+ xml.add(((Integer) callSegmentToCancel.invokeID), INVOKE_ID, Integer.class);
+                       }
+
+                       if (callSegmentToCancel.callSegmentID != null) {
+ xml.add(((Integer) callSegmentToCancel.callSegmentID), CALL_SEGMENT_ID, Integer.class);
+                       }
+
+               }
+       };
 }
-
=======================================
--- /cap/cap-impl/src/test/java/org/mobicents/protocols/ss7/cap/service/circuitSwitchedCall/CancelRequestTest.java Wed May 16 05:00:01 2012 +++ /cap/cap-impl/src/test/java/org/mobicents/protocols/ss7/cap/service/circuitSwitchedCall/CancelRequestTest.java Fri Jan 25 02:57:39 2013
@@ -1,6 +1,6 @@
 /*
- * JBoss, Home of Professional Open Source
- * Copyright 2011, Red Hat, Inc. and individual contributors
+ * TeleStax, Open Source Cloud Communications
+ * Copyright 2012, Telestax Inc and individual contributors
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
@@ -22,10 +22,18 @@

 package org.mobicents.protocols.ss7.cap.service.circuitSwitchedCall;

-import static org.testng.Assert.*;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertTrue;

+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.util.Arrays;

+import javolution.xml.XMLObjectReader;
+import javolution.xml.XMLObjectWriter;
+
 import org.mobicents.protocols.asn.AsnInputStream;
 import org.mobicents.protocols.asn.AsnOutputStream;
import org.mobicents.protocols.ss7.cap.service.circuitSwitchedCall.primitive.CallSegmentToCancelImpl;
@@ -34,6 +42,7 @@
 /**
  *
  * @author sergey vetyutnev
+ * @author Amit Bhayani
  *
  */
 public class CancelRequestTest {
@@ -49,8 +58,8 @@
        public byte[] getData3() {
                return new byte[] { (byte) 162, 3, (byte) 129, 1, 20 };
        }
-
-       @Test(groups = { "functional.decode","circuitSwitchedCall"})
+
+       @Test(groups = { "functional.decode", "circuitSwitchedCall" })
        public void testDecode() throws Exception {

                byte[] data = this.getData1();
@@ -82,10 +91,10 @@
                assertNull(elem.getInvokeID());
                assertFalse(elem.getAllRequests());
                assertNull(elem.getCallSegmentToCancel().getInvokeID());
-               
assertEquals((int)elem.getCallSegmentToCancel().getCallSegmentID(), 20);
+               assertEquals((int) 
elem.getCallSegmentToCancel().getCallSegmentID(), 20);
        }

-       @Test(groups = { "functional.encode","circuitSwitchedCall"})
+       @Test(groups = { "functional.encode", "circuitSwitchedCall" })
        public void testEncode() throws Exception {

                CancelRequestImpl elem = new CancelRequestImpl(11000);
@@ -105,5 +114,55 @@
                elem.encodeAll(aos);
                assertTrue(Arrays.equals(aos.toByteArray(), this.getData3()));
        }
+
+       @Test(groups = { "functional.xml.serialize", "circuitSwitchedCall" })
+       public void testXMLSerializaion() throws Exception {
+               CancelRequestImpl original = new CancelRequestImpl(11000);
+
+               // Writes the area to a file.
+               ByteArrayOutputStream baos = new ByteArrayOutputStream();
+               XMLObjectWriter writer = XMLObjectWriter.newInstance(baos);
+               // writer.setBinding(binding); // Optional.
+               writer.setIndentation("\t"); // Optional (use tabulation for
+                                                                               
// indentation).
+               writer.write(original, "cancelRequest", 
CancelRequestImpl.class);
+               writer.close();
+
+               byte[] rawData = baos.toByteArray();
+               String serializedEvent = new String(rawData);
+
+               System.out.println(serializedEvent);
+
+               ByteArrayInputStream bais = new ByteArrayInputStream(rawData);
+               XMLObjectReader reader = XMLObjectReader.newInstance(bais);
+ CancelRequestImpl copy = reader.read("cancelRequest", CancelRequestImpl.class);
+
+               assertEquals(copy.getInvokeID(), original.getInvokeID());
+
+ CallSegmentToCancelImpl callSegmentToCancel = new CallSegmentToCancelImpl(null, 20);
+               // Integer invokeID, Integer callSegmentID
+               original = new CancelRequestImpl(callSegmentToCancel);
+
+               // Writes the area to a file.
+               baos = new ByteArrayOutputStream();
+               writer = XMLObjectWriter.newInstance(baos);
+               // writer.setBinding(binding); // Optional.
+               writer.setIndentation("\t"); // Optional (use tabulation for
+                                                                               
// indentation).
+               writer.write(original, "cancelRequest", 
CancelRequestImpl.class);
+               writer.close();
+
+               rawData = baos.toByteArray();
+               serializedEvent = new String(rawData);
+
+               System.out.println(serializedEvent);
+
+               bais = new ByteArrayInputStream(rawData);
+               reader = XMLObjectReader.newInstance(bais);
+               copy = reader.read("cancelRequest", CancelRequestImpl.class);
+
+ assertEquals(copy.getCallSegmentToCancel().getCallSegmentID(), original.getCallSegmentToCancel()
+                               .getCallSegmentID());
+
+       }
 }
-
=======================================
--- /cap/cap-impl/src/test/java/org/mobicents/protocols/ss7/cap/service/circuitSwitchedCall/primitive/CallSegmentToCancelTest.java Mon Apr 23 10:53:14 2012 +++ /cap/cap-impl/src/test/java/org/mobicents/protocols/ss7/cap/service/circuitSwitchedCall/primitive/CallSegmentToCancelTest.java Fri Jan 25 02:57:39 2013
@@ -1,6 +1,6 @@
 /*
- * JBoss, Home of Professional Open Source
- * Copyright 2011, Red Hat, Inc. and individual contributors
+ * TeleStax, Open Source Cloud Communications
+ * Copyright 2012, Telestax Inc and individual contributors
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
@@ -25,8 +25,13 @@
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertTrue;

+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.util.Arrays;

+import javolution.xml.XMLObjectReader;
+import javolution.xml.XMLObjectWriter;
+
 import org.mobicents.protocols.asn.AsnInputStream;
 import org.mobicents.protocols.asn.AsnOutputStream;
 import org.mobicents.protocols.asn.Tag;
@@ -35,6 +40,7 @@
 /**
  *
  * @author sergey vetyutnev
+ * @author Amit Bhayani
  *
  */
 public class CallSegmentToCancelTest {
@@ -43,7 +49,7 @@
                return new byte[] { 48, 6, (byte) 128, 1, 3, (byte) 129, 1, 5 };
        }

-       @Test(groups = { "functional.decode","circuitSwitchedCall.primitive"})
+       @Test(groups = { "functional.decode", "circuitSwitchedCall.primitive" })
        public void testDecode() throws Exception {

                byte[] data = this.getData1();
@@ -52,11 +58,11 @@
                int tag = ais.readTag();
                assertEquals(tag, Tag.SEQUENCE);
                elem.decodeAll(ais);
-               assertEquals((int)elem.getInvokeID(), 3);
-               assertEquals((int)elem.getCallSegmentID(), 5);
+               assertEquals((int) elem.getInvokeID(), 3);
+               assertEquals((int) elem.getCallSegmentID(), 5);
        }

-       @Test(groups = { "functional.encode","circuitSwitchedCall.primitive"})
+       @Test(groups = { "functional.encode", "circuitSwitchedCall.primitive" })
        public void testEncode() throws Exception {

                CallSegmentToCancelImpl elem = new CallSegmentToCancelImpl(3, 
5);
@@ -64,5 +70,31 @@
                elem.encodeAll(aos);
                assertTrue(Arrays.equals(aos.toByteArray(), this.getData1()));
        }
-}

+       @Test(groups = { "functional.xml.serialize", "circuitSwitchedCall" })
+       public void testXMLSerializaion() throws Exception {
+               CallSegmentToCancelImpl original = new 
CallSegmentToCancelImpl(3, 5);
+
+               // Writes the area to a file.
+               ByteArrayOutputStream baos = new ByteArrayOutputStream();
+               XMLObjectWriter writer = XMLObjectWriter.newInstance(baos);
+               // writer.setBinding(binding); // Optional.
+               writer.setIndentation("\t"); // Optional (use tabulation for
+                                                                               
// indentation).
+ writer.write(original, "callSegmentToCancel", CallSegmentToCancelImpl.class);
+               writer.close();
+
+               byte[] rawData = baos.toByteArray();
+               String serializedEvent = new String(rawData);
+
+               System.out.println(serializedEvent);
+
+               ByteArrayInputStream bais = new ByteArrayInputStream(rawData);
+               XMLObjectReader reader = XMLObjectReader.newInstance(bais);
+ CallSegmentToCancelImpl copy = reader.read("callSegmentToCancel", CallSegmentToCancelImpl.class);
+
+               assertEquals(copy.getInvokeID(), original.getInvokeID());
+               assertEquals(copy.getCallSegmentID(), 
original.getCallSegmentID());
+
+       }
+}

--



Reply via email to