JavaRESTClient_ServiceMonitor

SheetsterRESTClient.java
package JavaRESTClient_ServiceMonitor;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.zip.GZIPInputStream;
import com.extentech.ExtenXLS.ExcelTools;
import com.extentech.toolkit.Logger;
/**
 * Helper class that encapsulates REST api calls to DocsFree Sheetster which provides for simplified REST handling
 * 
 * @author john :: Sep 17, 2011 :: Copyright &copy;2011 <a href = "http://www.extentech.com">Extentech Inc.</a>
 *
 */
public class SheetsterRESTClient implements Runnable {
	// change this to the ID of the report template
	private static final int REPORT_ID = 329;

	/*
	 * NOTE: set the following credentials:
	 */
	private String username = "", password = "",
			serveraddress = "http://127.0.0.1:8000", sessionAuthTicket = null,
			memeId;
	
	String portalSheetname = "Sheet1", errorSheetname = "Sheet1", dataSheetName="Sheet1";
	private boolean debug = true;
	/**
	 * @param debug
	 *            The debug to set.
	 */
	public void setDebug(boolean debug) {
		this.debug = debug;
	}

	/**
	 * @return Returns the memeId.
	 */
	public String getMemeId() {
		return memeId;
	}
	/**
	 * @param memeId
	 *            The memeId to set.
	 */
	public void setMemeId(String memeId) {
		this.memeId = memeId;
	}
	/**
	 * @return Returns the username.
	 */
	public String getUsername() {
		return username;
	}
	/**
	 * @param username
	 *            The username to set.
	 */
	public void setUsername(String u) {
		username = u;
	}
	/**
	 * @return Returns the password.
	 */
	public String getPassword() {
		return password;
	}
	/**
	 * @param password
	 *            The password to set.
	 */
	public void setPassword(String p) {
		password = p;
	}
	/**
	 * @return Returns the serveraddress.
	 */
	public String getServeraddress() {
		return serveraddress;
	}
	/**
	 * @param serveraddress
	 *            The serveraddress to set.
	 */
	public void setServeraddress(String s) {
		serveraddress = s;
	}
	
	/**
	 * Open a browser window and loads the current document
	 */
	public void launchBrowser(){
		String openstr = serveraddress + "/index.jsp?meme_id=" + memeId+"&sessionAuthTicket=" + sessionAuthTicket;
		launchBrowser(openstr);
		
	}
	/**
	 * launches native browser
	 * 
	 * @param cmd
	 */
	public static void launchBrowser(String cmd) {
		try {
			edu.stanford.cs.ejalbert.BrowserLauncher.openURL(cmd);
		} catch (Exception e) {
			Logger.logErr("WARNING: ServerControl.launchBrowser failed: " + e);
		}
	}
	/**
	 * run through the tests
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			String sid = null;
			SheetsterRESTClient test3 = new SheetsterRESTClient();
			test3.run();
			String openstr = test3.serveraddress
					+ "/index.jsp?sessionAuthTicket=" + test3.sessionAuthTicket
					+ "&meme_id=" + test3.memeId;
			Logger.logInfo("Opening: " + openstr);
			launchBrowser(openstr);
			for (int t = 0; t < 10; t++) {
				SheetsterRESTClient test = new SheetsterRESTClient();
				Thread tx = new Thread(test);
				tx.start();
				// cache session id
				if (sid == null)
					sid = test.session;
				else
					test.session = sid;
				try {
					long sleeps = Math.round(Math.random() * 3000);
					Logger.logInfo("New Session in :" + (float) sleeps / 1000
							+ " seconds.");
					Thread.sleep(sleeps);
				} catch (Exception e) {
					;
				}
			}
		} catch (Exception e) {
			Logger.logErr("SheetsterRESTClient failed" + e.toString());
		}
	}
	/**
	 * threaded runner allows stress testing
	 * 
	 * @see java.lang.Runnable#run()
	 */
	public void run() {
		try {
			// authenticate and create a new workbook
			createDocumentFromURL(serveraddress, username,password);
			// test a variety of REST commands
			testRESTapi(serveraddress);
		} catch (Exception e) {
			Logger.logErr("SheetsterRESTClient failed: " + e.toString());
		}
	}
	/**
	 * runs a suite of JSON/XML assertions against a running Server
	 * 
	 * optional load testing
	 * 
	 * @param srvurl
	 * @param testload
	 */
	public void testRESTapi(String srvurl) throws Exception {
		if (srvurl == null)
			srvurl = "http://127.0.0.1:8000"; // default to 127.0.0.1
		// create new document on server, return document ID String
		createDocumentFromURL(srvurl, username, password);
		String[] reqs = {
				// the usual init steps...
				// memeId + "/xml/workbook/copy/0", // test copy existing
				memeId + "/json/workbook/getsheetnames/",
				memeId + "/json/row/getheader/Sheet1/",
				memeId + "/json/style/getall/",
				memeId + "/json/namedrange/getallranges/",
				memeId + "/json/sheet/get/Sheet1/",
				memeId + "/json/chart/getcharts/Sheet1/",
				// memeId + "/json/cellbinder/getboundranges/Sheet1/",
				memeId + "/json/workbook/getrestlog/0",
				// insert a new sheet named "New Sheet" at index 0
				memeId + "/xml/sheet/add/New Sheet/0",
				// insert a new cell at "New Sheet!A1" with the initial value of
				// "Hello World!"
				memeId + "/xml/cell/get/New Sheet!A1/",
				// insert a new cell at "New Sheet!A2" with the initial value of
				// "=SUM(10+11)"
				memeId + "/json/cell/add/New%20Sheet!A2/=SUM(10+11)",
				// create a named range
				memeId + "/json/namedrange/createnamedrange/New Sheet/A1:A2?rangeName=input_1",
				
				// access a named range
				memeId + "/json/namedrange/getcellvalues/input_1",
				
				// runs the cellbinder to merge data with loaded workbook
				// memeId + "/json/cellbinder/bind/1314?year=2008",
				// gets a whole row of JavaScript Cell Objects from row 1:
				memeId + "/json/cellrange/get/Sheet1!A1:VU1",
				memeId + "/json/sheet/get/Sheet1/",
				memeId + "/json/cell/add/Sheet1!B3/%3Dsum(b2%2B100)",
				memeId + "/json/messaging/getmessages/",
				memeId + "/json/workbook/getsheetnames/",
				memeId + "/json/style/getall/",
				memeId + "/json/col/resize/Sheet1!A/324",
				// some more advanced stuff...
				memeId + "/json/col/resize/Sheet1!A/324",
				// rename this doc
				memeId + "/xml/workbook/setname/workbook/new name",
				memeId + "/json/col/resize/Sheet1!A/324",
				memeId + "/xml/cell/add/Sheet1!c4/5000",
				memeId + "/xml/cell/add/Sheet1!c5/1500",
				memeId + "/xml/cell/add/Sheet1!C6/=sum(c4+c5)",
		// cleanup
		// memeId + "/json/workbook/delete"
		};
		// convert human-readable commands to REST urls
		for (int t = 0; t < reqs.length; t++) {
			try {
				// TODO: implement assertions
				String svx = srvurl + "/workbook/id/" + reqs[t];
				if (debug)
					Logger.logInfo("TESTING: "+ com.extentech.ExtenXLS.plugin.builtin.REST2English.getTranslated(svx));
				if (debug)
					Logger.logInfo("COMMAND: " + svx);
				if (debug)
					Logger.logInfo("RESULT FROM SERVER: " + getHTTPData(svx)
							+ "\r\n");
			} catch (Exception ex) {
				Logger.logErr("SheetsterRESTClient testServer failed: "
						+ ex.toString());
			}
		}
	}
	
	/**
	 * create a WebWorkBook
	 * 
	 * @return
	 * @throws Exception
	 */
	public void createDocument() throws Exception {
		createDocumentFromURL(serveraddress, username, password);
	}
	/**
	 * create a WebWorkBook
	 * 
	 * @return
	 * @throws Exception
	 */
	public String destroyDocument() throws Exception {
		
		// cleanup
		String delete = "/workbook/id/" +memeId + "/json/workbook/delete";
		return getHTTPData(delete);
	}
	
	/**
	 * Creating the document from a URL
	 * 
	 * 
	 * Sep 13, 2011
	 * 
	 * @param srvurl
	 * @param username
	 * @param password
	 * @return
	 * @throws IOException
	 * @throws MalformedURLException
	 */
	public void createDocumentFromURL(String srvurl, String username,
			String password) throws Exception {
		authenticate();
		// create new workbook -- do the usual init steps
		String new_book_url = srvurl + "/workbook/id/-1/csv/workbook/getid";
		// copy existing template that you have already uploaded
		//String new_book_url = srvurl + "/workbook/id/"+REPORT_ID+"/csv/workbook/copy";
		
		memeId = new String(getHTTPBytes(new_book_url));
	}
	/**
	 * Logs in the user with the credentials set on this object
	 * 
	 */
	public void authenticate() throws Exception {
		if (session == null) {
			// authenticate the user
			String login_url = serveraddress
					+ "/workbook/id/-2/txt/system/login/?username=" + username
					+ "&password=" + password;
			sessionAuthTicket = new String(getHTTPBytes(login_url));
			if (debug)
				Logger
						.logInfo("SheetsterRESTClient successfully logged in user got session auth ticket: "
								+ sessionAuthTicket);
		}
	}
	String session = null;

	/**
	 * fetch the contents of a URL as a buest-guess Java data type...
	 * 
	 * @param urlstr
	 * @return Object value returned from Server
	 * @throws IOException
	 * @throws MalformedURLException
	 */
	public Object getHTTPObject(String urlstr) throws Exception {
		// Create a URL object from urlString
		String ret = new String(getHTTPBytes(urlstr));
		// try numbers
		try{
			Double d = Double.parseDouble(ret);
			return d;
		}catch(NumberFormatException de){
			try{
				Float f = Float.parseFloat(ret);
				return f;
			}catch(NumberFormatException fe){
				try{
					Integer i  = Integer.parseInt(ret);
					return i;
				}catch(NumberFormatException ie){
					// ok not a number
				}
			}
		}
		// try boolean
		if(ret.equalsIgnoreCase("true")){
			return new Boolean(true);
		}else if(ret.equalsIgnoreCase("false")){
			return new Boolean(false);
		}
		
		//finally returns string val
		return ret;
		
	}
	
	/**
	 * fetch the contents of a URL as a string...
	 * 
	 * @param urlstr
	 * @return String value returned from Server
	 * @throws IOException
	 * @throws MalformedURLException
	 */
	public String getHTTPData(String urlstr) throws Exception {
		// Create a URL object from urlString
		String strx = new String(getHTTPBytes(urlstr));
		// Logger.logInfo(urlstr + ":" + strx);
		return strx; 
	}
	/**
	 * fetch the contents of a URL as a byte array
	 * 
	 * @param urlstr
	 * @return byte values returned from server
	 * @throws IOException
	 * @throws MalformedURLException
	 */
	public byte[] getHTTPBytes(String urlstr) throws Exception {
		if(!urlstr.startsWith(serveraddress)){
			urlstr = serveraddress + urlstr;
		}
		
		// Create a URL object from urlString
		URL pageURL = new URL(urlstr);
		// Open a connection to the URL
		URLConnection pageConnection;
		pageConnection = pageURL.openConnection();
		if (session != null)
			pageConnection.addRequestProperty("Cookie", session);
		else
			Logger.logWarn("Getting Valid Session");
		
		// TODO: Use Base64 Security over HTTPS
		pageConnection.setConnectTimeout(10000);
		// Get the InputStream from the URL connection
		InputStream webPageInputStream;
		pageConnection.connect();
		webPageInputStream = pageConnection.getInputStream();
		// handle the InputStream encoding (for gzip output)
		String enc = pageConnection.getContentEncoding();
		try {
			if (enc.equalsIgnoreCase("gzip"))
				webPageInputStream = new GZIPInputStream(pageConnection
						.getInputStream());
		} catch (NullPointerException e) {
			;// normal
		}
		// Read the web page via the InputStream
		ByteArrayOutputStream webPageData = new ByteArrayOutputStream();
		int totalBytesRead = 0;
		boolean moreToRead = true;
		byte[] readBuf = new byte[2048]; // Read the web page in 4K chunks
		while (moreToRead) {
			int numBytesRead = 0;
			try {
				numBytesRead = webPageInputStream.read(readBuf);
			} catch (IOException e) {
				moreToRead = false;
			}
			if (numBytesRead > 0) {
				totalBytesRead += numBytesRead;
				webPageData.write(readBuf, 0, numBytesRead);
			} else
				moreToRead = false;
		}
		// Logger.logInfo("Communicator read: " + totalBytesRead +
		// " bytes from:" + urlstr);
		webPageInputStream.close();
		// webPageData.setLength(totalBytesRead);
		// Pull the session ID out of the connection headers
		if (session == null)
			try {
				session = pageConnection.getHeaderField("Set-Cookie");
				if (session == null)
					throw new Exception("session cookie not present");
				if (!session.startsWith("sessionid="))
					throw new Exception("invalid session cookie");
				// Strip off any extraneous header fields
				if (session.contains(";"))
					session = session.substring(0, session.indexOf(";"));
			} catch (Exception e) {
				throw new RuntimeException("failed to parse session cookie", e);
			}
		// 
		return webPageData.toByteArray();
	}
	/**
	 * @param celladdr
	 * @param val
	 * @throws Exception
	 */
	public void addCell(String celladdr, String sn, Object val)
	throws Exception{
		String cellval = "/workbook/id/" + getMemeId()+"/json/cell/add/"+sn+"!"; // always use Sheet1 
	
		cellval += celladdr;
		cellval += "/";
		cellval += val.toString();
		getHTTPData(cellval);
	}
	
	/**
	 * bulk sets a rows of data into sheet
	 * 
	 * @param objarr
	 */
	public void setDataRow(Object[][] objarr, String sn, int row)
	throws Exception{
		for(int t=0;t<objarr.length;t++){ // iterate rows
			Object[] bo = objarr[t];
			for(int x=0;x<bo.length;x++){ // put together
				String celladdr = ExcelTools.getAlphaVal(x);
				celladdr += (t+row);
				addCell(celladdr, sn, bo[x]);;
			}
		}
		// saveSheet();
	}
	
	int lastInsertRow = 1;
	/**
	 * bulk inserts rows of data into sheet
	 * 
	 * @param objarr
	 */
	public void addData(Object[][] objarr, String sn)
	throws Exception{
		for(int t=0;t<objarr.length;t++){ // iterate rows
			Object[] bo = objarr[t];
			for(int x=0;x<bo.length;x++){ // put together
				String celladdr = ExcelTools.getAlphaVal(x);
				celladdr += (t+lastInsertRow);
				addCell(celladdr, sn, bo[x]);;
			}
			lastInsertRow++;
		}
		// saveSheet();
	}
	/**
	 * 
	 * http://127.0.0.1:8000/workbook/id/133071/json/namedrange/createnamedrange/Sheet1/A5:C6?rangeName=test
	 * 
	 * @param rangename
	 * @param rangeaddr
	 * @throws Exception
	 */
	public void addName(String rangename, String rangeaddr)
	throws Exception{
		String cmdx = "/workbook/id/" + getMemeId()+"/json/namedrange/createnamedrange/"+portalSheetname+"/"; // always use Sheet1 
		cmdx += rangeaddr;
		cmdx += "?";
		cmdx +="rangename="+rangename;
		getHTTPData(cmdx);
	}
	/**
	 * save the workbook
	 * 
	 * @throws Exception
	 */
	public void saveSheet()
	throws Exception{
		String sv = "/workbook/id/" + getMemeId()+"/csv/workbook/save/0"; 
		getHTTPData(sv);
	}
	
	/**
	 * http://127.0.0.1:8000/workbook/id/133071/json/sheet/rename/Sheet1/Sheet1x
	 * 
	 * @param sheet
	 * @param newname
	 */
	public void renameSheet(String sheet, String newname)
	throws Exception{
		String rename = "/workbook/id/" + getMemeId() + "/json/sheet/rename/"+sheet+"/"+newname;
		if(sheet.equals(portalSheetname))
				portalSheetname = newname;
		else if(sheet.equals(errorSheetname))
			errorSheetname = newname;
		
		getHTTPData(rename);
	}
	
	/**
	 *	http://127.0.0.1:8000/workbook/id/133071/xml/workbook/setname/0/test
	 * 
	 * @param sheet
	 * @param newname
	 */
	public void renameWorkBook(String newname)
	throws Exception{
		String rename = "/workbook/id/" + getMemeId() + "/xml/workbook/setname/0/"+newname;
		getHTTPData(rename);
	}
	
}

SheetsterServiceMonitor.java
package JavaRESTClient_ServiceMonitor;
/** 
 * SheetsterServiceMonitor.java
 *
 * ##LICENSE##
 *
 * Sep 17, 2011
 *
 * @author john
 * 
 */

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.Hashtable;
import java.util.Map;
import com.extentech.ExtenXLS.ExcelTools;
import com.extentech.toolkit.Logger;
/**
 * SheetsterServiceMonitor is a sample REST Client for DocsFree Sheetster Server demonstrating
 * creation and execution of a service monitor which records results to a Sheetster Portal.
 * 
 * This demonstrates Java using the REST api.
 *  
 * @author John McMahon :: 1/11/2011 :: Copyright &copy;2011 <a href = "http://www.extentech.com">Extentech Inc.</a>
 *
 */
public class SheetsterServiceMonitor extends SheetsterRESTClient{
	private String portalSheetname = "Sheet1", errorSheetname = "Sheet2";
	private String watchedServerAddress;
	
	// TODO: set reporting credentials
	private static String reportingServerAddress = "http://127.0.0.1:8000",	
		reportingServerUserName = "admin@acme.com",	
		reportingServerPassword = "T1TAN1UM",
		
		// FYI: mail server configured through resources/jsmtp.server.properties
		mailServerUserName = "admin@acme.com",	
		mailServerPassword = "T1TAN1UM";
	
	private int watchedServerPort = 80;
	private int pulse = 1000 * 60 * 10; // every 10 minutes
	private int lastInsertRow = 4;
	/**
	 * @return Returns the watchedServerPort.
	 */
	public int getWatchedServerPort() {
		return watchedServerPort;
	}
	/**
	 * @param watchedServerPort The watchedServerPort to set.
	 */
	public void setWatchedServerPort(int watchedServerPort) {
		this.watchedServerPort = watchedServerPort;
	}
	/**
	 * Test a basic service monitoring
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		
		String[] monitored = {
				"http://extentech.com/workbook/id/117921/html/sheet/get/Sheet1/excel_2_html.xsl",
				"http://www.sheetster.com/workbook/id/66361/html/sheet/get/Printer_Troubleshooter/excel_2_html.xsl"
			};
		
		for(String msv : monitored){
			
			// create in-memory Spreadsheet for calculation
			SheetsterServiceMonitor cmd = new SheetsterServiceMonitor();
			
			// These credentials are for the reporting server -- NOT the monitored server.
			// For obvious reasons, the monitoring and reporting server should not be the same.
			cmd.setServeraddress(reportingServerAddress);
			cmd.setUsername(reportingServerUserName);
			cmd.setPassword(reportingServerPassword);
	
			// pick a fast public doc that should come up cleanly
			cmd.setWatchedServerAddress(msv);
			
			Thread runit = new Thread(cmd);
			//runit.setPriority(Thread.MAX_PRIORITY);
			//runit.setDaemon(true);
			try{
				runit.start();
				Thread.sleep(10000); // stagger start time for robustica
			}catch(Exception e){ // oh yes it will run
				Logger.logErr("Problem starting SheetsterServiceMonitor: "+ cmd.getWatchedServerAddress(),e);
			}
			
		}
	}
	
	/**
	 * threaded runner allows stress testing
	 * 
	 * @see java.lang.Runnable#run()
	 */
	public void run() {
		try {
			// login the user session
			authenticate();
			
			// create a new workbook
			// TODO: Load existing template
			createDocument();
			String smpl = this.getWatchedServerAddress().substring(7);
			try{
				smpl = smpl.substring(0,smpl.indexOf("/"));
			}catch(Exception e){;}
			
			String workbookname = "Service Monitoring: " + smpl;
			renameWorkBook(workbookname);
			
			//renameSheet("Sheet1","Service Portal");
			//renameSheet("Sheet2","Monitor_Data");
			
			// create the headers for report
			
			// add the named range
			addName("elapsed_time",portalSheetname + "!A8:C8");
			
			Object[][] data =  {
					{"Service Monitor Report", this.getWatchedServerAddress(), 	new Date(System.currentTimeMillis()).toString()},
					{"Average / Max Response Time",	"=AVERAGE(elapsed_time)", "=MAX(elapsed_time)"},
					{"Connected OK","Connect Elapsed Time","Timestamp"}
				};
			addData(data, portalSheetname);
			
			// begin monitoring 
			watch();
			
			// clean it up
			// destroyDocument();
			
			// let's take a look at the report
			// launchBrowser();
		} catch (Exception e) {
			Logger.logErr("SheetsterServiceMonitor failed: " + e.toString());
		}
	}
	
	/**
	 * bulk inserts rows of data into sheet
	 * 
	 * In order to leave room for the business logic calculations this 
	 * Adds cells starting at A3
	 * 
	 * @param objarr
	 */
	public void addData(Object[][] objarr, String sn)
	throws Exception{
		for(int t=0;t<objarr.length;t++){ // iterate rows
			Object[] bo = objarr[t];
			for(int x=0;x<bo.length;x++){ // put together
				String celladdr = ExcelTools.getAlphaVal(x);
				celladdr += (t+lastInsertRow);
				addCell(celladdr, sn, bo[x]);;
			}
			lastInsertRow++;
		}
		saveSheet();
	}
	
	
	/**
	 * http://127.0.0.1:8000/workbook/id/133071/json/sheet/rename/Sheet1/Sheet1x
	 * 
	 * @param sheet
	 * @param newname
	 */
	public void renameSheet(String sheet, String newname)
	throws Exception{
		String rename = "/workbook/id/" + super.getMemeId() + "/json/sheet/rename/"+sheet+"/"+newname;
		if(sheet.equals(portalSheetname))
				portalSheetname = newname;
		else if(sheet.equals(errorSheetname))
			errorSheetname = newname;
		
		getHTTPData(rename);
	}
	
	/**
	 *	http://127.0.0.1:8000/workbook/id/133071/xml/workbook/setname/0/test
	 * 
	 * @param sheet
	 * @param newname
	 */
	public void renameWorkBook(String newname)
	throws Exception{
		String rename = "/workbook/id/" + super.getMemeId() + "/xml/workbook/setname/0/"+newname;
		getHTTPData(rename);
	}
		
	
	/**
	 * adds a named business rule (formula) to the sheet at A2
	 * 
	 * @param s
	 * @param fmla
	 */
	public void watch()
	throws Exception{
        boolean OK = true;
        String errorcode = "";
        while(true){
        	long elapsed = System.currentTimeMillis();
        	errorcode = "";
        	try{
        		OK = connectCheck(getWatchedServerAddress(), getWatchedServerPort());
        	}catch(Exception e){
        		OK = false;
        		errorcode = e.toString();
        	}
            elapsed = System.currentTimeMillis() - elapsed;
           
            // create the headers for report
			Object[][] data =  {{new Boolean(OK),new Long(elapsed), 
				new Date(System.currentTimeMillis()).toString(), 
				errorcode}};
			
			if(errorcode.equals(""))
				addData(data, portalSheetname);
			else{
				addData(data, errorSheetname);
				restartService();
			}
            // TODO: use this to relaunch from within Luminet
            // _launcher.restartServer(i);
            
            OK = false;
            Thread.sleep(pulse);
        }
	};
	
    /**
     * check that the server is up, register data in report
     * 
     * Jan 11, 2011
     * @param host
     * @param p
     * @return
     * @throws IOException 
     */
    private boolean connectCheck(String host,int p) throws IOException
    {
        OutputStream out = null;
    	String stat = getPage(host);
    	if(stat.indexOf("Exception")==-1){
    		return true;
    	}
        return false;
    }
	
    public static final String getPage(String urlString) throws IOException{
		// Create a URL object from urlString
		URL pageURL = new URL(urlString);
		// Open a connection to the URL
		URLConnection pageConnection;
		pageConnection = pageURL.openConnection();
		//pageConnection.setConnectTimeout(15000);
		
		// Get the InputStream from the URL connection
		InputStream webPageInputStream = null;
		String mtyp  = pageConnection.getContentType();
		
		pageConnection.connect();
		webPageInputStream = pageConnection.getInputStream();
		// handle gunzip
		if(mtyp.equalsIgnoreCase("text/json")){
			java.util.zip.GZIPInputStream giz = new java.util.zip.GZIPInputStream(webPageInputStream);
			webPageInputStream = giz;
		}
		
		// Read the web page via the InputStream
		StringBuffer webPageData = new StringBuffer();	
		int totalBytesRead = 0;
		boolean moreToRead = true;
		byte[] readBuf = new byte[8192]; // Read the web page in 8K chunks
		while (moreToRead){
			int numBytesRead = 0;
			try {
				numBytesRead = webPageInputStream.read(readBuf);
			}catch (IOException e){
				moreToRead = false;
			}
						
			if (numBytesRead > 0) {
				totalBytesRead += numBytesRead;
				webPageData.append(new String(readBuf, 0, numBytesRead));
			}else
				moreToRead = false;
		}
		// Logger.logInfo("Communicator read: " + totalBytesRead + " bytes from:" + urlString);
		webPageInputStream.close();
		webPageData.setLength(totalBytesRead);
		return webPageData.toString();
	}
	
	/**
	 * delete a worksheet
	 * 
	 * @param portalSheetname
	 * @throws Exception
	 */
	public void deleteSheet(String portalSheetname)
	throws Exception{
		String delete = "/workbook/id/" + super.getMemeId() + "/json/sheet/delete/"+portalSheetname+"/0";
		getHTTPData(delete);
	}
	/**
	 * 
	 * http://127.0.0.1:8000/workbook/id/133071/json/namedrange/createnamedrange/Sheet1/A5:C6?rangeName=test
	 * 
	 * @param rangename
	 * @param rangeaddr
	 * @throws Exception
	 */
	public void addName(String rangename, String rangeaddr)
	throws Exception{
		String cmdx = "/workbook/id/" + getMemeId()+"/json/namedrange/createnamedrange/"+portalSheetname+"/"; // always use Sheet1 
		cmdx += rangeaddr;
		cmdx += "?";
		cmdx +="rangename="+rangename;
		getHTTPData(cmdx);
	}
	/**
	 * save the workbook
	 * 
	 * @throws Exception
	 */
	public void saveSheet()
	throws Exception{
		String sv = "/workbook/id/" + getMemeId()+"/csv/workbook/save/0"; 
		getHTTPData(sv);
	}
	
	/**
	 * @param celladdr
	 * @param val
	 * @throws Exception
	 */
	public void addCell(String celladdr, String sn, Object val)
	throws Exception{
		String cellval = "/workbook/id/" + getMemeId()+"/json/cell/add/"+sn+"!"; // always use Sheet1 
		cellval += celladdr;
		cellval += "/";
		cellval += val.toString();
		getHTTPData(cellval);
	}
	
	/**
	 * @param celladdr
	 * @throws Exception
	 */
	public Object getCell(String celladdr)
	throws Exception{
		String getcmd = "/workbook/id/" + getMemeId()+"/txt/cell/get/"+portalSheetname+"!"; // always use Sheet1 
		String cellval = getcmd;
		cellval += celladdr;
		cellval += "/0";
		
		return getHTTPObject(cellval);
	}
	
	
	/**
	 * @return Returns the watchedServerAddress.
	 */
	public String getWatchedServerAddress() {
		return watchedServerAddress;
	}

	/**
	 * @param watchedServerAddress The watchedServerAddress to set.
	 */
	public void setWatchedServerAddress(String w) {
		this.watchedServerAddress = w;
	}
	
	/**
	 * Make whatever onerous native calls needed to make the server restart gracefully
	 * 
	 */
	private synchronized void restartService(){
		Logger.logWarn("Flatlined Restarting: " + this.watchedServerAddress);
		
		
		
		String mailsubj = "[SERVICE MON] RESTART alert: " + this.watchedServerAddress;
		String mailbody = "Service Monitor detected that the service at: " + this.watchedServerAddress + " has stopped responding and requires a restart.";
		String mailsender = this.watchedServerAddress + "@extentech.com";
		String mailrecip = "support@extentech.com";
		
		// send mail message to admin
		Map msg = new Hashtable();
		msg.put("subject",mailsubj);
		msg.put("sender",mailsubj);
		msg.put("recipient",mailrecip);
		msg.put("body",mailbody);
		try{
			this.mailer.setRelayAuthUser(mailServerUserName);
			this.mailer.setRelayAuthPassword(mailServerPassword);
			
			this.mailer.sendMessage( msg);
		}catch(Exception e){
			Logger.logErr("Sheetster Service Monitor watching " + this.watchedServerAddress + " could not send restart alert email.",e);
		}
		
		Object[][] data =  {
				{"SERVICE RESTART ATTEMPT", this.getWatchedServerAddress(), 	new Date(System.currentTimeMillis()).toString()}
			};
		try {
			addData(data, portalSheetname);
		} catch (Exception e) {
			Logger.logErr("Sheetster Service Monitor watching " + this.watchedServerAddress + " could not log restart event.",e);		
		}
		
		// do the evil native stuff
		try {
			Runtime.getRuntime().exec("cd /usr/local/sheetster/");
			Runtime.getRuntime().exec("killall java -9");
			Runtime.getRuntime().exec("");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
		}
		
	}

	com.extentech.jsmtp.jsmtpDaemon mailer = new com.extentech.jsmtp.jsmtpDaemon();
	
	
}