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 javax.servlet.http.HttpSession; |
---|
17 | import org.biohackathon.SPARQLBuilder.OWL.*; |
---|
18 | |
---|
19 | /** |
---|
20 | * |
---|
21 | * @author atsuko |
---|
22 | */ |
---|
23 | @WebServlet(name = "PLServlet", urlPatterns = {"/plist"}) |
---|
24 | public class PLServlet extends HttpServlet { |
---|
25 | |
---|
26 | private static final String FILENAME = "cdata/"; |
---|
27 | |
---|
28 | /** |
---|
29 | * Processes requests for both HTTP <code>GET</code> and <code>POST</code> |
---|
30 | * methods. |
---|
31 | * |
---|
32 | * @param request servlet request |
---|
33 | * @param response servlet response |
---|
34 | * @throws ServletException if a servlet-specific error occurs |
---|
35 | * @throws IOException if an I/O error occurs |
---|
36 | */ |
---|
37 | protected void processRequest(HttpServletRequest request, HttpServletResponse response) |
---|
38 | throws ServletException, IOException { |
---|
39 | response.setContentType("text/html;charset=UTF-8"); |
---|
40 | try (PrintWriter out = response.getWriter()) { |
---|
41 | /* TODO output your page here. You may use following sample code. */ |
---|
42 | out.println("<!DOCTYPE html>"); |
---|
43 | out.println("<html>"); |
---|
44 | out.println("<head>"); |
---|
45 | out.println("<title>Servlet PLServlet</title>"); |
---|
46 | out.println("</head>"); |
---|
47 | out.println("<body>"); |
---|
48 | out.println("<h1>Servlet PLServlet at " + request.getContextPath() + "</h1>"); |
---|
49 | out.println("</body>"); |
---|
50 | out.println("</html>"); |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> |
---|
55 | /** |
---|
56 | * Handles the HTTP <code>GET</code> method. |
---|
57 | * |
---|
58 | * @param request servlet request |
---|
59 | * @param response servlet response |
---|
60 | * @throws ServletException if a servlet-specific error occurs |
---|
61 | * @throws IOException if an I/O error occurs |
---|
62 | */ |
---|
63 | @Override |
---|
64 | protected void doGet(HttpServletRequest request, HttpServletResponse response) |
---|
65 | throws ServletException, IOException { |
---|
66 | //processRequest(request, response); |
---|
67 | response.setContentType("application/json;charset=UTF-8"); |
---|
68 | response.setHeader("Access-Control-Allow-Origin", "*"); |
---|
69 | response.setHeader("Access-Control-Allow-Methods", "GET"); |
---|
70 | response.setHeader("Access-Control-Allow-Headers", request.getHeader("Access-Control-Request-Headers")); |
---|
71 | response.setHeader("Access-Control-Max-Age", "-1"); |
---|
72 | PrintWriter out = response.getWriter(); |
---|
73 | String ep = request.getParameter("ep"); |
---|
74 | String st = request.getParameter("startclass"); |
---|
75 | String en = request.getParameter("endclass"); |
---|
76 | //String ask = request.getParameter("ask"); |
---|
77 | |
---|
78 | // String |
---|
79 | String uri = request.getQueryString(); |
---|
80 | System.out.println(uri); |
---|
81 | |
---|
82 | HttpSession session = request.getSession(); |
---|
83 | QueryPathGenerator qpg = (QueryPathGenerator)session.getAttribute("qpg"); |
---|
84 | if ( qpg == null ){ |
---|
85 | qpg = new QueryPathGenerator(); |
---|
86 | } |
---|
87 | SClass[] classes = qpg.getClasses(null); |
---|
88 | qpg.setClassLabels(classes); |
---|
89 | |
---|
90 | Path[] paths = null; |
---|
91 | paths = qpg.getPaths(st, en); |
---|
92 | if ( paths == null ){ |
---|
93 | out.print(" "); |
---|
94 | }else{ |
---|
95 | String jsonstr = "["; |
---|
96 | for(int i = 0; i< paths.length; i++){ |
---|
97 | if (i > 0 && paths[i] != null){ |
---|
98 | jsonstr += ","; |
---|
99 | } |
---|
100 | if ( paths[i] == null ){ |
---|
101 | continue; |
---|
102 | } |
---|
103 | jsonstr += paths[i].toJSONString4(qpg); |
---|
104 | } |
---|
105 | jsonstr += "]"; |
---|
106 | // For debug |
---|
107 | /* |
---|
108 | System.out.println("JSON:"); |
---|
109 | System.out.println(jsonstr); |
---|
110 | */ |
---|
111 | out.print(jsonstr); |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * Handles the HTTP <code>POST</code> method. |
---|
117 | * |
---|
118 | * @param request servlet request |
---|
119 | * @param response servlet response |
---|
120 | * @throws ServletException if a servlet-specific error occurs |
---|
121 | * @throws IOException if an I/O error occurs |
---|
122 | */ |
---|
123 | @Override |
---|
124 | protected void doPost(HttpServletRequest request, HttpServletResponse response) |
---|
125 | throws ServletException, IOException { |
---|
126 | processRequest(request, response); |
---|
127 | } |
---|
128 | |
---|
129 | /** |
---|
130 | * Returns a short description of the servlet. |
---|
131 | * |
---|
132 | * @return a String containing servlet description |
---|
133 | */ |
---|
134 | @Override |
---|
135 | public String getServletInfo() { |
---|
136 | return "Short description"; |
---|
137 | }// </editor-fold> |
---|
138 | |
---|
139 | } |
---|