package org.wookie.qti; import ee.tlu.htk.waramu.*; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.TreeMap; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; import org.wookie.qti.R2Q2_Interface; import org.wookie.qti.util.PropertiesHelper; import org.wookie.qti.util.StatusHandler; /** * Servlet implementation class PostListener */ public class ServiceServlet extends HttpServlet { private static final long serialVersionUID = 1L; private Logger logger = Logger.getLogger(ServiceServlet.class.getName()); private static final String xmlDecl = ""; /** * @see HttpServlet#HttpServlet() */ public ServiceServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/xml; charset=UTF-8"); PrintWriter out = response.getWriter(); R2Q2_Interface R2Q2_Intf = new R2Q2_Interface(); XMLEngine api_XML = new XMLEngine(); Waramu_Interface waramuIntf = new Waramu_Interface(); //init new session PropertiesHelper p = new PropertiesHelper(); waramuIntf.newSession(p.getWaramuUsername(), p.getWaramuPassword()); //do query boolean doingQuery = false; try { if(request.getParameter("query").trim().length() > 0) { doingQuery = true; TreeMap testItems = waramuIntf.doQuery(request.getParameter("query"), request.getParameter("locale")); out.println(xmlDecl+api_XML.generateAssessmentList(testItems)); } } catch(NullPointerException e) { //Something went wrong if(doingQuery) { out.println(xmlDecl+new StatusHandler(2).getStatusXML()); } } try { if(!request.getParameter("attachmentId").isEmpty() && !request.getParameter("resourceKey").isEmpty()) { String resourceId = request.getParameter("resourceKey"); String attachmentId = request.getParameter("attachmentId"); logger.debug("Retrieved assessment ids "+resourceId+" "+attachmentId); byte[] zip = null; //get attachment and send to r2q2 if(!resourceId.equalsIgnoreCase("zip")) { zip = waramuIntf.getAttachment(resourceId, attachmentId); } else { zip = R2Q2_Intf.getZipViaUrl(attachmentId); } if(zip != null) { ArrayList itemList = R2Q2_Intf.getHtml(zip); //returns status code 1, if no items found out.println(xmlDecl+api_XML.generateAssessmentXML(itemList, R2Q2_Intf.getAssessmentTitle())); } else { String status = new StatusHandler(1).getStatusXML(); out.println(xmlDecl+status); } } } catch(NullPointerException e) { // out.println("No attachment or resource id"); } //close Waramu session waramuIntf.closeSession(); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub, data checking, if responseXML exists XMLEngine api_XML = new XMLEngine(); ArrayList itemsList = api_XML.parseWidgetResponse(request.getParameter("responseXML")); String responseXml = api_XML.generateServerResponse(itemsList); logger.debug("ServerResponse XML: \n"+responseXml); response.setContentType("text/xml; charset=UTF-8"); PrintWriter out = response.getWriter(); out.println(xmlDecl+responseXml); //TODO clean up } }