| 1 | /* |
|---|
| 2 | * To change this license header, choose License Headers in Project Properties. |
|---|
| 3 | * To change this template file, choose Tools | Templates |
|---|
| 4 | * and open the template in the editor. |
|---|
| 5 | */ |
|---|
| 6 | |
|---|
| 7 | package org.biohackathon.SPARQLBuilder.www; |
|---|
| 8 | |
|---|
| 9 | import java.io.IOException; |
|---|
| 10 | import java.io.PrintWriter; |
|---|
| 11 | import java.util.*; |
|---|
| 12 | import javax.json.*; |
|---|
| 13 | import javax.servlet.ServletException; |
|---|
| 14 | import javax.servlet.annotation.WebServlet; |
|---|
| 15 | import javax.servlet.http.HttpServlet; |
|---|
| 16 | import javax.servlet.http.HttpServletRequest; |
|---|
| 17 | import javax.servlet.http.HttpServletResponse; |
|---|
| 18 | import javax.servlet.http.HttpSession; |
|---|
| 19 | import org.biohackathon.SPARQLBuilder.OWL.*; |
|---|
| 20 | |
|---|
| 21 | /** |
|---|
| 22 | * |
|---|
| 23 | * @author atsuko |
|---|
| 24 | */ |
|---|
| 25 | @WebServlet(name = "CLServlet", urlPatterns = {"/clist"}) |
|---|
| 26 | public class CLServlet extends HttpServlet { |
|---|
| 27 | |
|---|
| 28 | private static final String FILENAME = "cdata/"; |
|---|
| 29 | |
|---|
| 30 | /** |
|---|
| 31 | * Processes requests for both HTTP <code>GET</code> and <code>POST</code> |
|---|
| 32 | * methods. |
|---|
| 33 | * |
|---|
| 34 | * @param request servlet request |
|---|
| 35 | * @param response servlet response |
|---|
| 36 | * @throws ServletException if a servlet-specific error occurs |
|---|
| 37 | * @throws IOException if an I/O error occurs |
|---|
| 38 | */ |
|---|
| 39 | protected void processRequest(HttpServletRequest request, HttpServletResponse response) |
|---|
| 40 | throws ServletException, IOException { |
|---|
| 41 | response.setContentType("text/html;charset=UTF-8"); |
|---|
| 42 | try (PrintWriter out = response.getWriter()) { |
|---|
| 43 | /* TODO output your page here. You may use following sample code. */ |
|---|
| 44 | out.println("<!DOCTYPE html>"); |
|---|
| 45 | out.println("<html>"); |
|---|
| 46 | out.println("<head>"); |
|---|
| 47 | out.println("<title>Servlet CLServlet</title>"); |
|---|
| 48 | out.println("</head>"); |
|---|
| 49 | out.println("<body>"); |
|---|
| 50 | out.println("<h1>Servlet CLServlet at " + request.getContextPath() + "</h1>"); |
|---|
| 51 | out.println("</body>"); |
|---|
| 52 | out.println("</html>"); |
|---|
| 53 | } |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> |
|---|
| 57 | /** |
|---|
| 58 | * Handles the HTTP <code>GET</code> method. |
|---|
| 59 | * |
|---|
| 60 | * @param request servlet request |
|---|
| 61 | * @param response servlet response |
|---|
| 62 | * @throws ServletException if a servlet-specific error occurs |
|---|
| 63 | * @throws IOException if an I/O error occurs |
|---|
| 64 | */ |
|---|
| 65 | @Override |
|---|
| 66 | protected void doGet(HttpServletRequest request, HttpServletResponse response) |
|---|
| 67 | throws ServletException, IOException { |
|---|
| 68 | //processRequest(request, response); |
|---|
| 69 | response.setContentType("application/json;charset=UTF-8"); |
|---|
| 70 | response.setHeader("Access-Control-Allow-Origin", "*"); |
|---|
| 71 | response.setHeader("Access-Control-Allow-Methods", "GET"); |
|---|
| 72 | response.setHeader("Access-Control-Allow-Headers", request.getHeader("Access-Control-Request-Headers")); |
|---|
| 73 | response.setHeader("Access-Control-Max-Age", "-1"); |
|---|
| 74 | PrintWriter out = response.getWriter(); |
|---|
| 75 | String ep = request.getParameter("ep"); |
|---|
| 76 | String classURI = request.getParameter("class"); |
|---|
| 77 | HttpSession session = request.getSession(); |
|---|
| 78 | QueryPathGenerator qpg = (QueryPathGenerator)session.getAttribute("qpg"); |
|---|
| 79 | if ( qpg == null ){ |
|---|
| 80 | qpg = new QueryPathGenerator(); |
|---|
| 81 | } |
|---|
| 82 | SortedSet<String> sortedClasses = new TreeSet<String>(); // display + url |
|---|
| 83 | qpg.setSPARQLendpoint(ep); |
|---|
| 84 | //OWLClassGraph ocg = qpg.getOWLClassGraph(); |
|---|
| 85 | if ( classURI != null ){ |
|---|
| 86 | qpg.setOWLClassGraph(classURI); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | SClass[] classes = qpg.getClasses(null); |
|---|
| 90 | for (int i = 0 ; i < classes.length; i++ ){ |
|---|
| 91 | String uri = classes[i].getClassURI(); |
|---|
| 92 | //if ( ocg.getNumberOfEdge(uri) == 0 ){ |
|---|
| 93 | // continue; |
|---|
| 94 | //} |
|---|
| 95 | if ( classURI != null ){ |
|---|
| 96 | if ( qpg.getOWLClassGraph().visitedNode(uri) == false ){ |
|---|
| 97 | continue; |
|---|
| 98 | } |
|---|
| 99 | } |
|---|
| 100 | Label[] labels = classes[i].getLabels(); |
|---|
| 101 | String label = null; |
|---|
| 102 | for ( int j = 0 ; j < labels.length; j++ ){ |
|---|
| 103 | if ( labels[j].getLanguage() == null ){ |
|---|
| 104 | label = labels[j].getLabel(); break; |
|---|
| 105 | }else if ( labels[j].getLanguage().equals("en") ){ |
|---|
| 106 | label = labels[j].getLabel(); break; |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | if ( label == null ){ |
|---|
| 110 | String[] uris = uri.split("/"); |
|---|
| 111 | String tmplabel = uris[uris.length-1]; |
|---|
| 112 | String[] tmplabel2 = tmplabel.split("#"); |
|---|
| 113 | label = tmplabel2[tmplabel2.length-1]; |
|---|
| 114 | } |
|---|
| 115 | StringBuilder classbuilder = new StringBuilder(label); |
|---|
| 116 | classbuilder.append(" "); |
|---|
| 117 | classbuilder.append(classes[i].getNumOfInstances()); |
|---|
| 118 | classbuilder.append(" "); |
|---|
| 119 | classbuilder.append(uri); |
|---|
| 120 | sortedClasses.add(classbuilder.toString()); |
|---|
| 121 | } |
|---|
| 122 | JsonBuilderFactory jbfactory = Json.createBuilderFactory(null); |
|---|
| 123 | JsonArrayBuilder jab = jbfactory.createArrayBuilder(); |
|---|
| 124 | Iterator<String> cit = sortedClasses.iterator(); |
|---|
| 125 | List<String> tmpclasses = new LinkedList<String>(); |
|---|
| 126 | JsonObjectBuilder job = jbfactory.createObjectBuilder(); |
|---|
| 127 | while( cit.hasNext() ){ |
|---|
| 128 | String classinfo = cit.next(); |
|---|
| 129 | String[] data = classinfo.split(" "); |
|---|
| 130 | if (data.length != 3 ){ |
|---|
| 131 | System.out.println("data is wrong?"); |
|---|
| 132 | } |
|---|
| 133 | if (data[0].matches("^[0-9]*$")){ |
|---|
| 134 | tmpclasses.add(classinfo); |
|---|
| 135 | }else{ |
|---|
| 136 | job.add("uri", data[2]); |
|---|
| 137 | job.add("label", data[0]); |
|---|
| 138 | job.add("number", data[1]); |
|---|
| 139 | jab.add(job); |
|---|
| 140 | } |
|---|
| 141 | } |
|---|
| 142 | cit = tmpclasses.iterator(); |
|---|
| 143 | while( cit.hasNext() ){ |
|---|
| 144 | String classinfo = cit.next(); |
|---|
| 145 | String[] data = classinfo.split(" "); |
|---|
| 146 | if (data.length != 3 ){ |
|---|
| 147 | System.out.println("data is wrong?"); |
|---|
| 148 | } |
|---|
| 149 | job.add("uri", data[2]); |
|---|
| 150 | job.add("label", data[0]); |
|---|
| 151 | job.add("number", data[1]); |
|---|
| 152 | jab.add(job); |
|---|
| 153 | } |
|---|
| 154 | JsonArray ja = jab.build(); |
|---|
| 155 | out.print(ja); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | /** |
|---|
| 159 | * Handles the HTTP <code>POST</code> method. |
|---|
| 160 | * |
|---|
| 161 | * @param request servlet request |
|---|
| 162 | * @param response servlet response |
|---|
| 163 | * @throws ServletException if a servlet-specific error occurs |
|---|
| 164 | * @throws IOException if an I/O error occurs |
|---|
| 165 | */ |
|---|
| 166 | @Override |
|---|
| 167 | protected void doPost(HttpServletRequest request, HttpServletResponse response) |
|---|
| 168 | throws ServletException, IOException { |
|---|
| 169 | processRequest(request, response); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | /** |
|---|
| 173 | * Returns a short description of the servlet. |
|---|
| 174 | * |
|---|
| 175 | * @return a String containing servlet description |
|---|
| 176 | */ |
|---|
| 177 | @Override |
|---|
| 178 | public String getServletInfo() { |
|---|
| 179 | return "Short description"; |
|---|
| 180 | }// </editor-fold> |
|---|
| 181 | |
|---|
| 182 | } |
|---|