Skip to content

Commit e977ea7

Browse files
DX-2469 Added modifyCallBxml to ApiController
1 parent d1cd58a commit e977ea7

File tree

3 files changed

+164
-3
lines changed

3 files changed

+164
-3
lines changed
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
eclipse.preferences.version=1
2-
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
3-
org.eclipse.jdt.core.compiler.compliance=1.5
2+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
3+
org.eclipse.jdt.core.compiler.compliance=1.8
4+
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
45
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
5-
org.eclipse.jdt.core.compiler.source=1.5
6+
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
7+
org.eclipse.jdt.core.compiler.processAnnotations=enabled
8+
org.eclipse.jdt.core.compiler.release=disabled
9+
org.eclipse.jdt.core.compiler.source=1.8
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
2+
package com.bandwidth.voice.bxml.verbs;
3+
4+
import lombok.Builder;
5+
6+
import java.io.ByteArrayOutputStream;
7+
import java.io.OutputStream;
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.List;
11+
import java.util.regex.Matcher;
12+
import java.util.regex.Pattern;
13+
import javax.xml.bind.JAXBContext;
14+
import javax.xml.bind.JAXBException;
15+
import javax.xml.bind.Marshaller;
16+
import javax.xml.bind.annotation.XmlElement;
17+
import javax.xml.bind.annotation.XmlElements;
18+
import javax.xml.bind.annotation.XmlRootElement;
19+
20+
@Builder
21+
@XmlRootElement(name = Bxml.TYPE_NAME)
22+
public class Bxml {
23+
public static final String TYPE_NAME = "Bxml";
24+
25+
private static JAXBContext jaxbContext;
26+
27+
@XmlElements({
28+
@XmlElement(name = Gather.TYPE_NAME, type = Gather.class),
29+
@XmlElement(name = Hangup.TYPE_NAME, type = Hangup.class),
30+
@XmlElement(name = Redirect.TYPE_NAME, type = Redirect.class),
31+
@XmlElement(name = PlayAudio.TYPE_NAME, type = PlayAudio.class),
32+
@XmlElement(name = SpeakSentence.TYPE_NAME, type = SpeakSentence.class),
33+
@XmlElement(name = Transfer.TYPE_NAME, type = Transfer.class),
34+
@XmlElement(name = Pause.TYPE_NAME, type = Pause.class),
35+
@XmlElement(name = Forward.TYPE_NAME, type = Forward.class),
36+
@XmlElement(name = SendDtmf.TYPE_NAME, type = SendDtmf.class),
37+
@XmlElement(name = Record.TYPE_NAME, type = Record.class),
38+
@XmlElement(name = StartRecording.TYPE_NAME, type = StartRecording.class),
39+
@XmlElement(name = StopRecording.TYPE_NAME, type = StopRecording.class),
40+
@XmlElement(name = PauseRecording.TYPE_NAME, type = PauseRecording.class),
41+
@XmlElement(name = ResumeRecording.TYPE_NAME, type = ResumeRecording.class),
42+
@XmlElement(name = Conference.TYPE_NAME, type = Conference.class),
43+
@XmlElement(name = Bridge.TYPE_NAME, type = Bridge.class),
44+
@XmlElement(name = Ring.TYPE_NAME, type = Ring.class),
45+
@XmlElement(name = StartGather.TYPE_NAME, type = StartGather.class),
46+
@XmlElement(name = StopGather.TYPE_NAME, type = StopGather.class),
47+
@XmlElement(name = Tag.TYPE_NAME, type = Tag.class),
48+
@XmlElement(name = SipUri.TYPE_NAME, type = SipUri.class)
49+
})
50+
private final List<Verb> verbs = new ArrayList<>();
51+
52+
private static final Pattern XML_PATTERN = Pattern.compile("&lt;([a-zA-Z//].*?)&gt;");
53+
54+
private static final Pattern SPEAK_SENTENCE_PATTERN = Pattern.compile("<SpeakSentence.*?>(.*?)<\\/SpeakSentence>");
55+
56+
57+
58+
public Bxml(){
59+
}
60+
61+
62+
63+
64+
/**
65+
* Returns list of verbs in this Bxml
66+
*/
67+
public List<Verb> getVerbs() {
68+
return verbs;
69+
}
70+
71+
/**
72+
* Adds a Verb to the list of Verbs in this Bxml
73+
*/
74+
public Bxml add(Verb verb) {
75+
this.verbs.add(verb);
76+
return this;
77+
}
78+
79+
/**
80+
* Adds Verbs to the list of Verbs in this Bxml
81+
*/
82+
public Bxml addAll(Verb... verbs) {
83+
this.verbs.addAll(Arrays.asList(verbs));
84+
return this;
85+
}
86+
87+
/**
88+
* Convert this Bxml and its verbs into their corresponding XML and get the result as a String
89+
*
90+
*/
91+
public String toBXML() {
92+
OutputStream outputStream = new ByteArrayOutputStream();
93+
try {
94+
getMarshaller().marshal(this, outputStream);
95+
} catch (JAXBException e) {
96+
throw new RuntimeException("Error creating BXML marshaller", e);
97+
}
98+
99+
Matcher matcherSpeakSentence = SPEAK_SENTENCE_PATTERN.matcher(outputStream.toString());
100+
101+
StringBuffer sb = new StringBuffer();
102+
103+
while(matcherSpeakSentence.find()){
104+
Matcher matcher = XML_PATTERN.matcher(matcherSpeakSentence.group());
105+
String replaced = matcher.replaceAll("<$1>").replaceAll("\\?", "\\?");
106+
matcherSpeakSentence.appendReplacement(sb, replaced);
107+
}
108+
109+
matcherSpeakSentence.appendTail(sb);
110+
111+
return sb.toString();
112+
113+
114+
}
115+
116+
private Marshaller getMarshaller() {
117+
try {
118+
// This isn't thread-unsafe, it's just expensive to create. So no locking necessary.
119+
if (jaxbContext == null) {
120+
jaxbContext = JAXBContext.newInstance(Bxml.class);
121+
}
122+
Marshaller marshaller = jaxbContext.createMarshaller();
123+
marshaller.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
124+
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
125+
126+
return marshaller;
127+
} catch (JAXBException e) {
128+
throw new RuntimeException("Error creating JAXB context for BXML serialization", e);
129+
}
130+
}
131+
}

src/test/java/com/bandwidth/BxmlTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,32 @@ public void testResponse() {
193193
assertEquals("BXML strings not equal", expected, response);
194194
}
195195

196+
@Test
197+
public void testBxml() {
198+
String bxml = new Bxml()
199+
.toBXML();
200+
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Bxml/>";
201+
202+
assertEquals("BXML strings not equal", expected, bxml);
203+
}
204+
205+
@Test
206+
public void testBxmlSpeakSentence() {
207+
SpeakSentence speakSentence = SpeakSentence.builder()
208+
.text("test")
209+
.voice("susan")
210+
.gender("female")
211+
.locale("en_US")
212+
.build();
213+
214+
String bxml = new Bxml()
215+
.add(speakSentence)
216+
.toBXML();
217+
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Bxml><SpeakSentence voice=\"susan\" gender=\"female\" locale=\"en_US\">test</SpeakSentence></Bxml>";
218+
219+
assertEquals("BXML strings not equal", expected, bxml);
220+
}
221+
196222
@Test
197223
public void testHangup() {
198224
Hangup hangup = Hangup.builder().build();

0 commit comments

Comments
 (0)