import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.*;
import com.twilio.sdk.verbs.TwiMLResponse;
import com.twilio.sdk.verbs.TwiMLException;
import com.twilio.sdk.verbs.Say;

public class Main extends HttpServlet {
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {

    if (req.getRequestURI().endsWith("/db")) {
      showDatabase(req,resp);
    } else if (req.getRequestURI().endsWith("/away")) {
        showAway(req,resp);
    } else if (req.getRequestURI().endsWith("/twilio")) {
        twilioService(req,resp);
    } else {
      showHome(req,resp);
    }
  }

  private void showHome(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    resp.getWriter().print("Hello from Java!");
  }

  private void showAway(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    resp.getWriter().print("this is away stuff");
  }


public void twilioService(HttpServletRequest request, HttpServletResponse response) 
      throws IOException {
        // Create a TwiML response and add our friendly message.
        TwiMLResponse twiml = new TwiMLResponse();
        Say say = new Say("Hello Twilio Monkey");
        try {
            twiml.append(say);
        } catch (TwiMLException e) {
            e.printStackTrace();
        }
        response.setContentType("application/xml");
        response.getWriter().print(twiml.toXML());
    }


  private void showDatabase(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    try {
      Connection connection = getConnection();

      Statement stmt = connection.createStatement();
      stmt.executeUpdate("CREATE TABLE IF NOT EXISTS ticks (tick timestamp)");
      stmt.executeUpdate("INSERT INTO ticks VALUES (now())");
      ResultSet rs = stmt.executeQuery("SELECT tick FROM ticks");

      String out = "Hello!\n";
      while (rs.next()) {
          out += "Read from DB: " + rs.getTimestamp("tick") + "\n";
      }

      resp.getWriter().print(out);
    } catch (Exception e) {
      resp.getWriter().print("There was an error: " + e.getMessage());
    }
  }

  private Connection getConnection() throws URISyntaxException, SQLException {
    URI dbUri = new URI(System.getenv("DATABASE_URL"));

    String username = dbUri.getUserInfo().split(":")[0];
    String password = dbUri.getUserInfo().split(":")[1];
    String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + dbUri.getPath();

    return DriverManager.getConnection(dbUrl, username, password);
  }

  public static void main(String[] args) throws Exception{
    Server server = new Server(Integer.valueOf(System.getenv("PORT")));
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    server.setHandler(context);
    context.addServlet(new ServletHolder(new Main()),"/*");
    server.start();
    server.join();
  }
}

@@ -435,13 +435,15 @@
 oid 
-doGet
+service
 (Htt
@@ -543,1161 +543,846 @@
     
-if (req.getRequestURI().endsWith(%22/db%22)) %7B%0A      showDatabase(req,resp);%0A    %7D else if (req.getRequestURI().endsWith(%22/away%22)) %7B%0A        showAway(req,resp);%0A    %7D else if (req.getRequestURI().endsWith(%22/twilio%22)) %7B%0A        twilioService(req,resp);%0A    %7D else %7B%0A      showHome(req,resp);%0A    %7D%0A  %7D%0A%0A  private void showHome(HttpServletRequest req, HttpServletResponse resp)%0A      throws ServletException, IOException %7B%0A    resp.getWriter().print(%22Hello from Java!%22);%0A  %7D%0A%0A  private void showAway(HttpServletRequest req, HttpServletResponse resp)%0A      throws ServletException, IOException %7B%0A    resp.getWriter().print(%22this is away stuff%22);%0A  %7D%0A%0A%0Apublic void twilioService(HttpServletRequest request, HttpServletResponse response) %0A      throws IOException %7B%0A        // Create a TwiML response and add our friendly message.%0A        TwiMLResponse twiml = new TwiMLResponse();%0A        Say say = new Say(%22Hello Twilio Monkey%22);%0A        try %7B%0A            twiml.append(say);%0A        %7D catch (TwiMLException e) %7B%0A            e.printStackTrace();%0A        %7D%0A        response.setContentType(%22application/xml%22);%0A        response.getWriter().print(twiml.toXML());%0A  
+// Example pulled from the Twilio guide at:%0A    // https://www.twilio.com/docs/quickstart/java/twiml/say-response%0A%0A        TwiMLResponse twiml = new TwiMLResponse();%0A        Say say = new Say(%22Hello Twilio Monkey%22);%0A        try %7B%0A            twiml.append(say);%0A        %7D catch (TwiMLException e) %7B%0A            e.printStackTrace();%0A        %7D%0A        response.setContentType(%22application/xml%22);%0A        response.getWriter().print(twiml.toXML());%0A  %7D%0A%0A%0A    // Testing methods - simply print out text%0A  private void showHome(HttpServletRequest req, HttpServletResponse resp)%0A      throws ServletException, IOException %7B%0A    resp.getWriter().print(%22Hello from Java!%22);%0A  %7D%0A%0A  private void showAway(HttpServletRequest req, HttpServletResponse resp)%0A      throws ServletException, IOException %7B%0A    resp.getWriter().print(%22this is away stuff%22);%0A
   %7D%0A

refactor twilio code into service method, which responds to both get and post