| 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 javax.servlet.ServletException; | 
|---|
| 12 | import javax.servlet.annotation.WebServlet; | 
|---|
| 13 | import javax.servlet.http.HttpServlet; | 
|---|
| 14 | import javax.servlet.http.HttpServletRequest; | 
|---|
| 15 | import javax.servlet.http.HttpServletResponse; | 
|---|
| 16 | import org.biohackathon.SPARQLBuilder.OWL.*; | 
|---|
| 17 |  | 
|---|
| 18 | /** | 
|---|
| 19 |  * | 
|---|
| 20 |  * @author atsuko | 
|---|
| 21 |  */ | 
|---|
| 22 | @WebServlet(name = "PLServlet", urlPatterns = {"/plist"}) | 
|---|
| 23 | public class PLServlet extends HttpServlet { | 
|---|
| 24 |  | 
|---|
| 25 |     private static final String FILENAME = "cdata/";     | 
|---|
| 26 |      | 
|---|
| 27 |     /** | 
|---|
| 28 |      * Processes requests for both HTTP <code>GET</code> and <code>POST</code> | 
|---|
| 29 |      * methods. | 
|---|
| 30 |      * | 
|---|
| 31 |      * @param request servlet request | 
|---|
| 32 |      * @param response servlet response | 
|---|
| 33 |      * @throws ServletException if a servlet-specific error occurs | 
|---|
| 34 |      * @throws IOException if an I/O error occurs | 
|---|
| 35 |      */ | 
|---|
| 36 |     protected void processRequest(HttpServletRequest request, HttpServletResponse response) | 
|---|
| 37 |             throws ServletException, IOException { | 
|---|
| 38 |         response.setContentType("text/html;charset=UTF-8"); | 
|---|
| 39 |         try (PrintWriter out = response.getWriter()) { | 
|---|
| 40 |             /* TODO output your page here. You may use following sample code. */ | 
|---|
| 41 |             out.println("<!DOCTYPE html>"); | 
|---|
| 42 |             out.println("<html>"); | 
|---|
| 43 |             out.println("<head>"); | 
|---|
| 44 |             out.println("<title>Servlet PLServlet</title>");             | 
|---|
| 45 |             out.println("</head>"); | 
|---|
| 46 |             out.println("<body>"); | 
|---|
| 47 |             out.println("<h1>Servlet PLServlet at " + request.getContextPath() + "</h1>"); | 
|---|
| 48 |             out.println("</body>"); | 
|---|
| 49 |             out.println("</html>"); | 
|---|
| 50 |         } | 
|---|
| 51 |     } | 
|---|
| 52 |  | 
|---|
| 53 |     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> | 
|---|
| 54 |     /** | 
|---|
| 55 |      * Handles the HTTP <code>GET</code> method. | 
|---|
| 56 |      * | 
|---|
| 57 |      * @param request servlet request | 
|---|
| 58 |      * @param response servlet response | 
|---|
| 59 |      * @throws ServletException if a servlet-specific error occurs | 
|---|
| 60 |      * @throws IOException if an I/O error occurs | 
|---|
| 61 |      */ | 
|---|
| 62 |     @Override | 
|---|
| 63 |     protected void doGet(HttpServletRequest request, HttpServletResponse response) | 
|---|
| 64 |             throws ServletException, IOException { | 
|---|
| 65 |         //processRequest(request, response); | 
|---|
| 66 |         response.setContentType("application/json;charset=UTF-8"); | 
|---|
| 67 |         PrintWriter out = response.getWriter(); | 
|---|
| 68 |         String ep = request.getParameter("ep"); | 
|---|
| 69 |         String st = request.getParameter("startclass"); | 
|---|
| 70 |         String en = request.getParameter("endclass"); | 
|---|
| 71 |          | 
|---|
| 72 |         QueryPathGenerator qpg = new QueryPathGenerator(ep, FILENAME); | 
|---|
| 73 |         Path[] paths = qpg.getPaths(st, en, true); | 
|---|
| 74 |         String jsonstr = "{\"paths\":["; | 
|---|
| 75 |         for(int i = 0; i< paths.length; i++){ | 
|---|
| 76 |             if (i > 0 ){ | 
|---|
| 77 |                 jsonstr += ","; | 
|---|
| 78 |             } | 
|---|
| 79 |             jsonstr += paths[i].toJSONString2(); | 
|---|
| 80 |         } | 
|---|
| 81 |         jsonstr += "]}"; | 
|---|
| 82 |         System.out.println("JSON:"); | 
|---|
| 83 |         System.out.println(jsonstr); | 
|---|
| 84 |         out.print(jsonstr); | 
|---|
| 85 |     } | 
|---|
| 86 |  | 
|---|
| 87 |     /** | 
|---|
| 88 |      * Handles the HTTP <code>POST</code> method. | 
|---|
| 89 |      * | 
|---|
| 90 |      * @param request servlet request | 
|---|
| 91 |      * @param response servlet response | 
|---|
| 92 |      * @throws ServletException if a servlet-specific error occurs | 
|---|
| 93 |      * @throws IOException if an I/O error occurs | 
|---|
| 94 |      */ | 
|---|
| 95 |     @Override | 
|---|
| 96 |     protected void doPost(HttpServletRequest request, HttpServletResponse response) | 
|---|
| 97 |             throws ServletException, IOException { | 
|---|
| 98 |         processRequest(request, response); | 
|---|
| 99 |     } | 
|---|
| 100 |  | 
|---|
| 101 |     /** | 
|---|
| 102 |      * Returns a short description of the servlet. | 
|---|
| 103 |      * | 
|---|
| 104 |      * @return a String containing servlet description | 
|---|
| 105 |      */ | 
|---|
| 106 |     @Override | 
|---|
| 107 |     public String getServletInfo() { | 
|---|
| 108 |         return "Short description"; | 
|---|
| 109 |     }// </editor-fold> | 
|---|
| 110 |  | 
|---|
| 111 | } | 
|---|