Update of /cvsroot/aimmath/AIM/WEB-INF/java
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14951/WEB-INF/java
Modified Files:
Tag: aim-xml
Alice.java Maple.java
Log Message:
Reorganization for web-services compatibility
Index: Alice.java
===================================================================
RCS file: /cvsroot/aimmath/AIM/WEB-INF/java/Alice.java,v
retrieving revision 1.8.4.1
retrieving revision 1.8.4.2
diff -C2 -d -r1.8.4.1 -r1.8.4.2
*** Alice.java 7 Oct 2004 10:55:52 -0000 1.8.4.1
--- Alice.java 14 Oct 2004 10:56:22 -0000 1.8.4.2
***************
*** 513,528 ****
throws ServletException, IOException
{
! // This is for output to be sent as our response.
! StringWriter webOutput_ = new StringWriter();
! PrintWriter webOutput = new PrintWriter(webOutput_);
!
! StringWriter msg_;
! PrintWriter msg;
!
! PrintWriter logWriter;
!
! Maple maple;
! String commandparam,initcommand,commandargs,page;
!
// logRequest(request);
--- 513,517 ----
throws ServletException, IOException
{
! String commandparam;
// logRequest(request);
***************
*** 537,549 ****
new AliceRequest(request,TempDir,MaxPostSize);
! commandparam = alicereq.getParameter("Command");
! if ((commandparam != null) &&
! commandparam.startsWith("Servlet")) {
! // this request is to be handled directly by the
! // servlet, not by Maple.
! doServletCommand(commandparam,request,response);
! return;
}
AliceLogEntry logentry = new AliceLogEntry(alicereq);
--- 526,567 ----
new AliceRequest(request,TempDir,MaxPostSize);
! PrintWriter out = response.getWriter();
! // out.println("Content type is " + alicereq.type);
! // out.close();
! // return;
! if (alicereq.isSOAP()) {
! doSOAP(alicereq,response);
! } else {
! commandparam = alicereq.getParameter("Command");
! if ((commandparam != null) &&
! commandparam.startsWith("Servlet")) {
! // this request is to be handled directly by the
! // servlet, not by Maple.
!
! doServletCommand(commandparam,request,response);
! } else {
! doMain(alicereq,response);
! }
}
+ }
+
+ public void doMain (AliceRequest alicereq,
+ HttpServletResponse response)
+ throws ServletException, IOException {
+
+
+ StringWriter webOutput_ = new StringWriter();
+ PrintWriter webOutput = new PrintWriter(webOutput_);
+ PrintWriter out = response.getWriter();
+
+ StringWriter msg_;
+ PrintWriter msg;
+
+ PrintWriter logWriter;
+
+ Maple maple;
+ String commandparam,initcommand,commandargs,page,tthOptions;
+
AliceLogEntry logentry = new AliceLogEntry(alicereq);
***************
*** 584,588 ****
writeLog("Command sent to maple process " +
! maple.id + ":\n" + "`aim/Main`(" + commandargs + "):\n\n");
if (LastRequestFile != null) {
--- 602,607 ----
writeLog("Command sent to maple process " +
! maple.id + ":\n" +
! "`aim/Main`(" + commandargs + "):\n\n");
if (LastRequestFile != null) {
***************
*** 600,641 ****
// execute command, sending output to the web page.
try {
!
! String tthOpt = "";
! //get user agent
! String browser = request.getHeader("User-Agent");
! if (browser != null) {
! browser = browser.toLowerCase();
! //System.out.println("Browser: " + browser);
!
! //this captures safari and real mozilla and netscape 6+ only
! //and not even Opera that will identify as Mozilla
! //add more if needed
! if (
! ((browser.indexOf("mozilla/5.0") >= 0) &&
! (browser.indexOf("gecko") >= 0))
! ||
! (browser.indexOf("safari") >= 0)
! ||
! (browser.indexOf("opera 7") >= 0)
! )
! {
! tthOpt = "-u";
! }
! }
!
! maple.exec("`aim/Main`(" + commandargs + "):\n", webOutput, alicereq.mathdisplay, tthOpt);
} catch(Exception e) {
logentry.close("ERROR");
maple.condemned = true;
maplePool.returnMaple(maple);
!
msg_ = new StringWriter();
msg = new PrintWriter(msg_);
!
msg.write("Error while executing Maple command:\n" +
"`aim/Main`(" + commandargs + "):\n-----------------\n" +
"Exception message:\n" + e.getMessage() + "\n\n" +
"Stack trace:\n");
!
e.printStackTrace(msg);
--- 619,636 ----
// execute command, sending output to the web page.
try {
! maple.exec("`aim/Main`(" + commandargs + "):\n", webOutput);
} catch(Exception e) {
logentry.close("ERROR");
maple.condemned = true;
maplePool.returnMaple(maple);
!
msg_ = new StringWriter();
msg = new PrintWriter(msg_);
!
msg.write("Error while executing Maple command:\n" +
"`aim/Main`(" + commandargs + "):\n-----------------\n" +
"Exception message:\n" + e.getMessage() + "\n\n" +
"Stack trace:\n");
!
e.printStackTrace(msg);
***************
*** 647,650 ****
--- 642,646 ----
page = webOutput_.toString();
boolean complete = true;
+ String responsetype = "text/html";
if (page.startsWith("ContentType=")) {
***************
*** 652,665 ****
int j = page.indexOf("\n");
if (j != -1) {
! response.setContentType(page.substring(i,j));
page = page.substring(j+1);
- } else {
- response.setContentType("text/html");
}
! } else {
if (page.indexOf("</html>") == -1) {
complete = false;
}
! response.setContentType("text/html");
}
--- 648,669 ----
int j = page.indexOf("\n");
if (j != -1) {
! responsetype = page.substring(i,j);
page = page.substring(j+1);
}
! }
!
! response.setContentType(responsetype);
!
! if (responsetype.equals("text/html")) {
if (page.indexOf("</html>") == -1) {
complete = false;
}
! tthOptions = (alicereq.useUnicode ? " -u " : "");
! try {
! page = tth.TtH.convert(page,tthOptions);
! } catch (Exception e) {
! writeSOAPFault(out,"Server.TtH","TtH error",alicereq.getServletPath(),e);
! return;
! }
}
***************
*** 676,679 ****
--- 680,783 ----
}
+ public void doSOAP (AliceRequest alicereq,
+ HttpServletResponse response)
+ throws ServletException, IOException {
+
+ PrintWriter out = response.getWriter();
+
+ StringWriter msg_;
+ PrintWriter msg;
+
+ PrintWriter logWriter;
+
+ Maple maple;
+ String commandparam,initcommand,commandargs,page,tthOptions;
+
+ AliceLogEntry logentry = new AliceLogEntry(alicereq);
+ aliceLog.addEntry(logentry);
+
+ // Build the command to send to Maple
+ commandargs = alicereq.mapleCommandArgs();
+
+ // Get a Maple process to execute the command
+ maple = null;
+
+ // out.println("DoSOAP");
+
+ try {
+ maple = maplePool.getMaple("(soap)");
+ } catch (Exception e) {
+ msg_ = new StringWriter();
+ msg = new PrintWriter(msg_);
+
+ msg.write("Fatal error getting new Maple process:\n" +
+ "Exception message:\n" + e.getMessage() + "\n\n" +
+ "Stack trace:\n");
+
+ e.printStackTrace(msg);
+
+ msg.write("\n\n");
+
+ throw(new UnavailableException(msg_.toString()));
+ }
+
+ if (maple == null) {
+ writeSOAPFault(out,"Server.busy","Server busy",alicereq.getServletPath());
+
+ logentry.close("SERVER BUSY");
+ return;
+ }
+
+ logentry.processID = maple.id;
+
+ initMaple(maple);
+
+ writeLog("SOAP request sent to maple process " + maple.id + "\n\n");
+
+ response.setContentType("application/soap+xml");
+ // execute command
+ try {
+ maple.exec("`aim/DoSOAP`(" + commandargs + "):\n",out);
+ } catch(Exception e) {
+ logentry.close("ERROR");
+ maple.condemned = true;
+ maplePool.returnMaple(maple);
+ writeSOAPFault(out,"Server","Server error",alicereq.getServletPath(),e);
+ return;
+ } finally {
+ alicereq.deleteFiles();
+ }
+ logentry.close();
+ maplePool.returnMaple(maple);
+ maplePool.clean(1000000,10000000);
+ }
+
+ private void writeSOAPFault(PrintWriter w,String faultcode,
+ String faultstring,String faultactor) {
+ writeSOAPFault(w,faultcode,faultstring,faultactor,"");
+ }
+
+ private void writeSOAPFault(PrintWriter w,String faultcode,
+ String faultstring, String faultactor, Exception e) {
+ writeSOAPFault(w,faultcode,faultstring,faultactor,e.getMessage());
+ }
+
+ private void writeSOAPFault(PrintWriter w,String faultcode,
+ String faultstring, String faultactor, String detail) {
+ w.println("<soap:Envelope ");
+ w.println(" xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'");
+ w.println(" soap:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>");
+ w.println(" <soap:Body>");
+ w.println(" <soap:Fault>");
+ w.println(" <faultcode>soap:" + faultcode + "</faultcode>");
+ w.println(" <faultstring>" + faultstring + "</faultstring>");
+ w.println(" <faultactor>" + faultactor + "</faultactor>");
+ if (detail != null && ! detail.equals("")) {
+ w.println(" <detail>" + detail + "</detail>");
+ }
+ w.println(" </soap:Fault>");
+ w.println(" </soap:Body>");
+ w.println("</soap:Envelope>");
+ }
public void printBusyPage(HttpServletResponse response) {
***************
*** 924,929 ****
}
-
-
// This will be called when the web server shuts down.
public void destroy() {
--- 1028,1031 ----
***************
*** 938,944 ****
/**
* A utility class encoding a request that might or might not be
! * of content type multipart/form-data. Typically requests that
! * include a file upload will have this type, whereas requests
! * without file uploads will have type text/plain (?)
*/
--- 1040,1048 ----
/**
* A utility class encoding a request that might or might not be
! * of content type multipart/form-data or application/soap+xml.
! * Typically requests that include a file upload will have type
! * multipart/form-data, requests in SOAP format will have
! * type application/soap+xml, and other requests will have type
! * text/plain (?)
*/
***************
*** 946,950 ****
--- 1050,1059 ----
private HttpServletRequest request;
private MultipartRequest mprequest;
+ private String XMLtext;
private Vector parameterNames;
+ private String fulltype;
+ public String useragent;
+ public boolean useUnicode;
+ public String type;
public String studentID;
public String command;
***************
*** 961,965 ****
public AliceRequest(HttpServletRequest r,
String uploadDirectory,
! int maxPostSize) throws IOException {
int i;
Enumeration names;
--- 1070,1074 ----
public AliceRequest(HttpServletRequest r,
String uploadDirectory,
! int maxPostSize) throws IOException, ServletException {
int i;
Enumeration names;
***************
*** 967,983 ****
request = r;
! try {
! mprequest =
! new MultipartRequest(r,
! uploadDirectory,
! maxPostSize);
! } catch(IOException e) {
! if (e.getMessage() ==
! "Posted content type isn't multipart/form-data") {
! mprequest = null;
! } else {
e.fillInStackTrace();
throw(e);
}
}
--- 1076,1146 ----
request = r;
!
! useUnicode = false;
! String useragent = request.getHeader("User-Agent");
! if (useragent != null) {
! useragent = useragent.toLowerCase();
! //System.out.println("Useragent: " + useragent);
!
! //this captures safari and real mozilla and netscape 6+ only
! //and not even Opera that will identify as Mozilla
! //add more if needed
! if (
! ((useragent.indexOf("mozilla/5.0") >= 0) &&
! (useragent.indexOf("gecko") >= 0))
! ||
! (useragent.indexOf("safari") >= 0)
! ||
! (useragent.indexOf("opera 7") >= 0)
! )
! {
! useUnicode = true;
! }
! }
!
! fulltype = r.getContentType();
! if (fulltype == null) {
! fulltype = "";
! } else {
! fulltype = fulltype.toLowerCase();
! }
!
! if (fulltype.startsWith("application/soap+xml")) {
! type = "application/soap+xml";
! studentID = "(soap)";
! // To do: use the right encoding rather than the default
! BufferedReader in =
! new BufferedReader(new InputStreamReader(request.getInputStream()));
! String line;
! StringWriter xmlwriter = new StringWriter();
! try {
! while((line = in.readLine()) != null) {xmlwriter.write(line);}
! } catch(IOException e) {
! e.fillInStackTrace();
! throw new ServletException("Could not read request body\n",e);
! }
! XMLtext = xmlwriter.toString();
! return;
! }
!
! if (fulltype.startsWith("multipart/form-data")) {
! type = "multipart/form-data";
! try {
! mprequest =
! new MultipartRequest(r,
! uploadDirectory,
! maxPostSize);
! } catch(IOException e) {
e.fillInStackTrace();
throw(e);
}
+ } else if (fulltype.startsWith("text/plain")) {
+ type = "text/plain";
+ } else if (fulltype.equals("")) {
+ // fulltype = "text/plain";
+ fulltype = "empty";
+ } else {
+ // throw new ServletException("Unrecognised content type: " + fulltype);
+ fulltype = type;
}
***************
*** 1028,1032 ****
subject = r.getParameter("SubjectName");
quiz = r.getParameter("QuizName");
! mathdisplay = r.getParameter("MathDisplay");
} else {
studentID = mprequest.getParameter("StudentID");
--- 1191,1195 ----
subject = r.getParameter("SubjectName");
quiz = r.getParameter("QuizName");
! mathdisplay = r.getParameter("MathDisplay");
} else {
studentID = mprequest.getParameter("StudentID");
***************
*** 1034,1038 ****
subject = mprequest.getParameter("SubjectName");
quiz = mprequest.getParameter("QuizName");
! mathdisplay = mprequest.getParameter("MathDisplay");
}
--- 1197,1201 ----
subject = mprequest.getParameter("SubjectName");
quiz = mprequest.getParameter("QuizName");
! mathdisplay = mprequest.getParameter("MathDisplay");
}
***************
*** 1041,1048 ****
}
! if (studentID == null) { studentID = ""; }
! if (command == null) { command = ""; }
! if (subject == null) { subject = ""; }
! if (quiz == null) { quiz = ""; }
if (mathdisplay == null) { mathdisplay = "html"; }
}
--- 1204,1211 ----
}
! if (studentID == null) { studentID = ""; }
! if (command == null) { command = ""; }
! if (subject == null) { subject = ""; }
! if (quiz == null) { quiz = ""; }
if (mathdisplay == null) { mathdisplay = "html"; }
}
***************
*** 1059,1063 ****
public boolean isMultiPart() {
! return (mprequest != null);
}
--- 1222,1234 ----
public boolean isMultiPart() {
! return (type == "multipart/form-data");
! }
!
! public boolean isSOAP() {
! return (type == "application/soap+xml");
! }
!
! public String getServletPath() {
! return request.getServletPath();
}
***************
*** 1137,1178 ****
public String mapleCommandArgs() {
! StringWriter params = new StringWriter();
! Enumeration names = parameterNames.elements();
! String name,value;
! File serverfile;
! String serverfilename,originalfilename;
! while(names.hasMoreElements()) {
! name = (String)names.nextElement();
! value = getParameter(name);
! params.write(",\"" + name + "\"=");
! params.write(Maple.quote(value));
! }
! names = getFileNames();
! while(names.hasMoreElements()) {
! name = (String)names.nextElement();
! if ( getFile(name) != null &&
! (serverfilename = getFile(name).getAbsolutePath()) != null) {
! params.write(",\"" + name + "\"=" +
! Maple.quote(serverfilename));
}
! originalfilename = getOriginalFileName(name);
! if (originalfilename != null) {
! params.write(",\"" + name + "Original\"=" +
! Maple.quote(originalfilename));
}
}
- params.flush();
-
- String paramstring = params.toString();
- if (paramstring.startsWith(",")) {
- paramstring = paramstring.substring(1);
- }
-
- return paramstring;
}
}
--- 1308,1353 ----
public String mapleCommandArgs() {
! if (isSOAP()) {
! return(Maple.quote(XMLtext));
! } else {
! StringWriter params = new StringWriter();
! Enumeration names = parameterNames.elements();
! String name,value;
! File serverfile;
! String serverfilename,originalfilename;
! while(names.hasMoreElements()) {
! name = (String)names.nextElement();
! value = getParameter(name);
! params.write(",\"" + name + "\"=");
! params.write(Maple.quote(value));
! }
! names = getFileNames();
! while(names.hasMoreElements()) {
! name = (String)names.nextElement();
! if ( getFile(name) != null &&
! (serverfilename = getFile(name).getAbsolutePath()) != null) {
! params.write(",\"" + name + "\"=" +
! Maple.quote(serverfilename));
! }
! originalfilename = getOriginalFileName(name);
! if (originalfilename != null) {
! params.write(",\"" + name + "Original\"=" +
! Maple.quote(originalfilename));
! }
}
! params.flush();
!
! String paramstring = params.toString();
! if (paramstring.startsWith(",")) {
! paramstring = paramstring.substring(1);
}
+
+ return paramstring;
}
}
}
***************
*** 1190,1197 ****
public AliceLogEntry (AliceRequest a) {
! studentID = (a.studentID.length() == 0) ? " " : a.studentID;
! command = (a.command.length() == 0) ? " " : a.command;
! subject = (a.subject.length() == 0) ? " " : a.subject;
! quiz = (a.quiz.length() == 0) ? " " : a.quiz;
processID = -1;
startTime = new Date();
--- 1365,1372 ----
public AliceLogEntry (AliceRequest a) {
! studentID = (a.studentID == null || a.studentID.length() == 0) ? " " : a.studentID;
! command = (a.command == null || a.command.length() == 0) ? " " : a.command;
! subject = (a.subject == null || a.subject.length() == 0) ? " " : a.subject;
! quiz = (a.quiz == null || a.quiz.length() == 0) ? " " : a.quiz;
processID = -1;
startTime = new Date();
Index: Maple.java
===================================================================
RCS file: /cvsroot/aimmath/AIM/WEB-INF/java/Maple.java,v
retrieving revision 1.5.4.1
retrieving revision 1.5.4.2
diff -C2 -d -r1.5.4.1 -r1.5.4.2
*** Maple.java 7 Oct 2004 10:54:28 -0000 1.5.4.1
--- Maple.java 14 Oct 2004 10:56:22 -0000 1.5.4.2
***************
*** 138,155 ****
// The command need not have a semicolon or newline at the end
public synchronized void exec(String cmd) throws Exception {
! exec(cmd, null, "html", "");
! }
!
! // Execute a given command 'cmd' and write the results to 'out'.
! // The command need not have a semicolon or newline at the end
! // Default to converting the latex output with tth
! public synchronized void exec(String cmd, PrintWriter out) throws Exception {
! exec(cmd, null, "html", "");
}
// Execute a given command 'cmd' and write the results to 'out'.
// The command need not have a semicolon or newline at the end
! // Convert latex using the option given
! public synchronized void exec(String cmd, PrintWriter out, String mathdisplay, String tthOpt)
throws Exception {
--- 138,147 ----
// The command need not have a semicolon or newline at the end
public synchronized void exec(String cmd) throws Exception {
! exec(cmd, null);
}
// Execute a given command 'cmd' and write the results to 'out'.
// The command need not have a semicolon or newline at the end
! public synchronized void exec(String cmd, PrintWriter out)
throws Exception {
***************
*** 163,167 ****
// Tell Maple to print the terminator string, so we can see
// that it has finished.
! this.write( ("printf(\"\\n" + terminator + "\\n\"):\n"));
// Next, read the output, line-by-line, until terminator is read:
--- 155,159 ----
// Tell Maple to print the terminator string, so we can see
// that it has finished.
! this.write( "printf(\"\\n" + terminator + "\\n\"):\n" );
// Next, read the output, line-by-line, until terminator is read:
***************
*** 173,187 ****
}
! outstring.append(s);
! outstring.append("\n");
! }
!
! if (out != null) {
! if (mathdisplay.toLowerCase().equals("html")) // this don't work! How do we do string comparisons?
! out.print(tth.TtH.convert(outstring.toString(), tthOpt));
! else if (mathdisplay.toLowerCase().equals("latex"))
! out.print(outstring.toString());
! else // default to using tth
! out.print(tth.TtH.convert(outstring.toString(), tthOpt));
}
}
--- 165,169 ----
}
! if (out != null) { out.println(s); }
}
}
***************
*** 196,200 ****
// as 'use', so we save and restore the value of lastUseTime.
l = lastUseTime;
! exec("2+2",p,"html","");
lastUseTime = l;
return( s.toString().trim().equals("4") );
--- 178,182 ----
// as 'use', so we save and restore the value of lastUseTime.
l = lastUseTime;
! exec("2+2",p);
lastUseTime = l;
return( s.toString().trim().equals("4") );
***************
*** 204,220 ****
}
- // I don't know why the checkout() method is here - it is never invoked
- // public synchronized String checkout() {
- // StringWriter s = new StringWriter();
- // PrintWriter p = new PrintWriter(s);
-
- // try {
- // exec("2+2",p,"html","");
- // return( s.toString().trim() );
- // } catch (Exception e) {
- // return("Aargh!");
- // }
- // }
-
public static String quote(String s) {
if (s == null) {
--- 186,189 ----
|