Upgrade from Java Legacy to v4.8.0 or Latest Version

Introduction

This is a major application update. Plivo recommends you always use the latest or an active version of our SDKs for guaranteed security, stability, and uptime. The active SDK versions are designed to handle intermittent and regional failures of API requests. In addition, they offer a host of security features, such as protection against DoS attacks and bot detection for suspicious user agents.

Deprecation notice: We’re deprecating Plivo Java SDK legacy versions lower than v4.8.0 on January 31, 2022. If you use a deprecated version of our SDK after that date, your API requests and voice calls may fail intermittently. Plivo will no longer provide bug fixes to these versions, and our support team may ask you to upgrade before debugging issues.

Migrate your applications

Java version support

The Plivo Java SDK supports OpenJDK 8 and 11 and OracleJDK 8 and 11.

Use the command Update-Package Plivo -Version 4.10.0 to upgrade to the active version of the SDK, or upgrade to the latest version.

After you upgrade to the latest version of the SDK, you should check every program that depends on it and make changes to the syntax for several kinds of operations. Here are examples of how coding differs between the deprecated legacy version of the SDK and the latest active versions.

Import the SDK

Legacy Latest
import com.plivo.helper.api.client.*;
import com.plivo.helper.xml.elements.Dial;
   
import com.plivo.api.Plivo;
import com.plivo.api.xml.Dial;
   

Initialize

Legacy Latest
RestAPI api = new RestAPI("<auth_id>","<auth_token>", "v1");
   
Plivo.init("<auth_id>","<auth_token>");
   

Accessing resources

Legacy Latest
Call resp = api.makeCall(parameters);
   
CallCreateResponse response = Call.creator(parameters)
                .create();
   

Make a call

Legacy Latest
package com.plivo.test;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.LinkedHashMap;
import com.plivo.helper.api.client.*;
import com.plivo.helper.api.response.call.Call;
import com.plivo.helper.exception.PlivoException;

public class App {
    public static void main(String[] args) throws IllegalAccessException {
        String auth_id = "<auth_id>";
        String auth_token = "<auth_token>";
        RestAPI api = new RestAPI(auth_id, auth_token, "v1");

        LinkedHashMap<String, String> parameters = new LinkedHashMap<String, String>();
        parameters.put("to","2025552323"); 
        parameters.put("from","2025551212"); 
        parameters.put("answer_url","https://s3.amazonaws.com/static.plivo.com/answer.xml");
        parameters.put("answer_method","GET"); 
        try {
            Call resp = api.makeCall(parameters);
            System.out.println(resp);
        } catch (PlivoException e) {
            System.out.println(e.getLocalizedMessage());
        }
    }
}
   
using System;
using System.Collections.Generic;
using Plivo;
package com.plivo.api.samples.call;

import java.io.IOException;
import java.util.Collections;

import com.plivo.api.Plivo;
import com.plivo.api.exceptions.PlivoRestException;
import com.plivo.api.models.call.Call;
import com.plivo.api.models.call.CallCreateResponse;

class CallCreate {
    public static void main(String [] args) {
        Plivo.init("<auth_id>","<auth_token>");
        try {
            CallCreateResponse response = Call.creator("+12025551212", Collections.singletonList("+12025552323"), "https://s3.amazonaws.com/static.plivo.com/answer.xml")
                .answerMethod("GET")
                .create();
            System.out.println(response);
        } catch (PlivoRestException | IOException e) {
            e.printStackTrace();
        }
    }
}
   

Dial XML

Legacy Latest
import java.io.IOException;

import com.plivo.helper.exception.PlivoException;
import com.plivo.helper.xml.elements.Number;
import com.plivo.helper.xml.elements.Dial;
import com.plivo.helper.xml.elements.PlivoResponse;

class CustomCallerTone {
   public static void main(String[] args) throws PlivoXmlException {
       PlivoResponse response = new PlivoResponse();
       Dial dial = new Dial();
       dial.setDialMusic("https://<yourdomain>.com/dial_music/");
       Number number = new Number("12025552323");
      

       response.append(dial);
       dial.append(number);
       System.out.println(response.toXML());
       resp.addHeader("Content-Type", "text/xml");
       resp.getWriter().print(response.toXML());;
   }
}
   
package com.plivo.api.xml.samples.dial;

import com.plivo.api.exceptions.PlivoXmlException;
import com.plivo.api.xml.Dial;
import com.plivo.api.xml.Number;
import com.plivo.api.xml.Response;

class CustomCallerTone {
    public static void main(String[] args) throws PlivoXmlException {
        Response response = new Response()
                .children(
                        new Dial()
                                .dialMusic("https://<yourdomain>.com/dial_music/")
                                .children(
                                        new Number("12025552323")
                                )
                );
        System.out.println(response.toXmlString());
    }
}
   

Conference XML

Legacy Latest
import java.io.IOException;

import com.plivo.helper.exception.PlivoException;
import com.plivo.helper.xml.elements.Conference;
import com.plivo.helper.xml.elements.PlivoResponse;

class ModeratedConference {
   public static void main(String[] args) throws PlivoException {
       PlivoResponse response = new PlivoResponse();
       Conference conference = new Conference("My Room");
       conference.setEnterSound("");
       conference.setStartConferenceOnEnter(true);
       conference.setEndConferenceOnExit(true);
       conference.setWaitSound("https://<yourdomain>.com/music/");
       response.append(conference);
       System.out.println(response.toXML());
       resp.addHeader("Content-Type", "text/xml");
       resp.getWriter().print(response.toXML());;
   }
}
   
package com.plivo.api.xml.samples.conference;

import com.plivo.api.exceptions.PlivoXmlException;
import com.plivo.api.xml.Conference;
import com.plivo.api.xml.Response;
import com.plivo.api.xml.Speak;

class ModeratedConference {
    public static void main(String[] args) throws PlivoXmlException {
        Response response = new Response()
                .children(
                        new Speak("You will now be placed into a demo conference"),
                        new Conference("demo")
                                .endConferenceOnExit(true)
                                .startConferenceOnEnter(false)
                                .waitSound("https://<yourdomain>.com/waitmusic/")
                );
        System.out.println(response.toXmlString());
    }
}
   

Record API

Legacy Latest
package plivoexample;

import java.io.IOException;
import java.util.LinkedHashMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.plivo.helper.api.client.RestAPI;
import com.plivo.helper.api.response.response.Record;
import com.plivo.helper.exception.PlivoException;

class recordApiAction {
   public static void main(String[] args) throws PlivoException {
       String auth_id = "<auth_id>";
       String auth_token = "<auth_token>";
       RestAPI api = new RestAPI(auth_Id, auth_Token, "v1");
       LinkedHashMap<String, String> parameters = new LinkedHashMap<String, String>();
       parameters.put("call_uuid",call_uuid);
       Record record = api.record(parameters);
       System.out.println(record);
      
   }
}
   
package com.plivo.api.samples.call.record;

import java.io.IOException;
import com.plivo.api.Plivo;
import com.plivo.api.exceptions.PlivoRestException;
import com.plivo.api.models.call.Call;
import com.plivo.api.models.call.actions.CallRecordCreateResponse;

class RecordCreate {
    public static void main(String [] args) {
        Plivo.init("<auth_id>","<auth_token>");
        try {
            CallRecordCreateResponse response = Call.recorder("eba53b9e-8fbd-45c1-9444-696d2172fbc8")
                .record();

            System.out.println(response);
        } catch (PlivoRestException | IOException e) {
            e.printStackTrace();
        }
    }
}
   

Record XML

Legacy Latest
import java.io.IOException;

import com.plivo.helper.exception.PlivoException;
import com.plivo.helper.xml.elements.Record;
import com.plivo.helper.xml.elements.Dial;
import com.plivo.helper.xml.elements.Number;
import com.plivo.helper.xml.elements.PlivoResponse;

class recordSession {
   public static void main(String[] args) throws PlivoException {
       response.append(record);
       response.append(dial);
       dial.append(number);
       System.out.println(response.toXML());
       resp.addHeader("Content-Type", "text/xml");
       resp.getWriter().print(response.toXML());;
   }
}
   
package com.plivo.api.xml.samples.record;

import com.plivo.api.exceptions.PlivoXmlException;
import com.plivo.api.xml.Dial;
import com.plivo.api.xml.Number;
import com.plivo.api.xml.Record;
import com.plivo.api.xml.Response;

class RecordACompleteCallSession {
    public static void main(String[] args) throws PlivoXmlException {
        Response response = new Response()
                .children(
                        new Record("https://<yourdomain>.com/get_recording/")
                                .redirect(false)
                                .startOnDialAnswer(true),
                        new Dial()
                                .children(
                                        new Number("12025552323")
                                )
                );
        System.out.println(response.toXmlString());
    }
}