/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package org.biohackathon.SPARQLBuilder.www; import java.io.IOException; import java.io.PrintWriter; import java.math.BigDecimal; import java.util.*; import javax.json.*; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.biohackathon.SPARQLBuilder.OWL.*; /** * * @author atsuko */ @WebServlet(name = "CLServlet", urlPatterns = {"/clist"}) public class CLServlet extends HttpServlet { private static final String FILENAME = "cdata/"; /** * Processes requests for both HTTP GET and POST * methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { /* TODO output your page here. You may use following sample code. */ out.println(""); out.println(""); out.println(""); out.println("Servlet CLServlet"); out.println(""); out.println(""); out.println("

Servlet CLServlet at " + request.getContextPath() + "

"); out.println(""); out.println(""); } } // /** * Handles the HTTP GET method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //processRequest(request, response); response.setContentType("application/json;charset=UTF-8"); response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Methods", "GET"); response.setHeader("Access-Control-Allow-Headers", request.getHeader("Access-Control-Request-Headers")); response.setHeader("Access-Control-Max-Age", "-1"); PrintWriter out = response.getWriter(); String ep = request.getParameter("ep"); String classURI = request.getParameter("class"); HttpSession session = request.getSession(); QueryPathGenerator qpg = (QueryPathGenerator)session.getAttribute("qpg"); SortedSet sortedClasses = null; SClass[] classes = null; JsonBuilderFactory jbfactory = Json.createBuilderFactory(null); JsonObjectBuilder job = jbfactory.createObjectBuilder(); JsonArray ja = null; if ( qpg == null ){ qpg = new QueryPathGenerator(); } if ( classURI == null ){ try{ classes = qpg.getClasses(); }catch(Exception e){ System.err.println(e); } }else{ //classes = qpg.getAdjclasses(classURI); } sortedClasses = qpg.getSortedClasses(classes); ja = getJsonArrayFromSortedClasses(jbfactory, sortedClasses); out.print(ja); } /** * Handles the HTTP POST method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// private JsonArray getJsonArrayFromSortedClasses(JsonBuilderFactory jbfactory, SortedSet sortedClasses){ JsonArrayBuilder jab = jbfactory.createArrayBuilder(); Iterator cit = sortedClasses.iterator(); List tmpclasses = new LinkedList(); JsonObjectBuilder job = jbfactory.createObjectBuilder(); while( cit.hasNext() ){ String classinfo = cit.next(); String[] data = classinfo.split("\t"); if (data.length != 3 ){ System.out.println("data is wrong?"); } if (data[0].matches("^[0-9]*$")){ tmpclasses.add(classinfo); }else{ job.add("uri", data[2]); job.add("label", data[0]); job.add("number", data[1]); jab.add(job); } } cit = tmpclasses.iterator(); while( cit.hasNext() ){ String classinfo = cit.next(); String[] data = classinfo.split("\t"); if (data.length != 3 ){ System.out.println("data is wrong?"); } job.add("uri", data[2]); job.add("label", data[0]); job.add("number", data[1]); jab.add(job); } JsonArray ja = jab.build(); return ja; } }