<%@ page language = "java" import = "r2q2.util.streams.*,r2q2.router.ws.*,java.net.*,javax.activation.*,javax.xml.soap.*,java.util.*,java.io.*, org.apache.commons.fileupload.*, org.apache.commons.fileupload.servlet.*, org.apache.commons.fileupload.disk.*, r2q2.rendering.RendererOpts"%><%-- Copyright (c) 2008, University of Southampton All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the University of Southampton nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --%><% String basePath = System.getProperty("R2Q2_HOME"); if (basePath==null) { basePath = System.getenv("R2Q2_HOME"); if (basePath == null) { basePath = System.getProperty("catalina.home"); basePath += "/webapps/r2q2/"; } } Properties p = new Properties(); p.load(new FileInputStream(basePath+"/r2q2.properties")); String router = (String)p.get("r2q2.router.ws.location"); String fileTitle = null; String _xmlIn = null; byte[] zip = null; RouterServiceLocator loc = new RouterServiceLocator(); RouterSoapBindingStub stub = new RouterSoapBindingStub( new URL(router), loc); int renderOpts = 31; boolean isContentPackage = true; boolean isPureXML = false; //file upload code (jakarta commons file upload) - if a file exists then override the drop down selected file if (ServletFileUpload.isMultipartContent(request)){ //file attached // Create a factory for disk-based file items FileItemFactory factory = new DiskFileItemFactory(); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // Parse the request List items = upload.parseRequest(request); //read the request and put the file into the zip byte array. Iterator iter = items.iterator(); while (iter.hasNext()) { FileItem item = (FileItem)iter.next(); if (item.isFormField()) { //out.println("FormField"); //field information String name = item.getFieldName(); if (name.equalsIgnoreCase("xmlInput") && item.getString().startsWith(" 1){ fileTitle = filenameParts[1]; FileInputStream is = new FileInputStream(new File(basePath + filenameParts[0])); zip = StreamCopier.copyToByteArray(is); is.close(); } } } else if (name.equalsIgnoreCase("filenamexml")){ String fileXmlTitle = item.getString(); if (fileXmlTitle != null && !fileXmlTitle.trim().equals("")){ fileTitle = fileXmlTitle; } } if(name.equals("renderOpts")){ if(item.getString().equals("xhtml")){ renderOpts = RendererOpts.RENDERWRAPPER | RendererOpts.RENDERTITLE | RendererOpts.RENDERBODY | RendererOpts.RENDERBUTTONS | RendererOpts.RENDERFEEDBACK | RendererOpts.RENDERFORM | RendererOpts.RENDERINCLUDES; } if(item.getString().equals("item")){ renderOpts = RendererOpts.RENDERWRAPPER | RendererOpts.RENDERTITLE | RendererOpts.RENDERBODY | RendererOpts.RENDERFEEDBACK | RendererOpts.RENDERFORM | RendererOpts.RENDERINCLUDES; } if(item.getString().equals("nofeed")){ renderOpts = RendererOpts.RENDERWRAPPER | RendererOpts.RENDERTITLE | RendererOpts.RENDERBODY | RendererOpts.RENDERBUTTONS | RendererOpts.RENDERFORM | RendererOpts.RENDERINCLUDES; } if(item.getString().equals("frag")){ renderOpts = RendererOpts.RENDERTITLE | RendererOpts.RENDERBODY | RendererOpts.RENDERBUTTONS | RendererOpts.RENDERFEEDBACK | RendererOpts.RENDERFORM | RendererOpts.RENDERINCLUDES; } if(item.getString().equals("notitle")){ renderOpts = RendererOpts.RENDERWRAPPER | RendererOpts.RENDERBODY | RendererOpts.RENDERBUTTONS | RendererOpts.RENDERFEEDBACK | RendererOpts.RENDERFORM | RendererOpts.RENDERINCLUDES; } } } else { //out.println("file"); //retrieve the file //System.out.println(item.getName()); if(item.getName().endsWith(".xml") || item.getName().endsWith(".XML")){ isContentPackage = false; if(item.getName().lastIndexOf("\\")!=-1){ //IE bug fix - strips full path off to give just the file name fileTitle=item.getName().substring(item.getName().lastIndexOf("\\")+1); }else{ fileTitle = item.getName(); } } if (item.getSize() > 100){ zip = item.get(); break; } } } } if (zip != null || isPureXML){ String guid = ""; if(isContentPackage){ ByteArrayDataSource bads = new ByteArrayDataSource("package.zip", zip, "application/zip"); stub.addAttachment(new DataHandler(bads)); guid = stub.newContentPackageSession(fileTitle, "r2q2.rendering.xhtml.XHTMLRenderer",renderOpts); }else{ if(isPureXML) { guid = stub.newXMLSession(_xmlIn, "r2q2.rendering.xhtml.XHTMLRenderer",renderOpts, ""); } else { ByteArrayDataSource bads = new ByteArrayDataSource("package.zip", zip, "application/zip"); stub.addAttachment(new DataHandler(bads)); guid = stub.newItemSession(fileTitle, "r2q2.rendering.xhtml.XHTMLRenderer",renderOpts,"http://localhost:8080/r2q2/"); } } session.setAttribute("guid", guid); HashMap resp = stub.firstStage(guid); Object[] attachments = stub.getAttachments(); AttachmentPart ap = (AttachmentPart) attachments[0]; byte[] file; file = StreamCopier.copyToByteArray(ap.getDataHandler().getInputStream()); //print the question String xhtml = new String(file); //serve xml if possible (not for fragments) if ( xhtml.startsWith("") && //must start with xml preamble if not fragment ((request.getHeader("accept") != null && request.getHeader("accept").contains("application/xhtml+xml")) || (request.getHeader("user-agent") != null && request.getHeader("user-agent").contains("MathPlayer")))) { //browsers with mathplayer can accept xml response.setContentType("application/xhtml+xml"); } out.println(xhtml); } else { //no file or list item selected out.println("Nothing selected."); } %>