0

For the life of me, I can't figure out why I'm getting an XML parsing error for the action URL of a Twilio response object. I've attached both the output page error and the java code I use to generate the XML.

OUTPUT ERROR

XML Parsing Error: not well-formed
Location: https://test.ignore.com/ApplicationName/go.acx?action=ivr.outbound.twilio.Introduction&rkey=1
Line Number 4, Column 101:<Response><Gather action="instanceurl.com/AccessWorx/go.acx?action=ivr.outbound.twilio.Selection&rkey=1" timeout="5" numDigits="1" finishOnKey="#" method="GET"><Say>This is an automated message from __________ to notify you of a service issue.Here is a sample message. Press 1 to accept this serviceissue, Press 2 to forward this call to the next contact in you company, press 3 if you are not the correct person to contact, press 4 to repeat these options.</Say></Gather></Response>
----------------------------------------------------------------------------------------------------^

The ^ doesn't match up in the above code formatting but its essentially pointing at the "=" sign in rkey=1 at the end of the action url.

JAVA CODE

StringBuffer sb = new StringBuffer();
    sb.append("This is an automated message from ___________ to notify you of a service issue.")
            .append(serviceMessage)
            .append("Press 1 to accept this service"
            + "issue, Press 2 to forward this call to the next contact in you company, press 3 "
            + "if you are not the correct person to contact, press 4 to repeat these options.");

// Create a TwiML response and add our friendly message.
TwiMLResponse twiml = new TwiMLResponse();
Say say = new Say(sb.toString());

Gather g = new Gather();
// set url to selection with paramter for rkey
IVRAgent ivrAgent = new IVRAgent();
g.setAction(ivrAgent.buildActionUrl(callBean.getInstanceUrl() + "go.acx?", "ivr.outbound.twilio.Selection", rkey.toString()));
g.setTimeout(TIMEOUT);
g.setNumDigits(1);
g.setFinishOnKey(POUND);
g.setMethod("GET");

try {
    g.append(say);
    twiml.append(g);    
} catch (TwiMLException e) {
    log.error("Error in creating twiml", e);
    e.printStackTrace();
}
  • Check this out http://stackoverflow.com/questions/1328538/how-do-i-escape-ampersands-in-xml. Try escaping that `&` char. – Bhesh Gurung Dec 29 '14 at 19:50
  • 1
    Thanks Bhesh ... turns out you were right! Something about posting questions seem to direct you immediately to your own answer. – user2671774 Dec 29 '14 at 20:01
  • Yeah. If read the answers in the link, you can see the similar experience by the posters there too. :) – Bhesh Gurung Dec 29 '14 at 20:02

1 Answers1

2

After doing some browser debugging, Firefox told me that the & needed to be escaped. Luckily, Java offers a few utility functions in java.net (URLEncoder) that handles escaping spaces, &, etc.

Here is the new implementation of my method generating the action url:

public String buildActionUrl(String instanceUrl, String action, String rkey) {
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("action", action));
    params.add(new BasicNameValuePair("rkey", rkey));

    String paramString = URLEncodedUtils.format(params, "UTF-8");
    try {
        return URLEncoder.encode(instanceUrl + paramString, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        logger.error("", e);
        return ".";     // return the action to be defaulted to the originating page
    }
}

One could generalize this method to accept any input mapping of parameters:

public String buildActionUrl(String baseUrl, Map<String, String> parameters, String encoding) throws UnsupportedEncodingException {
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    for (String param : parameters.keySet() ) {
        params.add(new BasicNameValuePair(param, parameters.get(param)));
    }
    return URLEncoder.encode(baseUrl + URLEncodedUtils.format(params, encoding), encoding);
}