Greet Caller by Name in Java

package pbxsampleapp;

import com.sun.net.httpserver.*;
import java.io.*;
import java.net.*;
import java.util.*;

public class GreetCallerbyName 
{
   public static void main(String[] args)
    {
        try
        {
            phoneBook = new LinkedHashMap<>();
            phoneBook.put("+12345671", "John");
            phoneBook.put("1001", "Kevin");
            phoneBook.put("+12345675", "Jack");

            System.out.println("Starting http server...");
            HttpServer server = HttpServer.create(new InetSocketAddress(InetAddress.getByAddress(new byte[]{ 0, 0, 0, 0 }), 12345), 0);
            server.createContext("/greetcallerbyname", new PbxSampleApp.PBXRequestHandler());
            server.start();
            System.out.println("http server running on " + server.getAddress().toString());
        }
        catch (IOException ex) { System.out.println("Error" + ex.toString()); }
    }
    static Map<String, String> phoneBook;

    static class PBXRequestHandler implements HttpHandler
    {
        @Override
        public void handle(HttpExchange httpExchange) throws IOException
        {
            Map parameters = parseQuery(httpExchange.getRequestURI().getQuery());
            String callerName = "Sir or Madame";
            if (parameters.containsKey("Caller") && phoneBook.containsKey(parameters.get("Caller")) )
            {
                callerName = phoneBook.get(parameters.get("Caller").toString()).toString();
            }
            httpExchange.getResponseHeaders().add("Content-type", "text/xml");
            String response = "<?xml version=\"1.0\"?>"
                    + "<Response>"
                    + "<Speak>Hello " + callerName + "!</Speak>"
                    + "</Response>";
            httpExchange.sendResponseHeaders(200, response.length());
            OutputStream os = httpExchange.getResponseBody();
            os.write(response.getBytes());
            os.close();
        }

        private Map<String, String> parseQuery(String params) throws UnsupportedEncodingException
        {
            Map<String, String> query_pairs = new LinkedHashMap<String, String>();
            String[] pairs = params.split("&");
            String paramname;
            String value;
            for (String pair : pairs)
            {
                int idx = pair.indexOf("=");
                paramname = pair.substring(0, idx);
                value = pair.substring(idx + 1);
                query_pairs.put(paramname, value);
                System.out.println(String.format("%s: %s", paramname, value));
            }
            return query_pairs;
        }
    }
}
Code example 1 - Greeting the caller by using a phonebook

IN MORE DETAILS

  1. When your application receives a call, you will get a Request with all of the call details.
  2. Fill an associative array with telephone numbers and names.
  3. In this associative array (phonebook) we are looking for the Caller parameter of the Request.
  4. After that, send back the response OzML to the caller.
If your application gets a call from 1001 (Caller), the caller will hear the following greeting message: “Hello Kevin”. Try it out! Add your telephone number and your name to the array and call your program. You will be greeted by your name. If the phonebook does not contain the number of the caller, it just says “Sir or Madame”.

More information