package org.cipres.webapp.guigen.servlet; import java.io.*; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.http.*; import org.cipres.guigen.*; import org.cipres.communication.Facilitator; import org.cipres.helpers.RegistryForApplet; import org.cipres.helpers.RegistryForAppletInitializer; import org.cipres.guigen.command.ServiceCommands; import org.cipres.CipresIDL.api1.CommandObject; import org.cipres.helpers.CommandObjectWrapper; import org.cipres.helpers.CommandObjectConvertor; /* Gets a ServiceCommands object, that's the result of running guigen, from the applet. Sends a string reply back to the applet, converts the ServiceCommands to a CipresIDL.api1.CommandObject that can be sent to a service, and writes the CommandObject out as an xml file named /data//parameters.xml. All hostnames of registry entries in parameters.xml have been replaced with the wildcard character "*" by RegistryForApplet. (I'm not sure if this was necessary since RunServiceApp can do the hostname globbing when it reads in the parameters.xml file). */ public class GuigenServlet2 extends HttpServlet { RegistryForApplet appletRegistry; // one time init happens here, before the servlet processes requests. public void init(ServletConfig config) throws ServletException { super.init(config); try { System.out.println("This is GuigenServlet2!"); System.out.flush(); Facilitator.initialize("GuigenServlet.servlet2"); } catch(Exception e) { throw new ServletException(e); } } public void doPost( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { InputStream in = null; ObjectInputStream inputFromApplet = null; try { response.setContentType("application/x-java-serialized-object"); in = request.getInputStream(); inputFromApplet = new ObjectInputStream(in); ServiceCommands serviceCommands = (ServiceCommands)inputFromApplet.readObject(); inputFromApplet.close(); HttpSession session = request.getSession(true); File parameterFile = saveParameterFile(serviceCommands, session); session.setAttribute("parameterFile", parameterFile.getAbsolutePath()); OutputStream outstr = response.getOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(outstr); oos.writeObject("All's well here in GuigenServlet2"); oos.writeObject(appletRegistry); oos.flush(); oos.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (inputFromApplet != null) { try { inputFromApplet.close(); } catch(Exception e) { } inputFromApplet = null; } } } File saveParameterFile(ServiceCommands serviceCommands, HttpSession session) throws Exception { CommandObject idlCmdObj = (CommandObject)(new CommandObjectConvertor()).convertServiceCommandsObj(serviceCommands); CommandObjectWrapper wrapper = new CommandObjectWrapper(idlCmdObj); ServletContext ctx = getServletContext(); String root = ctx.getRealPath("/"); File runDir = new File(root, "data/" + session.getId()); runDir.mkdir(); File parameterFile = new File(runDir, "parameters.xml"); //File.createTempFile("param", ".xml", new File(root + "data/")); System.out.println("Writing parameters to " + parameterFile.getAbsolutePath()); System.out.flush(); wrapper.asXml(parameterFile); return parameterFile; } }