Java tutorial
// Description: Java 8 XMsg Server Add Device Servlet /* * Code Factory Asterisk 11 Configuration Model * * Copyright (c) 2014-2015 Mark Sobkow * * This program is available as free software under the GNU GPL v3, or * under a commercial license from Mark Sobkow. For commercial licensing * details, please contact msobkow@sasktel.net. * * Under the terms of the GPL: * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ package net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskXMsgSrvWar; import java.io.*; import java.security.*; import java.util.*; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.output.WriterOutputStream; import net.sourceforge.msscodefactory.cflib.v2_3.CFLib.CFLib; import net.sourceforge.msscodefactory.cflib.v2_3.CFLib.ICFLibMessageLog; import net.sourceforge.msscodefactory.cflib.v2_3.CFLib.Tip.CFTipEnvelopeHandler; import net.sourceforge.msscodefactory.cflib.v2_3.CFLib.Tip.CFTipServerInfo; import net.sourceforge.msscodefactory.cfsecurity.v2_4.CFSecurity.*; import net.sourceforge.msscodefactory.cfinternet.v2_4.CFInternet.*; import net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsterisk.*; import net.sourceforge.msscodefactory.cfsecurity.v2_4.CFSecurityObj.*; import net.sourceforge.msscodefactory.cfinternet.v2_4.CFInternetObj.*; import net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskObj.*; import net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskXMsgRqstHandler.CFAsteriskXMsgRqstHandler; /** * Servlet implementation class CFAsteriskXMsgSrvWarRequestXml */ @WebServlet("/CFAsteriskXMsgSrvWarRequestXml") public class CFAsteriskXMsgSrvWarRequestXml extends HttpServlet { private static final long serialVersionUID = 1L; protected MessageLogHandler log = new MessageLogHandler(); protected PrintWriter printWriter = null; protected PrintStream printStream = null; protected int indent = 0; public class MessageLogHandler implements ICFLibMessageLog { public MessageLogHandler() { printWriter = null; printStream = null; indent = 0; } public void setPrintWriter(PrintWriter value) { if (value == null) { printWriter = null; printStream = null; indent = 0; } else { indent = 0; printWriter = value; WriterOutputStream writerOutputStream = new WriterOutputStream(printWriter); printStream = new PrintStream(writerOutputStream); } } public String getBacklog() { return (null); } public PrintStream getPrintStream() { if (printStream == null) { WriterOutputStream writerOutputStream = new WriterOutputStream(printWriter); printStream = new PrintStream(writerOutputStream); } return (printStream); } public int getMessageLogIndent() { return indent; } public void indent() { indent++; } public void dedent() { if (indent > 0) { indent--; } else { indent = 0; } } public void message(String msg) { if (msg == null) { return; } StringBuffer buff = new StringBuffer(); Calendar cal = Calendar.getInstance(); String stamp = String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%1$tL ", cal); buff.append(stamp); int i; for (i = 0; i < indent; i++) { buff.append("\t"); } buff.append(msg); if (!msg.endsWith("\n")) { buff.append("\n"); } if (printStream != null) { printStream.append(buff.toString()); printStream.flush(); } } public void openLogFile(String fileName) throws FileNotFoundException { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "openLogFile"); } public void closeLogFile() { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "openLogFile"); } } /** * @see HttpServlet#HttpServlet() */ public CFAsteriskXMsgSrvWarRequestXml() { super(); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { final String S_ProcName = "doGet"; ICFAsteriskSchemaObj schemaObj; HttpSession sess = request.getSession(false); if (sess == null) { sess = request.getSession(true); schemaObj = new CFAsteriskSchemaPooledObj(); sess.setAttribute("SchemaObj", schemaObj); } else { schemaObj = (ICFAsteriskSchemaObj) sess.getAttribute("SchemaObj"); if (schemaObj == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "SchemaObj"); } } CFTipServerInfo serverInfo = CFAsteriskXMsgSrvWarApplicationListener.getServerInfo(); CFTipEnvelopeHandler envelopeHandler = (CFTipEnvelopeHandler) sess.getAttribute("CFTipEnvelopeHandler"); if (envelopeHandler == null) { envelopeHandler = new CFTipEnvelopeHandler(); envelopeHandler.setLog(log); CFAsteriskXMsgRqstHandler requestHandler = new CFAsteriskXMsgRqstHandler(); requestHandler.setSchemaObj(schemaObj); envelopeHandler.setRequestHandler(requestHandler); envelopeHandler.setServerInfo(serverInfo); sess.setAttribute("CFTipEnvelopeHandler", envelopeHandler); } ICFAsteriskSchema dbSchema = null; try { dbSchema = (ICFAsteriskSchema) CFAsteriskSchemaPool.getSchemaPool().getInstance(); schemaObj.setBackingStore(dbSchema); schemaObj.beginTransaction(); ICFSecuritySysClusterObj sysCluster = schemaObj.getSysClusterTableObj().readSysClusterByIdIdx(1, false); if (sysCluster == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "SysCluster"); } ICFSecurityClusterObj secCluster = sysCluster.getRequiredContainerCluster(); if (secCluster == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "SysCluster.Cluster"); } if (null == serverInfo.getClusterDescr()) { serverInfo.setClusterDescr(secCluster.getRequiredDescription()); serverInfo.setClusterURL(secCluster.getRequiredFullDomainName()); } String clusterDescription = serverInfo.getClusterDescr(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">"); out.println("<HTML>"); out.println("<BODY>"); out.println("<form method=\"post\" formaction=\"CFAsteriskXMsgSrvWarRequestXml\">"); out.println("<H1 style=\"text-align:center\">" + clusterDescription + " XMsg Server</H1>"); out.println( "<H2 style=\"text-align:center\"> Enter a CFTipEnvelope XML request message to process</H2>"); out.println("<p>"); out.println("<table style=\"width:90%\">"); out.println( "<tr><th style=\"text-align:left\">Message Body:</th><td><textarea name=\"MessageBody\" cols=\"60\" rows=\"10\"></textarea></td></tr>"); out.println("</table>"); out.println( "<p style=\"text-align:center\"><button type=\"submit\" name=\"Ok\"\">Submit Request</button> <button type=\"button\" name=\"Cancel\"\" onclick=\"window.location.href='CFAsteriskXMsgSrvWarRequestXml'\">Cancel;</button>"); out.println("</form>"); out.println("</BODY>"); out.println("</HTML>"); } finally { if (dbSchema != null) { try { if (schemaObj.isTransactionOpen()) { schemaObj.rollback(); } schemaObj.minimizeMemory(); } catch (RuntimeException e) { } schemaObj.setBackingStore(null); CFAsteriskSchemaPool.getSchemaPool().releaseInstance(dbSchema); } } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { final String S_ProcName = "doPost"; ICFAsteriskSchemaObj schemaObj; HttpSession sess = request.getSession(false); if (sess == null) { sess = request.getSession(true); schemaObj = new CFAsteriskSchemaPooledObj(); sess.setAttribute("SchemaObj", schemaObj); } else { schemaObj = (ICFAsteriskSchemaObj) sess.getAttribute("SchemaObj"); if (schemaObj == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "SchemaObj"); } } CFTipServerInfo serverInfo = CFAsteriskXMsgSrvWarApplicationListener.getServerInfo(); CFTipEnvelopeHandler envelopeHandler = (CFTipEnvelopeHandler) sess.getAttribute("CFTipEnvelopeHandler"); if (envelopeHandler == null) { envelopeHandler = new CFTipEnvelopeHandler(); envelopeHandler.setLog(log); CFAsteriskXMsgRqstHandler requestHandler = new CFAsteriskXMsgRqstHandler(); requestHandler.setSchemaObj(schemaObj); envelopeHandler.setRequestHandler(requestHandler); envelopeHandler.setServerInfo(serverInfo); sess.setAttribute("CFTipEnvelopeHandler", envelopeHandler); } String clusterDescription = ""; ICFAsteriskSchema dbSchema = null; try { dbSchema = (ICFAsteriskSchema) CFAsteriskSchemaPool.getSchemaPool().getInstance(); schemaObj.setBackingStore(dbSchema); schemaObj.beginTransaction(); ICFSecuritySysClusterObj sysCluster = schemaObj.getSysClusterTableObj().readSysClusterByIdIdx(1, false); if (sysCluster == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "SysCluster"); } ICFSecurityClusterObj secCluster = sysCluster.getRequiredContainerCluster(); if (secCluster == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "SysCluster.Cluster"); } if (serverInfo.getClusterDescr() == null) { serverInfo.setClusterDescr(secCluster.getRequiredDescription()); serverInfo.setClusterURL(secCluster.getRequiredFullDomainName()); } clusterDescription = serverInfo.getClusterDescr(); // envelopeHandler.setLog( log ); String messageBody = request.getParameter("MessageBody"); if ((messageBody == null) || (messageBody.length() <= 0)) { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">"); out.println("<HTML>"); out.println("<BODY>"); out.println("<form method=\"post\" formaction=\"CFAsteriskXMsgSrvWarRequestXml\">"); out.println("<H1 style=\"text-align:center\">" + clusterDescription + " XMsg Server</H1>"); out.println( "<H2 style=\"text-align:center\"> Enter a CFTipEnvelope XML request message to process</H2>"); out.println("<p>"); out.println("<table style=\"width:90%\">"); out.println( "<tr><th style=\"text-align:left\">Message Body:</th><td><textarea name=\"MessageBody\" cols=\"60\" rows=\"10\"></textarea></td></tr>"); out.println("</table>"); out.println( "<p style=\"text-align:center\"><button type=\"submit\" name=\"Ok\"\">Submit Request</button> <button type=\"button\" name=\"Cancel\"\" onclick=\"window.location.href='CFAsteriskXMsgSrvWarRequestXml'\">Cancel;</button>"); out.println("</form>"); out.println("</BODY>"); out.println("</HTML>"); return; } PrintWriter out = response.getWriter(); log.setPrintWriter(out); envelopeHandler.parseStringContents(messageBody); response.setContentType("text/plain"); String rspnBase64EncodedEncryptedSessionBlock = envelopeHandler.getResponse(); out.append(rspnBase64EncodedEncryptedSessionBlock); out.flush(); } finally { if (dbSchema != null) { try { if (schemaObj.isTransactionOpen()) { schemaObj.rollback(); } schemaObj.minimizeMemory(); } catch (RuntimeException e) { } schemaObj.setBackingStore(null); CFAsteriskSchemaPool.getSchemaPool().releaseInstance(dbSchema); } } } }