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 | import javax.json.*; |
---|
19 | |
---|
20 | //import java.io.*; |
---|
21 | |
---|
22 | /** |
---|
23 | * |
---|
24 | * @author atsuko |
---|
25 | */ |
---|
26 | @WebServlet(name = "EPServlet", urlPatterns = {"/eplist"}) |
---|
27 | public class EPServlet extends HttpServlet { |
---|
28 | |
---|
29 | private static final String FILENAME = "cdata/"; |
---|
30 | |
---|
31 | /** |
---|
32 | * Processes requests for both HTTP <code>GET</code> and <code>POST</code> |
---|
33 | * methods. |
---|
34 | * |
---|
35 | * @param request servlet request |
---|
36 | * @param response servlet response |
---|
37 | * @throws ServletException if a servlet-specific error occurs |
---|
38 | * @throws IOException if an I/O error occurs |
---|
39 | */ |
---|
40 | protected void processRequest(HttpServletRequest request, HttpServletResponse response) |
---|
41 | throws ServletException, IOException { |
---|
42 | response.setContentType("text/html;charset=UTF-8"); |
---|
43 | try (PrintWriter out = response.getWriter()) { |
---|
44 | /* TODO output your page here. You may use following sample code. */ |
---|
45 | out.println("<!DOCTYPE html>"); |
---|
46 | out.println("<html>"); |
---|
47 | out.println("<head>"); |
---|
48 | out.println("<title>Servlet EPServlet</title>"); |
---|
49 | out.println("</head>"); |
---|
50 | out.println("<body>"); |
---|
51 | out.println("<h1>Servlet EPServlet at " + request.getContextPath() + "</h1>"); |
---|
52 | out.println("</body>"); |
---|
53 | out.println("</html>"); |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> |
---|
58 | /** |
---|
59 | * Handles the HTTP <code>GET</code> method. |
---|
60 | * |
---|
61 | * @param request servlet request |
---|
62 | * @param response servlet response |
---|
63 | * @throws ServletException if a servlet-specific error occurs |
---|
64 | * @throws IOException if an I/O error occurs |
---|
65 | */ |
---|
66 | @Override |
---|
67 | protected void doGet(HttpServletRequest request, HttpServletResponse response) |
---|
68 | throws ServletException, IOException { |
---|
69 | HttpSession session = request.getSession(); |
---|
70 | // Initialize the session |
---|
71 | session.removeAttribute("qpg"); |
---|
72 | session.removeAttribute("graph"); |
---|
73 | QueryPathGenerator qpg = new QueryPathGenerator(); |
---|
74 | |
---|
75 | response.setContentType("application/json;charset=UTF-8"); |
---|
76 | response.setHeader("Access-Control-Allow-Origin", "*"); |
---|
77 | response.setHeader("Access-Control-Allow-Methods", "GET"); |
---|
78 | response.setHeader("Access-Control-Allow-Headers", request.getHeader("Access-Control-Request-Headers")); |
---|
79 | response.setHeader("Access-Control-Max-Age", "-1"); |
---|
80 | PrintWriter out = response.getWriter(); |
---|
81 | |
---|
82 | String[] elist = qpg.getFactory().getEndpointURIList(); |
---|
83 | JsonBuilderFactory jbfactory = Json.createBuilderFactory(null); |
---|
84 | JsonArrayBuilder jab = jbfactory.createArrayBuilder(); |
---|
85 | for (int i = 0; i < elist.length; i++ ){ |
---|
86 | jab.add(elist[i]); |
---|
87 | } |
---|
88 | JsonArray ja = jab.build(); |
---|
89 | out.print(ja); |
---|
90 | session.setAttribute("qpg", qpg); |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * Handles the HTTP <code>POST</code> method. |
---|
95 | * |
---|
96 | * @param request servlet request |
---|
97 | * @param response servlet response |
---|
98 | * @throws ServletException if a servlet-specific error occurs |
---|
99 | * @throws IOException if an I/O error occurs |
---|
100 | */ |
---|
101 | @Override |
---|
102 | protected void doPost(HttpServletRequest request, HttpServletResponse response) |
---|
103 | throws ServletException, IOException { |
---|
104 | processRequest(request, response); |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * Returns a short description of the servlet. |
---|
109 | * |
---|
110 | * @return a String containing servlet description |
---|
111 | */ |
---|
112 | @Override |
---|
113 | public String getServletInfo() { |
---|
114 | return "Short description"; |
---|
115 | }// </editor-fold> |
---|
116 | |
---|
117 | } |
---|