# Genesys Quality Management Suite 8.1.520 : Sample Java Web Client Source Code for Pause/Resume API

This feature has been deprecated and is replaced by Call Recording Web Service API.

The source code for a complete sample client application in Java, demonstrating the principles of use for the Pause/Resume API is available.

    package cz.zoom.callrec.restful.client; import java.io.BufferedReader;
    import java.io.Console; import java.io.IOException; import
    java.io.InputStream; import java.io.InputStreamReader; import
    java.util.ArrayList; import java.util.Arrays; import java.util.HashMap;
    import java.util.Iterator; import java.util.List; import java.util.Map;
    import javax.xml.XMLConstants; import
    javax.xml.namespace.NamespaceContext; import
    javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathConstants; import
    javax.xml.xpath.XPathExpression; import
    javax.xml.xpath.XPathExpressionException; import
    javax.xml.xpath.XPathFactory; import
    org.apache.commons.httpclient.Cookie; import
    org.apache.commons.httpclient.HttpClient; import
    org.apache.commons.httpclient.HttpMethod; import
    org.apache.commons.httpclient.HttpState; import
    org.apache.commons.httpclient.cookie.CookiePolicy; import
    org.apache.commons.httpclient.methods.GetMethod; import
    org.apache.commons.httpclient.methods.PostMethod; import
    org.apache.commons.httpclient.methods.PutMethod; import
    org.apache.commons.httpclient.methods.StringRequestEntity; import
    org.w3c.dom.Document; import org.w3c.dom.NodeList; public class Main {
    private HttpState myInitialState; private HttpClient myClient; private
    String myServer; private XPath myXpath; private static final Console
    console = System.console(); private static final String PAUSED_STATE =
    "paused"; private interface ResponseProcessor<T> { T
    read(InputStream result) throws IOException; void onStatus(int status)
    throws IOException; } public static void main(String[] args) { String
    phone = null; boolean pause = false; boolean resume = false; String user
     = null; String server = null; for (int i = 0; i < args.length; i++) {
     if ("-p".equals(args[i]) || "--phone".equals(args[i])) { phone =
    args[++i]; } if ("-u".equals(args[i]) || "--user".equals(args[i])) {
    user = args[++i]; } if ("-s".equals(args[i]) ||
    "--server".equals(args[i])) { server = args[++i]; } if
    ("pause".equals(args[i])) { pause = true; } if
    ("resume".equals(args[i])) { resume = true; } } try { if (user == null) {
     user = readLine("Please enter user name"); } char[] password =
    readPassword("Please enter password"); //Normally, the phone number
    should also be enforced, but for this example, we do not require it.
    Main main = null; try { try { main = new Main(server, user, password); }
     finally { Arrays.fill(password, (char)0); } List<String> items =
    main.list(phone); if (items.isEmpty()) { System.out.println("There is no
     call in progress that matches the criteria"); } for (String next :
    items) { //If phone number is not null, there will be only one element
    here. if (pause) { main.setPausedState(next, true); } if (resume) {
    main.setPausedState(next, false); } } } finally { if (main != null) {
    main.logout(); } } System.exit(0); } catch (Exception e) {
    e.printStackTrace(); System.exit(1); } } private static String
    readLine(String prompt) throws IOException { if (console != null) {
    return console.readLine(prompt); } else { System.out.println(prompt);
    return new BufferedReader(new InputStreamReader(System.in)).readLine(); }
     } private static char[] readPassword(String prompt) throws IOException {
     if (console != null) { return console.readPassword(prompt); } else {
    System.out.println(prompt); return new BufferedReader(new
    InputStreamReader(System.in)).readLine().toCharArray(); } } private
    Main(String server, String user, char[] password) throws IOException {
    myServer = server; myInitialState = new HttpState(); Cookie cookie = new
     Cookie(); cookie.setDomain(myServer); cookie.setPath("/");
    cookie.setName("aaa"); cookie.setValue("bbb");
    myInitialState.addCookie(cookie); myClient = new HttpClient();
    myClient.setState(myInitialState);
    myClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
     login(user, password); } private void login(String user, char[]
    password) throws IOException { String uri = String.format("http://%s/callrec/loginAction.do",
     myServer); PostMethod login = new PostMethod(uri);
    login.addParameter("login", user); login.addParameter("password",
    String.valueOf(password)); executeMethod(login, IGNORE_OUTPUT); }
    private void logout() throws IOException { String uri = String.format("http://%s/callrec/logoutprocess",
     myServer); GetMethod logout = new GetMethod(uri); executeMethod(logout,
     IGNORE_OUTPUT); } private List<String> list(String phone) throws
    Exception { String uri = String.format("http://%s/callrec/restful/couples.xml/",
     myServer); //single point of access to RS. if (phone != null) { uri +=
    "?phoneNumber=" + phone; } GetMethod list = new GetMethod(uri); return
    executeMethod(list, new
    MultiValuedProducer("/couples/couples/link[@rel='self']/@href")); }
    private void setPausedState(String url, boolean isPaused) throws
    Exception { String pauseResumeUrl = getPauseResumeLink(url); if
    (pauseResumeUrl == null) { throw new RuntimeException("A problem
    prevents you from calling pause or resume. Probably, you don't have the
    sufficient rights to this operation"); } // PostMethod changeState = new
     PostMethod(pauseResumeUrl); // changeState.addParameter(PAUSED_STATE,
    String.valueOf(isPaused)); PutMethod changeState = new
    PutMethod(pauseResumeUrl); changeState.setRequestEntity(new
    StringRequestEntity(String.format(PAUSED_STATE + "=%b", isPaused),
    "application/x-www-form-urlencoded", "UTF-8"));
    executeMethod(changeState, IGNORE_OUTPUT); } private String
    getPauseResumeLink(String callUrl) throws Exception { GetMethod details =
     new GetMethod(callUrl); return executeMethod(details, new
    SingleValuedProducer("/couple/link[@rel='pause/resume']/@href")); }
    private <T> T executeMethod(HttpMethod method,
    ResponseProcessor<T> processor) throws IOException { try { int
    result = myClient.executeMethod(method); try {
    processor.onStatus(result); //let it fail on statuses it does not want. }
     catch (IOException e) {
    IGNORE_OUTPUT.read(method.getResponseBodyAsStream()); throw new
    IOException("status=" + result); } return
    processor.read(method.getResponseBodyAsStream()); } finally {
    method.releaseConnection(); } } private XPath getXpath() { if (myXpath
    == null) { myXpath = XPathFactory.newInstance().newXPath();
    myXpath.setNamespaceContext(new ListNamespaceContext("atom", "http://www.w3.org/2005/Atom"));
     } return myXpath; } private static final ResponseProcessor<Void>
    IGNORE_OUTPUT = new ResponseProcessor<Void>() { public void
    onStatus(int status) throws IOException { if (status > 399) { throw
    new IOException("Status=" + status); } } @Override public Void
    read(InputStream body) throws IOException { try { byte[] ignored = new
    byte[2048]; int len = 0; while (len >= 0) { len = body.read(ignored);
     // System.out.print(new String(ignored)); } } finally { body.close(); }
     return null; } }; private class MultiValuedProducer implements
    ResponseProcessor<List<String>> { private final
    XPathExpression myExpression; public MultiValuedProducer(String
    xpathExpression) throws XPathExpressionException { myExpression =
    getXpath().compile(xpathExpression); } public void onStatus(int status)
    throws IOException { if (status > 399) { throw new
    IOException("Status=" + status); } } @Override public List<String>
     read(InputStream response) throws IOException { try { Document doc =
    DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(response);
     NodeList items = (NodeList) myExpression.evaluate(doc,
    XPathConstants.NODESET); List<String> result = new
    ArrayList<String>(items.getLength()); for (int i = 0, iMax =
    items.getLength(); i < iMax; i++) {
    result.add(items.item(i).getNodeValue()); } return result; } catch
    (IOException e) { throw e; } catch (Exception e) { throw new
    RuntimeException(e); } } } private class SingleValuedProducer implements
     ResponseProcessor<String> { private final MultiValuedProducer
    myDelegate; public SingleValuedProducer(String xpathExpression) throws
    XPathExpressionException { myDelegate = new
    MultiValuedProducer(xpathExpression); } @Override public void
    onStatus(int status) throws IOException { myDelegate.onStatus(status); }
     @Override public String read(InputStream response) throws IOException {
     List<String> result = myDelegate.read(response); if
    (result.isEmpty()) { return null; } return result.get(0); } } private
    static class ListNamespaceContext implements NamespaceContext { private
    Map<String, String> myNamespaces = new HashMap<String,
    String>(); public ListNamespaceContext(String...context) { if
    (context == null) { return; //not particularly useful, but ok. } if
    ((context.length % 2) == 1) { throw new IllegalArgumentException(); }
    for (int i = 0; i < context.length; i += 2) {
    myNamespaces.put(context[i], context[i+1]); } } @Override public String
    getNamespaceURI(String prefix) { if ("xml".equals(prefix)) { return
    XMLConstants.XML_NS_URI; } String result = myNamespaces.get(prefix); if
    (result == null) { return XMLConstants.NULL_NS_URI; } return result; }
    @Override public String getPrefix(String namespaceURI) { throw new
    UnsupportedOperationException(); } @Override public Iterator
    getPrefixes(String namespaceURI) { throw new
    UnsupportedOperationException(); } } }