OraMonRESTgetData.java 4.41 KB


import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import se.lil.om.OraMon;
import se.lil.om.Registry;

/**
 * Servlet implementation class OraMonREST
 */
@WebServlet(description = "REST API for OraMon", urlPatterns = { "/OraMonREST/getData" })
public class OraMonRESTgetData extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public OraMonRESTgetData() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		try {
			StringBuffer sb = new StringBuffer();
			int age = 0;
			if(request.getParameterMap().containsKey("age")) {
				try {
					age = Integer.parseInt(request.getParameter("age"));
				} catch (Exception e) {
					response.setStatus(400);
					response.getWriter().println("{\"error\":true,\"msg\":\"The specified age parameter is not a valid integer number\"}");
					return;
				}
			}
			
			if(request.getParameterMap().containsKey("connectionString")) {
				// We have a connection string, find the monitor or create it and call getData() on the monitor
				
				// Check that we have a valid connection string
				String[] conStrParamArray = request.getParameter("connectionString").split(":");
				if(conStrParamArray.length != 5) {
					// Error, we need 5 parts in the connection string
					response.setStatus(400);
					response.getWriter().println("{\"error\":true,\"msg\":\"connectionString needs to be in format host:port:sid:username:password\"}");
					return;
				}
				String conStr = "jdbc:oracle:thin:@" + conStrParamArray[0] + ":" + conStrParamArray[1] + ":" + conStrParamArray[2];
				String usrStr = conStrParamArray[3];
				String pwdStr = conStrParamArray[4];
				
				// Try to find the monitor in our list
				OraMon monitor = Registry.findOrCreate(conStr, usrStr, pwdStr);
				
				// Call getData()
				boolean didUpdate = monitor.getData(age);
				String dbName = monitor.getDBName();
				if(didUpdate) {
					sb.append("{\"error\":false,\"msg\":\"Sucessfully collected data on instance '" + dbName + "'\"");
					sb.append(",\"ms\":"+monitor.getLastRTms()+"}");
				} else {
					response.setStatus(HttpServletResponse.SC_ACCEPTED);
					sb.append("{\"error\":false");
					sb.append(",\"msg\":\"Data does not need to be updated on instance '" + dbName + "' either because another thread did concurrently update the monitor and we just waited for it to complete, or because the age (if) specified was higher than the monitors data age.\"");
					sb.append(",\"age\":"+monitor.getAgeTs()+"}");
				}
				
			} else {
				// No input, just return the list of monitors
				sb.append("{\"count\":" + Registry.getList().size() + "}");
			}
			
			response.getWriter().println(sb.toString());
			
		} catch (Throwable e) {
			response.setStatus(500);
			response.getWriter().println("{\"error\":true,\"msg\":\""+e.toString().replace("\n"," ").replace("\"","\\\"").trim()+"\"}");
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPut(HttpServletRequest, HttpServletResponse)
	 */
	protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doDelete(HttpServletRequest, HttpServletResponse)
	 */
	protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doHead(HttpServletRequest, HttpServletResponse)
	 */
	protected void doHead(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}